Loading...
墨滴

Bayanbulake

2021/11/26  阅读:27  主题:自定义主题1

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

概要

本篇文章主要分为几何图形输入输出其它输入格式、几何图形输出(WKT/WKB)、边界操作符、距离操作符、拓扑空间关系函数、距离空间关系函数、测量函数这九部分。

Geometry Input Other Formats

ST_Box2dFromGeoHash

Return a BOX2D from a GeoHash string.

//语法
box2d ST_Box2dFromGeoHash(text geohash, integer precision=full_precision_of_geohash);
//示例
SELECT ST_Box2dFromGeoHash('9qqj7nmxncgyy4d0dbxqz0');
st_geomfromgeohash
--------------------------------------------------
BOX(-115.172816 36.114646,-115.172816 36.114646)
SELECT ST_Box2dFromGeoHash('9qqj7nmxncgyy4d0dbxqz0'0);
st_box2dfromgeohash
----------------------
BOX(-180 -90,180 90)
SELECT ST_Box2dFromGeoHash('9qqj7nmxncgyy4d0dbxqz0'10);
st_box2dfromgeohash
---------------------------------------------------------------------------
BOX(-115.17282128334 36.1146408319473,-115.172810554504 36.1146461963654)

ST_GeomFromGeoHash

Return a geometry from a GeoHash string.

//语法
geometry ST_GeomFromGeoHash(text geohash, integer precision=full_precision_of_geohash);
//示例
SELECT ST_AsText(ST_GeomFromGeoHash('9qqj7nmxncgyy4d0dbxqz0'));
st_astext
-------------------------------------------------------------------------------------------------------------------------- 
POLYGON((-115.172816 36.114646,-115.172816 36.114646,-115.172816 36.114646,-115.172816 
36.114646,-115.172816 36.114646))
SELECT ST_AsText(ST_GeomFromGeoHash('9qqj7nmxncgyy4d0dbxqz0'4));
st_astext
------------------------------------------------------------------------------------------------------------------------------ 
POLYGON((-115.3125 36.03515625,-115.3125 36.2109375,-114.9609375 36.2109375,-114.9609375 ←-
36.03515625,-115.3125 36.03515625))
SELECT ST_AsText(ST_GeomFromGeoHash('9qqj7nmxncgyy4d0dbxqz0'10));
st_astext 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
POLYGON((-115.17282128334 36.1146408319473,-115.17282128334 ←-
36.1146461963654,-115.172810554504 36.1146461963654,-115.172810554504 ←-
36.1146408319473,-115.17282128334 36.1146408319473))

ST_GeomFromGML

Takes as input GML representation of geometry and outputs a PostGIS geometry object.

//语法
geometry ST_GeomFromGML(text geomgml);
geometry ST_GeomFromGML(text geomgml, integer srid);
//示例
SELECT ST_GeomFromGML('
<gml:LineString srsName="EPSG:4269">
<gml:coordinates>
-71.16028,42.258729 -71.160837,42.259112 -71.161143,42.25932
</gml:coordinates>
</gml:LineString>'
);

ST_GeomFromGeoJSON

Takes as input a geojson representation of a geometry and outputs a PostGIS geometry object.

//语法
geometry ST_GeomFromGeoJSON(text geomjson);
geometry ST_GeomFromGeoJSON(json geomjson);
geometry ST_GeomFromGeoJSON(jsonb geomjson);
//示例
SELECT ST_AsText(ST_GeomFromGeoJSON('{"type":"Point","coordinates":[-48.23456,20.12345]}'))
As wkt;
wkt
------
POINT(-48.23456 20.12345)

-- a 3D linestring
SELECT ST_AsText(ST_GeomFromGeoJSON('{"type":"LineString","coordinates
":[[1,2,3],[4,5,6],[7,8,9]]}'
)) As wkt;
wkt
-------------------
LINESTRING(1 2,4 5,7 8)

ST_GeomFromKML

Takes as input KML representation of geometry and outputs a PostGIS geometry object.

//语法
geometry ST_GeomFromKML(text geomkml)
//示例
SELECT ST_GeomFromKML('
<LineString> <coordinates>-71.1663,42.2614
-71.1667,42.2616</coordinates>
</LineString>'
);

ST_GeomFromTWKB

Creates a geometry instance from a TWKB ("Tiny Well-Known Binary") geometry representation.

//语法
geometry ST_GeomFromTWKB(bytea twkb);
//示例
SELECT ST_AsText(ST_GeomFromTWKB(ST_AsTWKB('LINESTRING(126 34, 127 35)'::geometry)));
st_astext
-----------------------------
LINESTRING(126 34, 127 35)
(1 row)

SELECT ST_AsEWKT(
ST_GeomFromTWKB(E'\\x620002f7f40dbce4040105')
);
st_asewkt
------------------------------------------------------
LINESTRING(-113.98 39.198,-113.981 39.195)
(1 row)

ST_GMLToSQL

Return a specifified ST_Geometry value from GML representation. This is an alias name for ST_GeomFromGML.

//语法
geometry ST_GMLToSQL(text geomgml);
geometry ST_GMLToSQL(text geomgml, integer srid);
//示例
-- Create a line string from a polyline
SELECT ST_AsEWKT(ST_LineFromEncodedPolyline('_p~iF~ps|U_ulLnnqC_mqNvxq❵@'));
-- result --
SRID=4326;LINESTRING(-120.2 38.5,-120.95 40.7,-126.453 43.252)
-- Select different precision that was used for polyline encoding
SELECT ST_AsEWKT(ST_LineFromEncodedPolyline('_p~iF~ps|U_ulLnnqC_mqNvxq❵@',6));
-- result --
SRID=4326;LINESTRING(-12.02 3.85,-12.095 4.07,-12.6453 4.3252)

ST_PointFromGeoHash

Return a point from a GeoHash string.

//语法
point ST_PointFromGeoHash(text geohash, integer precision=full_precision_of_geohash);
//示例
SELECT ST_AsText(ST_PointFromGeoHash('9qqj7nmxncgyy4d0dbxqz0'));
st_astext
------------------------------
POINT(-115.172816 36.114646)
SELECT ST_AsText(ST_PointFromGeoHash('9qqj7nmxncgyy4d0dbxqz0'4));
st_astext
-----------------------------------
POINT(-115.13671875 36.123046875)
SELECT ST_AsText(ST_PointFromGeoHash('9qqj7nmxncgyy4d0dbxqz0'10));
st_astext
-------------------------------------------
POINT(-115.172815918922 36.1146435141563)

Geometry Output-WKT

ST_AsEWKT

Return the Well-Known Text (WKT) representation of the geometry with SRID meta data.

//语法
text ST_AsEWKT(geometry g1);
text ST_AsEWKT(geography g1);
//示例
SELECT ST_AsEWKT('0103000020E61000000100000005000000000000
000000000000000000000000000000000000000000000000000000
F03F000000000000F03F000000000000F03F000000000000F03
F000000000000000000000000000000000000000000000000'
::geometry);
st_asewkt
--------------------------------
PostGIS 3.0.5dev Manual 215 / 841
SRID=4326;POLYGON((0 0,0 1,1 1,1 0,0 0))
(1 row)
SELECT ST_AsEWKT('0108000080030000000000000060
E30A4100000000785C0241000000000000F03F0000000018
E20A4100000000485F024100000000000000400000000018
E20A4100000000305C02410000000000000840'
)
--st_asewkt---
CIRCULARSTRING(220268 150415 1,220227 150505 2,220227 150406 3)

ST_AsText

Return the Well-Known Text (WKT) representation of the geometry/geography without SRID metadata.

//语法
text ST_AsText(geometry g1);
text ST_AsText(geometry g1, integer maxdecimaldigits=15);
text ST_AsText(geography g1);
text ST_AsText(geography g1, integer maxdecimaldigits=15);
//示例
SELECT ST_AsText('01030000000100000005000000000000000000
000000000000000000000000000000000000000000000000
F03F000000000000F03F000000000000F03F000000000000F03
F000000000000000000000000000000000000000000000000'
);
st_astext
--------------------------------
POLYGON((0 0,0 1,1 1,1 0,0 0))
(1 row)

Geometry Output-WKB

ST_AsBinary

Return the Well-Known Binary (WKB) representation of the geometry/geography without SRID meta data.

//语法
bytea ST_AsBinary(geometry g1);
bytea ST_AsBinary(geometry g1, text NDR_or_XDR);
bytea ST_AsBinary(geography g1);
bytea ST_AsBinary(geography g1, text NDR_or_XDR);
//示例
SELECT ST_AsBinary(ST_GeomFromText('POLYGON((0 0,0 1,1 1,1 0,0 0))',4326));
st_asbinary
--------------------------------
\001\003\000\000\000\001\000\000\000\005
\000\000\000\000\000\000\000\000\000\000
\000\000\000\000\000\000\000\000\000\000
\000\000\000\000\000\000\000\000\000\000
\000\000\000\360?\000\000\000\000\000\000
\360?\000\000\000\000\000\000\360?\000\000
\000\000\000\000\360?\000\000\000\000\000
\000\000\000\000\000\000\000\000\000\000\000
\000\000\000\000\000\000\000\000
(1 row)

ST_AsEWKB

Return the Well-Known Binary (WKB) representation of the geometry with SRID meta data.

//语法
bytea ST_AsEWKB(geometry g1);
bytea ST_AsEWKB(geometry g1, text NDR_or_XDR);
//示例
SELECT ST_AsEWKB(ST_GeomFromText('POLYGON((0 0,0 1,1 1,1 0,0 0))',4326));
st_asewkb
--------------------------------
\001\003\000\000 \346\020\000\000\001\000
\000\000\005\000\000\000\000
\000\000\000\000\000\000\000\000
\000\000\000\000\000\000\000\000\000
\000\000\000\000\000\000\000\000\000\000
\000\000\360?\000\000\000\000\000\000\360?
\000\000\000\000\000\000\360?\000\000\000\000\000
\000\360?\000\000\000\000\000\000\000\000\000\000\000
\000\000\000\000\000\000\000\000\000\000\000\000\000
(1 row)

ST_AsHEXEWKB

Returns a Geometry in HEXEWKB format (as text) using either little-endian (NDR) or big-endian (XDR)

encoding.

