我正在尝试使用R在给定区域上制作等尺寸的正方形网格。我希望我的网格为1km x 1km正方形。我看到这样的例子,它们说明了相等的经/纬网格:

Creating a regular polygon grid over a spatial extent, rotated by a given angle

但这还不算大小。看来我应该能够使用st_make_grid函数并创建它,但是我无法理解如何将网格设置为1km x 1km。

https://r-spatial.github.io/sf/reference/st_make_grid.html

例如,我想从(37,-89.2)开始,到(36.2,-86.8)结束,并创建一个1km x 1km的均匀间隔的网格。我将如何用R做到这一点?

注意:看来棘手的部分是在很大的区域内将网格保持为真正的1km x 1km。我可以以十进制度度保持网格相等的尺寸,但是在地面上的距离不相等。

由于有了crafty answer here.,我已经能够使用PostGIS做到这一点。在PostGIS中,我创建了一个函数:

CREATE OR REPLACE FUNCTION public.makegrid_2d (
  bound_polygon public.geometry,
  width_step integer,
  height_step integer
)
RETURNS public.geometry AS
$body$
DECLARE
  Xmin DOUBLE PRECISION;
  Xmax DOUBLE PRECISION;
  Ymax DOUBLE PRECISION;
  X DOUBLE PRECISION;
  Y DOUBLE PRECISION;
  NextX DOUBLE PRECISION;
  NextY DOUBLE PRECISION;
  CPoint public.geometry;
  sectors public.geometry[];
  i INTEGER;
  SRID INTEGER;
BEGIN
  Xmin := ST_XMin(bound_polygon);
  Xmax := ST_XMax(bound_polygon);
  Ymax := ST_YMax(bound_polygon);
  SRID := ST_SRID(bound_polygon);

  Y := ST_YMin(bound_polygon); --current sector's corner coordinate
  i := -1;
  <<yloop>>
  LOOP
    IF (Y > Ymax) THEN
        EXIT;
    END IF;

    X := Xmin;
    <<xloop>>
    LOOP
      IF (X > Xmax) THEN
          EXIT;
      END IF;

      CPoint := ST_SetSRID(ST_MakePoint(X, Y), SRID);
      NextX := ST_X(ST_Project(CPoint, $2, radians(90))::geometry);
      NextY := ST_Y(ST_Project(CPoint, $3, radians(0))::geometry);

      i := i + 1;
      sectors[i] := ST_MakeEnvelope(X, Y, NextX, NextY, SRID);

      X := NextX;
    END LOOP xloop;
    CPoint := ST_SetSRID(ST_MakePoint(X, Y), SRID);
    NextY := ST_Y(ST_Project(CPoint, $3, radians(0))::geometry);
    Y := NextY;
  END LOOP yloop;

  RETURN ST_Collect(sectors);
END;
$body$
LANGUAGE 'plpgsql';

然后我可以调用它并将其传递给多边形:
SELECT (
    ST_Dump(
      makegrid_2d(
        ST_GeomFromText(
          'Polygon((-75 42, -75 40, -73 40, -73 42, -75 42))',
          4326
        )  ,
         1000, -- width step in meters
         1000  -- height step in meters
       )
    )
  ) .geom AS cell into test_grid_cell;

但是正如您所看到的,即使使用PostGIS,这也不是固定的例程。稍加努力,我想我可以将其移植到sf,但是我对此并不感到过分兴奋...

最佳答案

考虑这个例子。它使用布拉格市的边界作为网格的基础。

关键部分是确保sf对象位于公制CRS中(如果您是美国人并且感到爱国,则应选择脚尖的对象);只要在预计的CRS中是哪个都无所谓(即st_is_longlat(x)返回FALSE)。

library(dplyr)
library(RCzechia) # a package of Czech administrative areas
library(sf)


mesto <- kraje() %>% # All Czech NUTS3 ...
  filter(NAZ_CZNUTS3 == 'Hlavní město Praha') %>% # ... city of Prague
  st_transform(5514) # a metric CRS

grid_spacing <- 1000  # size of squares, in units of the CRS (i.e. meters for 5514)

polygony <- st_make_grid(mesto, square = T, cellsize = c(grid_spacing, grid_spacing)) %>% # the grid, covering bounding box
  st_sf() # not really required, but makes the grid nicer to work with later

plot(polygony, col = 'white')
plot(st_geometry(mesto), add = T)

r - 在R中创建等距空间网格-LMLPHP

关于r - 在R中创建等距空间网格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53789313/

10-09 01:20
查看更多