2

I'm performing a mySQL query to check if a point is within a multipolygon field. I've got a database field storing a multipolygon and I want to check if a point is within any of those polygons. However ST_Within seems to only be checking the first polygon. Here is an example of my query:

select name, ST_Within(ST_PointFromText('POINT(-34.9216954 138.6308949)'), service_polygons) as within, ST_AsText(service_polygons)

And service polygons contains a column with the value:

MULTIPOLYGON(((58.077876267875 108.80859375,55.578344672182 126.38671875,50.289339253292 136.58203125,32.546813173515 147.83203125,34.307143856288 107.75390625,58.077876267875 108.80859375),(-14.944784875088 138.69140625,-36.315125147481 126.73828125,-55.776573018668 155.91796875,-45.089035564831 167.16796875,-18.979025953255 151.34765625,-13.239945499286 139.39453125,-0.35156029399227 142.91015625,-14.944784875088 138.69140625)))

If I reverse the order of the polygons it returns true, for example if service polygons contains

MULTIPOLYGON(((42.811521745098 156.26953125,-5.9657536710655 162.24609375,-28.613459424004 167.51953125,-62.431074232921 164.70703125,-47.989921667414 115.48828125,42.811521745098 156.26953125),(58.077876267875 108.80859375,34.307143856288 107.75390625,32.546813173515 147.83203125,50.289339253292 136.58203125,55.578344672182 126.38671875,58.077876267875 108.80859375)))

Then true is returned.

Am I missing something when dealing with multipolygons?

3
  • What GIS software are you using?
    – PolyGeo
    Commented Feb 2, 2017 at 0:58
  • This is being done through a mySQL database query, hopefully this is still the right place to ask this question. Commented Feb 2, 2017 at 1:18
  • Please edit the question in response to requests for clarification. As the Tour puts it, "Use edits to fix mistakes, improve formatting, or clarify the meaning of a post. Use comments to ask for more information or clarify a question or answer."
    – Vince
    Commented Feb 2, 2017 at 1:36

1 Answer 1

0

Your multipolygons are not valid, in both cases "Ring 1 of Polygon 0 not in exterior ring", i.e the second part of your "multipolygon" is a hole and it falls outside the polygon.

You need to make your WTK look like:

MultiPolygon (((36.92098729881999475 208.80087890625003411, ...)),
((48.54364354882000043 183.15087890625002842, ...)))

Note how each part has two brackets to allow you to add a hole to it:

MultiPolygon (((36.92098729881999475 208.80087890625003411, ..),(Hole)),
((48.54364354882000043 183.15087890625002842, ...)))
1
  • Ah, I did wonder what the bracketing on the multipolygon definition was about, but couldn't find any clear documentation on it (certainly not anything that explained what "holes" were). So essentially my couple of hours frustration was a missing bracket! The bane of programmers everywhere! Commented Feb 2, 2017 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.