wiki:SpatiaLite

Version 12 (modified by aperi2007, 13 years ago) ( diff )

SpatiaLite

SpatiaLite is a relatively new GIS file format based on SQLite, which uses a single file to store multiple layers and data tables. It is an alternative format to ESRI Shapefiles, which have several legacy issues (such as storage types and lengths of column names, etc.) Furthermore it is simple to distribute and run, as there is only one file.

Exporting from PostGIS to SpatiaLite

Use ogr2ogr built with SQLite options.

To create the SpatiaLite file and export the first layer:

$ ogr2ogr -f SQLite -dsco SPATIALITE=yes myfile.sqlite PG:"dbname=mydb user=myuser password=mypass" myschema.mytable

And for additional layer/tables:

$ ogr2ogr -f SQLite -update myfile.sqlite b PG:"dbname=mydb user=myuser password=mypass" myschema.my2ndtable

To load all the tables of a schema from Postgres to Spatialite (with or without geometry) and without the views:

$ ogr2ogr --config PG_LIST_ALL_TABLES YES --config PG_SKIP_VIEWS YES -f "SQLite" mydb.sqlite -progress PG:"dbname='mydb' \
  active_schema=myschema schemas=myschema host='localhost' port='5432' user='myuser' password='mypass' " -lco LAUNDER=yes \
  -dsco SPATIALITE=yes -lco SPATIAL_INDEX=yes -gt 65536

Some advertising: To avoid the lost of the third dimension exporting geometries you need use almost gdal 1.9.0 and spatialite library 3.0.0.
Also the exportation using the last sintax described don't export any field of type integer that is also a primary key. Instead it don't lost the field of type TEXT that are also primary keys. To avoid this lost you should use the "single table" call.


To load a table (mytable) (with a geometry columns named "geometry") from a Sqlite/Spatialite (mysqlite.sqlite) into a schema (myschema) of a Postgres/Postgis, using gdal 1.9.0 and spatialite 3.0.0:

ogr2ogr --config SQLITE_LIST_ALL_TABLES YES -f "PostgreSQL" PG:"dbname='mydb' active_schema=myschema schemas=myschema host='localhost' port='5432' user='myuser' password='mypass' " -overwrite -lco SCHEMA=myschema -lco GEOMETRY_NAME=geometry -lco SPATIAL_INDEX=YES mysqlite.sqlite  mytable
Note: See TracWiki for help on using the wiki.