# Batch Coordinate Transformation

Coordinate transformation is an essential part and life of surveying. In this tutorial, we convert a batch of coordinates from a file using the Python programming language. Let's go...

Coordinate transformation is one of the common tasks performed by surveyors throughout their careers. Connecting points between different networks and coordinate reference systems (CRS) can be treated as an essential skill in the profession. Any silly mistake might lead to total disaster.

In a previous article, we learned how to use Python to transform coordinates from one system to another.

I had highlighted earlier that our next step would be to transform a batch of coordinates (say from a CSV file).

This tutorial will take you through the process of doing just that with Python.

### What you need

We are still going to be using PyProj for handling the conversion between System A to System B during this exercise. In addition, we will be adding another package for the task.

• Pandas

If you have done a little bit of data science with Python, you are already familiar with Pandas.

Just a little bit of some literature;

💡
Pandas is a software library written for the Python programming language for data manipulation and analysis.

For more detailed information and documentation about this library, you can check out the official Pandas site.

### Let's Write Some Code

Unlike the last time, this time unfortunately it's not going to be just 3 lines of code, but the code will be minimum though.

The first step is always to install and import all the libraries and packages that you will be using.

Now that our data is in a dataframe which we have placed in a variable `df`, let's create two lists.

• first list will contain all Y coordinates
• and the second list will contain all X coordinates.

We can easily do this with Pandas `.tolist()` function.

``````y_list = df['Y'].tolist() # Lat
x_list = df['X'].tolist() # Lon
``````
💡
Lists are used to store multiple values in a single variable

Instead of having y1, y2, y3, y4, ...., yx. All we need is a List.

Moving on, we define the transformation parameters using the `.from_crs()` function which takes two parameters.

• current CRS of the coordinates
• CRS to convert to.

For practice sake, lets convert WGS84 to Arc1950 Zone 35S.

``````transformer = Transformer.from_crs(4326, 20936)
``````

We can use the WKID to define these systems like above.

Remember, since we have so many values to convert, our results will also have a lot of results thus we need to create a list for results.

``````y_coords = []
x_coords = []
``````

We have created an empty list to store the results for both Y coordinates and X coordinates respectively.

Now that everything has been laid out and our data has been sorted in a perfect format which is suitable for manipulation, we can now move on to convert the coordinates one by one.

To do this, we loop through each and every coordinate pair from our lists and produce a transformation result for each pair.

We implement the `for` loop like so,

``````for y, x in zip(y_list, x_list):
trans_coords = transformer.transform(y, x)
# append to list
y_coords.append(trans_coords)
x_coords.append(trans_coords)
``````

In order to loop through two lists at once we used `zip()`.

The reason why we had declared the empty list to store our results will be justified here.

Notice when we append using `.append()` function, we will be appending to the empty list created above.

The `transformer.transform(y, x)` call will produce the result as a dictionary hence we need to extract the Y and X coordinates using the indexing value.

Since we have started with y, our index value for y becomes zero (0) and the value for x becomes one (1).

Thus we use `trans_coords` and `trans_coords` to get `y` and `x` respectively.

Our coordinates have been converted and stored in the list. The user needs to see the results and the best way to do this would be to give back the CSV they have uploaded and show the coordinates in both systems so they can make their comparison.

Let's create a dictionary,

Check your coordinate file and see the extra columns.

We have successfully converted coordinates in a file to another system. Imagine you have multiple coordinates, say a file with 1000 of them, you can easily use this script to do the work for you.

### Get the Source-Code

Remember like any other tutorials that involve coding, you can easily get the source code too within my repository on GitHub.

A simple `git clone` command or forking the repository could also work. Any updates I make, you will also stay in the loop.

### Integration with other systems

The same concept and script can be modified to suit different needs and purposes. In the next coming weeks or days, I will also be adding this to my list of web applications that I have been working on using the Django Framework.

You will have to make a few tweaks and changes for the script to fit with whatever technology or system you would be binding this to.

For example: In a web-application setting, you would have to add an upload, download, and storage functionality which results in more code being written to enhance the script for the task.