Use SQLite Virtual Tables to Connect to Shapefiles
Your data is in a shapefile and you want to run some spatial queries but don’t want to load your data into PostGIS or some other spatial database, what do you do? You can use SQLite’s virtual tables to create a quick link to your data, and here’s how.
- First, from the directory your shapefile is in start sqlite3.
- Next, use SELECT load_extension(‘libspatialite.so’); to enable the spatialite extension.
- Load your shapefile as a virtual table with:
CREATE VIRTUAL TABLE <table_name> USING VirtualShape(<shapefile_name>, 'UTF-8', );
Now you can run any read-only spatial query you want against your new table. Note, the ‘UTF-8’ value will need to be changed if your shapefile isn’t using Unicode encoding.