Changing CRS of a SF object
Asked Answered
J

1

16

I have some 'roads' as a sp object:

class       : SpatialLinesDataFrame 
features    : 17360 
extent      : 490176.4, 567680.9, 148639.1, 212821  (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:27700 +proj=tmerc +lat_0=49 +lon_0=-2     +k=0.9996012717 +x_0=400000 +y_0=-100000 +datum=OSGB36 +units=m +no_defs     +ellps=airy +towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894 
variables   : 4
names       :    osm_id, lanes,     type, width 
min values  :        73,     2, motorway,   6.3 
max values  : 587969162,     6,    trunk,  18.9

I convert them to a sf object like so:

roads                   <- st_as_sf(roads, crs = 27700)

After conversion, the SRID is empty, and the proj4string (possibly) looks incorrect. Why is this? Can I fix it?

Simple feature collection with 17360 features and 4 fields
geometry type:  LINESTRING
dimension:      XY
bbox:           xmin: 490176.4 ymin: 148639.1 xmax: 567680.9 ymax: 212821
epsg (SRID):    NA
proj4string:    +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.1502,0.247,0.8421,-20.4894 +units=m +no_defs
First 10 features:
     osm_id lanes    type width                       geometry
73       73     2 primary   6.3 LINESTRING (526256 191950, ...
74       74     2 primary   6.3 LINESTRING (525236.4 190755...
75       75     2 primary   6.3 LINESTRING (526324.4 192518...
79       79     2 primary   6.3 LINESTRING (524912.4 190235...
482     482     2 primary   6.3 LINESTRING (527283.9 198571...
546     546     2 primary   6.3 LINESTRING (506089.1 205091...
1210   1210     2 primary   6.3 LINESTRING (526023.9 195013...
1213   1213     2 primary   6.3 LINESTRING (524861.1 196243...
1214   1214     2 primary   6.3 LINESTRING (524482.3 196798...
1698   1698     2 primary   6.3 LINESTRING (527335.3 195524...

I think I need to fix this, because I then want to st_transform() my data to the same CRS as a raster I have to do some extraction of values.

crs(raster)
CRS arguments:
 +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs

I presume I can convert my 'roads' to the correct CRS by doing something like this? But it doesn't work. I get ....

roads <- st_transform(roads, crs(raster))
'Error: cannot create a crs from an object of class CRS

Help appreciated. Thank you.

Jonahjonas answered 16/5, 2018 at 13:40 Comment(1)
raster uses S4 classes, hence you need to call the appropriate slot to get the prj4string which sf can understand. roads <- st_transform(roads, crs(raster)@projargs) should do the trick.Bike
H
31

You have to adapt the st_transform arguments like that:

roads <- st_transform(roads, crs = 4326)

OR

roads <- st_transform(roads, crs = proj4string(raster))

OR as @TimSalabim said:

roads <- st_transform(roads, crs = crs(raster)@projargs)

OR as @R'n'E pointed out:

roads <- st_transform(roads, crs = st_crs(raster))
Hostess answered 16/5, 2018 at 13:55 Comment(1)
Seems like the code needs a slight update. Using st_crs() from sf_0.6-3 : roads <- st_transform(roads, crs = st_crs(raster))Weiland

© 2022 - 2024 — McMap. All rights reserved.