I would like to join the attributes of two shapefiles by location but with a command line (Linux). This feature is easy in QGIS but it is VERY slow.
Is there a way of doing this with ogr or PyQGIS?
These kinds of queries can run quicker with a spatial index. If the file is from Esri, it may have .sbn
and .sbx
files, which OGR 1.10 can use. However, if you've created/modified the shapefile with QGIS, you may need to create/update a .qix
spatial index. You can find some buttons in QGIS' Layer Properties, under General > Coordinate reference system, look for Create spatial index and Update extents. Or from a shell, similar:
ogrinfo -sql "CREATE SPATIAL INDEX ON \"your-geoms\"" your-geoms.shp
Either method will create/update a .qix
, which is a quadtree spatial index file.
Once you have a good spatial index, QGIS and other OGR-related things should have better spatial join performance. And for a spatial join from a command line, see this example.
ogr2ogr -f “PostgreSQL” PG:”host=myhost user=myloginname dbname=mydbname password=mypassword” myshapefile.shp
. Do you have a solution for me in that case ? Thank you