Converting XML Coordinate Polygon to GIS Coordinate Polygon with Python

Alexander Daniel Pratama
3 min readOct 29, 2020
source: https://www.freepik.com/vectors/background, by rawpixel.com

First of all, have you ever wondered how exactly coordinate works in GIS? How is it different than XML format? Why do I have to do it? The first question was answered when I was a 3rd year college student, but the last two were just answered recently when I finished a project.

The project I did before, there were a lot of improvisation. I learn a lot about python and how to make a backend-program full with python. No JS, C, C++, just Python. That was a great experience anyway.

So, back to the the question how is it related to my experience. It starts on the base concept of GIS in the coordinate format. GIS uses a ring of pair coordinate system which is widely known are xy (projected system) or lat-long (global system).

The coordinate format for each type GIS data
1. Point (X1Y1)
2. Polyline/ Line [[X1,Y1],[X2,Y2],…,[Xn,Yn])
3. Polygon/Area ([[[X1,Y1],[X2,Y2],[X3,Y3],…,[Xn,Yn],[X1,Y1]]])

As you can see from the concept, the polygon no matter how many points or vertexes created it will go back to the first coordinate.

This format is somehow different in the XML system. If you carefully look in the sample below, you will see the different in the first element and the last element:

-6.236125,106.872253 -6.252507,106.931992 -6.242951,106.952591 -6.231347,106.953964 -6.214965,106.940918 -6.208821,106.920319 -6.221108,106.87912 -6.236125,106.872253.

I actually need a month to understand the sample since I don’t know what is the format XML used. In short, it is start with Y1,X2 Y2,X3 Y3,…,Xn Yn,X1 Y1, X1. You see the different actually not really much only in the start-end of the list. AND! You must notice in the comma part it is not separated by space. The space is only inside the pair system.

Thus we are going to do is to change the XML Format to GIS Format.

First, we are going to make a variable from the STRING coordinate above,
coordinate_xml = -6.236125,106.872253 -6.252507,106.931992 -6.242951,106.952591 -6.231347,106.953964 -6.214965,106.940918 -6.208821,106.920319 -6.221108,106.87912 -6.236125,106.872253

Then, we divide each element in the string using string split function.
split_coordinate = coordinate_xml.split(‘,’). This function will create a list like this [[-6.236125],[106.872253 -6.252507],[106.931992 -6.242951],…,[106.872253]]

Next, we extract the first element and last element of the list and merge them into one variable
splitCoordinate_y = split_coordinate[0]
splitCoordinate_x = split_coordinate[-1]
firstCoordinate = ‘%s %s’%(splitCoordinate_x, splitCoordinate_y)

Finally, we replace the first and last element with the merged variable
split_coordinate[0] = firstCoordinate
split_coordinate[-1] = firstCoordinate

The pre-final data you will get should be like this

[[106.872253 -6.236125],[106.872253 -6.252507],[106.931992 -6.242951],…,[106.872253 -6.236125]]

The last thing you need to do is replace the the space between XY with comma. Since it is a list we need to extract per element and convert to string to simplify the process

for i in range(len(split_coordinate)):
pair_coordinate = str(split_coordinate[i])
replaceCoordinate = pair_coordinate.replace(‘ ‘, ‘, ‘)
create_list_element = [replaceCoordinate] listCoordinate.append(create_list_element)

Pardon me, since the medium cannot do the indent. I will show you the whole script should be like this

convertion script xml coordinate to gis coordinate

So that is how I create a GIS coordinate system from the XML coordinate system format.

--

--

Alexander Daniel Pratama

GIS Specialist, Data Engineer, and A proud Geodetic Engineer