Você está na página 1de 3

Generating a Grid (fishnet) of points or polygons for PostGIS

1 of 4

http://spatialdbadvisor.com/postgis_tips_tricks/300/generating-a-grid-fi...

Go to content Go to navigation and search

SpatialDB Advisor
Simon Greener, Independent GeoSpatial Solutions Architect
Home

Generating a Grid (fishnet) of points or polygons for PostGIS


Tuesday September 11 2012 at 01:55
Keywords

Current PostGIS Blog


Articles
Morton key function
for
PostgreSQL/PostGIS

grid fishnet points polygons squares postgis


Summary
An article showing how to generate a grid of points or polygons for a geometry object in PostGIS.

Generating a Grid
I wrote an article on Gridding a PostGIS Geometry object a while back.
(fishnet) of points or
polygons for PostGIS I had cause, recently, to use a variation on this for the generation of an array of point objects for a geometry polygon,
so I wrote a version that also allows for the generation of a grid of polygons or points depending on a boolean
ST_Parallel
parameter.
for PostGIS
R based Delaunay
Triangulation Function
for PostGIS using the
deldir package
COGO: Converting
Decimal Degrees to
Degrees Minutes and
Seconds - and back
again (PostGIS)
COGO: Finding
centre and radius of a
curve defined by three
points (PostGIS)
Gridding a geometry
object (PostGIS)
FOSSLIC PostGIS presentations
Vectorization:
Exploding a linestring
or polygon into
individual vectors
in PostGIS
How to apply spatial
constraints to
PostGIS tables
Loading and
Processing GPX 1.1
files using
PostgreSQL XML
Converting Oracle
Optimized Rectangles
to PostGIS
Building polygons
from overlapping
linestrings

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.

-- Create required type


DROP
TYPE IF EXISTS T_Grid CASCADE;
CREATE TYPE T_Grid AS (
gcol int4,
grow int4,
geom geometry
);
-- Drop function is exists
DROP FUNCTION IF EXISTS ST_RegularGrid(geometry, NUMERIC, NUMERIC, BOOLEAN);
-- Now create the function
CREATE OR REPLACE FUNCTION ST_RegularGrid(p_geometry
geometry,
p_TileSizeX NUMERIC,
p_TileSizeY NUMERIC,
p_point
BOOLEAN DEFAULT TRUE)
RETURNS SETOF T_Grid AS
$BODY$
DECLARE
v_mbr
geometry;
v_srid int4;
v_halfX NUMERIC := p_TileSizeX / 2.0;
v_halfY NUMERIC := p_TileSizeY / 2.0;
v_loCol int4;
v_hiCol int4;
v_loRow int4;
v_hiRow int4;
v_grid T_Grid;
BEGIN
IF ( p_geometry IS NULL ) THEN
RETURN;
END IF;
v_srid := ST_SRID(p_geometry);
v_mbr
:= ST_Envelope(p_geometry);
v_loCol := trunc((ST_XMIN(v_mbr) / p_TileSizeX)::NUMERIC );
v_hiCol := CEIL( (ST_XMAX(v_mbr) / p_TileSizeX)::NUMERIC ) - 1;
v_loRow := trunc((ST_YMIN(v_mbr) / p_TileSizeY)::NUMERIC );
v_hiRow := CEIL( (ST_YMAX(v_mbr) / p_TileSizeY)::NUMERIC ) - 1;
FOR v_col IN v_loCol..v_hiCol Loop
FOR v_row IN v_loRow..v_hiRow Loop
v_grid.gcol := v_col;
v_grid.grow := v_row;
IF ( p_point ) THEN
v_grid.geom := ST_SetSRID(
ST_MakePoint((v_col * p_TileSizeX) + v_halfX,
(v_row * p_TileSizeY) + V_HalfY),
v_srid);
ELSE
v_grid.geom := ST_SetSRID(
ST_MakeEnvelope((v_col * p_TileSizeX),
(v_row * p_TileSizeY),
(v_col * p_TileSizeX) + p_TileSizeX,
(v_row * p_TileSizeY) + p_TileSizeY),
v_srid);
END IF;
RETURN NEXT v_grid;
END Loop;

03-06-2015 18:07

Generating a Grid (fishnet) of points or polygons for PostGIS

2 of 4

requiring intersection
Loading Point Data
from a CSV File
in PostGIS
DMS2DD
for PostGIS
Implementing
Oracle's GetVertices
function in PostGIS
- ST_DumpPoints
Filtering Rings in
Polygon (PostGIS)

56.
57.
58.
59.
60.
61.
62.
63.
64.

http://spatialdbadvisor.com/postgis_tips_tricks/300/generating-a-grid-fi...

END Loop;
END;
$BODY$
LANGUAGE plpgsql IMMUTABLE
COST 100
ROWS 1000;
-- Assign ownership
ALTER FUNCTION ss.st_regulargrid(geometry, NUMERIC, NUMERIC, BOOLEAN)
OWNER TO postgres;

