将多个多边形的shapefile转换为R中的栅格


仙风道骨刘憨
2025-03-12 03:36:37 (9天前)
  1. 我在将多边形转换为R中的栅格时遇到了大麻烦。我想要做的是:我有574种的shapefile(即多边形)。那是在属性表中它有574行(即FID介于0 ...

2 条回复
  1. 0# 敢嫁就敢娶 | 2019-08-31 10-32



    你用的时候

    rasterize

    功能,重要的是指定

    field

    参数,否则默认会尝试为你创建一个;在您的情况下,它看起来像是从FID列创建的。



    我做了一些猜测来重新生成一组可能与你的类似的工作多边形。




    1. library(maptools)
      library(rgdal)
      library(sp)
      library(geosphere)

    2. set seed for duplicatable results

      set.seed(1)

    3. some data that looks a little like yours

      BINOMIAL <- c(“Controversial chimneyswift”, Dull dungbeetle”,
      Easternmost eel”, Jumping jaeger”, Qualified queenconch”)
      FID <- 0:(length(BINOMIAL) - 1)
      RANGE <- runif(length(BINOMIAL), min = 118, max = 3875370)
      MyData <- cbind.data.frame(FID, BINOMIAL, RANGE)
      row.names(MyData) <- FID

    4. some semi-random polygons in your extent box

      ext <- extent(c(-180, 180, -60, 90))
      create_polygon <- function(n = 4, lat, lon, r) {
      lengths <- rnorm(n, r, r/3)
      smoother_lengths <- c(sort(lengths), rev(sort(lengths)))
      lengths <- smoother_lengths[sort(sample(n * 2, n))]
      lengths <- rep(lengths[1], length(lengths))
      directions <- sort(runif(n, 0, 360))
      p <- cbind(lon, lat)
      vertices <- t(mapply(destPoint, b = directions,
      d = lengths, MoreArgs = list(p = p)))
      vertices <- rbind(vertices, vertices[1, ])
      sapply(vertices[,1], min, ext@xmax)
      sapply(vertices[,1], max, ext@xmin)
      sapply(vertices[,2], min, ext@ymax)
      sapply(vertices[,2], max, ext@ymin)
      Polygon(vertices)
      }
      rand_lats <- runif(nrow(MyData), min = -50, max = 60)
      rand_lons <- runif(nrow(MyData), min = -100, max = 100)
      rand_sides <- sample(4:20, nrow(MyData), replace = TRUE)
      rand_sizes <- rnorm(nrow(MyData), mean = 5e+06, sd = 1e+06)
      make_species_polygon <- function(i) {
      p.i <- list(create_polygon(rand_sides[i], rand_lats[i],
      rand_lons[i], rand_sizes[i]))
      P.i <- Polygons(p.i, FID[i])
      }

    5. polys <- SpatialPolygons(lapply(1:nrow(MyData), make_species_polygon))
      spdf <- SpatialPolygonsDataFrame(Sr = polys, data = MyData)

    6. t.shp <- tempfile(pattern = MyShapefile”, fileext = “.shp”)
      raster::shapefile(spdf, t.shp)

    7. </code>


    此时,在temp目录中写入了一个shapefile,其名称存储在变量t.shp中。我打算将shapefile作为一个可行的副本,无论大的shapefile是你真正的那个。现在我们可以查看您的代码,它正在做什么以及您希望它做什么:



    1.   ## now we get into your code
    2. library(raster)
      library(rgdal)
      library(maptools)

    3. define porjection

      projection1 <- CRS (“+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs”)

    4. I dont know what your shapefile looks like exactly,

      but substituting t.shp the tempfile that I created above

      also since the function readShapePoly is deprecated

      sp <- rgdal::readOGR(t.shp)

    5. #

      I don’t know what your tiff file looks like exactly,

      but I can duplicate its characteristics

      for speed I have decreased resolution by a factor of 10

      #

      raster1 <- raster(nrow = 1800, ncol = 4320, ext)

    6. rasterize our species polygon to the same resoluton of loaded raster

      r.sp <- rasterize(x = sp, y = raster1, field = MyData$RANGE)

    7. t.tif <- tempfile(pattern = MyRastfile”, fileext = “.tif”)
      writeRaster(r.sp, t.tif, format = GTiff”, overwrite = TRUE)

    8. </code>


    结果如下:




    1. raster(t.tif)
      class : RasterLayer
      dimensions : 1800, 4320, 7776000 (nrow, ncol, ncell)
      resolution : 0.08333333, 0.08333333 (x, y)
      extent : -180, 180, -60, 90 (xmin, xmax, ymin, ymax)
      coord. ref. : NA
      data source : [[“a file name in your temp directory”]]
      names : MyRastfile1034368f3cec
      values : 781686.3, 3519652 (min, max)

    2. </code>


    结果现在显示的是从RANGE列而不是FID列中获取的值。


登录 后才能参与评论