Moveton - 1 year ago 60
Python Question

# Python | NumPy | Coordinates - An issue with conversion of geographical coordinates

I have a pretty simple issue: I need to convert the file with geographical coordinates like

``````Lat         Long
50 0 50     35 1 40
50 2 50     35 10 20
50 3 1      35 13 22
50 2 38     35 14 40
49 59 6     35 13 22
49 57 14    35 13 21
49 57 10    35 13 0
49 57 0     35 6 20
``````

to the

``````Lat      Long
50.01389,35.02778
50.04722,35.17222
etc.
``````

Math is as simple as a pie: we have to devide minutes (0 and 1 in this particular case) by 60 and seconds (50 and 40) by 3600, then add these numbers and we will get the remainder of the degree (50 and 35).

Here is my script with numpy. I suppose, it looks to big for such a simple conversion, however I don't know how to do this simpler. Also I don't know how to end this script, so it could do what it should. Now it ends with adding minutes and seconds.

``````import sys
import numpy as np

filename = input('Please enter the file\'s name: ')
with open(filename, "r") as f:
sys.stdout = open('%s (converted).txt' % f.name, 'a')

degree_lat, degree_long = data[:, 0], data[:, 3]
min_lat, sec_lat, min_long, sec_long = \
(data[:, 1] / 60), (data[:, 2] / 3600), (data[:, 4] / 60), (data[:, 5] / 3600)

remainder_lat, remainder_long = min_lat + sec_lat, min_long + sec_long

degree_result_lat = degree_lat + remainder_lat
degree_result_long = degree_long + remainder_long
``````

Any suggestions would be greatly appreciated! Thanks and sorry for the amateur questions.

If I understand correctly what you have in `data` array,

``````data = data.T # not strictly necessary, but simplifies following indexing

lat = data[0]+data[1]/60.+data[2]/3600.
lon = data[3]+data[4]/60.+data[5]/3600.

converted = np.vstack((lat,lon)).T

np.savetxt(outname, converted)
``````

Line by line comment

1. `data.T` transposes the array, columns becomes tows and in Python it's easier to address rows than columns...
2. `data[0]+data[1]/60.+data[2]/3600.` is a vectorized expression, each row of the `data` array is an array on its own, and you can evaluate algebraic expressions, possibly using also `numpy`'s functions that accept, as arguments, vector expressions as well.
3. as aboveā¦
4. `np.vstack((lat,lon)).T` we have two names that reference two different expressions, we want to combine them in a single array, so that we can use `np.savetxt()` to save it. Using `np.vstack()` we get an array like

``````[[lt0, lt1, ..., ltN],
[ln0, ln1, ..., lnN]]
``````

but we want to save an array like

``````[[lt0, ln0],
[lt1, ln1],
...
``````

so we have to transpose the result of `np.vstack()`

5. `np.savetxt(outname, converted)` we save at once the whole array using one of the convenient conveniency functions offered by the `numpy` libraries.

Note that, when using `numpy` you should try to avoid explicit loops and instead relying on its ability to vectorize most expressions. This leads to much more efficient code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download