[第四篇] PostGIS:“我让PG更完美”

本文详细介绍了PostGIS中的关键功能,包括几何处理、仿射变换、聚类、边界分析、线性参考和轨迹计算等。通过一系列函数如ST_Buffer、ST_Centroid和ST_ClusterDBSCAN等,PostGIS提供了丰富的方法来操作和分析地理数据,为开发者提供了强大的地理空间数据处理能力。
摘要由CSDN通过智能技术生成

概要

本篇文章主要分为几何图形处理函数、仿生变换函数、聚类函数、边界分析函数、线性参考函数、轨迹函数、SFCGAL 函数、版本函数这八部分。

Geometry Processing

ST_Buffer

(T) Returns a geometry covering all points within a given distance from the input geometry.

//语法
geometry ST_Buffer(geometry g1, float radius_of_buffer, text buffer_style_parameters=”);
geometry ST_Buffer(geometry g1, float radius_of_buffer, integer num_seg_quarter_circle);
geography ST_Buffer(geography g1, float radius_of_buffer, text buffer_style_parameters);
geography ST_Buffer(geography g1, float radius_of_buffer, integer num_seg_quarter_circle);
//示例
SELECT ST_Buffer(
ST_GeomFromText('POINT(100 90)'),
50, 'quad_segs=8');

SELECT ST_Buffer(
ST_GeomFromText('POINT(100 90)'),
50, 'quad_segs=2');

SELECT ST_Buffer(
ST_GeomFromText( 'LINESTRING(50 50,150 150,150 50)'
), 10, 'join=bevel');

SELECT ST_Buffer(
ST_GeomFromText( 'LINESTRING(50 50,150 150,150 50)'
), 10, 'join=mitre mitre_limit=5.0');

SELECT ST_Buffer(
ST_GeomFromText( 'LINESTRING(50 50,150 150,150 50)'
), 10, 'side=left');

SELECT ST_Buffer(
ST_GeomFromText( 'LINESTRING(50 50,150 150,150 50)'
), 10, 'side=right');


SELECT ST_Buffer(
ST_ForceRHR(
ST_Boundary(
ST_GeomFromText( 'POLYGON ((50 50, 50 150, 150 150, 150
50, 50 50))'))),
), 20, 'side=left');

ST_BuildArea

Creates an areal geometry formed by the constituent linework of given geometry.

//语法
geometry ST_BuildArea(geometry A);
//示例
SELECT ST_BuildArea(ST_Collect(smallc,bigc))
FROM (SELECT
ST_Buffer(
ST_GeomFromText('POINT(100 90)'), 25) As smallc,
ST_Buffer(ST_GeomFromText('POINT(100 90)'), 50) As bigc) As foo;

SELECT ST_BuildArea(ST_Collect(line,circle))
FROM (SELECT
ST_Buffer(
ST_MakeLine(ST_MakePoint(10, 10),ST_MakePoint(190, 190)),
5) As line,
ST_Buffer(ST_GeomFromText('POINT(100 90)'), 50) As circle) As foo;
--this creates the same gaping hole
--but using linestrings instead of polygons
SELECT ST_BuildArea(
ST_Collect(ST_ExteriorRing(line),ST_ExteriorRing(circle))
)
FROM (SELECT ST_Buffer(
ST_MakeLine(ST_MakePoint(10, 10),ST_MakePoint(190, 190))
,5) As line,
ST_Buffer(ST_GeomFromText('POINT(100 90)'), 50) As circle) As foo;

ST_Centroid

Returns the geometric center of a geometry.

