OSGeo / gdal

GDAL is an open source MIT licensed translator library for raster and vector geospatial data formats.
https://gdal.org
Other
4.79k stars 2.51k forks source link

SrcSQL unable to select OGRVRTLayer based on name #10511

Closed frafra closed 1 month ago

frafra commented 1 month ago

What is the bug?

I have a VRT file containing a SQL query: <SrcSQL>select * from event join occurrence on occurrence.id=event.id join measurementorfact on measurementorfact.id=event.id</SrcSQL>. measurementorfact refers to a OGRVRTLayer`` with the same name:`. GDAL is unable to open the resource, as the layer is not found:

ERROR 1: Failed to find layer 'measurementorfact' on datasource 'CSV:/vsizip/{/vsicurl/https://ipt.nina.no/archive.do?r=calcareous_grassland_monitoring}/measurementorfacts.txt'.

The file is named measurementorfacts.txt while the layer is named measurementorfact, without the additional s letter and the file extension.

Renaming the layer to measurementorfacts (matching the filename without the extension, by adding the additional s) fixes the issue, but then what is the meaning of having a mandatory name attribute for OGRVRTLayer?

Even stranger, is that changing the name attribute to a random string, produces a different error:

ERROR 1: SELECT from table measurementorfact failed, no such table/featureclass.

Steps to reproduce the issue

Here is a smaller code example to reproduce the issue:

test.vrt:

<OGRVRTDataSource>
    <OGRVRTLayer name="data">
      <SrcDataSource><![CDATA[
        <OGRVRTDataSource>
          <OGRVRTLayer name="measurementorfact">
            <SrcDataSource>CSV:measurementorfacts.csv</SrcDataSource>
            <LayerSRS>WGS84</LayerSRS>
          </OGRVRTLayer>
        </OGRVRTDataSource>]]>
      </SrcDataSource>
      <SrcSQL>select * from measurementorfact</SrcSQL>
      <LayerSRS>WGS84</LayerSRS>
    </OGRVRTLayer>
  </OGRVRTDataSource>

measurementorfacts.csv:

x y
1 2
3 4

Versions and provenance

GDAL 3.8.4, released 2024/02/08 from Nix repositories. GDAL 3.8.3, released 2024/01/04 on openSUSE Leap 15.6.

Additional context

No response

rouault commented 1 month ago

In the VRT you've provided what is missing is a SrcLayer element in the most nested VRT:

<OGRVRTDataSource>
    <OGRVRTLayer name="data">
      <SrcDataSource><![CDATA[
        <OGRVRTDataSource>
          <OGRVRTLayer name="foo">
            <SrcDataSource>CSV:measurementorfacts.csv</SrcDataSource>
            <SrcLayer>measurementorfacts</SrcLayer> <!-- this is missing -->
            <LayerSRS>WGS84</LayerSRS>
          </OGRVRTLayer>
        </OGRVRTDataSource>]]>
      </SrcDataSource>
      <SrcSQL>select * from foo</SrcSQL>
      <LayerSRS>WGS84</LayerSRS>
    </OGRVRTLayer>
  </OGRVRTDataSource>
frafra commented 1 month ago

That works, thank you. I have not found that in the documentation.