简体   繁体   English

MySQL 查询声明点不在多边形内

[英]MySQL query claims point is not within polygon

I've drawn a polygon which contains a point in Google Maps.我绘制了一个多边形,其中包含谷歌地图中的一个点。 But if I pass the coordinates to MySQL to calculate if the point is within the polygon, it returns false.但是,如果我将坐标传递给 MySQL 以计算该点是否在多边形内,则返回 false。

SELECT ST_Within(
    ST_GeomFromText('POINT(8.34047 54.91320)', 4326),
    ST_GeomFromText('POLYGON((62.144619879597 10.486242310988,54.622536815923 2.3124141859883,55.403637023919 23.977453248488,62.144619879597 10.486242310988))', 4326)
) AS is_point_within_polygon;

=> returns 0 => 返回0

But the point is obviously within the polygon:但该点显然在多边形内:

在此处输入图像描述

I double-checked that using Python:我使用 Python 仔细检查了:

import numpy as np
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon

if __name__ == '__main__':
    v0 = [62.144619879597, 10.486242310988]
    v1 = [54.622536815923, 2.3124141859883]
    v2 = [55.403637023919, 23.977453248488]
    lats_vect = np.array([v0[0], v1[0], v2[0]])
    lons_vect = np.array([v0[1], v1[1], v2[1]])

    lats_vect = np.append(lats_vect, lats_vect[0])
    lons_vect = np.append(lons_vect, lons_vect[0])

    lons_lats_vect = np.column_stack((lons_vect, lats_vect))
    polygon = Polygon(lons_lats_vect)
    point = Point(8.34047, 54.9132)
    print(point.within(polygon))

=> prints True => 打印True

What's wrong with the MySQL query? MySQL 查询有什么问题?

I think there are two issues here:我认为这里有两个问题:

  1. First with the query.首先是查询。 You list polygon in lat-lon order, but the point seems to be in lon-lat order.您按纬度-经度顺序列出多边形,但该点似乎是经度-纬度顺序。 You probably want你可能想要
SELECT ST_Within(
    ST_GeomFromText('POINT(54.91320 8.34047)', 4326),  -- NOTE CHANGE HERE
    ST_GeomFromText('POLYGON((62.144619879597 10.486242310988,54.622536815923 2.3124141859883,55.403637023919 23.977453248488,62.144619879597 10.486242310988))', 4326)
) AS is_point_within_polygon;
  1. Even this query returns FALSE, and this is expected in MySQL.即使这个查询返回 FALSE,这在 MySQL 中也是预期的。 4326 is Geodesic coordinate system, meaning it operates on the spherical Earth, not on this flat map. 4326 是测地线坐标系,这意味着它在球形地球上运行,而不是在这个平面 map 上运行。 With geodesic CRS, edges follow the geodesic shortest lines on Earth, not straight lines on flat map, and for really long lines like here and points close to the edge it matter:对于测地线 CRS,边缘遵循地球上最短的测地线,而不是平坦 map 上的直线,对于像这里这样很长的线和靠近边缘的点很重要:

在此处输入图像描述

Points slightly further North would be within the polygon, eg check out稍微向北的点将在多边形内,例如检查

SELECT ST_Within(
    ST_GeomFromText('POINT(56 8.34047)', 4326),
    ST_GeomFromText('POLYGON((62.144619879597 10.486242310988,54.622536815923 2.3124141859883,55.403637023919 23.977453248488,62.144619879597 10.486242310988))', 4326)
) AS is_point_within_polygon

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

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