//语法
text ST_AsHEXEWKB(geometry g1, text NDRorXDR);
text ST_AsHEXEWKB(geometry g1);
//示例
SELECT ST_AsHEXEWKB(ST_GeomFromText('POLYGON((0 0,0 1,1 1,1 0,0 0))',4326));
which gives same answer as
SELECT ST_GeomFromText('POLYGON((0 0,0 1,1 1,1 0,0 0))',4326)::text;

Geometry Output Other Formats

ST_AsEncodedPolyline

Returns an Encoded Polyline from a LineString geometry.

//语法
text ST_AsEncodedPolyline(geometry geom, integer precision=5);
//示例
SELECT ST_AsEncodedPolyline(GeomFromEWKT('SRID=4326;LINESTRING(-120.2 38.5,-120.95 ←-
40.7,-126.453 43.252)'
));
--result--
|_p~iF~ps|U_ulLnnqC_mqNvxq❵@

ST_AsGeobuf

Return a Geobuf representation of a set of rows.

//语法
bytea ST_AsGeobuf(anyelement set row);
bytea ST_AsGeobuf(anyelement row, text geom_name);
//示例
SELECT encode(ST_AsGeobuf(q, 'geom'), 'base64')
FROM (SELECT ST_GeomFromText('POLYGON((0 0,0 1,1 1,1 0,0 0))'AS geom) AS q;
st_asgeobuf
----------------------------------
GAAiEAoOCgwIBBoIAAAAAgIAAAE=

ST_AsGeoJSON

Return the geometry as a GeoJSON element.

//语法
text ST_AsGeoJSON(record feature, text geomcolumnname, integer maxdecimaldigits=9, boolean pretty_bool=false);
text ST_AsGeoJSON(geometry geom, integer maxdecimaldigits=9, integer options=8);
text ST_AsGeoJSON(geography geog, integer maxdecimaldigits=9, integer options=0);
//示例
select json_build_object( 'type''FeatureCollection',
'features', json_agg(ST_AsGeoJSON(t.*)::json) )
from ( values (1'one''POINT(1 1)'::geometry),
(2'two''POINT(2 2)'),
(3'three''POINT(3 3)')
as t(idname, geom);
-----------------------------
{"type" : "FeatureCollection", "features" : [{"type": "Feature", "geometry": {"type":"Point 
","coordinates":[1,1]}, "properties": {"id": 1, "name": "one"}}, {"type": "Feature", " 
geometry": {"type":"Point","coordinates":[2,2]}, "properties": {"id": 2, "name": "two
"}}, {"type": "Feature", "geometry": {"type":"Point","coordinates":[3,3]}, "properties": 
{"id": 3, "name": "three"}}]}

ST_AsGML

Return the geometry as a GML version 2 or 3 element.

//语法
text ST_AsGML(geometry geom, integer maxdecimaldigits=15, integer options=0);
text ST_AsGML(geography geog, integer maxdecimaldigits=15, integer options=0, text nprefix=null, text id=null);
text ST_AsGML(integer version, geometry geom, integer maxdecimaldigits=15, integer options=0, text nprefix=null, text id=null);
text ST_AsGML(integer version, geography geog, integer maxdecimaldigits=15, integer options=0, text nprefix=null, text
id=null);
//示例
SELECT ST_AsGML(ST_GeomFromText('POLYGON((0 0,0 1,1 1,1 0,0 0))',4326));
st_asgml
--------
<gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates 
>0,0 0,1 1,1 1,0 0,0</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:
Polygon>

ST_AsKML

Return the geometry as a KML element. Several variants. Default version=2, default maxdecimaldigits=15.

//语法
text ST_AsKML(geometry geom, integer maxdecimaldigits=15, text nprefix=NULL);
text ST_AsKML(geography geog, integer maxdecimaldigits=15, text nprefix=NULL);
//示例
SELECT ST_AsKML(ST_GeomFromText('POLYGON((0 0,0 1,1 1,1 0,0 0))',4326));
st_askml
--------
<Polygon><outerBoundaryIs><LinearRing><coordinates>0,0 0,1 1,1 1,0 0,0</coordinates></
LinearRing></outerBoundaryIs></Polygon>
--3d linestring
SELECT ST_AsKML('SRID=4326;LINESTRING(1 2 3, 4 5 6)');
<LineString><coordinates>1,2,3 4,5,6</coordinates></LineString>

ST_AsLatLonText

Return the Degrees, Minutes, Seconds representation of the given point.

//语法
text ST_AsLatLonText(geometry pt, text format=”)
//示例
SELECT (ST_AsLatLonText('POINT (-3.2342342 -2.32498)'));
st_aslatlontext
----------------------------
2\textdegree{}19'29.928"S 3\textdegree{}14'3.243"W

ST_AsMVTGeom

Transform a geometry into the coordinate space of a Mapbox Vector Tile.

//语法
geometry ST_AsMVTGeom(geometry geom, box2d bounds, integer extent=4096, integer buffer=256, boolean clip_geom=true);
//示例
SELECT ST_AsText(ST_AsMVTGeom(
ST_GeomFromText('POLYGON ((0 0, 10 0, 10 5, 0 -5, 0 0))'),
ST_MakeBox2D(ST_Point(00), ST_Point(40964096)),
40960false));
st_astext
--------------------------------------------------------------------
MULTIPOLYGON(((5 4096,10 4091,10 4096,5 4096)),((5 4096,0 4101,0 4096,5 4096)))

ST_AsMVT

Aggregate function returning a Mapbox Vector Tile representation of a set of rows.

//语法
bytea ST_AsMVT(anyelement set row);
bytea ST_AsMVT(anyelement row, text name);
bytea ST_AsMVT(anyelement row, text name, integer extent);
bytea ST_AsMVT(anyelement row, text name, integer extent, text geom_name);
bytea ST_AsMVT(anyelement row, text name, integer extent, text geom_name, text feature_id_name);
//示例
WITH mvtgeom AS
(
SELECT ST_AsMVTGeom(geom, ST_TileEnvelope(12,513,412)) AS geom, name, description
FROM points_of_interest
WHERE ST_Intersects(geom, ST_TileEnvelope(12,513,412)
)
SELECT ST_AsMVT(mvtgeom.*)
FROM mvtgeom;

ST_AsSVG

Returns SVG path data for a geometry.

//语法
text ST_AsSVG(geometry geom, integer rel=0, integer maxdecimaldigits=15);
text ST_AsSVG(geography geog, integer rel=0, integer maxdecimaldigits=15);
//示例
SELECT ST_AsSVG('POLYGON((0 0,0 1,1 1,1 0,0 0))');
st_assvg
--------
M 0 0 L 0 -1 1 -1 1 0 Z

ST_AsTWKB

Returns the geometry as TWKB, aka "Tiny Well-Known Binary".

//语法
bytea ST_AsTWKB(geometry g1, integer decimaldigits_xy=0, integer decimaldigits_z=0, integer decimaldigits_m=0, boolean
include_sizes=false, boolean include_bounding boxes=false);
bytea ST_AsTWKB(geometry[] geometries, bigint[] unique_ids, integer decimaldigits_xy=0, integer decimaldigits_z=0, integer
decimaldigits_m=0, boolean include_sizes=false, boolean include_bounding_boxes=false);
//示例
SELECT ST_AsTWKB('LINESTRING(1 1,5 5)'::geometry);
st_astwkb
--------------------------------------------
\x02000202020808

ST_AsX3D

Returns a Geometry in X3D xml node element format: ISO-IEC-19776-1.2-X3DEncodings-XML.

//语法
text ST_AsX3D(geometry g1, integer maxdecimaldigits=15, integer options=0);
//示例
SELECT '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.0//EN" "http://www.web3d.org/specifications/x3d ←-
-3.0.dtd">
<X3D> <Scene> <Transform> <Shape> <Appearance> <Material emissiveColor=''0 0 1''/>
</Appearance> '
 ||
ST_AsX3D( ST_GeomFromEWKT('POLYHEDRALSURFACE( ((0 0 0, 0 0 1, 0 1 1, 0 1 0, 0 0 0)),
((0 0 0, 0 1 0, 1 1 0, 1 0 0, 0 0 0)), ((0 0 0, 1 0 0, 1 0 1, 0 0 1, 0 0 0)),
((1 1 0, 1 1 1, 1 0 1, 1 0 0, 1 1 0)),
((0 1 0, 0 1 1, 1 1 1, 1 1 0, 0 1 0)), ((0 0 1, 1 0 1, 1 1 1, 0 1 1, 0 0 1)) )'
)) ||
'</Shape>
</Transform>
</Scene>
</X3D>'
 As x3ddoc;
x3ddoc
--------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.0//EN" "http://www.web3d.org/specifications/x3d ←-
-3.0.dtd">
<X3D> <Scene> <Transform> <Shape> <Appearance> <Material emissiveColor='0 0 1'/>
</Appearance> <IndexedFaceSet coordIndex='0 1 2 3 -1 4 5 6 7 -1 8 9 10 11 -1 12 13 14 15 -1 16 17
18 19 -1 20 21 22 23'>
<Coordinate point='0 0 0 0 0 1 0 1 1 0 1 0 0 0 0 0 1 0 1 1 0 1 0 0 0 0 0 1 0 0 ←-
1 0 1 0 0 1 1 1 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 1 1 1 0 0 0 1 1 0 1 1 1 ←-
1 0 1 1' />
</IndexedFaceSet>
</Shape>
</Transform>
</Scene>
</X3D>

ST_GeoHash

Return a GeoHash representation of the geometry.

//语法
text ST_GeoHash(geometry geom, integer maxchars=full_precision_of_point);
//示例
SELECT ST_GeoHash(ST_SetSRID(ST_MakePoint(-126,48),4326));
st_geohash
----------------------
c0w3hf1s70w3hf1s70w3
SELECT ST_GeoHash(ST_SetSRID(ST_MakePoint(-126,48),4326),5);
st_geohash
------------
c0w3h

Operators-Bounding Box

&&

Returns TRUE if A’s 2D bounding box intersects B’s 2D bounding box.

//语法
boolean &&( geometry A , geometry B );
boolean &&( geography A , geography B );
//示例
SELECT tbl1.column1, tbl2.column1, tbl1.column2 && tbl2.column2 AS overlaps
FROM ( VALUES
(1'LINESTRING(0 0, 3 3)'::geometry),
(2'LINESTRING(0 1, 0 5)'::geometry)) AS tbl1, ( VALUES
(3'LINESTRING(1 2, 4 6)'::geometry)) AS tbl2;
column1 | column1 | overlaps
---------+---------+----------
1 | 3 | t
2 | 3 | f
(2 rows)

&&(geometry,box2df)

Returns TRUE if a geometry’s (cached) 2D bounding box intersects a 2D flfloat precision bounding box BOX2DF).

//语法
boolean &&( geometry A , box2df B );
//示例
SELECT ST_MakePoint(1,1) && ST_MakeBox2D(ST_MakePoint(0,0), ST_MakePoint(2,2)) AS overlaps;
overlaps
----------
t
(1 row)

&&(box2df,geometry)

Returns TRUE if a 2D flfloat precision bounding box (BOX2DF) intersects a geometry’s (cached) 2D

bounding box.

//语法
boolean &&( box2df A , geometry B );
SELECT ST_MakeBox2D(ST_MakePoint(0,0), ST_MakePoint(2,2)) && ST_MakePoint(1,1AS overlaps;
overlaps
----------
t
(1 row)

&&(box2df,box2df)

&&(box2df,box2df) — Returns TRUE if two 2D flfloat precision bounding boxes (BOX2DF) intersect each other.

//语法
boolean &&( box2df A , box2df B );
//示例
SELECT ST_MakeBox2D(ST_MakePoint(0,0), ST_MakePoint(2,2)) && ST_MakeBox2D(ST_MakePoint(1,1) ←- , ST_MakePoint(3,3)) AS overlaps;
overlaps
----------
t
(1 row)

&&&

Returns TRUE if A’s n-D bounding box intersects B’s n-D bounding box.

//语法
boolean &&&( geometry A , geometry B );
//示例
SELECT tbl1.column1, tbl2.column1, tbl1.column2 &&& tbl2.column2 AS overlaps_3d,
tbl1.column2 && tbl2.column2 AS overlaps_2d
FROM ( VALUES
(1'LINESTRING Z(0 0 1, 3 3 2)'::geometry),
(2'LINESTRING Z(1 2 0, 0 5 -1)'::geometry)) AS tbl1, ( VALUES
(3'LINESTRING Z(1 2 1, 4 6 1)'::geometry)) AS tbl2;
column1 | column1 | overlaps_3d | overlaps_2d
---------+---------+-------------+-------------
1 | 3 | t | t
2 | 3 | f | t

&&&(geometry,gidx)

Returns TRUE if a geometry’s (cached) n-D bounding box intersects a n-D flfloat precision bounding

box (GIDX).

//语法
boolean &&&( geometry A , gidx B );
//示例
SELECT ST_MakePoint(1,1,1) &&& ST_3DMakeBox(ST_MakePoint(0,0,0), ST_MakePoint(2,2,2)) AS 
overlaps;
overlaps
----------
t
(1 row)

&&&(gidx,geometry)

Returns TRUE if a n-D flfloat precision bounding box (GIDX) intersects a geometry’s (cached) n-D

bounding box.

//语法
boolean &&&( gidx A , geometry B );
//示例
SELECT ST_3DMakeBox(ST_MakePoint(0,0,0), ST_MakePoint(2,2,2)) &&& ST_MakePoint(1,1,1AS ←-
overlaps;
overlaps
----------
t
(1 row)

&&&(gidx,gidx)

Returns TRUE if two n-D flfloat precision bounding boxes (GIDX) intersect each other.

//语法
boolean &&&( gidx A , gidx B );
//示例
SELECT ST_3DMakeBox(ST_MakePoint(0,0,0), ST_MakePoint(2,2,2)) &&& ST_3DMakeBox(ST_MakePoint ←-
(1,1,1), ST_MakePoint(3,3,3)) AS overlaps;
overlaps
----------
t
(1 row)

&<

Returns TRUE if A’s bounding box overlaps or is to the left of B’s.

//语法
boolean &<( geometry A , geometry B )
//示例
SELECT tbl1.column1, tbl2.column1, tbl1.column2 &< tbl2.column2 AS overleft
FROM
VALUES
(1'LINESTRING(1 2, 4 6)'::geometry)) AS tbl1, ( VALUES
(2'LINESTRING(0 0, 3 3)'::geometry),
(3'LINESTRING(0 1, 0 5)'::geometry),
(4'LINESTRING(6 0, 6 1)'::geometry)) AS tbl2;
column1 | column1 | overleft
---------+---------+----------
1 | 2 | f
1 | 3 | f
1 | 4 | t
(3 rows)

&<|

Returns TRUE if A’s bounding box overlaps or is below B’s.

//语法
boolean &<|( geometry A , geometry B );
//示例
SELECT tbl1.column1, tbl2.column1, tbl1.column2 &<| tbl2.column2 AS overbelow
FROM
VALUES
(1'LINESTRING(6 0, 6 4)'::geometry)) AS tbl1, ( VALUES
(2'LINESTRING(0 0, 3 3)'::geometry),
(3'LINESTRING(0 1, 0 5)'::geometry),
(4'LINESTRING(1 2, 4 6)'::geometry)) AS tbl2;
column1 | column1 | overbelow
---------+---------+-----------
1 | 2 | f
1 | 3 | t
1 | 4 | t
(3 rows)

&>

Returns TRUE if A’ bounding box overlaps or is to the right of B’s.

//语法
boolean &>( geometry A , geometry B );
//示例
SELECT tbl1.column1, tbl2.column1, tbl1.column2 &> tbl2.column2 AS overright
FROM
VALUES
(1'LINESTRING(1 2, 4 6)'::geometry)) AS tbl1, ( VALUES
(2'LINESTRING(0 0, 3 3)'::geometry),
(3'LINESTRING(0 1, 0 5)'::geometry),
(4'LINESTRING(6 0, 6 1)'::geometry)) AS tbl2;
column1 | column1 | overright
---------+---------+-----------
1 | 2 | t
1 | 3 | t
1 | 4 | f
(3 rows)

<<

Returns TRUE if A’s bounding box is strictly to the left of B’s.

//语法
boolean <<( geometry A , geometry B );
//示例
SELECT tbl1.column1, tbl2.column1, tbl1.column2 << tbl2.column2 AS left
FROM
VALUES
(1'LINESTRING (1 2, 1 5)'::geometry)) AS tbl1, ( VALUES
(2'LINESTRING (0 0, 4 3)'::geometry),
(3'LINESTRING (6 0, 6 5)'::geometry),
(4'LINESTRING (2 2, 5 6)'::geometry)) AS tbl2;
column1 | column1 | left
---------+---------+------
1 | 2 | f
1 | 3 | t
1 | 4 | t
(3 rows)

