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!

6
  • 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 gis.stackexchange.com/questions/111226/…
    – soph
    May 11 at 7: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 at 7:58
  • maybe try and check out this post gis.stackexchange.com/questions/154195/…
    – soph
    May 11 at 8:03
  • 1
    Ok setting the extent manually when rasterizing seems to have done the trick! Thanks so much!
    – Chantel
    May 11 at 8:20
  • Your welcome! :)
    – soph
    May 11 at 8:21

1 Answer 1

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

Your Answer

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

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