Powershell and GDAL

Simon ‘Geosmiles’ Miles
4 min readSep 26, 2023

--

By Simon ‘geosmiles’ Miles

The background to this post is that at the beginning of September 2023, I gave a talk at the FOSS4G:UK Basingstoke local event, which I also chaired. My talk was on how I enjoy playing around with Powershell and GDAL to do stuff with data, chiefly Open Data, in my own time.

The talk went something like this..

Background

Powershell is OPEN SOURCE and you can get access to the source code for it on GitHub. Built on the .NET framework, PWSH (aka Powershell) is a cross platform tool that can be used on Mac, Linux and of course Windows to do a surprising number of things. And in my case, I use it in combination with GDAL, another Open Source tool or library that is chiefly used manipulate GIS vector or raster data.

I’ve used PWSH for many years and in may different ways but mainly around the automation of moving and testing of data and logging results/stages of the automation.

What do I like about PWSH?

I know that this is going to go down like a lead balloon, but I dislike Python and this is because you have have to write too many lines of code to do simple ‘things’ and indents! O’my indents, they do my head in.

PWSH uses less lines of code to do simple things and doesn't give a scooby about indents. The below illustrates my point.

Indents — brick Python when in the wrong place (see blue space above).

PWSH works off a verb-noun command or cmdlet, where the first part of the commands is a verb, telling you what action it’s going to do, and then noun element, the second part of the command is telling you what its going to do.

Examples

Get-Date -Format "dd-MM-yyyy"
Get-Date -Format "HH:mm:ss"
Get-Location
start-sleep 5s

Getting started with PWSH & GDAL.

So I’m a Mac user, so my blog bias is going to be getting these installed and setup on it, but i’ll try to point out other methods for Linux and Windows users.

In short for Mac (or linux), use homebrew. Install is simply done through a single commandline in terminal.

Once done, run in terminal: brew install — cask powershell and to get GDAL in run in terminal: brew install gdal

To see if Powershell is installed and working, in terminal use the command PWSH then hit enter. Then run the command Get-Host or $host.version to see what verson of PWSH has bene installed.

To see if GDAL is installed and working, in terminal use the commands gdalinfo — version You can run this command in a normal Mac termainal session or in a PWSH session, both will give you the same result.

If you are using Windows, then Powershell is more than likely installed on your machine already, unless you are running an older version of Windows (pre Windows 10). GDAL can be installed through the OSgeo4W installer which is really easy to use. Just make sure that when you install, that you see or check that the install is going to write GDAL as an environmental path!

How do we blend PSWH with GDAL?

Easy! Go into a PWSH session and first write a variable with a path to some data then hit enter. Then write out some GDAL to take that download file and output a SHP file and then hit enter.

$DownloadUrl = "https://files.planning.data.gov.uk/dataset/conservation-area.geojson"
ogr2ogr -f "ESRI Shapefile" ca.shp $DownloadUrl
Two lines of code to download a GeoJSON and output a SHP file. Note the errors when in column length that have been generated in the SHP. lol!!

If you write these two lines of code into a code-editor and save them as a .ps1 file, you can them run these as and went you like, or append them into a BAT file (on Windows) so that they can be scheduled.

My presentation went on to say how you can get data out of ArcGIS server using a similar method to the above, but branched out to include logging and error trapping.

I’ll leave you with screenshots of how this is done, but there will be more PWSH posts to come that will contain elements of the below.

Note. There should be an exist after line 49.

If you enjoyed this, then perhaps Buy Me A Coffee.

--

--