<<|

Returns TRUE if A’s bounding box is strictly below B’s.

//语法
boolean <<|( geometry A , geometry B );
//示例
SELECT tbl1.column1, tbl2.column1, tbl1.column2 <<| tbl2.column2 AS below
FROM
VALUES
(1'LINESTRING (0 0, 4 3)'::geometry)) AS tbl1, ( VALUES
(2'LINESTRING (1 4, 1 7)'::geometry),
(3'LINESTRING (6 1, 6 5)'::geometry),
(4'LINESTRING (2 3, 5 6)'::geometry)) AS tbl2;
column1 | column1 | below
---------+---------+-------
1 | 2 | t
1 | 3 | f
1 | 4 | f
(3 rows)

=

Returns TRUE if the coordinates and coordinate order geometry/geography A are the same as the coordinates and coordinate order of geometry/geography B.

//语法
boolean =( geometry A , geometry B );
boolean =( geography A , geography B );
//示例
SELECT 'LINESTRING(0 0, 0 1, 1 0)'::geometry = 'LINESTRING(1 1, 0 0)'::geometry; ?column?
----------
f
(1 row)
SELECT ST_AsText(column1)
FROM ( VALUES
('LINESTRING(0 0, 1 1)'::geometry),
('LINESTRING(1 1, 0 0)'::geometry)) AS foo;
st_astext
---------------------
LINESTRING(0 0,1 1)
LINESTRING(1 1,0 0)
PostGIS 3.0.5dev Manual 248 / 841
(2 rows)
-- Note: the GROUP BY uses the "=" to compare for geometry equivalency.
SELECT ST_AsText(column1)
FROM ( VALUES
('LINESTRING(0 0, 1 1)'::geometry),
('LINESTRING(1 1, 0 0)'::geometry)) AS foo
GROUP BY column1;
st_astext
---------------------
LINESTRING(0 0,1 1)
LINESTRING(1 1,0 0)
(2 rows)
-- In versions prior to 2.0, this used to return true --
SELECT ST_GeomFromText('POINT(1707296.37 4820536.77)') =
ST_GeomFromText('POINT(1707296.27 4820536.87)'As pt_intersect;
--pt_intersect --
f

>>

Returns TRUE if A’s bounding box is strictly to the right of B’s.

//语法
boolean >>( geometry A , geometry B );
//示例
SELECT tbl1.column1, tbl2.column1, tbl1.column2 >> tbl2.column2 AS right
FROM
VALUES
(1'LINESTRING (2 3, 5 6)'::geometry)) AS tbl1, ( VALUES
(2'LINESTRING (1 4, 1 7)'::geometry),
(3'LINESTRING (6 1, 6 5)'::geometry),
(4'LINESTRING (0 0, 4 3)'::geometry)) AS tbl2;
column1 | column1 | right
---------+---------+-------
1 | 2 | t
1 | 3 | f
1 | 4 | f
(3 rows)

@

Returns TRUE if A’s bounding box is contained by B’s.

//语法
boolean @( geometry A , geometry B );
//示例
SELECT tbl1.column1, tbl2.column1, tbl1.column2 @ tbl2.column2 AS contained
FROM
VALUES
(1'LINESTRING (1 1, 3 3)'::geometry)) AS tbl1, ( VALUES
(2'LINESTRING (0 0, 4 4)'::geometry),
(3'LINESTRING (2 2, 4 4)'::geometry),
(4'LINESTRING (1 1, 3 3)'::geometry)) AS tbl2;
column1 | column1 | contained
---------+---------+-----------
1 | 2 | t
1 | 3 | f
1 | 4 | t
(3 rows)

