Plotting Rasters and Shape file -> no overlap

Hello together,

I want to clip my rasters, which contain environmental variables by a mask, which is my study region. Although the projection and crs of the files is the same, they don`t overlap exactly when I plot them. Has anyone an idea why this does not work?

Here my code:

region <- readOGR("~/H/Ameisen_Arcgis/", "Study_area_ants_LV95")

rast.ele <- raster("swissALT3D_0_5M_elevation.tif")
plot(rast.ele)
rast.ele <- mask(rast.ele, region) ## mask before crop to remove NA`??
rast.ele <- crop(rast.ele, y = region, mask = T, touches = T)
plot(rast.ele, ext = region)
points(pt.22, cex = 0.5)
plot(region, add = T)

Greetings,
misi

Hi @misi,
This looks like a mismatch of the Coordinate Reference System (CRS) between the vector and the raster objects. You can try re-projecting your raster object to match, using the code below. However, this is untested as we don't have your data files:

# Coordinate Reference System (CRS) 
library(rgdal)
library(raster)

region <- readOGR("~/H/Ameisen_Arcgis/", "Study_area_ants_LV95")
ogrInfo(region)  # Check the CRS of this spatial vector object

# Save the CRS projection string
my_proj <- ogrInfo(region)
new_proj <- my_proj$p4s

rast.ele <- raster("swissALT3D_0_5M_elevation.tif")
crs(rast.ele)  # Check the CRS of this raster object

# Reproject the raster object to match the spatial vector object
new.ele <- projectRaster(rast.ele, crs = new_proj)

# Now, try plotting 'region' and 'new.ele' together

Hello @DavoWW ,

thanks a lot for your reply and the code! By entering the code ogrInfo(region) I get an error:

my_proj <- ogrInfo(region)
Error in ogrInfo(region) : is.character(dsn) ist nicht TRUE

Also, I recognise now a warning message, which appears already when I read in the shape file:

region <- readOGR(dsn ="~/H/Ameisen_Arcgis/", layer ="Study_area_ants_LV95")
OGR data source with driver: ESRI Shapefile
Source: "C:\Users\simma\Documents\H\Ameisen_Arcgis", layer: "Study_area_ants_LV95"
with 1 features
It has 4 fields
Integer64 fields read as strings: OBJECTID id
Warning message:
In OGRSpatialRef(dsn, layer, morphFromESRI = morphFromESRI, dumpSRS = dumpSRS, :
Discarded datum CH1903+ in Proj4 definition: +proj=somerc +lat_0=46.9524055555556 +lon_0=7.43958333333333 +k_0=1 +x_0=2600000 +y_0=1200000 +ellps=bessel +towgs84=674.374,15.056,405.346,0,0,0,0 +units=m +no_defs

Maybe the problem starts from there?

Hi @misi,
Sorry for delay in replying.
It seems that R (package rgdal) has a problem recognizing the datum CH1903+ that is used for this shape file. This is a complex topic e.g. see here: https://epsg.io/ and here: https://en.wikipedia.org/wiki/Swiss_coordinate_system

I notice that you may be using ArcGIS to produce/store your shape file. Can you use ArcGIS to save the shapefile in a more universal datum e.g. WGS84? You then may be able to make the same re-projection change to the raster image and get them to line-up correctly.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.