Select to view content in your preferred language

Issues Downloading WMTS Data as Raster in ArcGIS Pro

491
0
10-25-2024 05:08 AM
PerAndersen
New Contributor

Hello GIS community,

I’m trying to create a Python script to download a specific area of imagery from a WMTS service and save it as a raster image (e.g., PNG or TIFF). This script is intended to be used within an ArcGIS Pro toolbox, where the user defines the area they want to download. The downloaded image will be used for Deep Learning in ArcGIS Pro.

Here’s what I’ve tried so far:

I’m not using a layer_name parameter because the WMTS service should provide that based on the GetCapabilities response. The user also defines the extent within ArcGIS Pro.

Below is the script I’ve been working on:

import os
import logging
from osgeo import gdal

# Set up logging
logging.basicConfig(filename=os.path.join(output_folder, 'wmts_download.log'), level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logging.info('Log file created.')

# Parameters
wmts_url = 'https://api.dataforsyningen.dk/orto_foraar_wmts_DAF?service=WMTS&request=GetCapabilities&token=577b8...'
output_folder = r'D:\Ortofoto'
workspace = '713203,415311581 6173517,82703254 713491,018988423 6173662,51763386'
output_file = os.path.join(output_folder, 'geodanmark_2023_10cm.png')

# Log parameters
logging.info(f'URL: {wmts_url}')
logging.info(f'Output Folder: {output_folder}')

# Open the WMTS dataset
logging.info('Attempting to open WMTS dataset.')
ds = gdal.Open(wmts_url)
if ds is None:
logging.error('Failed to open WMTS dataset.')
raise RuntimeError('Failed to open WMTS dataset.')

# Set spatial reference
logging.info('Setting spatial reference.')
spatial_ref = gdal.osr.SpatialReference()
spatial_ref.ImportFromWkt(workspace)

# Attempt to create output file
logging.info(f'Attempting to create output file: {output_file}')
driver = gdal.GetDriverByName('PNG')
out_ds = driver.CreateCopy(output_file, ds)
if out_ds is None:
logging.error('Failed to create output file.')
raise RuntimeError('Failed to create output file.')

logging.info(f'Downloaded image saved as: {output_file}')

Has anyone faced a similar issue when using a WMTS service to download data as a raster image in ArcGIS Pro?

I would appreciate any advice or examples of scripts that successfully download and save WMTS data, or any alternative approaches for accomplishing this task.

Thanks in advance for your help!

//Per

0 Kudos
0 Replies