在R中创建等距空间栅格 [英] Creating an equal distance spatial grid in R

查看:7
本文介绍了在R中创建等距空间栅格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R在给定的区域上创建一个等维度的正方形网格。我希望我的网格是1千米x 1千米的正方形。我看到了这样的示例,它们说明了相等的经度/经度网格:

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

但这甚至不是大小。看起来我应该能够使用st_make_grid函数来创建这个,但是我不知道如何创建1千米x 1千米的网格。

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

例如,我想从(37,-89.2)开始,到(36.2,-86.8)结束,然后创建一个1公里x 1公里的等间距网格。我该如何使用R来执行此操作?

注意:看似棘手的部分是,在非常大的区域内真正保持1kmx1 kM的电网。我可以保持网格在十进制度下的尺寸相等,但在地面上这不是相等的距离。

我能够使用PostGIS执行此操作,这要归功于crafty answer here.我在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中创建等距空间栅格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