简体   繁体   English

错误空间子集和PostGIS数据库

[英]Error spatially subsetting and PostGIS database

I am trying to make an spatial operation using sqlalchemy and geoalchemy2 on Python 3.5. 我试图让使用空间操作sqlalchemygeoalchemy2关于Python 3.5。 I have a table with points as a geom attribute. 我有一个表作为geom属性的点。 I have already read the table and follow the documentation instructions: 我已阅读该表并按照文档说明操作:

metadata = MetaData()
table = Table('table', 
                 metadata, autoload=True,
                 schema = "schema",
                 autoload_with=engine)
print(table.columns)

This correctly returns me the name of the columns of my table. 这正确地返回了我的表的列的名称。 Now, I want to create a spatial subset of the data selecting only the points that are inside a POLYGON object. 现在,我想创建数据的空间子集,仅选择POLYGON对象内的点。 I tried with ST_Contains and ST_Intersection : 我试过ST_ContainsST_Intersection

# Create session for queries
Session = sessionmaker(bind=engine)
session = Session()

#SELECT * FROM table:
q = session.query(table).filter(table.c.geom.ST_Intersects(func.GeomFromEWKT(<POLYGON>)))

POLYGON is a WKT geometry with a defined SRID=4326 . POLYGON是WKT几何,定义的SRID=4326 I have tried already with different forms of that same polygon, but none have worked. 我已经尝试过使用相同多边形的不同形式,但没有一个有效。 When executing the query, the following error returns: 执行查询时,以下错误返回:

(psycopg2.InternalError) geometry contains non-closed rings
HINT:  "...140.965576171875 -11.11288507032144))" <-- parse error at position 166 within geometry

Where I am failing? 我失败的地方?

The polygon you are using is not closed. 您正在使用的多边形未关闭。 The first and last coordinates must be the same. 第一个和最后一个坐标必须相同。 Change it to: 将其更改为:

wkt_string = "POLYGON((141.0205078125 -9.166331387642987, 
                       143.602294921875 -9.155485188844034, 
                       143.67919921875 -11.112885070321443, 
                       140.965576171875 -11.11288507032144, 
                       141.0205078125 -9.166331387642987))"

Alternatively, you could construct the polygon from a line and automatically add the missing point 或者,您可以从一条线构造多边形并自动添加缺失点

SELECT ST_MakePolygon(
         ST_AddPoint(foo.open_line, ST_StartPoint(foo.open_line)))
FROM (
  SELECT ST_GeomFromText(
            'LINESTRING(141.0205078125 -9.166331387642987, 
                        143.602294921875 -9.155485188844034,
                        143.67919921875 -11.112885070321443, 
                        140.965576171875 -11.11288507032144)')
              As open_line) 
   As foo;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM