如何查询wkt格式的几何对象内的所有点

How to query all the points inside a geomtry object of wkt format

我有一个名为 slope 的 table,它包含以下列 XYZgeometry。上述值表示 x,y,z 坐标 的几何。我还有一个名为 fieldGeometry 的对象,它是 WKT 格式的。 我的问题是如何查询对象 fieldGeometry.

XY 的所有点

如下代码所示,是我的尝试,没有成功。 请让我知道如何以 wkt 格式查询几何对象内的所有点

代码:

def executeWithFetchallForST_Intersects(self, cursor, fieldAsPolygonsGeometry, gridPointsAsGeometry):
    #"SELECT ST_Intersects("+ fieldAsPolygonsGeometry + "," + gridPointsAsGeometry + ")"
    cursor.execute("SELECT ST_Intersects("+ fieldAsPolygonsGeometry + "," + gridPointsAsGeometry + ") FROM " + config['PostgreDB']['table_name'])
    return cursor.fetchall()

PostGIS 函数被调用 ST_Contains。要检查 xyz 坐标是否在 fieldGeometry 上的多边形内部,只需执行以下操作:

SELECT 
  ST_Contains(fieldGeometry,
  ST_SetSRID(ST_MakePoint(x,y,z),4326)) 
FROM slope;

演示:db<>fiddle

CREATE TABLE slope (x numeric, y numeric, z numeric, fieldGeometry geometry(polygon,4326));
INSERT INTO slope VALUES 
(-4.6726,54.1180,42,'SRID=4326;POLYGON((-4.52 54.32,-4.38 54.32,-4.38 54.26,-4.52 54.26,-4.52 54.32))'),
(-4.5449,54.1888,42,'SRID=4326;POLYGON((-4.59 54.21,-4.49 54.21,-4.49 54.17,-4.59 54.17,-4.59 54.21))');

SELECT 
  ST_Contains(fieldGeometry,
  ST_SetSRID(ST_MakePoint(x,y,z),4326)) 
FROM slope;

 st_contains 
-------------
 f
 t
(2 rows)

编辑:查看评论

PostGIS 尝试使用已知格式解析几何图形,例如 WKT、WKB、GeoJSON 等。因此您无需担心在 ST_Contains 函数中使用 WKT - 无论使用哪种编程语言你正在使用,例如

WITH j (p) AS ( VALUES
 ('0101000000925CFE43FA2D12C00EBE30992A184B40'),
 ('POINT(-4.5449 54.1888)'),
 ('{"type":"Point","coordinates":[-4.5449,54.1888]}')
)
SELECT 
  ST_Contains('POLYGON((-4.59 54.21,-4.49 54.21,-4.49 54.17,-4.59 54.17,-4.59 54.21))',p) 
FROM j;

 st_contains 
-------------
 t
 t
 t
(3 rows)

如果你想强制 PostGIS 解析特定格式,比方说 WKT,请使用相应的函数 ST_GeomFromText