Three ways to use text files to automate GIS on Windows – Geospatial World

Source: Esri

Early in my IT development, when Windows was exciting and new, a situation arose where there was work to be done with an IT department that needed super skills in MS Excel formula (that’s another story !). Every day the IT manager would open his beige computer and the second he logged in the command shell would open and all of his programs, emails and such would start up, it was amazing… I begged to learn Magic.

It turns out that there is a small tool built into Windows, up until the latest version of Windows 11. This goes back to DOS (the command line system before we had a GUI (Graphical User Interface). Batch Scripts allow you to bundle all of these command line commands into a text file which you can then run.

First of all, I’m not a coder, nor a fan of the command line, but I’m a big fan of batch scripting because it’s simple; actually it reminds me a bit of Python but uses the commands you use every day in the Windows command shell. If you want to make it really powerful (and you probably will anyway), you can put your other scripting tools in the PATH command shell. This probably sounds a bit confusing if you’re not a fan of the command line, so in simple terms adding something to the system PATH means it’s accessible through the command shell. You may have used OGR2OGR, Python or PostGIS in the past and couldn’t use the Windows command shell to run things. Making sure it can run them in the command shell, you do the following (example for Python):

Python

Add the directory of the executable file to the PATH variable. Often this needs to be done when installing Python.

The full path of python.exe can be entered by:

  1. Right-click on This PC and go to Properties.
  2. Click on advanced system settings in the left menu.
  3. Click the Environment Variables button at the bottom right.
  4. In the System Variables section, select the Path variable and click Edit. The next screen will show all the directories that are currently part of the PATH variable.
  5. Click New and enter the Python installation directory.

Once Python is in your environment, when you open the Windows command shell, you can run your python scripts there, or even better, link them with batch scripts!

READ ALSO: How Does GIS Improve Modern Field Service Operations?

Download multiple GIS data

Let’s download a file from Natural Earth using a batch file. Create a new folder, navigate to it, then open a text editor (Notepad++ is best, but regular notepad will be fine)

Type:

echo off

loop “http://naciscdn.org/naturalearth/packages/natural_earth_vector.zip” -output naturalearth.zip

Then save the text file as “naturalearth.bat”. Double-click the naturalearth.bat file and voila! There should be a new zip file in the folder. By changing the web link in the voice tags, we can download new data and then name it after the “output” text.

By using the same curl line, we can download a lot of data with a single double click of the mouse.

Download data

Download data in batches using OGR2OGR

If you use Notepad++, then uploading multiple files or converting formats using OGR2OGR is a dream, you can use Notepad++ to edit and copy/replace, then save the list of commands as a batch file will allow you to keep the list for another day when you want to run it again.

If you are running QGIS, chances are you already have OGR2OGR installed and you may just need to add it to your system path. If you find that you don’t have it installed, you can always download it via FWTools.

Here we will convert 4 GeoJSON data into shapefiles. As before, open your text editor and type:

echo off

cd c/gis/

ogr2ogr -f “ESRI Shapefile” destination_data1.shp “source-data1.json”

ogr2ogr -f “ESRI Shapefile” destination_data2.shp “source-data3.json”

ogr2ogr -f “ESRI Shapefile” destination_data3.shp “source-data3.json”

ogr2ogr -f “ESRI Shapefile” destination_data4.shp “source-data4.json”

Save this to a file with the suffix .bat

Above,

cd c/gis/ – this defines the file folder where the data you want to convert resides

-f “ESRI shapefile” defines the format in which you want the data to be converted. If you used -f “MapInfo File” the data would be converted to MapInfo

destination_data1.shp – this is the name of the shapefile you want. You can also add the file location (eg c/gis/shapefiles/shapefile1.shp)

“source-data1.json” – this is the name of the GeoJSON data, if the data was GeoPackage you would put the name and suffix here to convert it to a shapefile.

For a while when I first started using GDAL and OGR2OGR I was typing every command manually in the command shell and waiting for it to respond, this could take hours with a large list of data, putting it together in a batch file I soon discovered that I could just fire it up and let it run in the background.

There are several other ways to use OGR2OGR to merge data, change the coordinate system, and even cut data. One of my favorites is to use the -lco command to add sql commands to the download to add GIST indexes and vacuum scan when downloading PostGIS data.

Learn more about OGR2OGR and GDAL here.

Start GIS (or other things) on PC startup

Every day you start your work machine and open ArcGIS Pro, QGIS, email and everything else, which takes a good 30 minutes every morning. What if there was a way to automate the opening of all this software?

Again, batch file to the rescue! Here is a script that will open QGIS, ArcGIS Pro and Outlook 2016:

echo off

REM This opens qgis from osgeo4w folder

cd “C:OSGeo4Wbin”

run qgis-bin.exe

REM This opens Arcgis Pro

cd “C:Program FilesArcGISProbin”

start ArcGISPro.exe

REM This opens Outlook 2016

cd “C:Program FilesMicrosoft OfficerootOffice16”

launch OUTLOOK.EXE

REM Open an existing project or template (example QGIS template)

cd “C:GIS”

start UK_template.qgs

Green statements that start with “REM” are comments, as long as you put the text “REM” at the beginning, the batch file will not use it and will go to the next line.

You can see above that it’s relatively easy to add the program or file locations and there’s no need to worry about too many spaces or making sure you have commas at the end of each line.

You can just double-click it every time you start your machine, but adding it to your startup is really easy and from there you can choose whether to use it or not.

Move towards AppDataRoamingMicrosoftWindowsStart MenuProgramsStartup folder, then drag and drop the batch file into that folder.

Save Folder

Now when you go to your startup applications (type this text in your startup search), you should see that the batch file is one of your startup programs.

Starter apps

Starting apps

You can see the batch file is enabled at the bottom. Don’t worry if your screen doesn’t look the same. This was created in Windows 11 but also tested in Windows 10 and 11. Now when you restart your machine, your chosen software will open every time!

This is just a taste of what is possible with batch files. Once you’re more comfortable with how it all works, there’s a lot you can do. If you plan to mix some things discussed earlier in the article, where we open the software, download data and even use OGR2OGR to convert the data, imagine combining it to create one-click end-to-end solutions for certain jobs . Personally I have used it to start my GIS in the morning with the company template and start all my other software as well as automating large data lists it has saved me so much time I am happy that there are these media to share it with you.

ALSO READ: Empowering Indian Discoms with GIS

Comments are closed.