QGIS – Correct URL for Exporting Data from ArcGIS MapServer Layer to GeoJSON Using ogr2ogr

arcgis-rest-apigeojsonogr2ogrqgis

I found an ArcGIS REST MapServer showing data I care about:

https://kart.ra.no/arcgis/rest/services/Distribusjon/Kulturminner20180301/MapServer


QGIS

I can visualize it in QGIS: Layer -> Add Layer -> ArcGIS REST Server, then create a connection with the above URL. When I connect, I am presented with multiple options:

enter image description here

I choose Enkeltminner, as that's what I care about now. I can then export data to GeoJSON:

enter image description here


ogr2ogr

I would like to achieve this using ogr2ogr. Using this answer, I tried:

ogr2ogr -f GeoJSON test.json "https://kart.ra.no/arcgis/rest/services/Distribusjon/Kulturminner20180301/MapServer/" OGRGeoJSON 

Resulting in:

ERROR 4: Failed to read TopoJSON data
FAILURE:
Unable to open datasource `https://kart.ra.no/arcgis/rest/services/Distribusjon/Kulturminner20180301/MapServer/' with the following drivers.
  -> `FITS'
  -> `PCIDSK'
  -> `netCDF'
  -> `PDS4'
  -> `VICAR'
 ...

This website also provides an example of how to perform a similar task:

ogr2ogr -f GeoJSON CadastreAddress_Example1.json "https://services.slip.wa.gov.au/public/rest/services/SLIP_Public_Services/Property_and_Planning_WFS/MapServer/50/query?where=locality_name+%3D+%27PERTH%27&outfields=*&f=json" OGRGeoJSON -s_srs EPSG:3857 -t_srs EPSG:4326

Both the answer I followed, and this above example uses a query string after the core URL. I think if I did figure out the correct URL, I could export the Enkeltminner layer to GeoJSON.

I can click Enkeltminneikoner on the ArcGIS page I am using, to get to this URL (notice the /6 in the end):

https://kart.ra.no/arcgis/rest/services/Distribusjon/Kulturminner20180301/MapServer/6

Try again with this URL:

ogr2ogr -f GeoJSON test.json "https://kart.ra.no/arcgis/rest/services/Distribusjon/Kulturminner20180301/MapServer/6" OGRGeoJSON 

Output again:

ERROR 4: Failed to read TopoJSON data
FAILURE:
Unable to open datasource `https://kart.ra.no/arcgis/rest/services/Distribusjon/Kulturminner20180301/MapServer/6' with the following drivers.
  -> `FITS'
  -> `PCIDSK'
  -> `netCDF'
  -> `PDS4'
  -> `VICAR'
...

As suggested in the comments, I also tried:

ogr2ogr -f GeoJSON test.json "https://kart.ra.no/arcgis/rest/services/Distribusjon/Kulturminner20180301/MapServer/6/query?where1=1&outFields=*&f=json" OGRGeoJSON

This attempt seems like a closer attempt to the examples. Output however is:

ERROR 1: Invalid FeatureCollection object. Missing 'fields' member.
ERROR 1: Layer schema generation failed.
ERROR 4: Failed to read ESRIJSON data
FAILURE:
Unable to open datasource `https://kart.ra.no/arcgis/rest/services/Distribusjon/Kulturminner20180301/MapServer/6/query?where1=1&outFields=*&f=json' with the following drivers.
  -> `FITS'
  -> `PCIDSK'
  -> `netCDF'
  -> `PDS4'
  -> `VICAR'
  ...

It complains about fields member. I try replacing the outFields to fields then:

ogr2ogr -f GeoJSON test.json "https://kart.ra.no/arcgis/rest/services/Distribusjon/Kulturminner20180301/MapServer/6/query?where1=1&fields=*&f=json" OGRGeoJSON

The same error is returned.


ogrinfo

It was also suggested in the comments to use this command:

ogrinfo -so "https://kart.ra.no/arcgis/rest/services/Distribusjon/Kulturminner20180301/MapServer/6/query?where=1=1&outFields=*&outSR=4326&f=geojson" OGRGeoJSON

This outputs:

INFO: Open of `https://kart.ra.no/arcgis/rest/services/Distribusjon/Kulturminner20180301/MapServer/6/query?where=1=1&outFields=*&outSR=4326&f=geojson'
      using driver `GeoJSON' successful.

Layer name: OGRGeoJSON
Geometry: Polygon
Feature Count: 381014
Extent: (-8.940364, 56.715905) - (33.502886, 80.821061)
Layer SRS WKT:
GEOGCRS["WGS 84",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["geodetic latitude (Lat)",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["geodetic longitude (Lon)",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    ID["EPSG",4326]]
Data axis to CRS axis mapping: 2,1
kulturminneDatering: String (0.0)
kulturminneDateringKvalitet: String (0.0)
kulturminneDateringMetode: String (0.0)
kulturminneEnkeltminneArt: String (0.0)
kulturminneHovedMateriale: String (0.0)
kulturminneKategori: String (0.0)
kulturminneNavaerendeFunksjon: String (0.0)
kulturminneOpprinneligFunksjon: String (0.0)
kulturminneSynlig: Integer (0.0)
matrikkelnummer: String (0.0)
navn: String (0.0)
tilleggsvernedato: String (0.0)
tilleggsvernetype: String (0.0)
vernedato: String (0.0)
vernelov: String (0.0)
verneparagraf: String (0.0)
vernetype: String (0.0)
datafangstdato: String (0.0)
forsteDigitaliseringsdato: String (0.0)
lokalId: String (0.0)
navnerom: String (0.0)
versjonId: String (0.0)
informasjon: String (0.0)
kommune: String (0.0)
malemetode: String (0.0)
noyaktighet: Integer (0.0)
linkAskeladden: String (0.0)
linkKulturminnesok: String (0.0)
oppdateringsdato: String (0.0)
opphav: String (0.0)
OBJECTID: Integer (0.0)
SHAPE.STArea(): Real (0.0)
SHAPE.STLength(): Real (0.0)

which is nice, but I was aiming for a GeoJSON with the data.


What URL should I use so that I can export the Enkeltminner layer using ogr2ogr, or other command line methods?

Best Answer

ogr2ogr -f GeoJSON test.json "https://kart.ra.no/arcgis/rest/services/Distribusjon/Kulturminner20180301/MapServer/6/query?where=1=1&outFields=*&outSR=4326&f=geojson" OGRGeoJSON