[R-sig-Geo] get X and Y form point shapefile

Michael Sumner mdsumner at gmail.com
Wed Feb 10 23:33:52 CET 2010


Hello,

On Thu, Feb 11, 2010 at 9:17 AM, gianni lavaredo
<gianni.lavaredo at gmail.com> wrote:
> Thanks Mike,
>
> i have this question. Is It possible to get X,Y and have two new columns
> inside the dbf of shape? as GET X and GET Y of ArcMap
>

I have no idea about any of ArcMap's functions, and I'm sure many on
this list do not know either. I'm guessing that you want to add new
columns to the data-attributes of the object - this can be done
directly like this.

Assuming that sp package is loaded, and that "X" and "Y" do not
already exist, or you don't mind replacing them:

sample.df$X <-  coordinates(sample.df)[,1]
sample.df$Y <-  coordinates(sample.df)[,2]

A SpatialPointsDataFrame has no connection to the original shp/shx/dbf
files - you would need to write this object out to get the same result
in a shapefile.

Cheers, Mike.

> Gianni
>
> 2010/2/10 Michael Sumner <mdsumner at gmail.com>
>>
>> xy <- coordinates(sample.shp)
>>
>> will give you a 2-column matrix "xy" of the X and Y coordinates.
>>
>> For more complete reporting for this list, please provide the packages
>> you are using and the version. The easiest way is to use sessionInfo()
>>
>> See here for more information, and the posting guide:
>> http://www.r-project.org/mail.html
>>
>> Cheers, Mike.
>>
>> On Thu, Feb 11, 2010 at 8:54 AM, gianni lavaredo
>> <gianni.lavaredo at gmail.com> wrote:
>> > dear Rearchers
>> >
>> > I know this is easy but after 5 hours on R, I am honest and  I don't
>> > rememeber to get X, Y coordinate from point shapefile
>> >
>> > sample.shp <- readShapeSpatial("sample.shp")
>> > proj4string(sample.shp) <- CRS("+init=epsg:26911")
>> > str(sample.shp)
>> >  str(sample.shp)
>> >
>> > Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
>> >  ..@ data       :'data.frame': 50372 obs. of  1 variable:
>> >  .. ..$ Z: num [1:50372] 1498 1498 1499 1499 1503 ...
>> >  .. ..- attr(*, "data_types")= chr "N"
>> >  ..@ coords.nrs : num(0)
>> >  ..@ coords     : num [1:50372, 1:2] 268753 268754 268763 268761 268774
>> > ...
>> >  .. ..- attr(*, "dimnames")=List of 2
>> >  .. .. ..$ : chr [1:50372] "0" "1" "2" "3" ...
>> >  .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
>> >  ..@ bbox       : num [1:2, 1:2] 268501 4147540 268881 4147786
>> >  .. ..- attr(*, "dimnames")=List of 2
>> >  .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
>> >  .. .. ..$ : chr [1:2] "min" "max"
>> >  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
>> >  .. .. ..@ projargs: chr "+init=epsg:26911"
>> >
>> > thanks
>> > gianni
>> >
>> >        [[alternative HTML version deleted]]
>> >
>> > _______________________________________________
>> > R-sig-Geo mailing list
>> > R-sig-Geo at stat.math.ethz.ch
>> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> >
>
>



More information about the R-sig-Geo mailing list