3

I have my geographical coordinates of rectangles represented as numpy ndarray like this: (each row corresponds to a rectangle and each column contains its lower left and upper right longitudes and latitudes)

array([

  [ 116.17265886,   39.92265886,  116.1761427 ,   39.92536232],

  [ 116.20749721,   39.90373467,  116.21098105,   39.90643813],

  [ 116.21794872,   39.90373467,  116.22143255,   39.90643813]])

I want to call a coordinate-converting API whose input is a string like this:

'lon_0,lat_0;lon_1,lat_1;lon_2,lat_2;...;lon_n,lat_n'

So I wrote a stupid iteration to convert my ndarray to the required string like this:

coords = ''
for i in range(0, my_rectangle.shape[0]):
    coords = coords + '{left_lon},{left_lat};{right_lon},{rigth_lat}'.format(left_lon = my_rectangle[i][0], left_lat = my_rectangle[i][1], \
                                                               right_lon = my_rectangle[i][2], rigth_lat = my_rectangle[i][3])
    if i != my_rectangle.shape[0] - 1:
        coords = coords + ';'

And the output is like this:

'116.172658863,39.9226588629;116.176142698,39.9253623188;116.207497213,39.9037346711;116.210981048,39.9064381271;116.217948718,39.9037346711;116.221432553,39.9064381271'

I'm wondering whether there exists a smarter & faster approach achieving this without iteration(or some helpful documentation I could refer to)?

1
  • you can also do this: coords = ';'.join(','.join((i, j)) for i,j in my_rectangle.astype(str).reshape(-1, 2)) Commented Oct 13, 2014 at 8:25

1 Answer 1

3

Let's try using functional style:

values = [[ 116.17265886,   39.92265886,  116.1761427 ,   39.92536232],
          [ 116.20749721,   39.90373467,  116.21098105,   39.90643813],
          [ 116.21794872,   39.90373467,  116.22143255,   39.90643813]]

def prettyPrint(coords):
    return '{0},{1};{2},{3}'.format(coords[0], coords[1], coords[2], coords[3])

asString = formating(list(map(prettyPrint,values)))
print(";".join(asString)) #edited thanks to comments

map apply a function to each element of an iterable. So you define the process to apply on one element, and then using map replace each element by its result.

Hope you find it smarter ;)

Edit : You can also write it like this :

asString = [prettyPrint(value) for value in values] 
Sign up to request clarification or add additional context in comments.

5 Comments

The functional style looks indeed smarter and more elegant~
However, what I need is a string containing all the elements in the list linked by ';', not the list itself, so it seems like I have to sum all the elements in asString with another iteration? (Correct me if I'm wrong~)
Yes you're right. I edited my answer to match to your need. I know there is a way to convert it more conveniently and using functionnal style but I don't remember it right now. Will check this!
It should be ';'.join(asString)
Yes you're right! ;) I'll remove the useless part of the answer ^^

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.