MySQL Spatial support for OGR
Sunday, February 12th, 2006OGR had support for reading MySQL tables in the past, but it didn’t support native geometry types, and MySQL doesn’t really have a formal mechanism for supporting the OGC Simple Features for SQL specification. Under the direction of Frank Warmerdam, I have been bringing the OGR driver up to par with the rest of the vector data drivers in OGR. Here’s a list of the features that I added:
- Read geometry data types. All of the OGC data types that MySQL supports are handled.
- Read SRS information from the spatial_ref_sys table and layer information from the geometry_columns table. The OGC SF for SQL spec states that there should be a geometry_columns and spatial_ref_sys table that stores auxiliary information about the layers in the database. OGR now supports these tables.
- Better reading of data types. The MySQL driver now handles field precisions and widths much better, and it can auto-detect a surrogate feature ID and the geometry column for layers that are created from a SQL statement.
- Support for spatial filters. The driver will use the spatial index to query against geometries that are specified as filters.
- Write support. The MySQL OGR driver now has the ability to create features in the database, much like the PostGIS and Oracle Spatial drivers. This support includes options for preserving precision information and laundering field names.
Impressions of MySQL Spatial
MySQL is known for being fast, widely deployed, and frequently used. Unlike PostGIS, however, it does not support spatial predicates and operators (for testing whether one geometry exists in another, for example), its spatial indexing is only supported in MyISAM database backend, which is not transactional, and it doesn’t provide anything for the OGC Simple Features for SQL specification other than to provide the geometry types themselves.
I’ve found it interesting that people clammor so much for MySQL support. If you already have the development momentum moving that way, I can understand why it is desired, but if you are starting from scratch, start with PostgreSQL/PostGIS. Here’s some things that you’d be missing if you stuck with MySQL:
- No ability to query for an extent of a selection set and have that result come from the spatial index, ie “SELECT EXTENT(SHAPE_COLUMN) FROM TABLE WHERE SOME FILTER”. This functionality is critically important in my mind to the operation and utility of a spatial database.
- No spatial predicates. All the database can really do for you is store the geometry and give you the ability to quickly select things based on an extent. It can’t help you answer questions like “does this feature intersect this other feature” or “is this point, when buffered by a given distance, within the distance of this other set of features.”
Another thing I found really frustrating is the MySQL C API. How anyone gets anything done with that is astonishing. I found myself wishing I had Oracle’s Pro*C a few times, which is saying a lot. It isn’t self describing, you need to know a lot about the life cycle of a query, and you end up inferring the results of you queries in a very backwards way. I’m so happy that database abstractions like the Python DB API exist to squash this stuff out.
Give it a try
The OGR MySQL support will be available in the upcoming GDAL 1.3.2 release, but you can try it out via CVS if you are daring. I’m still working on a few bugs and issues, but most of the basics are there. If you are a MySQL shop looking to use it with MapServer, this is what you’ll want to use, rather than the unsupported and unmaintained native MyGIS driver.
Overall it was a worthwhile experience for me to develop this. I learned quite a bit about the internals of OGR and MySQL, gained some C++ development experience, and got a wonderful opportunity to work under a great mentor.