load multiple shapefiles into one table

237 views
Skip to first unread message

John Cole

unread,
May 19, 2013, 9:46:07 PM5/19/13
to spatiali...@googlegroups.com
I have several shapefiles with identical schema and I'd like to load them into one table (they are the same data for different counties).

Is there a way to do this in spatialite?  the spatialite_tool and .loadshp both throw an error on the second shapefile saying the table already exists.

Thanks,

John

Alex Mandel

unread,
May 20, 2013, 12:23:35 AM5/20/13
to spatiali...@googlegroups.com, John Cole
Try ogr2ogr with -append

Enjoy,
Alex

a.fu...@lqt.it

unread,
May 20, 2013, 3:29:10 AM5/20/13
to spatiali...@googlegroups.com
Hi John,

both spatialite_tool and .loadshp (and the same is for spatialite_gui)
absolutely require to create a new table for each SHP being imported.

this can be easily explained: checking if a table of the given name
already exist isn't obviously enough; in this case we'll have to check
if all column definitions correctly match, if the geometry column
strictly
have identical type, dimensions and SRID and so on.
in other worlds: supporting the capability to append further features
into an already existing table will add a lot of complexity.

Anyway you can easily use Spatialite in the most painless way so
to import many different Shapefiles sharing the same schema into
a single DB table:

a) manually create the DB target table using CREATE TABLE and
AddGeometryColumn
b) connect a single Shapefile at each time as a VirtualShape
c) then copy all features from the Shapefile into the DB Table;
you simply have to use an SQL statement like this one:

INSERT INTO dbtable (col1, col2, col3, ...., geometry)
SELECT col1, col2, col3, ...., geometry
FROM virtual_shp;

d) drop the VirtualShape
e) repeat steps b), c) and d) until you've imported any SHP;
at the end of the process you'll have a single DB table
corresponding to all re-aggregated Shapefiles.

hint: if you actually have lots of SHP to be imported writing
a simple SQL script will probably help

bye Sandro

--
Il messaggio e' stato analizzato alla ricerca di virus o
contenuti pericolosi da MailScanner, ed e'
risultato non infetto.

Reply all
Reply to author
Forward
0 new messages