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

Edzer Pebesma edzer.pebesma at uni-muenster.de
Thu Jun 16 12:51:14 CEST 2011


Some additions to Paul's comments:

-- method "over" was written to replace "overlay", as overlay has
inconsistencies (like overlay(x,y) would be identical to (overlay(y,x)).
-- if some of your points are not inside a polygon (you mentioned
"nearest"), function gDistance in package rgeos might be used to get
distances from points to polygons, from there it is fairly easy to get
the nearest
-- if some points fall in more than one polygon, over(x,y,returnList)
can return all polygons a point falls in

Hth,

On 06/16/2011 11:48 AM, Paul Hiemstra wrote:
>  Hi Mathieu,
> 
> You could take a look at the overlay() command from the sp-package. You
> could use this function to find out which  points are in which polygons.
> This should allow you assign the point label to a polygon, especially if
> one polygon is associated with just one point.
> 
> cheers,
> Paul
> 
> On 06/16/2011 09:33 AM, Mathieu Rajerison wrote:
>> 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
> 
> 

-- 
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