0
[' -80.82581786107986,39.83903198141125,0 -80.82377033116026,39.83364133601582,0 -80.82356083750963,39.82911201506083,0 -80.82285757569279,39.82686138006091,0']

I extracted coordinates from a KML file with 3 polygons and one of the polygon's coordinates are stored in the one element of the list. I want to calculate the min of the longitude and latitude respectively. Does anyone know how to do that?

Do I need to split the list somehow and then proceed?

Very much appreciated!

9
  • 2
    You have a list containing one string? Commented Feb 10, 2016 at 6:25
  • I was wondering the same, or is that one element from the list? Commented Feb 10, 2016 at 6:27
  • Actually I have a list containing three strings. This is just one of them. I think if I can figure out one, I may be able to do the rest by a simple loop. Thanks for your question! Commented Feb 10, 2016 at 6:27
  • It's one element from the list. For example, the -80.82581786107986 is longitude, 39.83903198141125 is latitude. and ",0" is something I don't need and it is contained in a bigger KML string. Commented Feb 10, 2016 at 6:29
  • 1
    What you displayed here is a list of only one string. Please update with the actual data Commented Feb 10, 2016 at 6:29

3 Answers 3

1

I think this is what you are looking for. Note: In Python 2.x

I take the single string, strip the outer whitespace, the split the values on whitespace or commas. That yields this array, which you can loop over in groups to get values.

['-80.82581786107986', '39.83903198141125', '0', '-80.82377033116026', '39.83364133601582', '0', '-80.82356083750963', '39.82911201506083', '0', '-80.82285757569279', '39.82686138006091', '0']

import re

l = [' -80.82581786107986,39.83903198141125,0 -80.82377033116026,39.83364133601582,0 -80.82356083750963,39.82911201506083,0',
    '-80.82285757569279,39.82686138006091,0 -80.82211394716366,39.82370641582035,0 -80.82079041778377,39.82101855094219,0',
    ' -80.82008287730855,39.84462640578131,0 -80.82581786107986,39.83903198141125,0']

for s in l: 
    parts = map(float, re.split(r'[,\s+]', s.strip()))

    lats = []
    longs = []

    for i in range(0, len(parts), 3):
        long = parts[i]
        lat = parts[i+1]
        longs.append(long)
        lats.append(lat)

    print min(lats), min(longs)

Output

39.8291120151 -80.8258178611
39.8210185509 -80.8228575757
39.8390319814 -80.8258178611
Sign up to request clarification or add additional context in comments.

6 Comments

[' -80.82581786107986,39.83903198141125,0 -80.82377033116026,39.83364133601582,0 -80.82356083750963,39.82911201506083,0' '-80.82285757569279,39.82686138006091,0 -80.82211394716366,39.82370641582035,0 -80.82079041778377,39.82101855094219,0',' -80.82008287730855,39.84462640578131,0 -80.82581786107986,39.83903198141125,0'] My whole list is like this, how can I convert each element to the type of list? Thanks! I am really new to Python!
I think you want each element of the type float, no?
My question is based on your code, you treat each element as a list: l = [' -80.82581786107986,39.83903198141125,0 -80.82377033116026,39.83364133601582,0 -80.82356083750963,39.82911201506083,0 -80.82285757569279,39.82686138006091,0'] I want to do something like for i in range(0,len(c)): some code for each of the three element. Will that be possible?
Thanks so much. The only problem is I have three elements. So how to get the min stats for each of the three elements?
Thanks so much! That is exactly what I want! Thanks a lot!
|
0

Using list comprehension, str.split() and str.strip methods:

l = [' -80.82581786107986,39.83903198141125,0 -80.82377033116026,39.83364133601582,0 -80.82356083750963,39.82911201506083,0 -80.82285757569279,39.82686138006091,0']
res = [list(map(float, coord.split(','))) for coord in l[0].strip().split()]

In [217]: res 
Out[217]:
[[-80.82581786107986, 39.83903198141125, 0.0],
 [-80.82377033116026, 39.83364133601582, 0.0],
 [-80.82356083750963, 39.82911201506083, 0.0],
 [-80.8228575756928, 39.82686138006091, 0.0]]

To calculate the min and longtitude and latitude you could convert that list to np.array for easy manipulation and use ndarray method min with axis=0 or np.amin:

import numpy as np
arr = np.array(res)

In [221]: arr
Out[221]:
array([[-80.82581786,  39.83903198,   0.        ],
       [-80.82377033,  39.83364134,   0.        ],
       [-80.82356084,  39.82911202,   0.        ],
       [-80.82285758,  39.82686138,   0.        ]])

In [224]: arr.min(axis=0)
Out[224]: array([-80.82581786,  39.82686138,   0.        ])

In [226]: np.amin(arr, axis=0)
Out[226]: array([-80.82581786,  39.82686138,   0.        ])

2 Comments

Thanks, but a element of the list is a string, will your code still work? I have three elements in the list. I want to do one and will loop to do the rest. Thanks for your help! I am really new to Python! Thanks for your help!
@tomtomxu if you'll check the solution you could find that it used map with float which means convert str to float.
0

here you go:

>>> my_list = [' -80.82581786107986,39.83903198141125,0 -80.82377033116026,39.83364133601582,0 -80.82356083750963,39.82911201506083,0 -80.82285757569279,39.82686138006091,0']
>>> map(lambda x: map(float, x.split(",")), my_list[0].strip().split())
[[-80.82581786107986, 39.83903198141125, 0.0], [-80.82377033116026, 39.83364133601582, 0.0], [-80.82356083750963, 39.82911201506083, 0.0], [-80.8228575756928, 39.82686138006091, 0.0]]

Mean of latitude and longitude:

>>> map(lambda x: (x[0]+x[1])/2, map(lambda x: map(float, x.split(",")), my_list[0].strip().split()))
[-20.4933929398343, -20.49506449757222, -20.4972244112244, -20.49799809781594]

you can use pandas too:

>>> import `pandas` as pd
>>> pd.DataFrame(map(lambda x: map(float, x.split(",")), my_list[0].strip().split()))
           0          1  2
0 -80.825818  39.839032  0
1 -80.823770  39.833641  0
2 -80.823561  39.829112  0
3 -80.822858  39.826861  0
>>> data = pd.DataFrame(map(lambda x: map(float, x.split(",")), my_list[0].strip().split()))
>>> data.mean()
0   -80.824002
1    39.832162
2     0.000000
dtype: float64

3 Comments

I think you missed the part of the question that said "I want to calculate the min of the longitude and latitude respectively"
Thanks, but a element of the list is a string, will your code still work? I have three elements in the list. I want to do one and will loop to do the rest. Thanks for your help!
averaging a latitude and a longitude value doesn't make any sense... also not what OP asked for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.