A lot of surveyors out there face some challenges when it comes to transforming coordinates from one system to the next. Normally we would go out in the field and collect our data using GPS Coordinate systems but the client requires the products in a local coordinate (in this case we could say Arc1950 which is used in Zimbabwe, Zambia and Malawi).
What if I told you that all these challenges could be solved in just 4 line of Python code?
In this article, we are going to be looking at how our fellow workmates and colleagues can use Python to transform coordinates from system A to system B.
Python has become a widely adopted programming languages with so many packages that have been developed by Open-Source contributors all over the globe. We are going to be using a python package named: PyProj.
What is Pyproj?
According to PyPi, PyProj is a python interface to PROJ (cartographic projections and coordinate transformations library).
The developers of this package have also managed to come up with documentation to help the geospatial community or anyone else who wishes to conduct coordinate transformations between systems.
Let’s start coding
So let’s get straight to the purposes of this post and get a little deep into some python programming.
First of all what you would want to do is to install Pyproj. To do this we can use Pip.
Open the Command-Line Interface on your machine and enter:
pip install pyproj
Once we have this package installed, it comes with all the libraries and functions that will help us limit our code and keep it to a minimum.
Enter the following line of code:
from pyproj import Transformer
Here we have told our Python interpreter that we would like to use the Pyproj package that we just imported. From that library, we would like to import the Transformer module which handles or 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 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.
transformed = transformer.transform(-19.0115438, 29.154858)
Now we create our last variable which is going to hold the result of the transformation and I have decided to name it transformed. It is always recommended to name your variables very differently, but for the sake of this tutorial, I would like to give illustration to the audience.
The piece of code above, calls the transform() function which takes two parameters, the Latitude and Longitude or Y and X coordinates to be transformed.
I took coordinates from around my area hoping you folks will not come hunting me around.
Let’s print the result of this transformation to the screen. The print function is basically used for display.
Run this Python code from your command line or from your code editor (depending on which one) and get a result for the transformation you have selected.
Your final piece of code should look like so:
# pip install pyproj from pyproj import Transformer transformer = Transformer.from_crs(4326, 20936) transformed = transformer.transform(-19.015438, 29.154858) print(transformed) # Result: -> (95097.5104640873, 7893332.773598025)
Hopefully you will get to adopt this easy to use way of transforming coordinates from one system to another.
I will also get back and work on a method to do this transformation for a batch of coordinates stored in CSV format and will definitely edit this section of the post.