[R-sig-Geo] sp::overlay() not conserving row.names ?

Edzer Pebesma edzer.pebesma at uni-muenster.de
Wed Jul 6 19:29:33 CEST 2011


I'm not too familiar with GIS terminology, but would you agree that
sp::over does (or allows for) a spatial table join?

On 07/06/2011 06:10 PM, Agustin Lobo wrote:
> Yes!
> 
> I'm doing:
>> delme2 <- over(q1km, rndp,fn = mean)
> 
> And now not only the row.names are correct and the order of the 2
> arguments makes more sense, but the dimensions of the result are also
> the same
> that that of the first argument
> (which implies rows of NA for those polygons with no points: the user
> can get rid of
> these rows with na.omit() later, for me having the NA is a lot better):
> 
>> delme2[1:3,]
>         ID      val
> 0 356.3333 760.5662
> 1 454.3333 541.5580
> 2 305.0000 467.0864
>> q1km at data[1:3,]
>   ID   XMIN   XMAX   YMIN   YMAX
> 0  0 -8.994 -8.594 43.595 43.995
> 1  1 -8.594 -8.194 43.595 43.995
> 2  2 -8.194 -7.794 43.595 43.995
> 
>> dim(delme2)
> [1] 299   3
>> dim(q1km at data)
> [1] 299   6
> 
> I can just join this table to the one of q1km:
>> delme2 = cbind(idname=row.names(delme2),delme2)
>> delme2[1:3,]
>   idname       ID      val
> 0      0 356.3333 760.5662
> 1      1 454.3333 541.5580
> 2      2 305.0000 467.0864
> 
>> q1kmv2 = q1km
>> q1kmv2 <- mijoin(q1kmv2,delme2[,-2], by.x=1,by.y=1)
>> q1kmv2 at data[1:3,]
>   ID   XMIN   XMAX   YMIN   YMAX idname      val
> 0  0 -8.994 -8.594 43.595 43.995      0 760.5662
> 1  1 -8.594 -8.194 43.595 43.995      1 541.5580
> 2  2 -8.194 -7.794 43.595 43.995      2 467.0864
> 3  3 -7.794 -7.394 43.595 43.995      3 487.6461
> 
> Thanks!
> Agus
> 
> 2011/7/6 Edzer Pebesma <edzer.pebesma at uni-muenster.de>:
>> Agus, sp::overlay will be deprecated at some stage in favour of
>> sp::over, which is a more consistent and complete approach to the same
>> problem. For instance, overlay(x,y) would do the same as overlay(y,x),
>> which is not good, if you think about it a bit longer (my own mistake,
>> long time ago).
>>
>> Could you please check if sp::over has the same problems? See also
>> http://cran.r-project.org/web/packages/sp/vignettes/over.pdf
>>
>> On 07/06/2011 05:28 PM, Agustin Lobo wrote:
>>> I'm using sp::overlay between a SpPolDF and a SpPointsDF:
>>>
>>>> delme <- overlay(rndp,q1km, fn = mean)
>>>> class(q1km)
>>> [1] "SpatialPolygonsDataFrame"
>>> attr(,"package")
>>> [1] "sp"
>>>> class(rndp)
>>> [1] "SpatialPointsDataFrame"
>>> attr(,"package")
>>> [1] "sp"
>>>> class(delme)
>>> [1] "data.frame"
>>>
>>> but I'm confused by the fact that the row.names(delme) are not the same
>>> as the row.names(q1km):
>>>> delme[1:10,]
>>>           ID       id      val
>>> X1  356.3333 357.3333 643.3162
>>> X2  454.3333 455.3333 653.1833
>>> X3  305.0000 306.0000 101.8930
>>> X4  486.7143 487.7143 448.4612
>>> X5  456.2500 457.2500 544.1646
>>> X6  255.4000 256.4000 139.7507
>>> X7  403.6667 404.6667 659.8114
>>> X8  462.5000 463.5000 537.7209
>>> X9  629.2000 630.2000 399.1040
>>> X10 416.5000 417.5000 278.2879
>>>> q1km at data[1:10,]
>>>    idname ID   XMIN   XMAX   YMIN   YMAX
>>> X0     X0  0 -8.994 -8.594 43.595 43.995
>>> X1     X1  1 -8.594 -8.194 43.595 43.995
>>> X2     X2  2 -8.194 -7.794 43.595 43.995
>>> X3     X3  3 -7.794 -7.394 43.595 43.995
>>> X4     X4  4 -7.394 -6.994 43.595 43.995
>>> X5     X5  5 -6.994 -6.594 43.595 43.995
>>> X6     X6  6 -6.594 -6.194 43.595 43.995
>>> X7     X7  7 -6.194 -5.794 43.595 43.995
>>> X8     X8  8 -5.794 -5.394 43.595 43.995
>>> X9     X9  9 -5.394 -4.994 43.595 43.995
>>>
>>> Is this the way it has to be? If it is, then it's kind of inconvenient
>>> as I would like to join the new table "delme" to q1km at data.
>>> It seems I just have to subtract 1 to the numeric part of
>>> row.names(delme), but would like to make sure.
>>>
>>> Agus
>>>
>>> _______________________________________________
>>> 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
>>
>> _______________________________________________
>> 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