mysql in边界_MySQL:空间查询以查找纬度/经度点是否位于给定边界内

bd96500e110b49cbb3cd949968f18be7.png

I'm working on google map search functionality. The vision of this is to find whether a (geolocation) point is located within a polygon or not as shown in the illustration below?

vCues.jpg

I'm using mysql 5.6.20 with Spatial extension, I know it has useful geometry functions built-in, so I will be allowed to query geocoded locations from a database directly.

My aim was to familiarize myself with geospatial functions, so I wrote up an experimental sql.

given a point geolocation: POINT(100.52438735961914 13.748889613522605)

and a polygon or boundary to be tested with was:

POLYGON(100.49503326416016 13.766897133254545,100.55940628051758 13.746555203977,100.56266784667969 13.72170897580617,100.48885345458984 13.739051587150175)

here is my sql example:

SELECT ST_Within(

ST_GEOMFROMTEXT('POINT(100.52438735961914 13.748889613522605)'),

ST_GEOMFROMTEXT('POLYGON(100.49503326416016 13.766897133254545,

100.55940628051758 13.746555203977,100.56266784667969 13.72170897580617,

100.48885345458984 13.739051587150175)'))

As geoFenceStatus

but after issuing the command, what I got in return seemed to be as follows:

geoFenceStatus

===============

null

I'm so unsure why it returned me 'null' value. since it was indicated in function documentation that this should return '1' in case a point resides within a given polygon

any advice would be appreciated, how to get my sql right.

解决方案

The error message isn't very clear but your issue is that you have an invalid Polygon. There are two problems with it:

You have to repeat the first point at the end -- this is to differentiate it from a LINESTRING, essentially, ie, it is closed.

POLYGONS start with and end with double parenthesis, ie, POLYGON((x1 y1, x2 y2.......xn yn, x1 y1)). This is to allow for inner rings to be delineated with single parenthesis sets inside the polygon.

See the wikipedia WKT article for more information.

You should find that if you write you query as:

SELECT ST_Within(ST_GEOMFROMTEXT('POINT(100.52438735961914 13.748889613522605)'),

ST_GEOMFROMTEXT('POLYGON((100.49503326416016 13.766897133254545, 100.55940628051758 13.746555203977,100.56266784667969 13.72170897580617, 100.48885345458984 13.739051587150175,

100.49503326416016 13.766897133254545))'))

As geoFenceStatus

then is should work.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值