4

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?

mgri
  • 16,159
  • 6
  • 47
  • 80
Renaud
  • 871
  • 3
  • 9
  • 20
  • Have you considered to add PostGIS (and learn SQL ) to your toolbox ? – simpleuser001 Sep 25 '13 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 Sep 25 '13 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 Oct 04 '13 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'. – MappaGnosis Oct 04 '13 at 16:08
  • And then, how can I perform the joint by location ? – Renaud Oct 04 '13 at 16:19

1 Answers1

3

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.

Mike T
  • 42,095
  • 10
  • 126
  • 187