15

If we want to get information about a vector layer (shapefile for example) we could use ogrinfo:

ogrinfo -so myshapefile.shp mylayer

This returns a lot of pertinent information about the shapefile. Is it possible to return whether or not a spatial index exists? If so, can we access info about the spatial index?

Note: I realize it is trivial to open the folder containing the shapefile and looking for a .sbn or .sbx and so the question is specific to ogrinfo.

1
  • I have the same question, with the emphasis on GeoPackage. Commented Feb 20, 2019 at 20:16

3 Answers 3

8
+50

With GDAL 2.2.0, one of the changes to the GPKG driver is:

  • add HasSpatialIndex(tblname,geomcolname) SQL function

For example:

ogrinfo -sql "SELECT HasSpatialIndex('some_layer', 'geom')" some-file.gpkg

Shows:

INFO: Open of `some-file.gpkg'
      using driver `GPKG' successful.

Layer name: SELECT
Geometry: Unknown (any)
Feature Count: 1
Layer SRS WKT:
(unknown)
HasSpatialIndex: Integer (0.0)
OGRFeature(SELECT):0
  HasSpatialIndex (Integer) = 1

Or better, select from gpkg_geometry_columns to show info for all geometry columns in the file:

ogrinfo -sql "SELECT table_name, column_name, HasSpatialIndex(table_name, column_name) FROM gpkg_geometry_columns" some-file.gpkg
5

As an answer to the comment by @Richard Law, the name of the virtual rtree table that contains the spatial index in GeoPackage is always named according to template rtree_[table_name]_[geometry_column_name]

Therefore you can make a SQL query that checks the existence of the rtree table.

For example you can check if table "table1" has a spatial index with ogrinfo

ogrinfo -sql "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE tbl_name like 'rtree_table1_%') as has_spatial_index" mygeopackage.gpkg


INFO: Open of `mygeopackage.gpkg'
      using driver `GPKG' successful.

Layer name: SELECT
Geometry: None
Feature Count: 1
Layer SRS WKT:
(unknown)
has_spatial_index: Integer (0.0)
OGRFeature(SELECT):0
  has_spatial_index (Integer) = 1

In this case the spatial indes does exist. Otherwise the would have been "0".

In GeoPackage a table can have only one geometry field and therefore it is enough to make a check by just "rtree" and name of the table without knowing the name of the geometry field as rtree_table1_%.

2
  • Good answer. I'm just disappointed that there is no abstraction for this across different vector formats. Commented Feb 21, 2019 at 20:33
  • By looking at the list of supported OGR formats gdal.org/ogr_formats.html quite few formats support spatial index at all. Most database formats support spatial indexes transparently and it is rather hard to find for example from Oracle if table has a spatial index or not.I guess that such abstaction will not land into GDAL soon.
    – user30184
    Commented Feb 21, 2019 at 20:52
1

Ogr does not use the ESRI spatial Index files .sbn, sbx. It creates a .qix file which may contain a spatial index and/or an attribute index. You can build the .qix via ogrinfo.
Ogr shapefile driver

I also found a GDAL ticket mentioning .sbn file is now readable. Not sure what this means.
ticket #4719

4
  • 2
    "Starting with OGR 1.10, [the ESRI Shapefile driver] can also use the ESRI spatial index files (.sbn / .sbx)" gdal.org/ogr/drv_shapefile.html
    – Mike T
    Commented Mar 6, 2013 at 22:32
  • @MikeToews Looks like I had better upgrade. Thanks!
    – Jay Laura
    Commented Mar 7, 2013 at 3:06
  • @JayLaura 1.10 is not released yet, but I'm certain the developers would love to have it tested
    – Mike T
    Commented Mar 7, 2013 at 3:31
  • 1
    GDAL/OGR 1.10.0 came out last week: trac.osgeo.org/gdal/wiki/Release/1.10.0-News
    – user10353
    Commented May 6, 2013 at 21:28

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.