//语法
geometry ST_Centroid(geometry g1);
geography ST_Centroid(geography g1, boolean use_spheroid=true);
//示例
SELECT ST_AsText(ST_Centroid('MULTIPOINT ( -1 0, -1 2, -1 3, -1 4, -1 7, 0 1, 0 3, 1 1, 2 ←-
0, 6 0, 7 8, 9 8, 10 6 )'));
st_astext
------------------------------------------
POINT(2.30769230769231 3.30769230769231)
(1 row)
SELECT ST_AsText(ST_centroid(g))
FROM ST_GeomFromText('CIRCULARSTRING(0 2, -1 1,0 0, 0.5 0, 1 0, 2 1, 1 2, 0.5 2, 0 2)') ←-
AS g ;
------------------------------------------
POINT(0.5 1)
SELECT ST_AsText(ST_centroid(g))
FROM ST_GeomFromText('COMPOUNDCURVE(CIRCULARSTRING(0 2, -1 1,0 0),(0 0, 0.5 0, 1 0), ←-
CIRCULARSTRING( 1 0, 2 1, 1 2),(1 2, 0.5 2, 0 2))' ) AS g;
------------------------------------------
POINT(0.5 1)

ST_ClipByBox2D

Returns the portion of a geometry falling within a rectangle.

//语法
geometry ST_ClipByBox2D(geometry geom, box2d box);
//示例
-- Rely on implicit cast from geometry to box2d for the second parameter
SELECT ST_ClipByBox2D(the_geom, ST_MakeEnvelope(0,0,10,10)) FROM mytab;

ST_ConcaveHull

The concave hull of a geometry represents a possibly concave geometry that encloses all geometries within

the set. You can think of it as shrink wrapping.

//语法
geometry ST_ConcaveHull(geometry geomA, float target_percent, boolean allow_holes=false);
//示例
--Get estimate of infected area based on point observations
SELECT d.disease_type,
ST_ConcaveHull(ST_Collect(d.pnt_geom), 0.99) As geom
FROM disease_obs As d
GROUP BY d.disease_type;

ST_ConvexHull

Computes the convex hull of a geometry.

//语法
geometry ST_ConvexHull(geometry geomA);
//示例
SELECT ST_AsText(ST_ConvexHull(
ST_Collect(
ST_GeomFromText('MULTILINESTRING((100 190,10 8),(150 10, 20 30))'),
ST_GeomFromText('MULTIPOINT(50 5, 150 30, 50 10, 10 10)')
)) );
---st_astext--
POLYGON((50 5,10 8,10 10,100 190,150 30,150 10,50 5))

--Get estimate of infected area based on point observations
SELECT d.disease_type,
ST_ConvexHull(ST_Collect(d.the_geom)) As the_geom
FROM disease_obs As d
GROUP BY d.disease_type;

ST_CurveToLine

Converts a CIRCULARSTRING/CURVEPOLYGON/MULTISURFACE to a LINESTRING/POLYGON/-

MULTIPOLYGON.

//语法
geometry ST_CurveToLine(geometry curveGeom, float tolerance, integer tolerance_type, integer flags);
//示例
SELECT ST_AsText(ST_CurveToLine(ST_GeomFromText('CIRCULARSTRING(220268 150415,220227 
150505,220227 150406)')));
--Result --
LINESTRING(220268 150415,220269.95064912 150416.539364228,220271.823415575
150418.17258804,220273.613787707 150419.895736857,
220275.317452352 150421.704659462,220276.930305234 150423.594998003,220278.448460847
150425.562198489,
220279.868261823 150427.60152176,220281.186287736 150429.708054909,220282.399363347
150431.876723113,
220283.50456625 150434.10230186,220284.499233914 150436.379429536,220285.380970099 
150438.702620341,220286.147650624 150441.066277505,
220286.797428488 150443.464706771,220287.328738321 150445.892130112,220287.740300149
150448.342699654,
220288.031122486 150450.810511759,220288.200504713 150453.289621251,220288.248038775
150455.77405574,
220288.173610157 150458.257830005,220287.977398166 150460.734960415,220287.659875492
150463.199479347,
220287.221807076 150465.64544956,220286.664248262 150468.066978495,220285.988542259
150470.458232479,220285.196316903 150472.81345077,
220284.289480732 150475.126959442,220283.270218395 150477.39318505,220282.140985384
150479.606668057,
220280.90450212 150481.762075989,220279.5637474 150483.85421628,220278.12195122
150485.87804878,
220276.582586992 150487.828697901,220274.949363179 150489.701464356,220273.226214362
150491.491836488,
220271.417291757 150493.195501133,220269.526953216 150494.808354014,220267.559752731
150496.326509628,
220265.520429459 150497.746310603,220263.41389631 150499.064336517,220261.245228106
150500.277412127,
220259.019649359 150501.38261503,220256.742521683 150502.377282695,220254.419330878
150503.259018879,
220252.055673714 150504.025699404,220249.657244448 150504.675477269,220247.229821107
150505.206787101,
220244.779251566 150505.61834893,220242.311439461 150505.909171266,220239.832329968
150506.078553494,
220237.347895479 150506.126087555,220234.864121215 150506.051658938,220232.386990804
150505.855446946,
220229.922471872 150505.537924272,220227.47650166 150505.099855856,220225.054972724
150504.542297043,
220222.663718741 150503.86659104,220220.308500449 150503.074365683,
220217.994991777 150502.167529512,220215.72876617 150501.148267175,
220213.515283163 150500.019034164,220211.35987523 150498.7825509,
220209.267734939 150497.441796181,220207.243902439 150496,
PostGIS 3.0.5dev Manual 338 / 841
220205.293253319 150494.460635772,220203.420486864 150492.82741196,220201.630114732
150491.104263143,
220199.926450087 150489.295340538,220198.313597205 150487.405001997,220196.795441592
150485.437801511,
220195.375640616 150483.39847824,220194.057614703 150481.291945091,220192.844539092
150479.123276887,220191.739336189 150476.89769814,
220190.744668525 150474.620570464,220189.86293234 150472.297379659,220189.096251815
150469.933722495,
220188.446473951 150467.535293229,220187.915164118 150465.107869888,220187.50360229
150462.657300346,
220187.212779953 150460.189488241,220187.043397726 150457.710378749,220186.995863664
150455.22594426,
220187.070292282 150452.742169995,220187.266504273 150450.265039585,220187.584026947
150447.800520653,
220188.022095363 150445.35455044,220188.579654177 150442.933021505,220189.25536018
150440.541767521,
220190.047585536 150438.18654923,220190.954421707 150435.873040558,220191.973684044
150433.60681495,
220193.102917055 150431.393331943,220194.339400319 150429.237924011,220195.680155039
150427.14578372,220197.12195122 150425.12195122,
220198.661315447 150423.171302099,220200.29453926 150421.298535644,220202.017688077
150419.508163512,220203.826610682 150417.804498867,
220205.716949223 150416.191645986,220207.684149708 150414.673490372,220209.72347298
150413.253689397,220211.830006129 150411.935663483,
220213.998674333 150410.722587873,220216.22425308 150409.61738497,220218.501380756
150408.622717305,220220.824571561 150407.740981121,
220223.188228725 150406.974300596,220225.586657991 150406.324522731,220227 150406)
--3d example
SELECT ST_AsEWKT(ST_CurveToLine(ST_GeomFromEWKT('CIRCULARSTRING(220268 150415 1,220227 
150505 2,220227 150406 3)')));
Output
------
LINESTRING(220268 150415 1,220269.95064912 150416.539364228 1.0181172856673,
220271.823415575 150418.17258804 1.03623457133459,220273.613787707 150419.895736857
1.05435185700189,....AD INFINITUM ....
220225.586657991 150406.324522731 1.32611114201132,220227 150406 3)
--use only 2 segments to approximate quarter circle
SELECT ST_AsText(ST_CurveToLine(ST_GeomFromText('CIRCULARSTRING(220268 150415,220227
150505,220227 150406)'),2));
st_astext
------------------------------
LINESTRING(220268 150415,220287.740300149 150448.342699654,220278.12195122
150485.87804878,
220244.779251566 150505.61834893,220207.243902439 150496,220187.50360229 150462.657300346,
220197.12195122 150425.12195122,220227 150406)
-- Ensure approximated line is no further than 20 units away from
-- original curve, and make the result direction-neutral
SELECT ST_AsText(ST_CurveToLine( 'CIRCULARSTRING(0 0,100 -100,200 0)'::geometry,
20, -- Tolerance
1, -- Above is max distance between curve and line
1 -- Symmetric flag
));
st_astext
---------------------------------------------------------------------------------------
LINESTRING(0 0,50 -86.6025403784438,150 -86.6025403784439,200 -1.1331077795296e-13,200 0)

ST_DelaunayTriangles

Return a Delaunay triangulation around the given input points.

//语法
geometry ST_DelaunayTriangles(geometry g1, float tolerance, int4 flags);
//示例
-- our original geometry --
ST_Union(ST_GeomFromText('POLYGON((175 150, 20 40,
50 60, 125 100, 175 150))'),
ST_Buffer(ST_GeomFromText('POINT(110 170)'), 20)
)

SELECT
ST_DelaunayTriangles(
ST_Union(ST_GeomFromText('POLYGON((175 150, 20 40,
50 60, 125 100, 175 150))'),
ST_Buffer(ST_GeomFromText('POINT(110 170)'), 20)
))
As dtriag;

ST_Difference

Returns a geometry that represents that part of geometry A that does not intersect with geometry B.

//语法
geometry ST_Difference(geometry geomA, geometry geomB);
//示例
SELECT ST_AsText(
ST_Difference( 'LINESTRING(50 100, 50 200)'::geometry, 'LINESTRING(50 50, 50 150)'::geometry
)
);
st_astext
---------
LINESTRING(50 150,50 200)

ST_FlipCoordinates

Returns a version of the given geometry with X and Y axis flflipped. Useful for people who have built

latitude/longitude features and need to fifix them.

//语法
geometry ST_FlipCoordinates(geometry geom);
//示例
SELECT ST_AsEWKT(ST_FlipCoordinates(GeomFromEWKT('POINT(1 2)')));
st_asewkt
------------
POINT(2 1)

ST_GeneratePoints

Converts a polygon or multi-polygon into a multi-point composed of randomly location points within the

original areas.

//语法
geometry ST_GeneratePoints( g geometry , npoints integer );
geometry ST_GeneratePoints( g geometry , npoints integer , seed integer );
//示例
SELECT ST_GeneratePoints(geom, 12, 1996)
FROM (
SELECT ST_Buffer(
ST_GeomFromText( 'LINESTRING(50 50,150 ←-
150,150 50)'),
10, 'endcap=round join= ←-
round') AS geom
) AS s;

ST_GeometricMedian

Returns the geometric median of a MultiPoint.

//语法
geometry ST_GeometricMedian ( geometry g , float8 tolerance , int max_iter , boolean fail_if_not_converged );
//示例
WITH test AS (
SELECT 'MULTIPOINT((0 0), (1 1), (2 2), (200 200))'::geometry geom)
SELECT
ST_AsText(ST_Centroid(geom)) centroid,
ST_AsText(ST_GeometricMedian(geom)) median
FROM test;
centroid | median
--------------------+----------------------------------------
POINT(50.75 50.75) | POINT(1.9761550281255 1.9761550281255)
(1 row)

ST_Intersection

(T) Returns a geometry that represents the shared portion of geomA and geomB.

//语法
geometry ST_Intersection( geometry geomA , geometry geomB );
geography ST_Intersection( geography geogA , geography geogB );
//示例
SELECT ST_AsText(ST_Intersection('POINT(0 0)'::geometry, 'LINESTRING ( 2 0, 0 2 )':: ←-
geometry));
st_astext
---------------
GEOMETRYCOLLECTION EMPTY
SELECT ST_AsText(ST_Intersection('POINT(0 0)'::geometry, 'LINESTRING ( 0 0, 0 2 )':: ←-
geometry));
st_astext
---------------
POINT(0 0)

ST_LineToCurve

Converts a LINESTRING/POLYGON to a CIRCULARSTRING, CURVEPOLYGON.

//语法
geometry ST_LineToCurve(geometry geomANoncircular);
//示例
-- 2D Example
SELECT ST_AsText(ST_LineToCurve(foo.the_geom)) As curvedastext,ST_AsText(foo.the_geom) As
non_curvedastext
FROM (SELECT ST_Buffer('POINT(1 3)'::geometry, 3) As the_geom) As foo;
curvedatext non_curvedastext
--------------------------------------------------------------------|-------------------
CURVEPOLYGON(CIRCULARSTRING(4 3,3.12132034355964 0.878679656440359, | POLYGON((4 
3,3.94235584120969 2.41472903395162,3.77163859753386 1.85194970290473,
1 0,-1.12132034355965 5.12132034355963,4 3)) | 3.49440883690764 
1.33328930094119,3.12132034355964 0.878679656440359,
| 2.66671069905881 
0.505591163092366,2.14805029709527 
0.228361402466141,
| 1.58527096604839 
0.0576441587903094,1 
0,
| 0.414729033951621 
0.0576441587903077,-0.148050297095264
0.228361402466137,
| -0.666710699058802 
0.505591163092361,-1.12132034355964 
0.878679656440353,
| -1.49440883690763 
1.33328930094119,-1.77163859753386 
1.85194970290472
| --ETC-- 
,3.94235584120969 
3.58527096604839,4
3))
--3D example
SELECT ST_AsText(ST_LineToCurve(geom)) As curved, ST_AsText(geom) AS not_curved
FROM (SELECT
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值