Connect to and get data from ArcGIS REST services

O.Kaufmann, 2023.

[1]:
from geometron.plot import basemaps
from geometron.utils.url import url_open_with_retry, http_request, url_request_string
from geometron.utils.str import remove_accents
from geometron.utils.geoservices import arcgis_rest_services_directory, esri_rest_server_request, esri_rest_server_metadata
import urllib
import json
import pandas as pd
[2]:
#server = 'https://geoservices.wallonie.be/arcgis/rest/services/'
server = 'https://ags.geo.api.vlaanderen.be/arcgis/rest/services/'

Retrieve a dataframe describing available services from an ESRI REST server using a keyword

[3]:
arcgis_rest_services_directory(server, keyword='vlm')
<urlopen error [Errno -2] Name or service not known>
<urlopen error [Errno -2] Name or service not known>
<urlopen error [Errno -2] Name or service not known>
Unable to get response after 3 attempts.
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[3], line 1
----> 1 arcgis_rest_services_directory(server, keyword='vlm')

File ~/PycharmProjects/geometron/geometron/utils/geoservices.py:20, in arcgis_rest_services_directory(server, keyword, verbose)
     19 def arcgis_rest_services_directory(server, keyword=None, verbose=True):
---> 20     resp = http_request(server, data={'f': 'json'})
     21     list_layers = []
     22     folders = resp['folders']

File ~/PycharmProjects/geometron/geometron/utils/url.py:53, in http_request(server, data)
     51 def http_request(server, data=None):
     52     request = urllib.request.Request(server, data=urllib.parse.urlencode(data, encoding='utf-8').encode('utf-8'))
---> 53     return json.load(url_open_with_retry(request))

