SF对象CRS转换异常及坐标系匹配问题求助
Hey there, let's break down and fix these CRS problems step by step:
1. Repair the sf object's missing SRID and incomplete CRS
When you converted your SpatialLinesDataFrame to sf with st_as_sf(roads, crs = 27700), you might have overwritten the full, official CRS definition that was already present in the original spatial object. Instead, use st_set_crs() with the EPSG code to load the complete, standardized CRS for EPSG:27700 (which includes the datum=OSGB36 info you're missing):
# Set the correct CRS using the EPSG code roads <- st_set_crs(roads, 27700) # Verify the fix st_crs(roads)
This should now show epsg (SRID): 27700 along with the full proj4string including the datum details.
2. Fix the st_transform error
The error cannot create a crs from an object of class CRS happens because st_transform() expects an sf-compatible CRS object (from st_crs()), not the sp-style CRS object returned by crs(raster). You have two simple fixes here:
Option 1: Convert the raster's CRS to an sf-compatible object
# Get the raster's CRS in sf format target_crs <- st_crs(crs(raster)) # Transform the roads sf object roads <- st_transform(roads, target_crs)
Option 2: Use the proj4 string directly from the raster
# Extract the proj4 string as a character target_proj <- as.character(crs(raster)) # Transform using the proj4 string roads <- st_transform(roads, target_proj)
3. Verify the transformation
After running either option, check that the CRS matches your raster:
# Check roads CRS st_crs(roads) # Compare to raster CRS crs(raster)
They should now align, and you can proceed with extracting values from your raster.
内容的提问来源于stack exchange,提问作者TheRealJimShady