@(geometry,box2df)

Returns TRUE if a geometry’s 2D bounding box is contained into a 2D flfloat precision bounding box

(BOX2DF).

//语法
boolean @( geometry A , box2df B );
//示例
SELECT ST_Buffer(ST_GeomFromText('POINT(2 2)'), 1) @ ST_MakeBox2D(ST_MakePoint(0,0), ←-
ST_MakePoint(5,5)) AS is_contained;
is_contained
--------------
t
(1 row)

@(box2df,geometry)

Returns TRUE if a 2D flfloat precision bounding box (BOX2DF) is contained into a geometry’s 2D

bounding box.

//语法
boolean @( box2df A , geometry B );
//示例
SELECT ST_MakeBox2D(ST_MakePoint(2,2), ST_MakePoint(3,3)) @ ST_Buffer(ST_GeomFromText(' ←-
POINT(1 1)'
), 10AS is_contained;
is_contained
--------------
t
(1 row)

@(box2df,box2df)

Returns TRUE if a 2D flfloat precision bounding box (BOX2DF) is contained into another 2D flfloat precision

bounding box.

//语法
boolean @( box2df A , box2df B );
//示例
SELECT ST_MakeBox2D(ST_MakePoint(2,2), ST_MakePoint(3,3)) @ ST_MakeBox2D(ST_MakePoint(0,0), ←-
ST_MakePoint(5,5)) AS is_contained;
is_contained
--------------
t
(1 row)

|&>

Returns TRUE if A’s bounding box overlaps or is above B’s.

//语法
boolean  **|&> **( geometry A , geometry B );
//示例
SELECT tbl1.column1, tbl2.column1, tbl1.column2 |&> tbl2.column2 AS overabove
FROM
VALUES
(1'LINESTRING(6 0, 6 4)'::geometry)) AS tbl1, ( VALUES
(2'LINESTRING(0 0, 3 3)'::geometry),
(3'LINESTRING(0 1, 0 5)'::geometry),
(4'LINESTRING(1 2, 4 6)'::geometry)) AS tbl2;
column1 | column1 | overabove
---------+---------+-----------
1 | 2 | t
1 | 3 | f
1 | 4 | f
(3 rows)

|>>

Returns TRUE if A’s bounding box is strictly above B’s.

//语法
boolean |>>( geometry A , geometry B );
//示例
SELECT tbl1.column1, tbl2.column1, tbl1.column2 |>> tbl2.column2 AS above
FROM
VALUES
(1'LINESTRING (1 4, 1 7)'::geometry)) AS tbl1, ( VALUES
(2'LINESTRING (0 0, 4 2)'::geometry),
(3'LINESTRING (6 1, 6 5)'::geometry),
(4'LINESTRING (2 3, 5 6)'::geometry)) AS tbl2;
column1 | column1 | above
---------+---------+-------
1 | 2 | t
1 | 3 | f
1 | 4 | f
(3 rows)

~

Returns TRUE if A’s bounding box contains B’s.

//语法
boolean ~( geometry A , geometry B );
//示例
SELECT tbl1.column1, tbl2.column1, tbl1.column2 ~ tbl2.column2 AS contains
FROM
VALUES
(1'LINESTRING (0 0, 3 3)'::geometry)) AS tbl1, ( VALUES
(2'LINESTRING (0 0, 4 4)'::geometry),
(3'LINESTRING (1 1, 2 2)'::geometry),
(4'LINESTRING (0 0, 3 3)'::geometry)) AS tbl2;
column1 | column1 | contains
---------+---------+----------
1 | 2 | f
1 | 3 | t
1 | 4 | t
(3 rows)

~(geometry,box2df)

Returns TRUE if a geometry’s 2D bonding box contains a 2D flfloat precision bounding box (GIDX).

//语法
boolean ~( geometry A , box2df B );
//示例
SELECT ST_Buffer(ST_GeomFromText('POINT(1 1)'), 10) ~ ST_MakeBox2D(ST_MakePoint(0,0),
ST_MakePoint(2,2)) AS contains;
contains
----------
t
(1 row)

~(box2df,geometry)

Returns TRUE if a 2D flfloat precision bounding box (BOX2DF) contains a geometry’s 2D bonding box.

//语法
boolean ~( box2df A , geometry B );
//示例
SELECT ST_MakeBox2D(ST_MakePoint(0,0), ST_MakePoint(5,5)) ~ ST_Buffer(ST_GeomFromText(
POINT(2 2)'
), 1AS contains;
contains
----------
t
(1 row)

~(box2df,box2df)

Returns TRUE if a 2D flfloat precision bounding box (BOX2DF) contains another 2D flfloat precision bounding

box (BOX2DF).

//语法
boolean ~( box2df A , box2df B );
//示例
SELECT ST_MakeBox2D(ST_MakePoint(0,0), ST_MakePoint(5,5)) ~ ST_MakeBox2D(ST_MakePoint(2,2), ←-
ST_MakePoint(3,3)) AS contains;
contains
----------
t
(1 row)

~=

Returns TRUE if A’s bounding box is the same as B’s.

//语法
boolean ~=( geometry A , geometry B );
//示例
select 'LINESTRING(0 0, 1 1)'::geometry ~= 'LINESTRING(0 1, 1 0)'::geometry as equality;
equality |
-----------------
t |

Distance Operators

<->

Returns the 2D distance between A and B.

//语法
double precision <->( geometry A , geometry B );
double precision <->( geography A , geography B );
//示例
SELECT ST_Distance(geom, 'SRID=3005;POINT(1011102 450541)'::geometry) as d,edabbr, vaabbr
FROM va2005
ORDER BY d limit 10;
d | edabbr | vaabbr
------------------+--------+--------
0 | ALQ | 128
5541.57712511724 | ALQ | 129A
5579.67450712005 | ALQ | 001
6083.4207708641 | ALQ | 131
7691.2205404848 | ALQ | 003
7900.75451037313 | ALQ | 122
8694.20710669982 | ALQ | 129B
9564.24289057111 | ALQ | 130
12089.665931705 | ALQ | 127
18472.5531479404 | ALQ | 002
(10 rows)

|=|

Returns the distance between A and B trajectories at their closest point of approach.

//语法
double precision |=|( geometry A , geometry B );
//示例
\set qt 'ST_AddMeasure(ST_MakeLine(ST_MakePointM(-350,300,0),ST_MakePointM(-410,490,0)) ←-
,10,20)'

-- Run the query !
SELECT track_id, dist FROM (
SELECT track_id, ST_DistanceCPA(tr,:qt) dist
FROM trajectories
ORDER BY tr |=| :qt
LIMIT 5
) foo;
track_id dist
----------+-------------------
395 | 0.576496831518066
380 | 5.06797130410151
390 | 7.72262293958322
385 | 9.8004461358071
405 | 10.9534397988433
(5 rows)

<#>

Returns the 2D distance between A and B bounding boxes.

