inconsistent as.data.frame(SpatialPointsDF)
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 -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 490 bytes Desc: OpenPGP digital signature URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20150320/4b2333c1/attachment.bin>