Updated SQLite (markdown)

springmeyer 2013-03-14 13:44:23 -07:00
parent 198ffaa2bc
commit 54e953f35e

@ -1,85 +1,17 @@
<!-- Name: SQLite -->
<!-- Version: 11 -->
<!-- Last-Modified: 2010/11/13 10:07:41 -->
<!-- Author: kunitoki -->
Mapnik's PluginArchitecture supports the use of different input formats.
One such plugin supports the sqlite ([SQLite](http://en.wikipedia.org/wiki/SQLite)) / spatialite ([Spatialite](http://www.gaia-gis.it/spatialite)) extension to the popular SQLite database.
One such plugin supports the sqlite ([SQLite](http://en.wikipedia.org/wiki/SQLite)). The plugin works with two types of geometry storage methods - raw OGC WKB and ([Spatialite](http://www.gaia-gis.it/spatialite)) geometries.
# Installation
You can create a pure SQLite + WKB geometry based sqlite db using ogr like:
Make sure that running _python scons/scons.py DEBUG=y_ shows the following line
ogr2ogr -f SQLite test.sqlite some.shp
Checking for C library sqlite3... yes
You can create a spatialite enabled db also using ogr:
To check if the sqlite plugin built and was installed correctly, try the usual Python _from mapnik import *_ on a DEBUG=y build, and look for the following debug line
ogr2ogr -f SQLite test.sqlite some.shp -dsco SPATIALITE=YES
registered datasource : sqlite
# Creating an example database
## Getting the tools
First of all we need a bit of external tools for loading and preparing our first spatial database with sqlite.
Go to [Spatialite](http://www.gaia-gis.it/spatialite-2.3/) and download (depending on your arch) the "spatialite executable [statically linked, no deps]" and unpack them in a directory of choice. Also download and unpack [InitSpatialiteSql](http://www.gaia-gis.it/spatialite/init_spatialite-2.2.sql.zip) in the same directory.
If you have problems with your spatial indexes (mbr calculated wrong in tables idx_table_geometry), you will need to rebuild spatialite-2.3 yourself using a recent version of GEOS (>=3.0.3): refer to [Spatialite](http://www.gaia-gis.it/spatialite-2.3) compilation guide.
## Prepare the data
Now execute spatialite on a new empty database:
user@geo ~/spatialite/ $ spatialite spatial_test.sqlite
SpatiaLite version ..: 2.3 Supported Extensions:
- 'VirtualShape' [direct Shapefile access]
- 'VirtualText [direct CSV/TXT access]
- 'VirtualNetwork [Dijkstra shortest path]
- 'RTree' [Spatial Index - R*Tree]
- 'MbrCache' [Spatial Index - MBR cache]
- 'VirtualFDO' [FDO-OGR interoperability]
- 'SpatiaLite' [Spatial SQL - OGC]
PROJ.4 version ......: Rel. 4.6.1, 21 August 2008
GEOS version ........: 3.0.0-CAPI-1.4.1
SQLite version ......: 3.6.6.1
Enter ".help" for instructions
spatialite>
From the spatialite shell you must initialize the spatialite tables (_geom_cols_ref_sys_, _geometry_columns_, _spatial_ref_sys_):
spatialite> .read init_spatialite-2.2.sql ASCII
1
We import a shapefile directly inside our database (in this example using utf-8 encoding, 3004 as SRID(epsg) and 'geom' as the geometry column). This will take care of creating the table for us, executing *AddGeometryColumn* which will initialize the _geometry_columns_ table and add a BLOB column to our table for the geometry, then it will insert the rows into it:
spatialite> .loadshp bridges bridges UTF-8 3004 geom
========
Loading shapefile at 'bridges' into SQLite table 'bridges'
BEGIN;
CREATE TABLE bridges (
PK_UID INTEGER PRIMARY KEY AUTOINCREMENT,
ANNO_COSTR INTEGER,
RAG_GIUR INTEGER,
DECORAZ TEXT);
SELECT AddGeometryColumn('bridges', 'geom', 3004, 'MULTIPOLYGON', 2);
INSERT INTO ponte (
PK_UID,ANNO_COSTR,RAG_GIUR,DECORAZ,geom)
VALUES (1,0,1,'',GeomFromWkb(X'010600000001000000010300000001000000150000001A...',3004));
...
COMMIT;
Inserted 499 rows into 'bridges' from SHAPEFILE
========
At this point we can decide also to create the R*Tree index on this table, for speed spatial access to the table:
spatialite> select CreateSpatialIndex('bridges', 'geom');
1
You can now exit the spatialite shell:
spatialite> .quit
The main difference is that a spatialite enabled db will include a spatial index inside the database. A pure SQLite db will not contain a spatial index, but Mapnik will create one on the fly.
# Parameters
@ -129,4 +61,4 @@ A Sqlite datasource may be created as follows:
}
```
## Further References
## Further References