1.指定时间区间的轨迹段和几何图形空间是否相交
ST_intersects
指定时间区间的轨迹段和几何图形空间是否相交。

ST_Intersects简单的说就是判断geometry geomA 与geometry geomB 是否存在geometry格式的交集;也可以判断geography geogA 与 geography geogB 是否存在geography这种类型的交
ST_Intersects

2.postgresql计算两点距离(经纬度地理位置)
下面两种方法:
select
ST_Distance(ST_SetSRID(ST_MakePoint(115.97166453999147,28.716493914230423),4326)::geography,ST_SetSRID(ST_MakePoint(106.00231199774656,29.719258550486572),4326)::geography
),
ST_Length(ST_MakeLine(ST_MakePoint(115.97166453999147,28.716493914230423),ST_MakePoint(106.00231199774656,29.719258550486572))::geography
)
备注:sql 中的 4326 是指的wsg84 的系统,ST_Distance计算出来的距离单位是米ST_GeomFromText('LINESTRING(115.97166453999147 28.716493914230423,106.00231199774656 29.719258550486572)')与
ST_MakeLine(ST_MakePoint(115.97166453999147,28.716493914230423),ST_MakePoint(106.00231199774656,29.719258550486572)
)等价ST_GeomFromText('POINT(115.97166453999147 28.716493914230423)',4326)与
ST_SetSRID(ST_MakePoint(115.97166453999147,28.716493914230423),4326)等价ST_SetSRID(ST_MakePoint(115.97166453999147,28.716493914230423),4326)::geography与
Geography(ST_SetSRID(ST_MakePoint(115.97166453999147,28.716493914230423),4326))、
ST_GeographyFromText('SRID=4326;POINT(115.97166453999147 28.716493914230423)')等价
(::geography是postgis中的转换类型语法,把geometry转成geography)
资料:
https://blog.csdn.net/seapeak007/article/details/83059638
3.PostgreSQL PostGIS 的5种空间距离排序(km)算法
该方法不推荐,建议使用本文第二点的方法
PostgreSQL PostGIS 的5种空间距离排序(knn)算法

4.某一个点到某条线的距离是不是在某个范围内:
4.1 ST_Contains
select ST_Contains(St_Astext(ST_Buffer(geography(geomfromtext('MULTILINESTRING((线的坐标点))')),25.00{以米为单位的距离})),st_astext(geography(geomfromtext('POINT(121.37805 37.54142)')))) as result
使用类似上面的方式,就可以输入以米为单位的距离判断某个点是否在某个距离范围内;
资料:
https://blog.csdn.net/duanmuxiao/article/details/45667221
postgis 中的距离计算
4.2 ST_distance和ST_DWithin
SELECT T.*,ST_distance (ST_geomfromtext ( 'POINT(' || T.longitude || ' ' || T.latitude || ')' ) :: geography,ST_geomfromtext ( 'LINESTRING(' || '115.157801 22.885121,115.157801 22.885121' || ')' ) :: geography ) AS distance
FROMt_table T where 1=1and ST_DWithin (ST_geomfromtext ( 'POINT(' || t.longitude || ' ' || t.latitude || ')' ) :: geography,ST_geomfromtext ( 'LINESTRING(' || '115.157801 22.885121,115.157801 22.885121' || ')' ) :: geography,50)
ORDER BY distance LIMIT 5
以上使用ST_distance计算距离进行展示,距离的单位是米,使用ST_DWithin 框定和设定的线段50米范围内的点查询出来,
LINESTRING的规则为必须为两个以上的点才能连成线,所以如果是同样一个点则会退化为计算两点之间的距离
5.Geometry SQL参考
Geometry SQL参考

6.ST_GeomFromGeoJSON
描述
仅适用于GeoJSON中的Geometry片段。如果您尝试在整个JSON文档上使用它,则会引发错误。
该函数支持3D对象,并且不会丢弃Geometry对象的z-index。

如果是多个[{“type”:“Point”,“coordinates”:[116,40]},{“type”:“Point”,“coordinates”:[118,50]}]使用ST_GeomFromGeoJSON转换会报如下错误
7. 空间扩展函数st_geomfromgeojson无法识别完整的geojson问题解决
PGSQL空间扩展函数st_geomfromgeojson无法识别完整的geojson问题解决
解决方法,将多要素geoJson转换为Geometry,再提取成Geojson

