I'm having trouble with one SQL query I'm using on a shapefile in my VRT layer. My goal is to import several shapefiles to a PDF using OGR, GDAL and Python.

My shapefile has follwing structure: shapefile1.shp: {FID: integer, Type: string, Name: string, Show: integer ...}. Nothing out of the usual, no spaces, no underscores, no points.

If I call ogrinfo on a OSGeo Shell, the shapefile behaves nominally and all attributes are shown, i.e:

> ogrinfo -sql "SELECT Type AS foo FROM shapefile1" shapefile1.shp

 Layer name: shapefile1
 Geometry: Point
 Feature Count: 19
 foo: String (150.0)
 foo (String) = correctString
 POINT (coordinates)

But if I use the same SQL query on my VRT file, I get following error:

ERROR 1: Unrecognized field name Type.
ERROR 1: SQL statement failed, or returned no layer result:
SELECT Type AS foo FROM shapefile1
ERROR 1: Invalid geometry field index : 0

This is my VRT file:

    <OGRVRTLayer name="points">
        <SrcDataSource relativeToVRT="1">shapefile1.shp</SrcDataSource>
        <SrcSQL>SELECT Type AS foo FROM shapefile1</SrcSQL>

First I thought that OGR doens't like attribute names as "FID", "Type", "Name" and so on. But even if i change the SQL query to SELECT Show AS foo FROM shapefile1 the error keeps appearing. Even on an attribute called Azimuth, which should definetly not be a problem.

Any ideas on what I might be doing wrong?

  • Have you tried enclosing the field name in quotes? Commented Apr 17, 2018 at 13:34
  • I have, but SELECT "Type" AS foo FROM shapefile1 throws the same error. I even tried with up to 4 quote marks on each side.
    – Iridium
    Commented Apr 17, 2018 at 13:37
  • Related? Merging layers via Python OGR ExecuteSQL by user30184
    – Kazuhito
    Commented Apr 17, 2018 at 13:42

2 Answers 2


Works for me with your .vrt file and a shapefile that I digitized for the test.

ogrinfo vrt.vrt -al
INFO: Open of `vrt.vrt'
      using driver `OGR_VRT' successful.

Layer name: points
Geometry: Point
Feature Count: 2
Extent: (705.000000, 467.000000) - (837.000000, 491.000000)
Layer SRS WKT:
        SPHEROID["WGS 84",6378137,298.257223563,
foo: String (1.0)
  foo (String) = a
  POINT (837 491)

  foo (String) = b
  POINT (705 467)

My GDAL version is 2.3.0-dev.

  • Thanks for your time and test. I just digitized a similar shapefile and it works too on GDAL 2.2.4. I´m starting to believe that there is something wrong with the shapefile, but I don't quite understand why ogrinfo gets it correct and the SQL query throws an error...
    – Iridium
    Commented Apr 17, 2018 at 14:09

Thank you all for your time, answers and comments. I finally found out what was missing.

The SQL query and everything was fine, the shapefile was just missing its .dbf part. Turns out that OGR is capable to read the attributes with the .shp and .shx parts, but for GDAL the .dbf part seems to be mandatory.

  • Mysterious. Only place where the attributes os shapefile exist is .dbf file. OGR is part of GDAL so that makes no difference. If you see the attributes the .dbf file must be found somewhere.
    – user30184
    Commented Apr 17, 2018 at 14:22

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.