File ~/.pyenv/versions/3.10.13/lib/python3.10/json/__init__.py:293, in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    274 def load(fp, *, cls=None, object_hook=None, parse_float=None,
    275         parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
    276     """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
    277     a JSON document) to a Python object.
    278
   (...)
    291     kwarg; otherwise ``JSONDecoder`` is used.
    292     """
--> 293     return loads(fp.read(),
    294         cls=cls, object_hook=object_hook,
    295         parse_float=parse_float, parse_int=parse_int,
    296         parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)

AttributeError: 'NoneType' object has no attribute 'read'

Create a request to an ESRI REST server and display the reply as a json

[4]:
esri_rest_server_request?
[5]:
r = esri_rest_server_request('geoservices.wallonie.be', folder='IMAGERIE', service='ORTHO_2020', service_type='MapServer', verbose=True)
url:
https://geoservices.wallonie.be/arcgis/rest/services/IMAGERIE/ORTHO_2020/MapServer/?f=pjson

[6]:
resp = url_open_with_retry(r)
[7]:
resp.read()
[7]:
b'{\n "currentVersion": 10.51,\n "serviceDescription": "Ce service de visualisation REST permet de consulter l\'image orthorectifi&#233;e et mosa&#239;qu&#233;e couvrant l\'enti&#232;ret&#233; du territoire wallon &#224; une r&#233;solution de 25 cm pour l\'ann&#233;e 2020.\\nPour tout compl&#233;ment d\'informations, veuillez consulter https://geoportail.wallonie.be/catalogue/cf1f6fae-d938-42ba-8e2c-18de5d450122.html",\n "mapName": "Layers",\n "description": "",\n "copyrightText": "SPW",\n "supportsDynamicLayers": true,\n "layers": [\n  {\n   "id": 0,\n   "name": "ORTHO_2020",\n   "parentLayerId": -1,\n   "defaultVisibility": true,\n   "subLayerIds": null,\n   "minScale": 0,\n   "maxScale": 0\n  }\n ],\n "tables": [],\n "spatialReference": {\n  "wkid": 102199,\n  "latestWkid": 3812\n },\n "singleFusedMapCache": false,\n "initialExtent": {\n  "xmin": 495814.35533628846,\n  "ymin": 484814.15198506153,\n  "xmax": 823759.8872449,\n  "ymax": 703125.8034499779,\n  "spatialReference": {\n   "wkid": 102199,\n   "latestWkid": 3812\n  }\n },\n "fullExtent": {\n  "xmin": 542000.7987416779,\n  "ymin": 519988.6172621446,\n  "xmax": 796016.2987416779,\n  "ymax": 668017.8672621446,\n  "spatialReference": {\n   "wkid": 102199,\n   "latestWkid": 3812\n  }\n },\n "minScale": 0,\n "maxScale": 0,\n "units": "esriMeters",\n "supportedImageFormatTypes": "PNG32,PNG24,PNG,JPG,DIB,TIFF,EMF,PS,PDF,GIF,SVG,SVGZ,BMP",\n "documentInfo": {\n  "Title": "ORTHO_2020",\n  "Author": "",\n  "Comments": "Ce service de visualisation REST permet de consulter l\'image orthorectifi\xc3\xa9e et mosa\xc3\xafqu\xc3\xa9e couvrant l\'enti\xc3\xa8ret\xc3\xa9 du territoire wallon \xc3\xa0 une r\xc3\xa9solution de 25 cm pour l\'ann\xc3\xa9e 2020.\\nPour tout compl\xc3\xa9ment d\'informations, veuillez consulter http://geoportail.wallonie.be/catalogue/17dab011-b21a-486c-b602-6ec141f1f059.html\\n",\n  "Subject": "Ce service de visualisation REST permet de consulter l\'image orthorectifi&#233;e et mosa&#239;qu&#233;e couvrant l\'enti&#232;ret&#233; du territoire wallon pour l\'ann&#233;e 2020.",\n  "Category": "",\n  "AntialiasingMode": "None",\n  "TextAntialiasingMode": "Force",\n  "Keywords": "ortho,orthophoto,orthophotoplan,vue a&#233;rienne,image a&#233;rienne,IR,infra-rouge,ortho_2020,orthoimagerie,infrarouge,REST"\n },\n "capabilities": "Map,Query,Data",\n "supportedQueryFormats": "JSON, AMF, geoJSON",\n "exportTilesAllowed": false,\n "datumTransformations": [\n  {\n   "geoTransforms": [\n    {\n     "wkid": 108001,\n     "latestWkid": 1241,\n     "transformForward": true,\n     "name": "NAD_1927_To_NAD_1983_NADCON"\n    }\n   ]\n  },\n  {\n   "geoTransforms": [\n    {\n     "wkid": 108001,\n     "latestWkid": 1241,\n     "transformForward": false,\n     "name": "NAD_1927_To_NAD_1983_NADCON"\n    }\n   ]\n  },\n  {\n   "geoTransforms": [\n    {\n     "wkid": 1609,\n     "latestWkid": 1609,\n     "transformForward": true,\n     "name": "Belge_1972_To_WGS_1984_1"\n    }\n   ]\n  },\n  {\n   "geoTransforms": [\n    {\n     "wkid": 1609,\n     "latestWkid": 1609,\n     "transformForward": false,\n     "name": "Belge_1972_To_WGS_1984_1"\n    }\n   ]\n  }\n ],\n "supportsDatumTransformation": true,\n "maxRecordCount": 1000,\n "maxImageHeight": 4096,\n "maxImageWidth": 4096,\n "supportedExtensions": "WMSServer"\n}'

Retrieve metadata from an ESRI REST server

[8]:
host = 'geoservices.wallonie.be'
folder = 'IMAGERIE'
service = 'ORTHO_2020'
esri_rest_server_metadata(host, folder=folder, service=service)
url:
https://geoservices.wallonie.be/arcgis/rest/services/IMAGERIE/ORTHO_2020/MapServer/?f=pjson

[8]:
{'currentVersion': 10.51,
 'serviceDescription': "Ce service de visualisation REST permet de consulter l'image orthorectifi&#233;e et mosa&#239;qu&#233;e couvrant l'enti&#232;ret&#233; du territoire wallon &#224; une r&#233;solution de 25 cm pour l'ann&#233;e 2020.\nPour tout compl&#233;ment d'informations, veuillez consulter https://geoportail.wallonie.be/catalogue/cf1f6fae-d938-42ba-8e2c-18de5d450122.html",
 'mapName': 'Layers',
 'description': '',
 'copyrightText': 'SPW',
 'supportsDynamicLayers': True,
 'layers': [{'id': 0,
   'name': 'ORTHO_2020',
   'parentLayerId': -1,
   'defaultVisibility': True,
   'subLayerIds': None,
   'minScale': 0,
   'maxScale': 0}],
 'tables': [],
 'spatialReference': {'wkid': 102199, 'latestWkid': 3812},
 'singleFusedMapCache': False,
 'initialExtent': {'xmin': 495814.35533628846,
  'ymin': 484814.15198506153,
  'xmax': 823759.8872449,
  'ymax': 703125.8034499779,
  'spatialReference': {'wkid': 102199, 'latestWkid': 3812}},
 'fullExtent': {'xmin': 542000.7987416779,
  'ymin': 519988.6172621446,
  'xmax': 796016.2987416779,
  'ymax': 668017.8672621446,
  'spatialReference': {'wkid': 102199, 'latestWkid': 3812}},
 'minScale': 0,
 'maxScale': 0,
 'units': 'esriMeters',
 'supportedImageFormatTypes': 'PNG32,PNG24,PNG,JPG,DIB,TIFF,EMF,PS,PDF,GIF,SVG,SVGZ,BMP',
 'documentInfo': {'Title': 'ORTHO_2020',
  'Author': '',
  'Comments': "Ce service de visualisation REST permet de consulter l'image orthorectifiée et mosaïquée couvrant l'entièreté du territoire wallon à une résolution de 25 cm pour l'année 2020.\nPour tout complément d'informations, veuillez consulter http://geoportail.wallonie.be/catalogue/17dab011-b21a-486c-b602-6ec141f1f059.html\n",
  'Subject': "Ce service de visualisation REST permet de consulter l'image orthorectifi&#233;e et mosa&#239;qu&#233;e couvrant l'enti&#232;ret&#233; du territoire wallon pour l'ann&#233;e 2020.",
  'Category': '',
  'AntialiasingMode': 'None',
  'TextAntialiasingMode': 'Force',
  'Keywords': 'ortho,orthophoto,orthophotoplan,vue a&#233;rienne,image a&#233;rienne,IR,infra-rouge,ortho_2020,orthoimagerie,infrarouge,REST'},
 'capabilities': 'Map,Query,Data',
 'supportedQueryFormats': 'JSON, AMF, geoJSON',
 'exportTilesAllowed': False,
 'datumTransformations': [{'geoTransforms': [{'wkid': 108001,
     'latestWkid': 1241,
     'transformForward': True,
     'name': 'NAD_1927_To_NAD_1983_NADCON'}]},
  {'geoTransforms': [{'wkid': 108001,
     'latestWkid': 1241,
     'transformForward': False,
     'name': 'NAD_1927_To_NAD_1983_NADCON'}]},
  {'geoTransforms': [{'wkid': 1609,
     'latestWkid': 1609,
     'transformForward': True,
     'name': 'Belge_1972_To_WGS_1984_1'}]},
  {'geoTransforms': [{'wkid': 1609,
     'latestWkid': 1609,
     'transformForward': False,
     'name': 'Belge_1972_To_WGS_1984_1'}]}],
 'supportsDatumTransformation': True,
 'maxRecordCount': 1000,
 'maxImageHeight': 4096,
 'maxImageWidth': 4096,
 'supportedExtensions': 'WMSServer'}
[ ]: