0

I need to import a shapefile into R and turn it into a Raster object. I am using the following code:

map <- readOGR("path and file name")
r <- raster(ncol=4001, nrow=4001)
r <- rasterize(map, r)

When I import the shapefile, the resulting SpatialPolygonsDataFrame has the correct coordinates and extent:

map class : SpatialPolygonsDataFrame features : 1 extent : 22.97518, 23.09783, -34.09091, -34.0243 (xmin, xmax, ymin, ymax) crs : +proj=longlat +datum=WGS84 +no_defs variables : 11 names : Name, descriptio, timestamp, begin, end, altitudeMo, tessellate, extrude, visibility, drawOrder, icon value : Knysna box, NA, NA, NA, NA, NA, -1, 0, -1, NA, NA

However, as soon as I try to rasterize the object, it completely changes the coordinates and extent:

r class : RasterLayer dimensions : 4001, 4001, 16008001 (nrow, ncol, ncell) resolution : 0.08997751, 0.04498875 (x, y) extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) crs : +proj=longlat +datum=WGS84 +no_defs source : memory names : layer values : 1, 1 (min, max) attributes : ID Name descriptio timestamp begin end altitudeMo tessellate extrude visibility drawOrder 1 Knysna box -1 0 -1 icon

Why is it doing this and how can I fix it? Any help would be greatly appreciated!

Chantel
  • 3
  • 1
  • You can specify the coordinate system with afterwards crs(r) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0" as shown here https://gis.stackexchange.com/questions/111226/how-to-assign-crs-to-rasterlayer-in-r – soph May 11 '23 at 07:54
  • Thanks for your quick response - the crs is already set to WGS84, and even if I run your code, it still has the same changed coordinates and extent:

    crs(r) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0" r

    class : RasterLayer dimensions : 4001, 4001, 16008001 (nrow, ncol, ncell) resolution : 0.08997751, 0.04498875 (x, y) extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) crs : +proj=longlat +datum=WGS84 +no_defs source : memory

    – Chantel May 11 '23 at 07:58
  • maybe try and check out this post https://gis.stackexchange.com/questions/154195/how-to-rasterize-a-shapefile-in-r – soph May 11 '23 at 08:03
  • 1
    Ok setting the extent manually when rasterizing seems to have done the trick! Thanks so much! – Chantel May 11 '23 at 08:20
  • Your welcome! :) – soph May 11 '23 at 08:21
  • maybe you can accept the answer I added below? – soph May 11 '23 at 08:48

1 Answers1

0

This is the code for converting a shapefile into a raster

## german borders
library(rgdal)
library(rworldmap)
library(raster)

data(countriesCoarse) spy_germany <- subset(countriesCoarse, GEOUNIT == "Germany") sln_germany <- as(spy_germany, "SpatialLinesDataFrame")

raster template

rst_template <- raster(ncols = 30, nrows = 60, crs = projection(sln_germany), ext = extent(sln_germany))

rasterize

rst_germany <- rasterize(sln_germany, rst_template) plot(rst_germany, col = "grey75", legend = FALSE, xlab = "lon", ylab = "lat") plot(rasterToPolygons(rst_germany), add = TRUE)

as @fdetsch answered in this post

soph
  • 320
  • 2
  • 11