Now, lets do some testing:


First, generate grid of points over a geometrys MBR.
1. SELECT gcol, grow,ST_AsText(geom) AS geomWKT
2.
FROM ss.ST_RegularGrid(ST_GeomFromText('LINESTRING(0 0, 100 100)',0),20,20);

Result:

gcol grow geomWKT


0
0
POINT (10 10)
Search
0
1
POINT (10 30)
0
2
POINT (10 50)
0
3
POINT (10 70)
Browse
0
4
POINT (10 90)
1
0
POINT (30 10)
RSS / Atom
1
1
POINT (30 30)
Email me
1
2
POINT (30 50)
1
3
POINT (30 70)
textpattern
1
4
POINT (30 90)
2
0
POINT (50 10)
2
1
POINT (50 30)
All Blog Articles, Data
2
2
POINT (50 50)
Models and Free Source
Code by Simon Greener, The 2
3
POINT (50 70)
SpatialDB Advisor is
2
4
POINT (50 90)
licensed under a Creative
3
0
POINT (70 10)
Commons Attribution3
1
POINT (70 30)
ShareAlike 3.0 Unported
3
2
POINT (70 50)
License.
3
3
POINT (70 70)
3
4
POINT (70 90)
4
0
POINT (90 10)
4
1
POINT (90 30)
4
2
POINT (90 50)
4
3
POINT (90 70)
4
4
POINT (90 90)
Secondly, generate grid of polygons over the MBR of the same object.
1. SELECT gcol, grow,ST_AsText(geom) AS geomWKT
2.
FROM ss.ST_RegularGrid(ST_GeomFromText('LINESTRING(0 0, 100 100)',0),20,20,FALSE);

Result:
gcol grow
geomWKT
0
0
POLYGON ((0 0,0 20,20 20,20 0,0 0))
0
1
POLYGON ((0 20,0 40,20 40,20 20,0 20))
0
2
POLYGON ((0 40,0 60,20 60,20 40,0 40))
0
3
POLYGON ((0 60,0 80,20 80,20 60,0 60))
0
4
POLYGON ((0 80,0 100,20 100,20 80,0 80))
1
0
POLYGON ((20 0,20 20,40 20,40 0,20 0))
1
1
POLYGON ((20 20,20 40,40 40,40 20,20 20))
1
2
POLYGON ((20 40,20 60,40 60,40 40,20 40))
1
3
POLYGON ((20 60,20 80,40 80,40 60,20 60))
1
4
POLYGON ((20 80,20 100,40 100,40 80,20 80))
2
0
POLYGON ((40 0,40 20,60 20,60 0,40 0))
2
1
POLYGON ((40 20,40 40,60 40,60 20,40 20))
2
2
POLYGON ((40 40,40 60,60 60,60 40,40 40))

03-06-2015 18:07

Generating a Grid (fishnet) of points or polygons for PostGIS

3 of 4

2
2
3
3
3
3
3
4
4
4
4
4

3
4
0
1
2
3
4
0
1
2
3
4

http://spatialdbadvisor.com/postgis_tips_tricks/300/generating-a-grid-fi...

POLYGON ((40 60,40 80,60 80,60 60,40 60))


POLYGON ((40 80,40 100,60 100,60 80,40 80))
POLYGON ((60 0,60 20,80 20,80 0,60 0))
POLYGON ((60 20,60 40,80 40,80 20,60 20))
POLYGON ((60 40,60 60,80 60,80 40,60 40))
POLYGON ((60 60,60 80,80 80,80 60,60 60))
POLYGON ((60 80,60 100,80 100,80 80,60 80))
POLYGON ((80 0,80 20,100 20,100 0,80 0))
POLYGON ((80 20,80 40,100 40,100 20,80 20))
POLYGON ((80 40,80 60,100 60,100 40,80 40))
POLYGON ((80 60,80 80,100 80,100 60,80 60))
POLYGON ((80 80,80 100,100 100,100 80,80 80))

Visually.

I hope this is useful for someone.

Comment [2]
Hi,
A probably more efficient way of generating a grid (of point or polygons) would be to use the generate_series
postgresql function.
You can find some similar use cases here :
http://gis.stackexchange.com/questions/4663/how-to-create-regular-point-grid-inside-a-polygon-in-postgis
or here :
http://www.bostongis.com/postgis_translate.snippet
The Postgis in Action book includes some great examples of this kind too.
vincent 11 September 2012, 13:42 #
Vincent,
Thanks for your comments.
I am aware of these techniques and have used them myself beforehand.
Sometimes in the spirit of horses for courses I use one or the other.
This technique is useful because it encapsulates the processing inside a function cleaning up the SQL from extraneous
detail that would otherwise complicate a SQLs declarative power in solving a problem.

03-06-2015 18:07

Você também pode gostar