//语法
double precision <#>( geometry A , geometry B );
//示例
SELECT *
FROM (
SELECT b.tlid, b.mtfcc, b.geom <#> ST_GeomFromText('LINESTRING(746149 2948672,745954 2948576,
745787 2948499,745740 2948468,745712 2948438,
745690 2948384,745677 2948319)',2249) As b_dist,
ST_Distance(b.geom, ST_GeomFromText('
LINESTRING(746149 2948672,745954 2948576,
745787 2948499,745740 2948468,745712 2948438,
745690 2948384,745677 2948319)',2249)) As act_dist
FROM bos_roads As b
ORDER BY b_dist, b.tlid
LIMIT 100) As foo
ORDER BY act_dist, tlid LIMIT 10;
tlid | mtfcc | b_dist | act_dist
-----------+-------+------------------+------------------
85732027 | S1400 | 0 | 0
85732029 | S1400 | 0 | 0
85732031 | S1400 | 0 | 0
85734335 | S1400 | 0 | 0
85736037 | S1400 | 0 | 0
624683742 | S1400 | 0 | 128.528874268666
85719343 | S1400 | 260.839270432962 | 260.839270432962
85741826 | S1400 | 164.759294123275 | 260.839270432962
85732032 | S1400 | 277.75 | 311.830282365264
85735592 | S1400 | 222.25 | 311.830282365264
(10 rows)

<<->>

Returns the n-D distance between the centroids of A and B bounding boxes.

//语法
double precision <<->>( geometry A , geometry B );

<<#>>

Returns the n-D distance between A and B bounding boxes.

//语法
double precision <<#>>( geometry A , geometry B )

Spatial Relationships-Topological

ST_3DIntersects

Returns TRUE if the Geometries "spatially intersect" in 3D - only for points, linestrings, polygons, polyhe

dral surface (area).

//语法
boolean ST_3DIntersects( geometry geomA , geometry geomB );
//示例
SELECT ST_3DIntersects(pt, line), ST_Intersects(pt, line)
FROM (SELECT 'POINT(0 0 2)'::geometry As pt, 'LINESTRING (0 0 1, 0 2 3)'::geometry As
line) As foo;
st_3dintersects | st_intersects
-----------------+---------------
f | t
(1 row)

ST_Contains

Returns true if and only if no points of B lie in the exterior of A, and at least one point of the interior of B lies

in the interior of A.

//语法
boolean ST_Contains(geometry geomA, geometry geomB);
//示例
SELECT ST_Contains(smallc, bigc) As smallcontainsbig,
ST_Contains(bigc,smallc) As bigcontainssmall,
ST_Contains(bigc, ST_Union(smallc, bigc)) as bigcontainsunion,
ST_Equals(bigc, ST_Union(smallc, bigc)) as bigisunion,
ST_Covers(bigc, ST_ExteriorRing(bigc)) As bigcoversexterior,
ST_Contains(bigc, ST_ExteriorRing(bigc)) As bigcontainsexterior
FROM (SELECT ST_Buffer(ST_GeomFromText('POINT(1 2)'), 10As smallc,
ST_Buffer(ST_GeomFromText('POINT(1 2)'), 20As bigc) As foo;

ST_ContainsProperly

Returns true if B intersects the interior of A but not the boundary (or exterior). A does not contain

properly itself, but does contain itself.

//语法
boolean ST_ContainsProperly(geometry geomA, geometry geomB);
//示例
--a circle within a circle
SELECT ST_ContainsProperly(smallc, bigc) As smallcontainspropbig,
ST_ContainsProperly(bigc,smallc) As bigcontainspropsmall,
ST_ContainsProperly(bigc, ST_Union(smallc, bigc)) as bigcontainspropunion,
ST_Equals(bigc, ST_Union(smallc, bigc)) as bigisunion,
ST_Covers(bigc, ST_ExteriorRing(bigc)) As bigcoversexterior,
ST_ContainsProperly(bigc, ST_ExteriorRing(bigc)) As bigcontainsexterior
FROM (SELECT ST_Buffer(ST_GeomFromText('POINT(1 2)'), 10As smallc,
ST_Buffer(ST_GeomFromText('POINT(1 2)'), 20As bigc) As foo;
--Result
smallcontainspropbig | bigcontainspropsmall | bigcontainspropunion | bigisunion | 
bigcoversexterior | bigcontainsexterior
------------------+------------------+------------------+------------+-------------------+--------------------- ←- f | t | f | t | t ←- | f
--example demonstrating difference between contains and contains properly
SELECT ST_GeometryType(geomA) As geomtype, ST_Contains(geomA,geomA) AS acontainsa, 
ST_ContainsProperly(geomA, geomA) AS acontainspropa,
ST_Contains(geomA, ST_Boundary(geomA)) As acontainsba, ST_ContainsProperly(geomA, 
ST_Boundary(geomA)) As acontainspropba
FROM (VALUES ( ST_Buffer(ST_Point(1,1), 5,1) ),
( ST_MakeLine(ST_Point(1,1), ST_Point(-1,-1) ) ),
( ST_Point(1,1) )
As foo(geomA);
geomtype | acontainsa | acontainspropa | acontainsba | acontainspropba
--------------+------------+----------------+-------------+-----------------
ST_Polygon | t | f | f | f
ST_LineString | t | f | f | f
ST_Point | t | t | f | f

ST_Covers

Returns 1 (TRUE) if no point in Geometry B is outside Geometry A.

//语法
boolean ST_Covers(geometry geomA, geometry geomB);
boolean ST_Covers(geography geogpolyA, geography geogpointB);
//示例
--a circle covering a circle
SELECT ST_Covers(smallc,smallc) As smallinsmall,
ST_Covers(smallc, bigc) As smallcoversbig,
ST_Covers(bigc, ST_ExteriorRing(bigc)) As bigcoversexterior,
ST_Contains(bigc, ST_ExteriorRing(bigc)) As bigcontainsexterior
FROM (SELECT ST_Buffer(ST_GeomFromText('POINT(1 2)'), 10As smallc,
ST_Buffer(ST_GeomFromText('POINT(1 2)'), 20As bigc) As foo;
--Result
smallinsmall | smallcoversbig | bigcoversexterior | bigcontainsexterior
--------------+----------------+-------------------+---------------------
t | f | t | f
(1 row)

-- a point with a 300 meter buffer compared to a point, a point and its 10 meter buffer
SELECT ST_Covers(geog_poly, geog_pt) As poly_covers_pt,
ST_Covers(ST_Buffer(geog_pt,10), geog_pt) As buff_10m_covers_cent
FROM (SELECT ST_Buffer(ST_GeogFromText('SRID=4326;POINT(-99.327 31.4821)'), 300As 
geog_poly,
ST_GeogFromText('SRID=4326;POINT(-99.33 31.483)'As geog_pt ) As foo;
poly_covers_pt | buff_10m_covers_cent
----------------+------------------
f | t

ST_CoveredBy

Returns 1 (TRUE) if no point in Geometry/Geography A is outside Geometry/Geography B.

//语法
boolean ST_CoveredBy(geometry geomA, geometry geomB);
boolean ST_CoveredBy(geography geogA, geography geogB);
//示例
--a circle coveredby a circle
SELECT ST_CoveredBy(smallc,smallc) As smallinsmall,
ST_CoveredBy(smallc, bigc) As smallcoveredbybig,
ST_CoveredBy(ST_ExteriorRing(bigc), bigc) As exteriorcoveredbybig,
ST_Within(ST_ExteriorRing(bigc),bigc) As exeriorwithinbig
FROM (SELECT ST_Buffer(ST_GeomFromText('POINT(1 2)'), 10As smallc,
ST_Buffer(ST_GeomFromText('POINT(1 2)'), 20As bigc) As foo;
--Result
smallinsmall | smallcoveredbybig | exteriorcoveredbybig | exeriorwithinbig
--------------+-------------------+----------------------+------------------
t | t | t | f
(1 row)

ST_Crosses

Returns TRUE if the supplied geometries have some, but not all, interior points in common.

//语法
boolean ST_Crosses(geometry g1, geometry g2)
//示例
SELECT roads.id
FROM roads, highways
WHERE ST_Crosses(roads.the_geom, highways.the_geom);

ST_LineCrossingDirection

Given 2 linestrings, returns a number between -3 and 3 denoting what kind of crossing behavior.

0 is no crossing.

//语法
integer ST_LineCrossingDirection(geometry linestringA, geometry linestringB);
//穿越枚举
• 0: LINE NO CROSS
• -1: LINE CROSS LEFT
• 1: LINE CROSS RIGHT
• -2: LINE MULTICROSS END LEFT
• 2: LINE MULTICROSS END RIGHT
• -3: LINE MULTICROSS END SAME FIRST LEFT
• 3: LINE MULTICROSS END SAME FIRST RIGHT
//示例
SELECT ST_LineCrossingDirection(foo.line1  , foo.line2) As l1_cross_l2 ,
ST_LineCrossingDirection(foo.
line2, foo.line1) As l2_cross_l1
FROM (
SELECT
ST_GeomFromText('LINESTRING(25 169,89
114,40 70,86 43)'
As line1,
ST_GeomFromText('LINESTRING(171 154,20
140,71 74,161 53)'
As line2
As foo;
l1_cross_l2 | l2_cross_l1
-------------+-------------
3 | -3

ST_Disjoint

Returns TRUE if the Geometries do not "spatially intersect" - if they do not share any space together.

//语法
boolean ST_Disjoint( geometry A , geometry B );
//示例
SELECT ST_Disjoint('POINT(0 0)'::geometry, 'LINESTRING ( 2 0, 0 2 )'::geometry);
st_disjoint
---------------
t
(1 row)
SELECT ST_Disjoint('POINT(0 0)'::geometry, 'LINESTRING ( 0 0, 0 2 )'::geometry);
st_disjoint
---------------
f
(1 row)

ST_Equals

Returns true if the given geometries represent the same geometry. Directionality is ignored.

//语法
boolean ST_Equals(geometry A, geometry B);
//示例
SELECT ST_Equals(ST_GeomFromText('LINESTRING(0 0, 10 10)'),
ST_GeomFromText('LINESTRING(0 0, 5 5, 10 10)'));
st_equals
-----------
t
(1 row)
SELECT ST_Equals(ST_Reverse(ST_GeomFromText('LINESTRING(0 0, 10 10)')),
ST_GeomFromText('LINESTRING(0 0, 5 5, 10 10)'));
st_equals
-----------
t
(1 row)

ST_Intersects

Returns TRUE if the Geometries/Geography "spatially intersect in 2D" - (share any portion of space) and

FALSE if they don’t (they are Disjoint). For geography tolerance is 0.00001 meters (so any points that close are considered to intersect)

//语法
boolean ST_Intersects( geometry geomA , geometry geomB );
boolean ST_Intersects( geography geogA , geography geogB );
//示例
SELECT ST_Intersects('POINT(0 0)'::geometry, 'LINESTRING ( 2 0, 0 2 )'::geometry);
st_intersects
---------------
f
(1 row)
SELECT ST_Intersects('POINT(0 0)'::geometry, 'LINESTRING ( 0 0, 0 2 )'::geometry);
st_intersects
---------------
t
(1 row)
-- Look up in table. Make sure table has a GiST index on geometry column for faster lookup.
SELECT idname FROM cities WHERE ST_Intersects(geom, 'SRID=4326;POLYGON((28 53,27.707
52.293,27 52,26.293 52.293,26 53,26.293 53.707,27 54,27.707 53.707,28 53))'
);
PostGIS 3.0.5dev Manual 278 / 841
id | name
----+-------
2 | Minsk
(1 row)

SELECT ST_Intersects( 'SRID=4326;LINESTRING(-43.23456 72.4567,-43.23456 72.4568)'::geography, 'SRID=4326;POINT(-43.23456 72.4567772)'::geography
);
st_intersects
---------------
t

ST_OrderingEquals

Returns true if the given geometries represent the same geometry and points are in the same directional

order.

//语法
boolean ST_OrderingEquals(geometry A, geometry B);
//示例
SELECT ST_OrderingEquals(ST_GeomFromText('LINESTRING(0 0, 10 10)'),
ST_GeomFromText('LINESTRING(0 0, 5 5, 10 10)'));
st_orderingequals
-----------
f
(1 row)
SELECT ST_OrderingEquals(ST_GeomFromText('LINESTRING(0 0, 10 10)'),
ST_GeomFromText('LINESTRING(0 0, 0 0, 10 10)'));
st_orderingequals
-----------
t
(1 row)
SELECT ST_OrderingEquals(ST_Reverse(ST_GeomFromText('LINESTRING(0 0, 10 10)')),
ST_GeomFromText('LINESTRING(0 0, 0 0, 10 10)'));
st_orderingequals
-----------
f
(1 row)

ST_Overlaps

Returns TRUE if the Geometries share space, are of the same dimension, but are not completely contained by

each other.

//语法
boolean ST_Overlaps(geometry A, geometry B);
//示例
--a point on a line is contained by the line and is of a lower dimension, and therefore 
does not overlap the line
nor crosses
SELECT ST_Overlaps(a,b) As a_overlap_b,
ST_Crosses(a,b) As a_crosses_b,
ST_Intersects(a, b) As a_intersects_b, ST_Contains(b,a) As b_contains_a
FROM (SELECT ST_GeomFromText('POINT(1 0.5)'As a, ST_GeomFromText('LINESTRING(1 0, 1 1, 3
5)'
As b)
As foo
a_overlap_b | a_crosses_b | a_intersects_b | b_contains_a
------------+-------------+----------------+--------------
f | f | t | t
--a line that is partly contained by circle, but not fully is defined as intersecting and
crossing,
-- but since of different dimension it does not overlap
SELECT ST_Overlaps(a,b) As a_overlap_b, ST_Crosses(a,b) As a_crosses_b,
ST_Intersects(a, b) As a_intersects_b,
ST_Contains(a,b) As a_contains_b
FROM (SELECT ST_Buffer(ST_GeomFromText('POINT(1 0.5)'), 3As a, ST_GeomFromText('
LINESTRING(1 0, 1 1, 3 5)'
As b)
As foo;
a_overlap_b | a_crosses_b | a_intersects_b | a_contains_b
-------------+-------------+----------------+--------------
f | t | t | f
-- a 2-dimensional bent hot dog (aka buffered line string) that intersects a circle,
-- but is not fully contained by the circle is defined as overlapping since they are of ←-
the same dimension,
-- but it does not cross, because the intersection of the 2 is of the same dimension
-- as the maximum dimension of the 2
SELECT ST_Overlaps(a,b) As a_overlap_b, ST_Crosses(a,b) As a_crosses_b, ST_Intersects(a, b)
As a_intersects_b,
ST_Contains(b,a) As b_contains_a,
ST_Dimension(a) As dim_a, ST_Dimension(b) as dim_b, ST_Dimension(ST_Intersection(a,b)) As 
dima_intersection_b
FROM (SELECT ST_Buffer(ST_GeomFromText('POINT(1 0.5)'), 3As a,
ST_Buffer(ST_GeomFromText('LINESTRING(1 0, 1 1, 3 5)'),0.5As b)
PostGIS 3.0.5dev Manual 281 / 841
As foo;
a_overlap_b | a_crosses_b | a_intersects_b | b_contains_a | dim_a | dim_b |
dima_intersection_b
-------------+-------------+----------------+--------------+-------+-------+--------------------- ←- t | f | t | f | 2 | 2 | 2

ST_PointInsideCircle

Is the point geometry inside the circle defifined by center_x, center_y, radius.

//语法
boolean ST_PointInsideCircle(geometry a_point, float center_x, float center_y, float radius);
//示例
SELECT ST_PointInsideCircle(ST_Point(1,2), 0.523);
st_pointinsidecircle
------------------------
t

ST_Relate

Returns true if this Geometry is spatially related to anotherGeometry, by testing for intersections between the

Interior, Boundary and Exterior of the two geometries as specifified by the values in the intersectionMatrixPattern. If no intersectionMatrixPattern is passed in, then returns the maximum intersectionMatrixPattern that relates the 2 geometries.

//语法
boolean ST_Relate(geometry geomA, geometry geomB, text intersectionMatrixPattern);
text ST_Relate(geometry geomA, geometry geomB);
text ST_Relate(geometry geomA, geometry geomB, integer BoundaryNodeRule);
//示例
--Find all compounds that intersect and not touch a poly (interior intersects)
SELECT l.* , b.name As poly_name
FROM polys As b
INNER JOIN compounds As l
ON (p.the_geom && b.the_geom
AND ST_Relate(l.the_geom, b.the_geom,'T********'));
SELECT ST_Relate(ST_GeometryFromText('POINT(1 2)'), ST_Buffer(ST_GeometryFromText('POINT(1
2)'
),2));
st_relate
-----------
0FFFFF212
SELECT ST_Relate(ST_GeometryFromText('LINESTRING(1 2, 3 4)'), ST_GeometryFromText(
LINESTRING(5 6, 7 8)'
));
st_relate
-----------
FF1FF0102
SELECT ST_Relate(ST_GeometryFromText('POINT(1 2)'), ST_Buffer(ST_GeometryFromText('POINT(1
2)'
),2), '0FFFFF212');
st_relate
-----------
t
SELECT ST_Relate(ST_GeometryFromText('POINT(1 2)'), ST_Buffer(ST_GeometryFromText('POINT(1
2)'
),2), '*FF*FF212');
st_relate
-----------
t

ST_RelateMatch

Returns true if intersectionMattrixPattern1 implies intersectionMatrixPattern2.

//语法
boolean ST_RelateMatch(text intersectionMatrix, text intersectionMatrixPattern);
//示例
SELECT ST_RelateMatch('101202FFF''TTTTTTFFF') ;
-- result --
t
--example of common intersection matrix patterns and example matrices
-- comparing relationships of involving one invalid geometry and ( a line and polygon that ←-
intersect at interior and boundary)
SELECT mat.name, pat.name, ST_RelateMatch(mat.val, pat.val) As satisfied
FROM
VALUES ('Equality''T1FF1FFF1'),
('Overlaps''T*T***T**'),
('Within''T*F**F***'),
('Disjoint''FF*FF****'As pat(name,val)
CROSS JOIN
VALUES ('Self intersections (invalid)''111111111'),
('IE2_BI1_BB0_BE1_EI1_EE2''FF2101102'),
('IB1_IE1_BB0_BE0_EI2_EI1_EE2''F11F00212')
As mat(name,val);

ST_Touches

Returns TRUE if the geometries have at least one point in common, but their interiors do not intersect.

//语法
boolean ST_Touches(geometry g1, geometry g2);
//示例
SELECT ST_Touches('LINESTRING(0 0, 1 1, 0 2)'::geometry, 'POINT(1 1)'::geometry);
st_touches
------------
f
(1 row)
SELECT ST_Touches('LINESTRING(0 0, 1 1, 0 2)'::geometry, 'POINT(0 2)'::geometry);
st_touches
------------
t
(1 row)

ST_Within

Returns true if the geometry A is completely inside geometry B

//语法
boolean ST_Within(geometry A, geometry B);
//示例
--a circle within a circle
SELECT ST_Within(smallc,smallc) As smallinsmall,
ST_Within(smallc, bigc) As smallinbig,
ST_Within(bigc,smallc) As biginsmall,
ST_Within(ST_Union(smallc, bigc), bigc) as unioninbig,
ST_Within(bigc, ST_Union(smallc, bigc)) as biginunion,
ST_Equals(bigc, ST_Union(smallc, bigc)) as bigisunion
FROM
(
SELECT ST_Buffer(ST_GeomFromText('POINT(50 50)'), 20As smallc,
ST_Buffer(ST_GeomFromText('POINT(50 50)'), 40As bigc) As foo;
--Result
smallinsmall | smallinbig | biginsmall | unioninbig | biginunion | bigisunion
--------------+------------+------------+------------+------------+------------
t | t | f | t | t | t
(1 row)

Spatial Relationships-Distance

ST_3DDWithin

For 3d (z) geometry type Returns true if two geometries 3d distance is within number of units.

//语法
boolean ST_3DDWithin(geometry g1, geometry g2, double precision distance_of_srid);
//示例
-- Geometry example - units in meters (SRID: 2163 US National Atlas Equal area) (3D point
and line compared 2D point and line)
-- Note: currently no vertical datum support so Z is not transformed and assumed to be same
units as final.
SELECT ST_3DDWithin(
ST_Transform(ST_GeomFromEWKT('SRID=4326;POINT(-72.1235 42.3521 4)'),2163),
ST_Transform(ST_GeomFromEWKT('SRID=4326;LINESTRING(-72.1260 42.45 15, -72.123 42.1546
20)'
),2163),126.8
As within_dist_3d,
ST_DWithin(
ST_Transform(ST_GeomFromEWKT('SRID=4326;POINT(-72.1235 42.3521 4)'),2163),
ST_Transform(ST_GeomFromEWKT('SRID=4326;LINESTRING(-72.1260 42.45 15, -72.123 42.1546 
20)'
),2163),
126.8
As within_dist_2d;
within_dist_3d | within_dist_2d
----------------+----------------
f | t

ST_3DDFullyWithin

Returns true if all of the 3D geometries are within the specifified distance of one another.

//语法
boolean ST_3DDFullyWithin(geometry g1, geometry g2, double precision distance);
//示例
-- This compares the difference between fully within and distance within as well
-- as the distance fully within for the 2D footprint of the line/point vs. the 3d fully ←-
within
SELECT ST_3DDFullyWithin(geom_a, geom_b, 10as D3DFullyWithin10, ST_3DDWithin(geom_a, ←-
geom_b, 10as D3DWithin10,
ST_DFullyWithin(geom_a, geom_b, 20as D2DFullyWithin20,
ST_3DDFullyWithin(geom_a, geom_b, 20as D3DFullyWithin20 from
(select ST_GeomFromEWKT('POINT(1 1 2)'as geom_a,
ST_GeomFromEWKT('LINESTRING(1 5 2, 2 7 20, 1 9 100, 14 12 3)'as geom_b) t1;
d3dfullywithin10 | d3dwithin10 | d2dfullywithin20 | d3dfullywithin20
------------------+-------------+------------------+------------------
f | t | t | f

ST_DFullyWithin

Returns true if all of the geometries are within the specifified distance of one another.

//语法
boolean ST_DFullyWithin(geometry g1, geometry g2, double precision distance);
//示例
SELECT ST_DFullyWithin(geom_a, geom_b, 10as DFullyWithin10, ST_DWithin(geom_a,
geom_b, 10as DWithin10, ST_DFullyWithin(geom_a, geom_b, 20as DFullyWithin20 from
(select ST_GeomFromText('POINT(1 1)'as geom_a,ST_GeomFromText('LINESTRING(1 5, 2 7, 1 ←-
9, 14 12)'
as geom_b) t1;
-----------------
DFullyWithin10 | DWithin10 | DFullyWithin20 |
---------------+----------+---------------+
f | t | t |

ST_DWithin

Returns true if the geometries are within the specifified distance of one another. For geometry units are in those

of spatial reference and for geography units are in meters and measurement is defaulted to use_spheroid=true (measure around spheroid), for faster check, use_spheroid=false to measure along sphere.

//语法
boolean ST_DWithin(geometry g1, geometry g2, double precision distance_of_srid);
boolean ST_DWithin(geography gg1, geography gg2, double precision distance_meters, boolean use_spheroid);
//示例
-- Find the nearest hospital to each school
-- that is within 3000 units of the school.
-- We do an ST_DWithin search to utilize indexes to limit our search list
-- that the non-indexable ST_Distance needs to process
-- If the units of the spatial reference is meters then units would be meters
SELECT DISTINCT ON (s.gid) s.gid, s.school_name, s.geom, h.hospital_name
FROM schools s
LEFT JOIN hospitals h ON ST_DWithin(s.the_geom, h.geom, 3000)
ORDER BY s.gid, ST_Distance(s.geom, h.geom);
-- The schools with no close hospitals
-- Find all schools with no hospital within 3000 units
-- away from the school. Units is in units of spatial ref (e.g. meters, feet, degrees)
SELECT s.gid, s.school_name
FROM schools s
LEFT JOIN hospitals h ON ST_DWithin(s.geom, h.geom, 3000)
WHERE h.gid IS NULL;
-- Find broadcasting towers that receiver with limited range can receive.
-- Data is geometry in Spherical Mercator (SRID=3857), ranges are approximate.
-- Create geometry index that will check proximity limit of user to tower
CREATE INDEX ON broadcasting_towers using gist (geom);
-- Create geometry index that will check proximity limit of tower to user
CREATE INDEX ON broadcasting_towers using gist (ST_Expand(geom, sending_range));
-- Query towers that 4-kilometer receiver in Minsk Hackerspace can get
-- Note: two conditions, because shorter LEAST(b.sending_range, 4000) will not use index.
SELECT b.tower_id, b.geom
FROM broadcasting_towers b
WHERE ST_DWithin(b.geom, 'SRID=3857;POINT(3072163.4 7159374.1)'4000)
AND ST_DWithin(b.geom, 'SRID=3857;POINT(3072163.4 7159374.1)', b.sending_range);

Measurement Functions

ST_Area

Returns the area of a polygonal geometry.

//语法
float ST_Area(geometry g1);
float ST_Area(geography geog, boolean use_spheroid=true);
//示例
select ST_Area(geom) sqft,
ST_Area(geom) * 0.3048 ^ 2 sqm
from (
select 'SRID=2249;POLYGON((743238 2967416,743238 2967450,
743265 2967450,743265.625 2967416,743238 2967416))'
 :: geometry geom
) subquery;
&#x250c;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x252c;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2510;
&#x2502; sqft &#x2502; sqm &#x2502;
&#x251c;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x253c;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2524;
&#x2502; 928.625 &#x2502; 86.27208552 &#x2502;
&#x2514;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2534;&#x2500;&#x2

ST_Azimuth

Returns the north-based azimuth as the angle in radians measured clockwise from the vertical on pointA to

pointB.

//语法
float ST_Azimuth(geometry pointA, geometry pointB);
float ST_Azimuth(geography pointA, geography pointB);
//示例
SELECT degrees(ST_Azimuth(ST_Point(2545), ST_Point(75100))) AS degA_B,
degrees(ST_Azimuth(ST_Point(75100), ST_Point(2545))) AS degB_A;
dega_b | degb_a
------------------+------------------
42.2736890060937 | 222.273689006094

ST_Angle

Returns the angle between 3 points, or between 2 vectors (4 points or 2 lines).

//语法
float ST_Angle(geometry point1, geometry point2, geometry point3, geometry point4);
float ST_Angle(geometry line1, geometry line2);
//示例
WITH rand AS (
SELECT s, random() * 2 * PI() AS rad1
, random() * 2 * PI() AS rad2
    FROM generate_series(1,2,2AS s ), points AS (
SELECT s, rad1,rad2, ST_MakePoint(cos1+s,sin1+s) as p1, ST_MakePoint(s,s) AS p2,
ST_MakePoint(cos2+s,sin2+s) as p3
FROM rand
,cos(rad1) cos1, sin(rad1) sin1
,cos(rad2) cos2, sin(rad2) sin2
)
SELECT s, ST_AsText(ST_SnapToGrid(ST_MakeLine(ARRAY[p1,p2,p3]),0.001)) AS line
degrees(ST_Angle(p1,p2,p3)) as computed_angle
round(degrees(2*PI()-rad2 -2*PI()+rad1+2*PI()))::int%360 AS reference
round(degrees(2*PI()-rad2 -2*PI()+rad1+2*PI()))::int%360 AS reference
FROM points ;
1 | line | computed_angle | reference
------------------+------------------
1 | LINESTRING(1.511 1.86,1 1,0.896 0.005) | 155.27033848688 | 155

ST_ClosestPoint

Returns the 2D point on g1 that is closest to g2. This is the fifirst point of the shortest line.

//语法
geometry ST_ClosestPoint(geometry g1, geometry g2);
//示例
SELECT ST_AsText(ST_ClosestPoint(pt,line) ) AS cp_pt_line,
ST_AsText(ST_ClosestPoint(line,pt
)) As cp_line_pt
FROM (SELECT 'POINT(100 100)'::geometry
As pt, 'LINESTRING (20 80, 98
190, 110 180, 50 75 )'
::geometry As line
As foo;
cp_pt_line | cp_line_pt
----------------+------------------------------------------
POINT(100 100) | POINT(73.0769230769231
115.384615384615)

ST_3DClosestPoint

Returns the 3D point on g1 that is closest to g2. This is the fifirst point of the 3D shortest line.

//语法
geometry ST_3DClosestPoint(geometry g1, geometry g2);
//示例
SELECT ST_AsEWKT(ST_3DClosestPoint(line,pt)) AS cp3d_line_pt,
ST_AsEWKT(ST_ClosestPoint(line,pt)) As cp2d_line_pt
FROM (SELECT 'POINT(100 100 30)'::geometry As pt, 'LINESTRING (20 80 20, 98 190 1, 110 180 3, 50 75 1000)':: 
geometry As line
As foo;
cp3d_line_pt | 
cp2d_line_pt
-----------------------------------------------------------
POINT(54.6993798867619 128.935022917228 11.5475869506606) | POINT(73.0769230769231 
115.384615384615)

ST_Distance

Returns the distance between two geometry or geography values.

//语法
float ST_Distance(geometry g1, geometry g2);
float ST_Distance(geography geog1, geography geog2, >boolean use_spheroid=true);
//示例
SELECT ST_Distance( 'SRID=4326;POINT(-72.1235 42.3521)'::geometry, 'SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)'::geometry
);
st_distance
-----------------
0.00150567726382282

SELECT ST_Distance(
ST_Transform('SRID=4326;POINT(-72.1235 42.3521)'::geometry, 3857),
ST_Transform('SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)'::geometry, 3857)
);
st_distance
-----------------
167.441410065196

ST_3DDistance

Returns the 3D cartesian minimum distance (based on spatial ref) between two geometries in projected units.

//语法
float ST_3DDistance(geometry g1, geometry g2);
//示例
-- Geometry example - units in meters (SRID: 2163 US National Atlas Equal area) (3D point ←-
and line compared 2D point and line)
-- Note: currently no vertical datum support so Z is not transformed and assumed to be same ←-
units as final.
SELECT ST_3DDistance(
ST_Transform('SRID=4326;POINT(-72.1235 42.3521 4)'::geometry,2163),
ST_Transform('SRID=4326;LINESTRING(-72.1260 42.45 15, -72.123 42.1546 20)'::geometry ←-
,2163)
As dist_3d,
ST_Distance(
ST_Transform('SRID=4326;POINT(-72.1235 42.3521)'::geometry,2163),
ST_Transform('SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)'::geometry,2163)
As dist_2d;
dist_3d | dist_2d
------------------+-----------------
127.295059324629 | 126.66425605671

ST_DistanceSphere

Returns minimum distance in meters between two lon/lat geometries using a spherical earth model.

//语法
float ST_DistanceSphere(geometry geomlonlatA, geometry geomlonlatB);
//示例
SELECT round(CAST(ST_DistanceSphere(ST_Centroid(the_geom), ST_GeomFromText('POINT(-118 38)
'
,4326)) As numeric),2As dist_meters,
round(CAST(ST_Distance(ST_Transform(ST_Centroid(the_geom),32611),
ST_Transform(ST_GeomFromText('POINT(-118 38)'4326),32611)) As numeric),2As 
dist_utm11_meters,
round(CAST(ST_Distance(ST_Centroid(the_geom), ST_GeomFromText('POINT(-118 38)'4326)) As
numeric),5As dist_degrees,
round(CAST(ST_Distance(ST_Transform(the_geom,32611),
ST_Transform(ST_GeomFromText('POINT(-118 38)'4326),32611)) As numeric),2As
min_dist_line_point_meters
FROM
(SELECT ST_GeomFromText('LINESTRING(-118.584 38.374,-118.583 38.5)'4326As the_geom) 
as foo;
dist_meters | dist_utm11_meters | dist_degrees | min_dist_line_point_meters
-------------+-------------------+--------------+----------------------------
70424.47 | 70438.00 | 0.72900 | 65871.18

ST_DistanceSpheroid

Returns the minimum distance between two lon/lat geometries using a spheroidal earth model.

//语法
float ST_DistanceSpheroid(geometry geomlonlatA, geometry geomlonlatB, spheroid measurement_spheroid);
//示例
SELECT round(CAST(
ST_DistanceSpheroid(ST_Centroid(the_geom), ST_GeomFromText('POINT(-118 38)',4326), '
SPHEROID["WGS 84",6378137,298.257223563]'
)
As numeric),2As dist_meters_spheroid,
round(CAST(ST_DistanceSphere(ST_Centroid(the_geom), ST_GeomFromText('POINT(-118 38)
'
,4326)) As numeric),2As dist_meters_sphere,
round(CAST(ST_Distance(ST_Transform(ST_Centroid(the_geom),32611),
ST_Transform(ST_GeomFromText('POINT(-118 38)'4326),32611)) As numeric),2As
dist_utm11_meters
FROM
(SELECT ST_GeomFromText('LINESTRING(-118.584 38.374,-118.583 38.5)'4326As the_geom)
as foo;
dist_meters_spheroid | dist_meters_sphere | dist_utm11_meters
----------------------+--------------------+-------------------
70454.92 | 70424.47 | 70438.00

ST_FrechetDistance

Returns the Fréchet distance between two geometries.

//语法
float ST_FrechetDistance(geometry g1, geometry g2, float densifyFrac = -1);
//示例
float ST_FrechetDistance(geometry g1, geometry g2, float densifyFrac = -1);

ST_HausdorffDistance

Returns the Hausdorff distance between two geometries.

//语法
float ST_HausdorffDistance(geometry g1, geometry g2);
float ST_HausdorffDistance(geometry g1, geometry g2, float densifyFrac);
//示例
SELECT DISTINCT ON(buildings.gid) buildings.gid, parcels.parcel_id
FROM buildings INNER JOIN parcels ON ST_Intersects(buildings.geom,parcels.geom)
ORDER BY buildings.gid, ST_HausdorffDistance(buildings.geom, parcels.geom);

ST_Length

Returns the 2D length of a linear geometry.

//语法
float ST_Length(geometry a_2dlinestring);
float ST_Length(geography geog, boolean use_spheroid=true);
//示例
SELECT ST_Length(ST_GeomFromText('LINESTRING(743238 2967416,743238 2967450,743265 2967450,
743265.625 2967416,743238 2967416)'
,2249));
st_length
---------
122.630744000095
--Transforming WGS 84 LineString to Massachusetts state plane meters
SELECT ST_Length(
ST_Transform(
ST_GeomFromEWKT('SRID=4326;LINESTRING(-72.1260 42.45, -72.1240 42.45666, -72.123 ←-
42.1546)'
),
26986
)
);
st_length
---------
34309.4563576191

ST_Length2D

Returns the 2D length of a linear geometry. Alias for ST_Length.

//语法
float ST_Length2D(geometry a_2dlinestring);

ST_3DLength

Returns the 3D length of a linear geometry.

//语法
float ST_3DLength(geometry a_3dlinestring)
//示例
SELECT ST_3DLength(ST_GeomFromText('LINESTRING(743238 2967416 1,743238 2967450 1,743265 ←-
2967450 3,
743265.625 2967416 3,743238 2967416 3)'
,2249));
ST_3DLength
-----------
122.704716741457

ST_LengthSpheroid

Returns the 2D or 3D length/perimeter of a lon/lat geometry on a spheroid.

//语法
float ST_LengthSpheroid(geometry a_geometry, spheroid a_spheroid);
//示例
SELECT ST_LengthSpheroid( geometry_column, 'SPHEROID["GRS_1980",6378137,298.257222101]' )
FROM geometry_table;
SELECT ST_LengthSpheroid( the_geom, sph_m ) As tot_len,
ST_LengthSpheroid(ST_GeometryN(the_geom,1), sph_m) As len_line1,
ST_LengthSpheroid(ST_GeometryN(the_geom,2), sph_m) As len_line2
FROM (SELECT ST_GeomFromText('MULTILINESTRING((-118.584 38.374,-118.583 38.5),
(-71.05957 42.3589 , -71.061 43))'
As the_geom,
CAST('SPHEROID["GRS_1980",6378137,298.257222101]' As spheroid) As sph_m) as foo;
tot_len | len_line1 | len_line2
------------------+------------------+------------------
85204.5207562955 | 13986.8725229309 | 71217.6482333646

--3D
SELECT ST_LengthSpheroid( the_geom, sph_m ) As tot_len,
ST_LengthSpheroid(ST_GeometryN(the_geom,1), sph_m) As len_line1,
ST_LengthSpheroid(ST_GeometryN(the_geom,2), sph_m) As len_line2
FROM (SELECT ST_GeomFromEWKT('MULTILINESTRING((-118.584 38.374 20,-118.583 38.5 30) ←- ,
(-71.05957 42.3589 75, -71.061 43 90))'
As the_geom,
CAST('SPHEROID["GRS_1980",6378137,298.257222101]' As spheroid) As sph_m) as foo;
tot_len | len_line1 | len_line2
------------------+-----------------+------------------
85204.5259107402 | 13986.876097711 | 71217.6498130292

ST_LongestLine

Returns the 2D longest line between two geometries.

//语法
geometry ST_LongestLine(geometry g1, geometry g2);
//示例
SELECT ST_AsText(
ST_LongestLine('POINT(100 100)'::
geometry, 'LINESTRING (20 80, 98
190, 110 180, 50 75 )'
::geometry) ) As lline;
lline
-----------------
LINESTRING(100 100,98 190)

ST_3DLongestLine

Returns the 3D longest line between two geometries.

//语法
geometry ST_3DLongestLine(geometry g1, geometry g2);
//示例
SELECT ST_AsEWKT(ST_3DLongestLine(line,pt)) AS lol3d_line_pt,
ST_AsEWKT(ST_LongestLine(line,pt)) As lol2d_line_pt
FROM (SELECT 'POINT(100 100 30)'::geometry As pt, 'LINESTRING (20 80 20, 98 190 1, 110 180 3, 50 75 1000)':: ←-
geometry As line
As foo;
lol3d_line_pt | lol2d_line_pt
-----------------------------------+----------------------------
LINESTRING(50 75 1000,100 100 30) | LINESTRING(98 190,100 100)

ST_MaxDistance

Returns the 2D largest distance between two geometries in projected units.

//语法
float ST_MaxDistance(geometry g1, geometry g2);
//示例
SELECT ST_MaxDistance('POINT(0 0)'::geometry, 'LINESTRING ( 2 0, 0 2 )'::geometry
);
st_maxdistance
-----------------
2
(1 row)
SELECT ST_MaxDistance('POINT(0 0)'::geometry, 'LINESTRING ( 2 2, 2 2 )'::geometry
);
st_maxdistance
------------------
2.82842712474619
(1 row)

ST_3DMaxDistance

Returns the 3D cartesian maximum distance (based on spatial ref) between two geometries in projected

units.

//语法
float ST_3DMaxDistance(geometry g1, geometry g2);
//示例
-- Geometry example - units in meters (SRID: 2163 US National Atlas Equal area) (3D point
and line compared 2D point and line)
-- Note: currently no vertical datum support so Z is not transformed and assumed to be same
units as final.
SELECT ST_3DMaxDistance(
ST_Transform(ST_GeomFromEWKT('SRID=4326;POINT(-72.1235 42.3521 10000)'),2163),
ST_Transform(ST_GeomFromEWKT('SRID=4326;LINESTRING(-72.1260 42.45 15, -72.123 42.1546
20)'
),2163)
As dist_3d,
ST_MaxDistance(
ST_Transform(ST_GeomFromEWKT('SRID=4326;POINT(-72.1235 42.3521 10000)'),2163),
ST_Transform(ST_GeomFromEWKT('SRID=4326;LINESTRING(-72.1260 42.45 15, -72.123 42.1546
20)'
),2163)
As dist_2d;
dist_3d | dist_2d
------------------+------------------
24383.7467488441 | 22247.8472107251

ST_MinimumClearance

Returns the minimum clearance of a geometry, a measure of a geometry’s robustness.

//语法
float ST_MinimumClearance(geometry g);
//示例
SELECT ST_MinimumClearance('POLYGON ((0 0, 1 0, 1 1, 0.5 3.2e-4, 0 0))');
st_minimumclearance
---------------------
0.00032

ST_MinimumClearanceLine

Returns the two-point LineString spanning a geometry’s minimum clearance.

//语法
Geometry ST_MinimumClearanceLine(geometry g);
//示例
SELECT ST_AsText(ST_MinimumClearanceLine('POLYGON ((0 0, 1 0, 1 1, 0.5 3.2e-4, 0 0))'));
st_astext
-------------------------------
LINESTRING(0.5 0.00032,0.5 0)

ST_Perimeter

Returns the length of the boundary of a polygonal geometry or geography.

//语法
float ST_Perimeter(geometry g1);
float ST_Perimeter(geography geog, boolean use_spheroid=true);
//示例
SELECT ST_Perimeter(ST_GeomFromText('POLYGON((743238 2967416,743238 2967450,743265 2967450,
743265.625 2967416,743238 2967416))'
2249));
st_perimeter
---------
122.630744000095
(1 row)
SELECT ST_Perimeter(ST_GeomFromText('MULTIPOLYGON(((763104.471273676 2949418.44119003,
763104.477769673 2949418.42538203,
763104.189609677 2949418.22343004,763104.471273676 2949418.44119003)),
((763104.471273676 2949418.44119003,763095.804579742 2949436.33850239,
763086.132105649 2949451.46730207,763078.452329651 2949462.11549407,
763075.354136904 2949466.17407812,763064.362142565 2949477.64291974,
763059.953961626 2949481.28983009,762994.637609571 2949532.04103014,
762990.568508415 2949535.06640477,762986.710889563 2949539.61421415,
763117.237897679 2949709.50493431,763235.236617789 2949617.95619822,
763287.718121842 2949562.20592617,763111.553321674 2949423.91664605,
763104.471273676 2949418.44119003)))'
2249));
st_perimeter
---------
845.227713366825
(1 row)

ST_Perimeter2D

Returns the 2D perimeter of a polygonal geometry. Alias for ST_Perimeter.

//语法
float ST_Perimeter2D(geometry geomA);

ST_3DPerimeter

Returns the 3D perimeter of a polygonal geometry.

//语法
float ST_3DPerimeter(geometry geomA);
//示例
SELECT ST_3DPerimeter(the_geom), ST_Perimeter2d(the_geom), ST_Perimeter(the_geom) FROM
(SELECT ST_GeomFromEWKT('SRID=2249;POLYGON((743238 2967416 2,743238 2967450 1,
743265.625 2967416 1,743238 2967416 2))'
As the_geom) As foo;
ST_3DPerimeter | st_perimeter2d | st_perimeter
------------------+------------------+------------------
105.465793597674 | 105.432997272188 | 105.432997272188

ST_Project

Returns a point projected from a start point by a distance and bearing (azimuth).

//语法
geography ST_Project(geography g1, float distance, float azimuth);
//示例
SELECT ST_AsText(ST_Project('POINT(0 0)'::geography, 100000radians(45.0)));
st_astext
--------------------------------------------
POINT(0.635231029125537 0.639472334729198)
(1 row)

ST_ShortestLine

Returns the 2D shortest line between two geometries.

//语法
geometry ST_ShortestLine(geometry g1, geometry g2);
//示例
SELECT ST_AsText(
ST_ShortestLine('POINT(100 100)
'
::geometry, 'LINESTRING (20 80, 98
190, 110 180, 50 75 )'
::geometry) ) As sline;
sline
-----------------
LINESTRING(100 100,73.0769230769231
115.384615384615)

Bayanbulake

2021/11/26  阅读:27  主题:自定义主题1

作者介绍

Bayanbulake