[R-sig-Geo] Assign a point to its nearest polygon

Edzer Pebesma edzer.pebesma at uni-muenster.de
Thu Jun 16 13:01:07 CEST 2011


So I missed your answer. For finding nearest polygons, try:

library(sp)
loadMeuse()
pols = as(meuse.grid[sample(length(meuse.grid),10),], "SpatialPolygons")
plot(meuse)
plot(pols, col = 'red', add=T)
library(rgeos)
m = gDistance(meuse, pols,byid=TRUE)
dim(m)
col = apply(m, 2, function(x) which(x==min(x)))
plot(meuse, col = col, add=TRUE)

(note that R has not so many unique colors if passed as numbers, they
recycle pretty quickly)

On 06/16/2011 12:44 PM, Mathieu Rajerison wrote:
> Hi,
> 
> 
> Thanks for your answers!
> 
> @paul: Overlay will not work as my polygons do not overlay any points.
> 
> @José: I know v.distance command, but I was interested in doing the same in
> R.
> 
> 
> So, if you have any idea on how to accomplish that in R!..
> 
> Mathieu
> 
> 2011/6/16 José Miguel Barrios <jmbarriosg at gmail.com>
> 
>> Hi,
>>
>> What about using spgrass6 to interface with GRASS and use v.distance in
>> GRASS.?
>>
>> http://grass.fbk.eu/grass62/manuals/html62_user/v.distance.html
>>
>>
>> Regards,
>>
>> J.Miguel
>>
>> 2011/6/16 Mathieu Rajerison <mathieu.rajerison at gmail.com>
>>
>>> HI,
>>>
>>>
>>> I've got a SpatialPointsDataFrame with labels of polygons but the polygon
>>> objects do not contain these labels.
>>>
>>> I want to find a way to assign the labels to their nearest polygons.
>>>
>>> I tried spatstat:nncros but my SpatialPolygonsDataFrame, when coerced to a
>>> psp object, is split up in many many lines. I couldn't find a way, with
>>> spatstat to accomplish this operation. But there may be one (?)
>>>
>>> I found out the geosphere package with function dist2Line. It seems nice
>>> because it works with both SpatialPointsData and SpatialPolygons.
>>>
>>> But when launching the command, I get:
>>>
>>> Erreur dans geosphere:::.pointsToMatrix(p) :
>>>  Points are projected. They should be in degrees (longitude/latitude)
>>>
>>> So my questions are:
>>> -is there a way to make this assignment easily with spatstat::nncross
>>> -is geosphere::dist2Line preferrable and if so, how can I convert my
>>> projected coordinates into lon/lat: any package existent? or necessary
>>> to type in the formula
>>> <
>>> http://www.ehow.co.uk/how_8449009_convert-xy-coordinates-longitude-latitude.html
>>>> ?
>>>
>>>
>>> Thanks in advance, any help would be appreciated
>>>
>>> Mathieu
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>
>>
> 
> 	[[alternative HTML version deleted]]
> 
> 
> 
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics      e.pebesma at wwu.de



More information about the R-sig-Geo mailing list