postgis生成網(wǎng)格

用的比較多的網(wǎng)格有六邊形網(wǎng)格、正方形網(wǎng)格、長(zhǎng)方形網(wǎng)格。網(wǎng)格的生成方法有很多種,在這里做一個(gè)分類,按照實(shí)現(xiàn)方式分為:

  • 前端實(shí)現(xiàn)
  • 后端實(shí)現(xiàn)(java、python、C#)
  • 數(shù)據(jù)庫(kù)層實(shí)現(xiàn)
前端實(shí)現(xiàn)的方式可以使用turf進(jìn)行網(wǎng)格生成rurf鏈接
image.png
后端實(shí)現(xiàn)方式目前只嘗試過(guò)一種:使用geotools vector grid包 link
ReferencedEnvelope gridBounds =
                new ReferencedEnvelope(110.0, 150.0, -45.0, -5.0, DefaultGeographicCRS.WGS84);

        SimpleFeatureSource grid = Grids.createSquareGrid(gridBounds, 10.0);
image.png
最后一種就是借助postgis,通過(guò)數(shù)據(jù)庫(kù)生成
首先是生成六邊形網(wǎng)格 link
----------------------------------------------------------------------------------
-- INPUT
--
--   areakm2     : area of each hexagon in square km.
--                   - note: hexagon size can be off slightly due to coordinate rounding in the calcs.
--
--   xmin, ymin  : min coords of the grid extents.
--
--   xmax, ymax  : max coords of the grid extents.
--
--   inputsrid   : the coordinate system (SRID) of the input min/max coords.
--
--   workingsrid : the SRID used to process the polygons.
--                   - SRID must be a projected coord sys (i.e. in metres) as the calcs require ints. Degrees are out.
--                   - should be an equal area SRID such as Albers or Lambert Azimuthal (e.g. Australia = 3577, US = 2163).
--                   - using Mercator will NOT return hexagons of equal area due to its distortions (don't try it in Greenland).
--
--   ouputsrid   : the SRID of the output polygons.
--
-- NOTES
--
--   Hexagon height & width are rounded up & down to the nearest metre, hence the area may be off slightly.
--   This is due to the Postgres generate_series function which doesn't support floats.
--
--   Why are my areas wrong in QGIS, MapInfo, etc...?
--      Let's assume you created WGS84 lat/long hexagons, you may have noticed the areas differ by almost half in a desktop GIS
--      like QGIS or MapInfo Pro. This is due to the way those tools display geographic coordinate systems like WGS84 lat/long.
--      Running the following query in PostGIS will confirm the min & max sizes of your hexagons (in km2):
--
--         SELECT (SELECT (MIN(ST_Area(geom::geography, FALSE)) / 1000000.0)::numeric(10,3) From my_hex_grid) AS minarea,
--               (SELECT (MAX(ST_Area(geom::geography, FALSE)) / 1000000.0)::numeric(10,3) From my_hex_grid) AS maxarea;
--
--------------------------------------------------------------------------------------------------------------------------------

--DROP FUNCTION IF EXISTS hex_grid(areakm2 FLOAT, xmin FLOAT, ymin FLOAT, xmax FLOAT, ymax FLOAT, inputsrid INTEGER,
--  workingsrid INTEGER, ouputsrid INTEGER);
CREATE OR REPLACE FUNCTION hex_grid(areakm2 FLOAT, xmin FLOAT, ymin FLOAT, xmax FLOAT, ymax FLOAT, inputsrid INTEGER,
  workingsrid INTEGER, ouputsrid INTEGER)
  RETURNS SETOF geometry AS
$BODY$

DECLARE
  minpnt GEOMETRY;
  maxpnt GEOMETRY;
  x1 INTEGER;
  y1 INTEGER;
  x2 INTEGER;
  y2 INTEGER;
  aream2 FLOAT;
  qtrwidthfloat FLOAT;
  qtrwidth INTEGER;
  halfheight INTEGER;

BEGIN

  -- Convert input coords to points in the working SRID
  minpnt = ST_Transform(ST_SetSRID(ST_MakePoint(xmin, ymin), inputsrid), workingsrid);
  maxpnt = ST_Transform(ST_SetSRID(ST_MakePoint(xmax, ymax), inputsrid), workingsrid);

  -- Get grid extents in working SRID coords
  x1 = ST_X(minpnt)::INTEGER;
  y1 = ST_Y(minpnt)::INTEGER;
  x2 = ST_X(maxpnt)::INTEGER;
  y2 = ST_Y(maxpnt)::INTEGER;

  -- Get height and width of hexagon - FLOOR and CEILING are used to get the hexagon size closer to the requested input area
  aream2 := areakm2 * 1000000.0;
  qtrwidthfloat := sqrt(aream2/(sqrt(3.0) * (3.0/2.0))) / 2.0;
  
  qtrwidth := FLOOR(qtrwidthfloat);
  halfheight := CEILING(qtrwidthfloat * sqrt(3.0));

  -- Return the hexagons - done in pairs, with one offset from the other
  RETURN QUERY (
    SELECT ST_Transform(ST_SetSRID(ST_Translate(geom, x_series::FLOAT, y_series::FLOAT), workingsrid), ouputsrid) AS geom
      FROM generate_series(x1, x2, (qtrwidth * 6)) AS x_series,
           generate_series(y1, y2, (halfheight * 2)) AS y_series,
           (
             SELECT ST_GeomFromText(
               format('POLYGON((0 0, %s %s, %s %s, %s %s, %s %s, %s %s, 0 0))',
                 qtrwidth, halfheight,
                 qtrwidth * 3, halfheight,
                 qtrwidth * 4, 0,
                 qtrwidth * 3, halfheight * -1,
                 qtrwidth, halfheight * -1
               )
             ) AS geom
             UNION
             SELECT ST_Translate(
               ST_GeomFromText(
                 format('POLYGON((0 0, %s %s, %s %s, %s %s, %s %s, %s %s, 0 0))',
                   qtrwidth, halfheight,
                   qtrwidth * 3, halfheight,
                   qtrwidth * 4, 0,
                   qtrwidth * 3, halfheight * -1,
                   qtrwidth, halfheight * -1
                 )
               )
             , qtrwidth * 3, halfheight) as geom
           ) AS two_hex);

END$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

CREATE TABLE us_hex_grid (
  gid SERIAL NOT NULL PRIMARY KEY,
  geom GEOMETRY('POLYGON', 4326, 2) NOT NULL
)
WITH (OIDS=FALSE);


INSERT INTO us_hex_grid (geom)
SELECT hex_grid(5, 119.95, 30.03, 120.43, 30.43, 4326, 3857, 4326);
image.png
生成正方形網(wǎng)格 link
raster ST_MakeEmptyRaster(raster rast);

raster ST_MakeEmptyRaster(integer width, integer height, float8 upperleftx, float8 upperlefty, float8 scalex, float8 scaley, float8 skewx, float8 skewy, integer srid=unknown);

raster ST_MakeEmptyRaster(integer width, integer height, float8 upperleftx, float8 upperlefty, float8 pixelsize);

--width  寬上多少個(gè)格子
-- height 高上多少格子
-- upperlefty 左上角坐標(biāo)y
-- upperleftx 坐上角坐標(biāo)x
-- pixelsize 距離
-- scalex x方向距離
-- scaley y方向距離
-- skewx x方向上旋轉(zhuǎn)
-- skewy y方向上旋轉(zhuǎn)
SELECT
    (
        ST_PixelAsPolygons (
            ST_AddBand (
                ST_SetSRID(ST_MakeEmptyRaster( 50, 30, 119.96, 30.40, 0.01),4326),
                '8BSI' :: TEXT,
                1,
                0
            ),
            1,
            FALSE
        )).geom
        
        
image.png
生成矩形
-- scalex x方向距離
-- scaley y方向距離 
-- scalex scaley帶正負(fù)號(hào),和象限一致
SELECT
    (
        ST_PixelAsPolygons (
            ST_AddBand (
                ST_MakeEmptyRaster( 50, 50, 119.96, 30.40, 0.01, 0.01, 0, 0, 4326),
                '8BSI' :: TEXT,
                1,
                0
            ),
            1,
            FALSE
        )).geom
image.png
SELECT
    (
        ST_PixelAsPolygons (
            ST_AddBand (
                ST_MakeEmptyRaster( 50, 50, 119.96, 30.40, 0.01, -0.02, 0, 0, 4326),
                '8BSI' :: TEXT,
                1,
                0
            ),
            1,
            FALSE
        )).geom
image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 網(wǎng)格線(Grid Line) 構(gòu)成網(wǎng)格結(jié)構(gòu)的分界線。它們既可以是垂直的(“列網(wǎng)格線(column grid lin...
    晚溪呀閱讀 1,287評(píng)論 0 0
  • CSS Grid(網(wǎng)格) 布局(又稱為 “Grid(網(wǎng)格)” ),是一個(gè)二維的基于網(wǎng)格的布局系統(tǒng)它的目標(biāo)是完全改變...
    諾CIUM閱讀 1,350評(píng)論 0 3
  • 她似乎從來(lái)都不怕冷,愛(ài)穿裙子,不論春夏秋冬。 她不愛(ài)笑,穿裙子也是亭亭玉立,如茶花一樣的鮮嫩也像...
    介眉閱讀 464評(píng)論 4 3
  • 一條寬闊的大河在這片綠色灌木叢裹繞的岬角處打了個(gè)轉(zhuǎn)兒,滾滾東流而去。此時(shí)的我,正站在這岬角的一塊堤石上,遠(yuǎn)望著大河...
    那棵老橡樹(shù)閱讀 459評(píng)論 0 2
  • 晨間與傍晚時(shí)分,總是涼風(fēng)習(xí)習(xí),而中午卻艷陽(yáng)高照,可以多給孩子帶件外套,晚上放學(xué)讓孩子們穿上。 周一...
    劉瑤_de90閱讀 356評(píng)論 0 5

友情鏈接更多精彩內(nèi)容