[R-sig-Geo] convert points into SHP polygons

Roger Bivand Roger.Bivand at nhh.no
Sat Nov 28 15:06:14 CET 2009


On Fri, 27 Nov 2009, rusers.sh wrote:

> Dear Robert,
> My method to generate the Voronoi tessellation maybe not better, You can
> show me you method to get it.
>  What i want to get is the the class of SpatialPolygons(sp) based on the
> generated Voronoi tessellation.
>  Any ideas or suggestions are greatly appreciated.

In the list archives for March 2007 (search in Nabble, for example), there 
are hints. A more up to date version might be:

set.seed(1)
crds <- cbind(runif(100), runif(100))
library(deldir)
z <- deldir(crds[,1], crds[,2])
w <- tile.list(z)
library(sp)
polys <- vector(mode="list", length=length(w))
for (i in seq(along=polys)) {
   pcrds <- cbind(w[[i]]$x, w[[i]]$y)
   pcrds <- rbind(pcrds, pcrds[1,])
   polys[[i]] <- Polygons(list(Polygon(pcrds)), ID=as.character(i))
}
SP <- SpatialPolygons(polys)
SPDF <- SpatialPolygonsDataFrame(SP, data=data.frame(x=crds[,1],
   y=crds[,2], row.names=row.names(SP)))

plot(SPDF)
points(crds, pch=3, col="red")
text(coordinates(SPDF), label=row.names(SPDF))

then export the SpatialPolygonsDataFrame in the usual way.

By the way, to get the centroids (aka label points) for Polygons objects, 
use coordinates() on their enclosing SpatialPolygons object. The (very) 
deprecated maptools function you refered to in an earlier posting will 
cease to be available very shortly.

Hope this helps,

Roger

>
> 2009/11/27 rusers.sh <rusers.sh at gmail.com>
>
>>   Yes, what i want is really the Voronoi tessellation that you showed in
>> the link. I can generate the Voronoi tessellation  (see my codes), but my
>> problem is how to generate the the class of SpatialPolygons(sp) based on the
>> generated  Voronoi tessellation? (SHP means shape file of polygon.)
>>   Thanks a lot.
>> ####My code to get the Voronoi tessellation########
>> library(spatstat)
>> win<-owin(c(0,1),c(0,1))
>> pp <- runifpoint(10,win=win)
>> #plot(pp)
>> library(tripack)
>> vm <- voronoi.mosaic(pp$x,pp$y)
>> plot(vm)
>> par(new=T)
>> points(pp$x,pp$y,col='blue')
>> axis(1,xlim=c(0,1)) #1=below
>> axis(2,ylim=c(0,1)) #2=left
>> box(xlim=c(0,1),ylim=c(0,1))
>>
>> 2009/11/27 Robert J. Hijmans <r.hijmans at gmail.com>
>>
>> I think you want a Voronoi tessalation (diagram):
>>> http://en.wikipedia.org/wiki/Voronoi_diagram
>>>
>>> But what are SHP polygons?  Is 'SHP' a class in an R package?
>>>
>>> Robert
>>>
>>> On Fri, Nov 27, 2009 at 2:03 PM, rusers.sh <rusers.sh at gmail.com> wrote:
>>>>  To make this problem more clear. Suppose i have some points in a
>>> certain
>>>> area (e.g. 10 points in a unit square), how to cut and generate the
>>>> corresponding SHP polygons of containing the points (1 point in 1
>>> polygon,
>>>> maybe the point as their centroid) in this certain area?
>>>>  In GeoDa, there is a menu "tools->shape->point  to polygon" to achieve
>>>> that. I wonder how to do that in R. Thanks a lot.
>>>> ##Example##
>>>> win<-owin(c(0,1),c(0,1))
>>>> pp <- runifpoint(10) #generate 10 points
>>>> #i want to change these 10 points into 10 SHP polygons,something like
>>> the
>>>> manipulation in GeoDA, "tools->shape->point  to polygon"
>>>> #plot(pp)
>>>>
>>>> 2009/11/27 rusers.sh <rusers.sh at gmail.com>
>>>>
>>>>> Hi,
>>>>>   I always need to convert between the points and polygons. For
>>> converting
>>>>> the polygons to points, we can use the "get.Pcent(maptools) " to get
>>> the
>>>>> centroids of the polygons. But for converting points to polygons
>>> (something
>>>>> like GeoDa's "tools->shape->point  to polygon"), i cannot find the
>>>>> corresponding method. I searched in R Site Search with "point to
>>> polygon"
>>>>> and "generate polygon", but still cannot get the answers.
>>>>>  Does anybody know how to convert points to the SHP polygons?
>>>>>  OR whether we can generate some SHP polygons randomly in R?
>>>>> ##Example##
>>>>> win<-owin(c(0,1),c(0,1))
>>>>> pp <- runifpoint(10) #generate 10 points
>>>>> #i want to change these 10 points into 10 SHP polygons,something like
>>> the
>>>>> manipulation in GeoDA, "tools->shape->point  to polygon"
>>>>> #plot(pp)
>>>>>   Thanks a lot.
>>>>>
>>>>>
>>>>> --
>>>>> -----------------
>>>>> Jane Chang
>>>>> Queen's
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> -----------------
>>>> Jane Chang
>>>> Queen's
>>>>
>>>>        [[alternative HTML version deleted]]
>>>>
>>>> _______________________________________________
>>>> R-sig-Geo mailing list
>>>> R-sig-Geo at stat.math.ethz.ch
>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>>
>>>
>>
>>
>>
>> --
>> -----------------
>> Jane Chang
>> Queen's
>>
>
>
>
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list