[R-sig-Geo] classify point by nearest polygon

rundel rundel at gmail.com
Wed Feb 16 01:35:52 CET 2011


This can also be done with rgeos, a simple example is below. The 2nd half is
relevant to what you are trying to accomplish the rest is just generating
lakelike polygons.


set.seed(1)
txt = rep("",5)
for(i in 1:5) {
	xc = floor(runif(1,1,25))
	yc = floor(runif(1,1,25))
	
	xpts = round(rnorm(10,0,0.5),3)
	ypts = round(rnorm(10,0,0.5),3)
	
	txt[i] = paste("MULTIPOINT(",paste( "(",xpts+xc,"
",ypts+yc,")",sep="",collapse="," ),")",sep="")
}

wkt = paste( "GEOMETRYCOLLECTION(",paste(txt,collapse=","),")", sep="")
z2=gConvexHull(readWKT(wkt),byid=TRUE)

lakes = gBuffer(z2,byid=TRUE,width=1,quadsegs=10)
plot(lakes,col=1:5+1)

pts = matrix( runif(100,1,25),ncol=2)

pts_wkt = paste( "GEOMETRYCOLLECTION(", paste(paste("POINT(",pts[,1],"
",pts[,2],")",sep=""),collapse=",") ,")", sep="")
pts_sp = readWKT(pts_wkt)

cols = apply(gDistance(pts_sp,z3,byid=TRUE),2,which.min)
plot(pts_sp,pch=16,col=cols+1,add=TRUE)
plot(pts_sp,pch=1,add=TRUE)

-- 
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/classify-point-by-nearest-polygon-tp6019714p6030040.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list