1

I'm working with R. And to have a Rasterfile and a Shapefile. Now I want to fit their CRS/ EPSG so I can plot them together. Several approaches I've tried won't work.

Someone may help?

THE CRS/EPSG:

st_crs(Shape)
CRS: Shapefile crs(Shape) oordinate Reference System: Deprecated Proj.4 representation: +proj=longlat +datum=WGS84 +no_defs WKT2 2019 representation: GEOGCRS["WGS 84", DATUM["World Geodetic System 1984", ELLIPSOID["WGS 84",6378137,298.257223563, LENGTHUNIT["metre",1]]], PRIMEM["Greenwich",0, ANGLEUNIT["degree",0.0174532925199433]], CS[ellipsoidal,2], AXIS["latitude",north, ORDER1, ANGLEUNIT["degree",0.0174532925199433]], AXIS["longitude",east, ORDER[2], ANGLEUNIT["degree",0.0174532925199433]], ID["EPSG",4326]]

crs(Raster)
Coordinate Reference System:
Deprecated Proj.4 representation: +proj=longlat +datum=WGS84 +no_defs 
WKT2 2019 representation:
GEOGCRS["WGS 84 (with axis order normalized for visualization)",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["geodetic longitude (Lon)",east,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433,
                ID["EPSG",9122]]],
        AXIS["geodetic latitude (Lat)",north,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433,
                ID["EPSG",9122]]]] 

I read the the both files by:

Raster <- raster("CMR10adjv4.tif")
Shape <-  st_read("CMR_adm3.shp")

Different approaches to change the EPSG didn't work (see the following error messages):

    shp <- spTransform(Shape, crs(Raster))
    Error in (function (classes, fdef, mtable)  : 
      unable to find an inherited method for function ‘spTransform’ for signature ‘"sf", "CRS"’
st_crs(Shape) <- st_transform(Shape, crs = '+init=EPSG:9122')
Error in CPL_transform(x, crs, aoi, pipeline, reverse, desired_accuracy,  : 
  crs not found: is it missing?
In addition: Warning message:
In CPL_crs_from_input(x) : GDAL Error 1: PROJ: proj_create: crs not found
> pr1 <- projectRaster(Raster, crs= '+init=EPSG:9122')
Error in sp::CRS(...) : NA


     st_crs(Shape) <- st_transform(Shape, crs = '+init=EPSG:9122')
    Error in CPL_transform(x, crs, aoi, pipeline, reverse, desired_accuracy,  : 
      crs not found: is it missing?
    In addition: Warning message:
    In CPL_crs_from_input(x) : GDAL Error 1: PROJ: proj_create: crs not found

Additional information to my Files are:

    > Raster
class      : RasterLayer 
dimensions : 13713, 9238, 126680694  (nrow, ncol, ncell)
resolution : 0.0008333, 0.0008333  (x, y)
extent     : 8.494606, 16.19263, 1.652261, 13.0793  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : CMR10adjv4.tif 
names      : CMR10adjv4 
values     : 0, 1344.71  (min, max)

S

hape
Simple feature collection with 360 features and 15 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 8.499454 ymin: 1.652267 xmax: 16.19105 ymax: 13.07739
Geodetic CRS:  WGS 84
First 10 features:

Using: Shape <- st_transform(Shape, crs(Raster)) giving me a wrong picture like:

Plot Shapefile/ Raster

4
  • Your formatting here is a bit of a mess so I'm not certain what is your input text and what is output. If you can, please edit and make it look tidy when you save it.
    – Spacedman
    Commented Jun 28, 2022 at 20:03
  • yes! I came to that. see my last edit with sf_transform...with no error but also no solution
    – bobo
    Commented Jun 28, 2022 at 20:03
  • I can't replicate your raster problem. Can we get access to the data files, or similar ones that also show the problem, or can you make data we can use?
    – Spacedman
    Commented Jun 28, 2022 at 20:10
  • This question does seem to have about four problems in it which makes it a prime candidate for possible closure for being unfocused. You should maybe restate it as a) what you are trying to achieve (which seems to be "how to project a vector object to the CRS of a raster") and then your attempts and how they fail.
    – Spacedman
    Commented Jun 28, 2022 at 20:12

1 Answer 1

0

This fails because Shape is an sf object and spTransform is for sp objects only:

shp <- spTransform(Shape, crs(Raster))

This fails because what it is trying to do is change the CRS metadata of Shape to the result of a transformed copy of Shape:

st_crs(Shape) <- st_transform(Shape, crs = '+init=EPSG:9122')

Setting the CRS like this is only used when the CRS is wrong or missing. Suppose you read lat-long from a text data file you wouldn't have a CRS, so when you make a sf object you'd have to do something like:

st_crs(mydata) = "epsg:4326"

To do a coordinate transform, save the object from st_transform something like:

Shape = st_transform(Shape, "epsg:9122")
2
  • This gives me the follwowing error message: Error in CPL_transform(x, crs, aoi, pipeline, reverse, desired_accuracy, : crs not found: is it missing? I
    – bobo
    Commented Jun 28, 2022 at 22:22
  • 9122 is the EPSG code for "Degrees" as a unit of angle. Its not a coordinate system code.
    – Spacedman
    Commented Jun 29, 2022 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.