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?

  • Have you considered to add PostGIS (and learn SQL ) to your toolbox ? Commented Sep 25, 2013 at 11:49
  • Yes but let's say... next year ! I need first to get use to classe, objet, database.... and then I guess I will be ready for PostGIS. In the meanwhile, easy sql statement like the one used in ogr2ogr are ok to me
    – Renaud
    Commented Sep 25, 2013 at 13:32
  • @MappaGnosis,I had a look on ogr2ogr documentation, putting a shape file to PosGIS seems quite easy. 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
    – Renaud
    Commented Oct 4, 2013 at 15:04
  • You can do it that way, or PostGIS comes with a plugin (PostGIS Shapefile and DBF loader) which can make life even easier. Just watch the encoding - I usually need to explicitly set it to 'LATIN1'. Commented Oct 4, 2013 at 16:08
  • And then, how can I perform the joint by location ?
    – Renaud
    Commented Oct 4, 2013 at 16:19

1 Answer 1


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.