I have some different kinds of geometry (Polygons and line strings) stored in my mysql database. On different rows.

I know I can retrieve the envelope of one row with: SELECT Envelope(zoneShape) FROM geometryTable WHERE refid=4

If there are three different shapes associated with reference id 4, i'm getting three results, obviously.

How can I (preferably with one sql query) get the envelope/MBR of all the geometry with e.g. refid=4?


I'm pretty sure that MySQL spatial does not yet support the (ST_ )Collect aggregate function which could be used in conjunction with the Envelope function by performing a GROUP BY on your refid to yield the envelope of the combined geometries.


Perhaps you could loop over each refid and use the Union function to do what you need.

If not, you can achieve the desired result using QGIS to dissolve the geometries based on your refid and then calculate the envelope(s) on the dissolved geometr(y)(ies)

  • Damn. This is the first time MySQL is not sufficient. – stUrb May 14 '12 at 18:05
  • MySQL is far from being a good choice for GIS. It is very slow and rather limited, probably because it is owned by the makers of Oracle Spatial, who would like to sell their flagship product and not the free DBMS. PostGIS is much better but if you want something lightweight, go with Spatialite. Both PostGIS and Spatialite outperform MySQL in GIS benchtests. If you are serious about GIS, ditch MySQL. – MappaGnosis May 15 '12 at 11:30
  • It's not my first choice to use MySQL for this project. But I do not own the server so I can't install Postgis or spatialite. – stUrb May 19 '12 at 23:13

I Know you've already accepted a answer but for people still looking for this, here's one query i wrote

FROM SUBSTR( ASTEXT( object.polygon ) , 9 ) ) ,  ')' ) ,  '))' ) ) ) AS POLYGON
FROM object

This give's you the Minimal bounding rectangle for a row containing polygon's. If you want this to work with different geometry's in one table. Use GEOMETRYCOLLECTION instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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