I've noticed more and more people talking about SQLite and in particular Spatialite. This is typically surrounded by talk about the hopeful end of shapefiles as a defacto exchange format. I suppose the success of shapefiles has mostly been because of the openess, so every proprietary GIS software was able to support it. Now pretty much everything spatial is available in shapefiles. In typical government fashion the U.S. Census Bureau is just now releasing their data as shapefiles instead of Tigerline files...that's why it seems appropriate to move to another format now. Is there any greater sign of the death of technology when a government takes it on board? When I worked on the FEMA map modernization one of my greatest frustrations was that they required ESRI coverages to be submitted with the final product. Not only is ESRI stopping support for coverages, it requires an ArcInfo license...and even then you have manually add the coverages toolbox. Of course, if you want to try and edit a coverage you have to go back into ArcInfo workstation. Anyhoo, that's not relevant to this entry. I decided to take a look at Spatialite and see what it was about. First lesson learned - it's spelled with one L.
So what is the beauty of this format? It doesn't install ANYTHING. Even the gui interface can just be run from the exe file. This makes it really really easy to get started. So I created a simple spatialite db and imported some Open Street Map shapefiles which contained thousands of polygons and polylines to check out the stability. I thought I'd start with some simple SQL commands and started with AREA for the polygon geometry. Well this crashed the gui without any explanation. So then I moved to IsValid (this uses the GEOS API) to see what was wrong with the polygon geometry, and this crashed the gui again. I switched at this point to the commandline thinking the gui just couldn't handle the amount of data I was using. At least when the commandline came up with an error it told me what the error was and where it was stopping. The first thing I discovered was I had a dozen or so polygons that were not closed (start and end point did not match). I extracted these and moved them to another table using this select statement:
CREATE TABLE buildings_err
AS SELECT * FROM buildings WHERE IsClosed(ExteriorRing(Geometry)) = 0
DELETE FROM buildings WHERE IsClosed(ExteriorRing(Geometry)) = 0
After this it still would crash if I ran IsValid. So I tried this Select statment to extract all the polygons that had less than or equal to three points creating it:
INSERT INTO buildings_err SELECT * from buildings where NumPoints(ExteriorRing(Geometry)) <= 3;
DELETE from buildings where NumPoints(ExteriorRing(Geometry)) <= 3;
Finally I then removed all the invalid polygons:
INSERT INTO buildings_err SELECT * from buildings where IsValid(Geometry) <= 0;
DELETE from buildings where IsValid(Geometry) <= 0;
And then ran an itnersect with another polygon table (cleaned that one up as well):
SELECT *, intersection(buildings.Geometry, natural.Geometry) FROM [buildings], [natural]
This hung up on me too. I'm not sure where all the polygon errors were coming from, either the shapefile, or the import has issues. The GEOS api seemed pretty sensitive to these errors as well. So I gave up on trying after it crashed and moved on. One thing I discovered is the geometry column, and thus the table, can only store one type of Geometry. This extends to MULTI as well. So you have to separate your POLYGONs from your MULTIPOLYGONS or I guess explode the multipolygons to polygons. I think that might get confusing...Note the David in this google group is not me.
Finally, after all this I thought to myself - "Hey wait a second, AIR uses a SQLite database!" But the SQLite functionality is all built-in so you can't swap dlls, and actionscript doesn't reference external libraries unfortunately. So I tried the handy built-in SQLite function load_extension, but this too is not apart of the AIR SQLite functionality!! KAAAAAAAHHHHHNNNNNNN!!!!! Sorry for the Star Trek reference. So no go to use a Spatialite database with FLEX/AIR. And it is sort of moot anyway because I don't know any commercial GIS that fully supports Spatialite. I think FME does...ESRI certainly doesn't. So thus was my foray into Spatialite. Still think it is pretty cool, but I'm a visual person and need to see the data not just type in a few sql statements.