Python 如何查询wkt格式Geometry对象内的所有点
我有一个名为Python 如何查询wkt格式Geometry对象内的所有点,python,postgresql,postgis,Python,Postgresql,Postgis,我有一个名为slope的表,它包含以下列X,Y,Z和几何体。上述值表示x、y、z坐标 几何学的。我还有一个名为fieldGeometry的对象,它是WKT格式的。 我的问题是如何查询对象fieldGeometry内的X、Y中的所有点 如下面的代码所示,这是我的尝试,但没有成功。 请告诉我如何以wkt格式查询几何体对象内的所有点 代码: def executeWithFetchallForST_Intersects(self, cursor, fieldAsPolygonsGeometry, gr
slope
的表,它包含以下列X
,Y
,Z
和几何体
。上述值表示x、y、z坐标
几何学的。我还有一个名为fieldGeometry
的对象,它是WKT格式的。
我的问题是如何查询对象fieldGeometry
内的X
、Y
中的所有点
如下面的代码所示,这是我的尝试,但没有成功。
请告诉我如何以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函数。要检查
x
、y
、z
坐标是否在fieldGeometry
上的多边形内部,只需执行以下操作:
SELECT
ST_Contains(fieldGeometry,
ST_SetSRID(ST_MakePoint(x,y,z),4326))
FROM slope;
演示:
编辑:参见备注
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,请使用相应的函数。感谢您的时间和努力。但我想告诉你,正如问题中所述,
fieldGeometry
是一个对象,而不是一个strting。您认为将其作为对象传递给st_contains()是否有效?请告诉我如何将几何体对象fieldGeometry
传递给select statement。我将使用python中的select语句。应该是这样的:def executeWithFetchallForST_Contains(self,cursor,fieldsPolygonsGeometry):cursor.execute(“选择ST_Contains”(+fieldsPolygonsGeometry+),“+ST_SetGrid(ST_MakePoint(x,y,z),4326)+”)FROM“+config['PostgreDB'['table_name'])返回cursor.fetchall()
SELECT
ST_Contains(fieldGeometry,
ST_SetSRID(ST_MakePoint(x,y,z),4326))
FROM slope;
st_contains
-------------
f
t
(2 rows)
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)