Convert Rasters for Map Serving

Convert Rasters for Map Serving

Overview

These instructions require a GDAL version >= 3.1. If you are using an older version of GDAL then see the previous version of this page: https://astuntech.atlassian.net/wiki/x/EoAmBAE

The following scripts convert images into suitable Cloud Optimized GeoTIFFs (COG) for fast map serving without losing clarity. Completely based upon Paul Ramsey's Geotiff for Dummies guide.

Either run in the folder containing the source raster files or pass a directory as an argument. Generated files are created in an 'output' sub-folder. 

Follow up process would be to build a vrt - see Mosaic thousands of raster images.

Projection set?

Check that the source file have a projection set? Check by using gdalinfo <some file> in the command line window. If not then a -s_srs "EPSG:27700" can be added to the gdal_translate line in each batch file.

Convert source tiles using gdal_translate

Convert to Cloud Optimized GeoTIFF

The following .bat file converts all source file .tif files in a directory to COG format, output in an output directory.

If the source files a are GeoTIFFs then you may not need to optimise them but if they are missing overviews then converting to COG format will likely improve rendering performance.

@ECHO OFF SET runDir=%cd% :: If we are passed a directory then use it, otherwise default to current working directory if "%~1"=="" ( echo no folder given ) else ( cd /d %1 ) echo Processing files in %cd% :: Create \output folder if not present IF NOT EXIST "%cd%\output" ( mkdir output ) :: Convert to COG format, output to \output directory for %%f in (*.tif) do ( :: echo %cd%\%%f gdal_translate -of COG -co COMPRESS=JPEG -co QUALITY=50 "%cd%\%%f" "%cd%\output\%%~nf.tif" ) :: Change back to original working directory cd /d %runDir%

Save a copy of , edit as required and run it from the GDAL Command Prompt.

Alternative source formats

If your source files are in a different format such as .sid or .jpg then update the filter on line 18 to match their file extension.

Other options

The gdal_translate utility supports a long list of other options including resizing and reprojecting source files as well as working with their bands. See https://gdal.org/en/stable/programs/gdal_translate.html for a full list of options.

Creating a Web Mapping Service

Create a tileindex

MapServer requires a vector table with a feature per source file that covers its extent. This known as a tileindex and can be created with the gdaltindex utility like so:

cd <folder> dir /s/b *.tif > optfile.txt gdaltindex tileindex.shp --optfile optfile.txt

Create a mapfile

Below is a sample mapfile which includes a single raster layer.

MAP EXTENT 0 0 700000 1300000 INCLUDE "_common.map" UNITS METERS TEMPLATEPATTERN '.' IMAGECOLOR 238 238 237 IMAGETYPE AGG_Q RESOLUTION 72 SHAPEPATH "../../" SIZE 512 512 MAXSIZE 8192 STATUS ON CONFIG "MS_ERRORFILE" "D:/mapserver/tmp/logs/base_rasters_debug.log" NAME "Raster_WMS" PROJECTION "init=epsg:27700" END DEBUG 0 IMAGETYPE png OUTPUTFORMAT NAME "png" MIMETYPE "image/png" DRIVER "AGG/PNG" EXTENSION "png" IMAGEMODE "RGBA" #CHANGED TRANSPARENT TRUE FORMATOPTION "TRANSPARENT=ON" END OUTPUTFORMAT NAME "AGG_JPEG" MIMETYPE "image/jpeg" DRIVER "AGG/JPEG" EXTENSION "JPG" IMAGEMODE "RGB" FORMATOPTION "interlace=off" END WEB IMAGEPATH "D:\mapserver\tmp\\" IMAGEURL "" TEMPLATE "globexml.xml" METADATA ows_title "Rasters" ows_srs "EPSG:27700" ows_abstract "WMS Rasters" ows_enable_request "*" END END LAYER NAME "Aerial" TYPE RASTER STATUS OFF METADATA "ows_title" "Aerial Imagery" "ows_abstract" "Aerial Imagery" END TILEINDEX "<folder>\tileindex.shp" END END

Testing in QGIS

If you are working on an iShare GIS or Maps server you should have access to QGIS which can be used to test the WMS layer. To open the MapServer WMS layer:

  • Open the Data Source Manager and choose the WMS/WMTS tab

  • Choose New and provide a Name and the URL to the MapServer WMS

    • The MapServer WMS URL is likely to be along the lines of http://localhost:81/mapserver/ms761?map=E:\iShareData\LIVE\_MapServerConfig\base_aerial.map where http://localhost:81/mapserver/ms761 is the URL that MapServer is available (within iShare Studio check Settings > Master Settings > MapServer URL) and E:\iShareData\LIVE\_MapServerConfig\base_aerial.map is the path to your mapfile.

  • Save the connection

  • Select the connection from the list and click Connect to see a list of layers that can be added to the map.

Cache with MapProxy

Commonly raster base maps are cached with MapProxy to improve performance. The WMS/WMTS service provided by MapProxy can be used both within iShare GIS and Maps together with desktop GIS such as GIS or MapInfo.

See https://astuntech.atlassian.net/wiki/x/hQBg4Q.