QGIS – Add Vector Layer Feature from REST API URL (HTTPS)

apigeojsonhttpqgisrest

I'm simply trying to import this feature directly into QGIS 3.16.3-Hannover as a new vector layer:

https://api3.geo.admin.ch/rest/services/api/MapServer/find?layer=ch.swisstopo.swissboundaries3d-land-flaeche.fill&searchText=ch&searchField=id&returnGeometry=true&geometryFormat=geojson

When loading it into a web browser, it shows the GeoJSON object as follows:

geojson object

Well, it's not directly a GeoJSON as it's "encapsulated" as the first (and only) element of the JSON array called results.
Which is probably the source of the following problem…

Because when trying to load it in QGIS*, I first get this message:

Download with "Protocol" source type has failed, do you want to try the "File" source type?

and if I click "No", it then shows this error:

Invalid Data Source: /vsicurl/https://api3.geo.admin.ch/rest/services/api/MapServer/find?layer=ch.swisstopo.swissboundaries3d-land-flaeche.fill&searchText=ch&searchField=id&returnGeometry=true&geometryFormat=geojson is not a valid or recognized data source.

How could I tell QGIS to first unpack the first element of the results array so that it can (hopefully) read the GeoJSON feature?

Changing the protocol type from HTTP/HTTPS/FTP to GeoJSON does help, but then the feature is not properly loaded on the table of content (it has no geometry):

no geometry feature loaded

*I'm trying to load it through the Data Source Manager > Vector > Source Type: "Protocol: HTTP(S), cloud, etc…" and by simply pasting the URL into, well, the URI field.

This is the API doc: https://api3.geo.admin.ch/services/sdiservices.html#find

Best Answer

It may be convenient to use a VRT file if you often use the same remote layer in different QGIS projects instead of copying/pasting it in each of them.

Put in file myvrt_demo.vrt the following content and drag and drop the vrt file within QGIS

<OGRVRTDataSource>
    <OGRVRTLayer name="swissboundaries3d_land_flaeche_fill">
        <SrcDataSource>https://api3.geo.admin.ch/rest/services/api/MapServer/ch.swisstopo.swissboundaries3d-land-flaeche.fill/CH?geometryFormat=geojson&sr=4326</SrcDataSource>
        <SrcLayer>feature</SrcLayer>
        <LayerSRS>EPSG:4326</LayerSRS>
    </OGRVRTLayer>
</OGRVRTDataSource>
Related Question