Shapefile to raster conversion in R?

前端 未结 1 1880
南笙
南笙 2021-01-12 06:30

I have a shapefile downloaded from the worldwildlife.org for the terrestrial ecoregions of the world. The file can be loaded here: http://worldwildlife.org/publications/terr

相关标签:
1条回答
  • 2021-01-12 07:29

    You are right to think that you should be using raster (rather than the sp raster Spatial classes) for spatial raster data. You should also use rgdal (rather than maptools) for reading, writing, and otherwise manipulating spatial vector data.

    This should get you started:

    library(rgdal)
    library(raster)
    
    ## Read in the ecoregion shapefile (located in R's current working directory)
    teow <- readOGR(dsn = "official_teow/official", layer = "wwf_terr_ecos")
    
    ## Set up a raster "template" to use in rasterize()
    ext <-  extent (-95, -50, 24, 63)
    xy <- abs(apply(as.matrix(bbox(ext)), 1, diff))
    n <- 5
    r <- raster(ext, ncol=xy[1]*n, nrow=xy[2]*n)
    
    ## Rasterize the shapefile
    rr <-rasterize(teow, r)
    
    ## A couple of outputs
    writeRaster(rr, "teow.asc")
    plot(rr)
    

    enter image description here

    0 讨论(0)
提交回复
热议问题