Coordinate Transformation with Python

Performing a coordinate transformation can be simplified with a programming language like Python. Let's do this with just 3 lines of code.

Coordinate Transformation with Python

Before technology was a thing, surveyors and other geospatial professionals used to do things like coordinate transformation with paper, pen, and calculator.

With the coming of technology, most computations are just a button click away. That means less work is done and less time spent doing such a monotonous task. Not to mention the mistakes that come along during those computations.

Today we are going to be looking at how Python code can be used to perform a quick coordinate transformation between different coordinate systems.

Python is an interpreted high-level general-purpose programming language. Its design philosophy emphasizes code readability with its use of significant indentation. Its language constructs as well as its object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.

I do not think you need to know much about Python for you to do what we are about to do.

It's just going to be 3 lines of code only.

Before we go anywhere, you can take a look at a working example of such a function on my Django practice page on Heroku

Here we go;

Install PyProj

We are going to be using a python package called Pyproj for this exercise.

Pyproj is a Python interface to PROJ (cartographic projections and coordinate transformations library).

Of course, we can write our own code to do the transformation but that would take us several days maybe months for us to cover all the transformations and their parameters. After all, Why re-invent the wheel?

  • open up a terminal or a command-line tool (CMD in windows)
  • type in the following

pip install pyproj

The Pyproj packages and library together with all its dependencies will install on your machine.

  • open up a code editing tool. I'd recommend Visual Studio Code or PyCharm, especially when coding in Python.
  • For VS-Code, make sure the Python extension has been downloaded and enabled.

Create a file and name it something like I always like to keep my code editor on Auto-save. That way I do not forget or in-case anything happens.

  • copy and paste the code below
from pyproj import Transformer
transformer = Transformer.from_crs(4326, 20936)
print(transformer.transform(-17.835622, 31.044403))

First time writing code? Don't worry, the explanation is below.

The explanation

In the code,

from pyproj import Transformer

We have told our Python interpreter that we would like to use the Pyproj package. From that library/package, we would like to import the Transformer module which handles all the transformation requests.

transformer = Transformer.from_crs(4326, 20936)

Next, we assign a variable called transformer to the function which is going to let the Transformer know which system we are coming from and which system we are going to.

The from_crs() function is responsible for stating out the coordinate systems by taking two parameters, from and to respectively.

In this example we have selected 4326 and 20936, which is from WGS84 (EPSG:4326) to Arc1950 (EPSG:20936 Arc 1950) respectively.
If you are not sure about the codes to the CRS you want to use you can always refer to the list of coordinate reference systems or enquire with the Geology Department within your region.

print(transformer.transform(-17.835622, 31.044403)

Finally, we are going to use the print() function, to display the result of our transformation.

But within print, we have included another function transform() which takes to parameters, the Lat and Lon respectively to produce our result.

So here we have passed in the coordinates that we want to transform.

With these parameters, the display result should be:

(292753.7797485887, 8027216.719876923)

If the code was executed well, you should have your new coordinates now. Try changing the coordinate and the different systems to see what you come up with.

Looking to Transform a Batch?

Yes, it is also very possible to do a batch transformation. We can have a list of coordinates in a CSV file and use libraries like Pandas together with PyProj to do the work for us.

Will be adding that tutorial in the next session. Keep an eye out.

Need the Source Code?

Just in case you might need the source code of these few lines and more tutorials to come, I have created a GitHub repository where I will be posted all the source files.

The direct source to this tutorial.