postgresql ST_REPORT IN函数在POSTGIS中不起作用

xurqigkl  于 5个月前  发布在  PostgreSQL
关注(0)|答案(1)|浏览(77)

为什么使用空间函数(例如ST_Within)的SQL查询在提供PostgreSQL/PostGIS数据库中存储的多边形内的纬度和经度坐标时返回“Location not found”而不是“Location found”?

@app.get('/polygons/<latitude>/<longitude>')
def verify_polygon(latitude, longitude):
    try:
        conn = connect_db()
        cur = conn.cursor()

        cur.execute(f'SELECT id_0 FROM public."polygons-c3" WHERE ST_Within(ST_SetSRID(ST_MakePoint({longitude}, {latitude}), 4326), geom)')
        result = cur.fetchone()
        cur.close()
        conn.close()

        if result:
            return jsonify({'status': 'Location found', 'lote': result[0]}), 200
        else:
            return jsonify({'status': 'Location not found'}), 404
    except Exception as e:
        return jsonify({'error': str(e)}), 500

字符串
我期待“位置找到”,因为我得到一个点与谷歌Map是在其中一个多边形

disho6za

disho6za1#

ST_MakePoint()接受x+y,lon+lat,按此顺序。
我在谷歌Map上得到一个点,
我敢打赌,你是直接从谷歌Mapr-click/URL中比较坐标,而这些坐标的顺序与之相反:y+x,lat+lon。
https://www.google.com/maps/@85.0,1.0,4.0z
参数对和右键单击时显示的内容是北纬85°,西经1°(缩放4.0)。

ST_SetSRID(ST_MakePoint(1,85),4326)

字符串
请确保您没有将纬度传递为经度,反之亦然。如果您不小心将它们翻转,则above变为:

ST_SetSRID(ST_MakePoint(85,1),4326)


它位于斯里兰卡南部的某个地方(西经85°,北纬1°),靠近赤道(热),而不是格陵兰岛(冷)。如果你把坐标翻转回来,你的测试点应该适合你的多边形。

相关问题