[R-sig-Geo] inconsistent as.data.frame(SpatialPointsDF)

MacQueen, Don macqueen1 at llnl.gov
Fri Mar 20 18:10:38 CET 2015


Thanks, Edzer, this is helpful.

-Don


-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 3/20/15, 9:28 AM, "Edzer Pebesma" <edzer.pebesma at uni-muenster.de> wrote:

>There is some logic: sp tries to track coordinate names when it can, but
>if coordinates are set as a nameless matrix, as in the last example
>below, it will choose names itself. coordnames() helps you discover how
>the coordinates of a SpatialPointsDataFrame are called:
>
>> df = data.frame(x=1:2, y=2:1, z = 3:4)
>> df1 = df
>> library(sp)
>> coordinates(df1) = ~x+y
>> as.data.frame(df1)
>  x y z
>1 1 2 3
>2 2 1 4
>> coordnames(df1)
>[1] "x" "y"
>> df1=df
>> coordinates(df1) = df[1:2]
>> coordnames(df1)
>[1] "x" "y"
>> df1=df
>> coordinates(df1) = cbind(df$x,df$y) # nameless
>> df1
>  coordinates x y z
>1      (1, 2) 1 2 3
>2      (2, 1) 2 1 4
>> coordnames(df1)
>[1] "coords.x1" "coords.x2"
>
>
>a bit of a semantic trap is this: if your coordinates are longitude and
>latitude and carry these names, after you project the object with
>rgdal::spTransform they're still called longitude and latitude, although
>they are no longer understood as such. You can then solve this yourself by
>
>> coordnames(df1) = c("x", "y")
>
>after which
>
>> coordnames(df1)
>[1] "x" "y"
>
>
>On 03/20/2015 05:01 PM, MacQueen, Don wrote:
>> In my experience, relying on column names to extract the coordinates is
>> not at all a good idea. I would strongly recommend that you take the
>>time
>> to update all of your scripts to use the coordinates() function. I think
>> it will be worth it in the long run.
>> 
>> It's not a good idea because the column names of the coordinates depend
>>on
>> how the SpatialPointsDataFrame was originally created, and in my own
>> applications that is highly variable.  Sometimes ('x','y'), sometimes
>> ('lon','lat'), or any of several other variations of how to spell or
>> abbreviate latitude and longitude (with or without capitalization). Or
>> ('easting','northing'). Or, or, or... Trying to carefully control all
>>that
>> is more trouble than it's worth; I just use, for example,
>> coordinates(obj)[,1] and coordinates(obj)[,2] if I want to pull them out
>> as vectors. Ugly, but I can count on it.
>> 
>> That said, if
>>   as.data.frame(locs)
>> produces different names for the coordinates when used in different
>> contexts, then you've got something else going on that should not be
>>going
>> on. This is where Frede's suggestions might help. You will need to
>> carefully track the construction of your locs object and see if it is
>> somehow different in the two situations. I don't know of any "designed
>> circumstance" that would explain this.
>> 
>> -Don
>> 
>
>-- 
>Edzer Pebesma
>Institute for Geoinformatics (ifgi),  University of Münster,
>Heisenbergstraße 2, 48149 Münster, Germany; +49 251 83 33081
>Journal of Statistical Software:   http://www.jstatsoft.org/
>Computers & Geosciences:   http://elsevier.com/locate/cageo/
>Spatial Statistics Society http://www.spatialstatistics.info
>



More information about the R-sig-Geo mailing list