8.pg数据库几何类型转换:
postgis常用操作手册
1.wkt转geometry:st_geomfromtext(wkt)
select st_geomfromtext('Point(122 33)')
2.geometry转wkt:st_astext(geometry)
select st_astext(st_geomfromtext('Point(122 33)'))
3.geometry转geojson:st_asgeojson(geometry)
select st_asgeojson(st_geomfromtext('Point(122 33)'))
4.geojson转geometry:st_geomfromgeojson(geojson)
select
st_geomfromgeojson(
st_asgeojson(st_geomfromtext('Point(122 33)')))
5.geometry转geohash:st_geohash(geometry)
select st_geohash(st_geomfromtext('Point(116 39)'))
6.geohash转geometry:st_geomfromgeohash
select st_geomfromgeohash('wwfmzesx7yvjugxr3nzv')
目前没有找到类似这样的函数st_geomfromgeojson(‘[{“type”:“Point”,“coordinates”:[116,40]},{“type”:“Point”,“coordinates”:[118,50]}]’),如果有找到的朋友可以留言贴上
9.两个geometry之间关系
postgis常用操作手册
within、disjoint、intersects、union、intersection,difference
St_within(geom A,geom B)返回A是否处于B中
St_disjoint(geom A,geom B)返回A是否不在B中
St_intersects(geom A,geom B)返回A是否和B有接触
St_union(geom A,geom B)返回A+B两个几何的合并
St_intersection(geom A,geom B)返回A和B的交集
st_isempty(geom A)判断几何是否空
St_difference(geom A,geom B)返回A与B不相交的部分几何
select
st_difference(
st_buffer(
st_geomfromtext('Point(116 39)'),0.7),
st_buffer(
st_geomfromtext('Point(117 39)'),0.7)) geom
10.ST_AsGeoJSON
ST_AsGeoJSON
ST_AsGeoJSON 返回输入几何体或地理的 GeoJSON 表示形式。有关 GeoJSON 的更多信息,请参阅 Wikipedia 中的 GeoJSON。
对于 3DZ 和 4D 几何体,输出几何体是输入 3DZ 或 4D 几何体的 3DZ 投影。也就是说,x、y, 和 z 坐标存在于输出中。对于 3DZ 几何体,输出几何体是输入 3DM 几何体的 2D 投影。也就是说,只有 x 和 y 坐标存在于输出中。
对于输入地理,ST_AsGeoJSON 返回输入地理的 GeoJSON 表示形式。地理的坐标使用指定的精度显示。

11.各种表述经纬度格式样例
WKT
```xml
点: POINT(116.39088 39.90763)
线: LINESTRING(121.626446 37.28054,121.62621 37.278753,121.32149 37.013646)',4326)
面: POLYGON((121.366052 37.415496,121.370904 37.421809,121.366333 37.414761,121.366052 37.415496))',4326)
geojson
点:{"coordinates":[113.74582959807395,22.76202773278976],"type":"Point"}
线:{"coordinates":[[113.74582959807395,22.76202773278976],[113.74465565850886,22.760118452898325]],"type":"LineString"}
面:{"coordinates":[[[113.74606839235616,22.74360942946352],[113.74405025155386,22.741626940812907]]],"type":"Polygon"}
geometry
01010000000000000000805E400000000000804040
Postgis关于Point类型的一些查询操作
点的WKT表述如POINT(116.39088 39.90763),在Postgis中创建一个点几个方式:
ST_Point、ST_MakePoint:
–用法:ST_Point(float x_lon, float y_lat);
–输出:0101000000452A8C2D04195D402A6F47382DF44340
SELECT ST_Point(116.39088,39.90763)
上面虽然创建了点,但是SRID却是0(unknown),可以通过ST_SRID查看。下面方式设置坐标系为4326:
–用法:ST_Point(float x_lon, float y_lat);
–输出:0101000020E6100000452A8C2D04195D402A6F47382DF44340
SELECT ST_SetSRID(ST_Point(116.39088,39.90763),4326)
坐标顺序是经度(或x)在前,纬度(或y)在后。ST_MakePoint和ST_Point一样,ST_MakePoint可以快速高效的创建一个点。