Extracting an adjaceny matrix containing haversine distance from points on map The 2019 Stack Overflow Developer Survey Results Are In2-opt algorithm for the Traveling Salesman and/or SRODistance between two n-dimensional points (NASM)Compute spherical distance matrix from list of geographical coordinatesCalculating the distance between several spatial pointsFind minimum distance in matrixQuadrilaterals containing a square number of lattice pointsDe-linearize a 2d linearized grid and calculate distance between 2 pointsCalculate distance between points and price per area in PandasCompetition - Qualifications - E - Matrix distanceAlgorithm that parses through input of points and finds distance2-opt algorithm for the Traveling Salesman and/or SRO

Why do I get badly formatted numerical results when I use StringForm?

How can I fix this gap between bookcases I made?

Should I use my personal or workplace e-mail when registering to external websites for work purpose?

Idiomatic way to prevent slicing?

What is the best strategy for white in this position?

What does "rabbited" mean/imply in this sentence?

How long do I have to send payment?

What does Linus Torvalds mean when he says that Git "never ever" tracks a file?

Inline version of a function returns different value than non-inline version

Is there a name of the flying bionic bird?

Which Sci-Fi work first showed weapon of galactic-scale mass destruction?

How to make payment on the internet without leaving a money trail?

What tool would a Roman-age civilization have to grind silver and other metals into dust?

Springs with some finite mass

Are there any other methods to apply to solving simultaneous equations?

Inflated grade on resume at previous job, might former employer tell new employer?

JSON.serialize: is it possible to suppress null values of a map?

Why don't Unix/Linux systems traverse through directories until they find the required version of a linked library?

Are USB sockets on wall outlets live all the time, even when the switch is off?

Is this food a bread or a loaf?

What is this 4-propeller plane?

In microwave frequencies, do you use a circulator when you need a (near) perfect diode?

Inversion Puzzle

Could a US political party gain complete control over the government by removing checks & balances?



Extracting an adjaceny matrix containing haversine distance from points on map



The 2019 Stack Overflow Developer Survey Results Are In2-opt algorithm for the Traveling Salesman and/or SRODistance between two n-dimensional points (NASM)Compute spherical distance matrix from list of geographical coordinatesCalculating the distance between several spatial pointsFind minimum distance in matrixQuadrilaterals containing a square number of lattice pointsDe-linearize a 2d linearized grid and calculate distance between 2 pointsCalculate distance between points and price per area in PandasCompetition - Qualifications - E - Matrix distanceAlgorithm that parses through input of points and finds distance2-opt algorithm for the Traveling Salesman and/or SRO



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








6












$begingroup$


I am extracting 10 lat/long points from Google Maps and placing these into a text file. The program should be able to read in the text file, calculate the haversine distance between each point, and store in an adjacency matrix. The adjacency matrix will eventually be fed to a 2-opt algorithm, which is outside the scope of the code I am about to present.



The following code is functional, but extremely inefficient. If I had 1000 points instead of 10, the adjacency matrix would need 1000 x 1000 iterations to be filled. Can this be optimized?



import csv
from haversine import haversine
import matplotlib.pyplot as plt
import numpy as np


def read_two_column_file(file_name):
with open(file_name, 'r') as f_input:
csv_input = csv.reader(f_input, delimiter=' ', skipinitialspace=True, )
long = []
lat = []
for col in csv_input:
x = float(col[0]) # converting to float
y = float(col[1])
long.append(x)
lat.append(y)

return long, lat


def display_points(long, lat):
plt.figure()
plt.ylabel('longitude')
plt.xlabel('latitude')
plt.title('longitude vs latitude')
plt.scatter(long, lat)
plt.show()


def main():
long, lat = read_two_column_file('latlong.txt')

points = []
for i in range(len(lat)):
coords = tuple([lat[i], long[i]]) # converting to tuple to be able to perform haverine calc.
points.append(coords)

hav = []
for i in range(len(lat)):
for j in range(len(long)):
hav.append(haversine(points[i], points[j]))

np.asarray(hav)
adj_matrix = np.reshape(hav, (10, 10)) # reshaping to 10 x 10 matrix
print(adj_matrix)

display_points(long, lat)


main()


Sample Input:



35.905333, 14.471970
35.896389, 14.477780
35.901281, 14.518173
35.860491, 14.572245
35.807607, 14.535320
35.832267, 14.455894
35.882414, 14.373217
35.983794, 14.336096
35.974463, 14.351006
35.930951, 14.401137


Sample Output:



[[ 0. 1.15959635 5.15603243 12.15003864 12.66090817 8.06760374
11.25481465 17.31108648 15.37358741 8.34541481]
[ 1.15959635 0. 4.52227294 11.19223786 11.50131214 7.32033758
11.72388583 18.35259685 16.41378987 9.29953014]
[ 5.15603243 4.52227294 0. 7.44480948 10.26177912 10.15688933
16.24592213 22.1101544 20.18967731 13.40020548]
[12.15003864 11.19223786 7.44480948 0. 7.01813758 13.28961044
22.25645098 29.42422794 27.49154954 20.48281039]
[12.66090817 11.50131214 10.26177912 7.01813758 0. 9.22215871
19.74293886 29.16680205 27.25540014 19.97465594]
[ 8.06760374 7.32033758 10.15688933 13.28961044 9.22215871 0.
10.66219491 21.06632671 19.24994647 12.24773666]
[11.25481465 11.72388583 16.24592213 22.25645098 19.74293886 10.66219491
0. 11.67502344 10.21846781 6.08016463]
[17.31108648 18.35259685 22.1101544 29.42422794 29.16680205 21.06632671
11.67502344 0. 1.93885474 9.20353461]
[15.37358741 16.41378987 20.18967731 27.49154954 27.25540014 19.24994647
10.21846781 1.93885474 0. 7.28280909]
[ 8.34541481 9.29953014 13.40020548 20.48281039 19.97465594 12.24773666
6.08016463 9.20353461 7.28280909 0. ]]


Plot:



enter image description here










share|improve this question











$endgroup$







  • 1




    $begingroup$
    Welcome to Code Review and congratulations on writing a decent question on your first try.
    $endgroup$
    – Mast
    Nov 12 '18 at 9:10






  • 2




    $begingroup$
    This is a great first question, I hope you get some good feedback.
    $endgroup$
    – esote
    Nov 12 '18 at 14:54






  • 2




    $begingroup$
    If you want your code to be error prone, you actually want it so it is easy to have bugs. You maybe meant error proof?
    $endgroup$
    – Graipher
    Nov 12 '18 at 15:55






  • 1




    $begingroup$
    Well spotted @Graipher! Seems like I want suggestions on how to actually include bugs in my code. Thanks for pointing out, will edit.
    $endgroup$
    – Rrz0
    Nov 12 '18 at 16:11


















6












$begingroup$


I am extracting 10 lat/long points from Google Maps and placing these into a text file. The program should be able to read in the text file, calculate the haversine distance between each point, and store in an adjacency matrix. The adjacency matrix will eventually be fed to a 2-opt algorithm, which is outside the scope of the code I am about to present.



The following code is functional, but extremely inefficient. If I had 1000 points instead of 10, the adjacency matrix would need 1000 x 1000 iterations to be filled. Can this be optimized?



import csv
from haversine import haversine
import matplotlib.pyplot as plt
import numpy as np


def read_two_column_file(file_name):
with open(file_name, 'r') as f_input:
csv_input = csv.reader(f_input, delimiter=' ', skipinitialspace=True, )
long = []
lat = []
for col in csv_input:
x = float(col[0]) # converting to float
y = float(col[1])
long.append(x)
lat.append(y)

return long, lat


def display_points(long, lat):
plt.figure()
plt.ylabel('longitude')
plt.xlabel('latitude')
plt.title('longitude vs latitude')
plt.scatter(long, lat)
plt.show()


def main():
long, lat = read_two_column_file('latlong.txt')

points = []
for i in range(len(lat)):
coords = tuple([lat[i], long[i]]) # converting to tuple to be able to perform haverine calc.
points.append(coords)

hav = []
for i in range(len(lat)):
for j in range(len(long)):
hav.append(haversine(points[i], points[j]))

np.asarray(hav)
adj_matrix = np.reshape(hav, (10, 10)) # reshaping to 10 x 10 matrix
print(adj_matrix)

display_points(long, lat)


main()


Sample Input:



35.905333, 14.471970
35.896389, 14.477780
35.901281, 14.518173
35.860491, 14.572245
35.807607, 14.535320
35.832267, 14.455894
35.882414, 14.373217
35.983794, 14.336096
35.974463, 14.351006
35.930951, 14.401137


Sample Output:



[[ 0. 1.15959635 5.15603243 12.15003864 12.66090817 8.06760374
11.25481465 17.31108648 15.37358741 8.34541481]
[ 1.15959635 0. 4.52227294 11.19223786 11.50131214 7.32033758
11.72388583 18.35259685 16.41378987 9.29953014]
[ 5.15603243 4.52227294 0. 7.44480948 10.26177912 10.15688933
16.24592213 22.1101544 20.18967731 13.40020548]
[12.15003864 11.19223786 7.44480948 0. 7.01813758 13.28961044
22.25645098 29.42422794 27.49154954 20.48281039]
[12.66090817 11.50131214 10.26177912 7.01813758 0. 9.22215871
19.74293886 29.16680205 27.25540014 19.97465594]
[ 8.06760374 7.32033758 10.15688933 13.28961044 9.22215871 0.
10.66219491 21.06632671 19.24994647 12.24773666]
[11.25481465 11.72388583 16.24592213 22.25645098 19.74293886 10.66219491
0. 11.67502344 10.21846781 6.08016463]
[17.31108648 18.35259685 22.1101544 29.42422794 29.16680205 21.06632671
11.67502344 0. 1.93885474 9.20353461]
[15.37358741 16.41378987 20.18967731 27.49154954 27.25540014 19.24994647
10.21846781 1.93885474 0. 7.28280909]
[ 8.34541481 9.29953014 13.40020548 20.48281039 19.97465594 12.24773666
6.08016463 9.20353461 7.28280909 0. ]]


Plot:



enter image description here










share|improve this question











$endgroup$







  • 1




    $begingroup$
    Welcome to Code Review and congratulations on writing a decent question on your first try.
    $endgroup$
    – Mast
    Nov 12 '18 at 9:10






  • 2




    $begingroup$
    This is a great first question, I hope you get some good feedback.
    $endgroup$
    – esote
    Nov 12 '18 at 14:54






  • 2




    $begingroup$
    If you want your code to be error prone, you actually want it so it is easy to have bugs. You maybe meant error proof?
    $endgroup$
    – Graipher
    Nov 12 '18 at 15:55






  • 1




    $begingroup$
    Well spotted @Graipher! Seems like I want suggestions on how to actually include bugs in my code. Thanks for pointing out, will edit.
    $endgroup$
    – Rrz0
    Nov 12 '18 at 16:11














6












6








6





$begingroup$


I am extracting 10 lat/long points from Google Maps and placing these into a text file. The program should be able to read in the text file, calculate the haversine distance between each point, and store in an adjacency matrix. The adjacency matrix will eventually be fed to a 2-opt algorithm, which is outside the scope of the code I am about to present.



The following code is functional, but extremely inefficient. If I had 1000 points instead of 10, the adjacency matrix would need 1000 x 1000 iterations to be filled. Can this be optimized?



import csv
from haversine import haversine
import matplotlib.pyplot as plt
import numpy as np


def read_two_column_file(file_name):
with open(file_name, 'r') as f_input:
csv_input = csv.reader(f_input, delimiter=' ', skipinitialspace=True, )
long = []
lat = []
for col in csv_input:
x = float(col[0]) # converting to float
y = float(col[1])
long.append(x)
lat.append(y)

return long, lat


def display_points(long, lat):
plt.figure()
plt.ylabel('longitude')
plt.xlabel('latitude')
plt.title('longitude vs latitude')
plt.scatter(long, lat)
plt.show()


def main():
long, lat = read_two_column_file('latlong.txt')

points = []
for i in range(len(lat)):
coords = tuple([lat[i], long[i]]) # converting to tuple to be able to perform haverine calc.
points.append(coords)

hav = []
for i in range(len(lat)):
for j in range(len(long)):
hav.append(haversine(points[i], points[j]))

np.asarray(hav)
adj_matrix = np.reshape(hav, (10, 10)) # reshaping to 10 x 10 matrix
print(adj_matrix)

display_points(long, lat)


main()


Sample Input:



35.905333, 14.471970
35.896389, 14.477780
35.901281, 14.518173
35.860491, 14.572245
35.807607, 14.535320
35.832267, 14.455894
35.882414, 14.373217
35.983794, 14.336096
35.974463, 14.351006
35.930951, 14.401137


Sample Output:



[[ 0. 1.15959635 5.15603243 12.15003864 12.66090817 8.06760374
11.25481465 17.31108648 15.37358741 8.34541481]
[ 1.15959635 0. 4.52227294 11.19223786 11.50131214 7.32033758
11.72388583 18.35259685 16.41378987 9.29953014]
[ 5.15603243 4.52227294 0. 7.44480948 10.26177912 10.15688933
16.24592213 22.1101544 20.18967731 13.40020548]
[12.15003864 11.19223786 7.44480948 0. 7.01813758 13.28961044
22.25645098 29.42422794 27.49154954 20.48281039]
[12.66090817 11.50131214 10.26177912 7.01813758 0. 9.22215871
19.74293886 29.16680205 27.25540014 19.97465594]
[ 8.06760374 7.32033758 10.15688933 13.28961044 9.22215871 0.
10.66219491 21.06632671 19.24994647 12.24773666]
[11.25481465 11.72388583 16.24592213 22.25645098 19.74293886 10.66219491
0. 11.67502344 10.21846781 6.08016463]
[17.31108648 18.35259685 22.1101544 29.42422794 29.16680205 21.06632671
11.67502344 0. 1.93885474 9.20353461]
[15.37358741 16.41378987 20.18967731 27.49154954 27.25540014 19.24994647
10.21846781 1.93885474 0. 7.28280909]
[ 8.34541481 9.29953014 13.40020548 20.48281039 19.97465594 12.24773666
6.08016463 9.20353461 7.28280909 0. ]]


Plot:



enter image description here










share|improve this question











$endgroup$




I am extracting 10 lat/long points from Google Maps and placing these into a text file. The program should be able to read in the text file, calculate the haversine distance between each point, and store in an adjacency matrix. The adjacency matrix will eventually be fed to a 2-opt algorithm, which is outside the scope of the code I am about to present.



The following code is functional, but extremely inefficient. If I had 1000 points instead of 10, the adjacency matrix would need 1000 x 1000 iterations to be filled. Can this be optimized?



import csv
from haversine import haversine
import matplotlib.pyplot as plt
import numpy as np


def read_two_column_file(file_name):
with open(file_name, 'r') as f_input:
csv_input = csv.reader(f_input, delimiter=' ', skipinitialspace=True, )
long = []
lat = []
for col in csv_input:
x = float(col[0]) # converting to float
y = float(col[1])
long.append(x)
lat.append(y)

return long, lat


def display_points(long, lat):
plt.figure()
plt.ylabel('longitude')
plt.xlabel('latitude')
plt.title('longitude vs latitude')
plt.scatter(long, lat)
plt.show()


def main():
long, lat = read_two_column_file('latlong.txt')

points = []
for i in range(len(lat)):
coords = tuple([lat[i], long[i]]) # converting to tuple to be able to perform haverine calc.
points.append(coords)

hav = []
for i in range(len(lat)):
for j in range(len(long)):
hav.append(haversine(points[i], points[j]))

np.asarray(hav)
adj_matrix = np.reshape(hav, (10, 10)) # reshaping to 10 x 10 matrix
print(adj_matrix)

display_points(long, lat)


main()


Sample Input:



35.905333, 14.471970
35.896389, 14.477780
35.901281, 14.518173
35.860491, 14.572245
35.807607, 14.535320
35.832267, 14.455894
35.882414, 14.373217
35.983794, 14.336096
35.974463, 14.351006
35.930951, 14.401137


Sample Output:



[[ 0. 1.15959635 5.15603243 12.15003864 12.66090817 8.06760374
11.25481465 17.31108648 15.37358741 8.34541481]
[ 1.15959635 0. 4.52227294 11.19223786 11.50131214 7.32033758
11.72388583 18.35259685 16.41378987 9.29953014]
[ 5.15603243 4.52227294 0. 7.44480948 10.26177912 10.15688933
16.24592213 22.1101544 20.18967731 13.40020548]
[12.15003864 11.19223786 7.44480948 0. 7.01813758 13.28961044
22.25645098 29.42422794 27.49154954 20.48281039]
[12.66090817 11.50131214 10.26177912 7.01813758 0. 9.22215871
19.74293886 29.16680205 27.25540014 19.97465594]
[ 8.06760374 7.32033758 10.15688933 13.28961044 9.22215871 0.
10.66219491 21.06632671 19.24994647 12.24773666]
[11.25481465 11.72388583 16.24592213 22.25645098 19.74293886 10.66219491
0. 11.67502344 10.21846781 6.08016463]
[17.31108648 18.35259685 22.1101544 29.42422794 29.16680205 21.06632671
11.67502344 0. 1.93885474 9.20353461]
[15.37358741 16.41378987 20.18967731 27.49154954 27.25540014 19.24994647
10.21846781 1.93885474 0. 7.28280909]
[ 8.34541481 9.29953014 13.40020548 20.48281039 19.97465594 12.24773666
6.08016463 9.20353461 7.28280909 0. ]]


Plot:



enter image description here







python performance beginner coordinate-system






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 9:22







Rrz0

















asked Nov 12 '18 at 7:25









Rrz0Rrz0

1636




1636







  • 1




    $begingroup$
    Welcome to Code Review and congratulations on writing a decent question on your first try.
    $endgroup$
    – Mast
    Nov 12 '18 at 9:10






  • 2




    $begingroup$
    This is a great first question, I hope you get some good feedback.
    $endgroup$
    – esote
    Nov 12 '18 at 14:54






  • 2




    $begingroup$
    If you want your code to be error prone, you actually want it so it is easy to have bugs. You maybe meant error proof?
    $endgroup$
    – Graipher
    Nov 12 '18 at 15:55






  • 1




    $begingroup$
    Well spotted @Graipher! Seems like I want suggestions on how to actually include bugs in my code. Thanks for pointing out, will edit.
    $endgroup$
    – Rrz0
    Nov 12 '18 at 16:11













  • 1




    $begingroup$
    Welcome to Code Review and congratulations on writing a decent question on your first try.
    $endgroup$
    – Mast
    Nov 12 '18 at 9:10






  • 2




    $begingroup$
    This is a great first question, I hope you get some good feedback.
    $endgroup$
    – esote
    Nov 12 '18 at 14:54






  • 2




    $begingroup$
    If you want your code to be error prone, you actually want it so it is easy to have bugs. You maybe meant error proof?
    $endgroup$
    – Graipher
    Nov 12 '18 at 15:55






  • 1




    $begingroup$
    Well spotted @Graipher! Seems like I want suggestions on how to actually include bugs in my code. Thanks for pointing out, will edit.
    $endgroup$
    – Rrz0
    Nov 12 '18 at 16:11








1




1




$begingroup$
Welcome to Code Review and congratulations on writing a decent question on your first try.
$endgroup$
– Mast
Nov 12 '18 at 9:10




$begingroup$
Welcome to Code Review and congratulations on writing a decent question on your first try.
$endgroup$
– Mast
Nov 12 '18 at 9:10




2




2




$begingroup$
This is a great first question, I hope you get some good feedback.
$endgroup$
– esote
Nov 12 '18 at 14:54




$begingroup$
This is a great first question, I hope you get some good feedback.
$endgroup$
– esote
Nov 12 '18 at 14:54




2




2




$begingroup$
If you want your code to be error prone, you actually want it so it is easy to have bugs. You maybe meant error proof?
$endgroup$
– Graipher
Nov 12 '18 at 15:55




$begingroup$
If you want your code to be error prone, you actually want it so it is easy to have bugs. You maybe meant error proof?
$endgroup$
– Graipher
Nov 12 '18 at 15:55




1




1




$begingroup$
Well spotted @Graipher! Seems like I want suggestions on how to actually include bugs in my code. Thanks for pointing out, will edit.
$endgroup$
– Rrz0
Nov 12 '18 at 16:11





$begingroup$
Well spotted @Graipher! Seems like I want suggestions on how to actually include bugs in my code. Thanks for pointing out, will edit.
$endgroup$
– Rrz0
Nov 12 '18 at 16:11











2 Answers
2






active

oldest

votes


















2












$begingroup$

More than half of your code is being used to convert from one data format to another (from two lat and long list to tuples and then from a list of lists to an array).



The easiest to understand version would be to use numpy.loadtxt:



def read_two_column_file(file_name):
return np.loadtxt(file_name, delimiter=", ")


This is then a 2D numpy.array. However, this is actually a lot slower than it could be, so you could also use pandas.read_csv instead:



import pandas as pd

def read_two_column_file(file_name):
return pd.read_csv(file_name, header=None).values


Which one is faster depends on the size of your file.



Now we need to modify the display_points function to work with this new data format:



def display_points(points):
plt.figure()
plt.ylabel('longitude')
plt.xlabel('latitude')
plt.title('longitude vs latitude')
plt.scatter(points[:, 0], points[:, 1])
plt.show()


Now for the actual calculation. First, you can use itertools.combinations_with_replacement to get all pairs of points. Then you can insert them directly into the correct row of an array:



from itertools import combinations_with_replacement

def main():

points = read_two_column_file(file_name)

adj_matrix = np.empty(len(points)**2)
for i, (point1, point2) in enumerate(combinations_with_replacement(points, 2)):
adj_matrix[i] = haversine(point1, point2)
adj_matrix.reshape((len(points), len(points))

print(adj_matrix)
display_points(points)


This can probably be further improved by using numpy.meshgrid to get the combinations of points and using a vectorized version of the haversine function.






share|improve this answer









$endgroup$












  • $begingroup$
    Thanks for the constructive feedback. Will get back after I look into all suggestions and try to implement.
    $endgroup$
    – Rrz0
    Nov 12 '18 at 17:51






  • 1




    $begingroup$
    For some reason I'm getting an error on Line 28 being: adj_matrix[i] = haversine(point1, point2). ValueError: not enough values to unpack (expected 2, got 1) I haven't yet found what's wrong.
    $endgroup$
    – Rrz0
    Nov 12 '18 at 18:36











  • $begingroup$
    @Rrz0 That sounds odd. Will also investigate when I get home later.
    $endgroup$
    – Graipher
    Nov 12 '18 at 18:43










  • $begingroup$
    I suspect tuple unpacking gone wrong, somewhere.
    $endgroup$
    – Mast
    Nov 13 '18 at 10:29










  • $begingroup$
    @Rrz0: I just tested it again, it works on my machine with Python 3.6.3. Did you maybe not replace the read_two_column_file function?
    $endgroup$
    – Graipher
    Nov 13 '18 at 13:22


















0












$begingroup$

I am trying to Extract an adjacency matrix distance from points on a map (latitude and longitude) in Python, can you share the optimized code?






share|improve this answer








New contributor




diego Marshall is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207461%2fextracting-an-adjaceny-matrix-containing-haversine-distance-from-points-on-map%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2












    $begingroup$

    More than half of your code is being used to convert from one data format to another (from two lat and long list to tuples and then from a list of lists to an array).



    The easiest to understand version would be to use numpy.loadtxt:



    def read_two_column_file(file_name):
    return np.loadtxt(file_name, delimiter=", ")


    This is then a 2D numpy.array. However, this is actually a lot slower than it could be, so you could also use pandas.read_csv instead:



    import pandas as pd

    def read_two_column_file(file_name):
    return pd.read_csv(file_name, header=None).values


    Which one is faster depends on the size of your file.



    Now we need to modify the display_points function to work with this new data format:



    def display_points(points):
    plt.figure()
    plt.ylabel('longitude')
    plt.xlabel('latitude')
    plt.title('longitude vs latitude')
    plt.scatter(points[:, 0], points[:, 1])
    plt.show()


    Now for the actual calculation. First, you can use itertools.combinations_with_replacement to get all pairs of points. Then you can insert them directly into the correct row of an array:



    from itertools import combinations_with_replacement

    def main():

    points = read_two_column_file(file_name)

    adj_matrix = np.empty(len(points)**2)
    for i, (point1, point2) in enumerate(combinations_with_replacement(points, 2)):
    adj_matrix[i] = haversine(point1, point2)
    adj_matrix.reshape((len(points), len(points))

    print(adj_matrix)
    display_points(points)


    This can probably be further improved by using numpy.meshgrid to get the combinations of points and using a vectorized version of the haversine function.






    share|improve this answer









    $endgroup$












    • $begingroup$
      Thanks for the constructive feedback. Will get back after I look into all suggestions and try to implement.
      $endgroup$
      – Rrz0
      Nov 12 '18 at 17:51






    • 1




      $begingroup$
      For some reason I'm getting an error on Line 28 being: adj_matrix[i] = haversine(point1, point2). ValueError: not enough values to unpack (expected 2, got 1) I haven't yet found what's wrong.
      $endgroup$
      – Rrz0
      Nov 12 '18 at 18:36











    • $begingroup$
      @Rrz0 That sounds odd. Will also investigate when I get home later.
      $endgroup$
      – Graipher
      Nov 12 '18 at 18:43










    • $begingroup$
      I suspect tuple unpacking gone wrong, somewhere.
      $endgroup$
      – Mast
      Nov 13 '18 at 10:29










    • $begingroup$
      @Rrz0: I just tested it again, it works on my machine with Python 3.6.3. Did you maybe not replace the read_two_column_file function?
      $endgroup$
      – Graipher
      Nov 13 '18 at 13:22















    2












    $begingroup$

    More than half of your code is being used to convert from one data format to another (from two lat and long list to tuples and then from a list of lists to an array).



    The easiest to understand version would be to use numpy.loadtxt:



    def read_two_column_file(file_name):
    return np.loadtxt(file_name, delimiter=", ")


    This is then a 2D numpy.array. However, this is actually a lot slower than it could be, so you could also use pandas.read_csv instead:



    import pandas as pd

    def read_two_column_file(file_name):
    return pd.read_csv(file_name, header=None).values


    Which one is faster depends on the size of your file.



    Now we need to modify the display_points function to work with this new data format:



    def display_points(points):
    plt.figure()
    plt.ylabel('longitude')
    plt.xlabel('latitude')
    plt.title('longitude vs latitude')
    plt.scatter(points[:, 0], points[:, 1])
    plt.show()


    Now for the actual calculation. First, you can use itertools.combinations_with_replacement to get all pairs of points. Then you can insert them directly into the correct row of an array:



    from itertools import combinations_with_replacement

    def main():

    points = read_two_column_file(file_name)

    adj_matrix = np.empty(len(points)**2)
    for i, (point1, point2) in enumerate(combinations_with_replacement(points, 2)):
    adj_matrix[i] = haversine(point1, point2)
    adj_matrix.reshape((len(points), len(points))

    print(adj_matrix)
    display_points(points)


    This can probably be further improved by using numpy.meshgrid to get the combinations of points and using a vectorized version of the haversine function.






    share|improve this answer









    $endgroup$












    • $begingroup$
      Thanks for the constructive feedback. Will get back after I look into all suggestions and try to implement.
      $endgroup$
      – Rrz0
      Nov 12 '18 at 17:51






    • 1




      $begingroup$
      For some reason I'm getting an error on Line 28 being: adj_matrix[i] = haversine(point1, point2). ValueError: not enough values to unpack (expected 2, got 1) I haven't yet found what's wrong.
      $endgroup$
      – Rrz0
      Nov 12 '18 at 18:36











    • $begingroup$
      @Rrz0 That sounds odd. Will also investigate when I get home later.
      $endgroup$
      – Graipher
      Nov 12 '18 at 18:43










    • $begingroup$
      I suspect tuple unpacking gone wrong, somewhere.
      $endgroup$
      – Mast
      Nov 13 '18 at 10:29










    • $begingroup$
      @Rrz0: I just tested it again, it works on my machine with Python 3.6.3. Did you maybe not replace the read_two_column_file function?
      $endgroup$
      – Graipher
      Nov 13 '18 at 13:22













    2












    2








    2





    $begingroup$

    More than half of your code is being used to convert from one data format to another (from two lat and long list to tuples and then from a list of lists to an array).



    The easiest to understand version would be to use numpy.loadtxt:



    def read_two_column_file(file_name):
    return np.loadtxt(file_name, delimiter=", ")


    This is then a 2D numpy.array. However, this is actually a lot slower than it could be, so you could also use pandas.read_csv instead:



    import pandas as pd

    def read_two_column_file(file_name):
    return pd.read_csv(file_name, header=None).values


    Which one is faster depends on the size of your file.



    Now we need to modify the display_points function to work with this new data format:



    def display_points(points):
    plt.figure()
    plt.ylabel('longitude')
    plt.xlabel('latitude')
    plt.title('longitude vs latitude')
    plt.scatter(points[:, 0], points[:, 1])
    plt.show()


    Now for the actual calculation. First, you can use itertools.combinations_with_replacement to get all pairs of points. Then you can insert them directly into the correct row of an array:



    from itertools import combinations_with_replacement

    def main():

    points = read_two_column_file(file_name)

    adj_matrix = np.empty(len(points)**2)
    for i, (point1, point2) in enumerate(combinations_with_replacement(points, 2)):
    adj_matrix[i] = haversine(point1, point2)
    adj_matrix.reshape((len(points), len(points))

    print(adj_matrix)
    display_points(points)


    This can probably be further improved by using numpy.meshgrid to get the combinations of points and using a vectorized version of the haversine function.






    share|improve this answer









    $endgroup$



    More than half of your code is being used to convert from one data format to another (from two lat and long list to tuples and then from a list of lists to an array).



    The easiest to understand version would be to use numpy.loadtxt:



    def read_two_column_file(file_name):
    return np.loadtxt(file_name, delimiter=", ")


    This is then a 2D numpy.array. However, this is actually a lot slower than it could be, so you could also use pandas.read_csv instead:



    import pandas as pd

    def read_two_column_file(file_name):
    return pd.read_csv(file_name, header=None).values


    Which one is faster depends on the size of your file.



    Now we need to modify the display_points function to work with this new data format:



    def display_points(points):
    plt.figure()
    plt.ylabel('longitude')
    plt.xlabel('latitude')
    plt.title('longitude vs latitude')
    plt.scatter(points[:, 0], points[:, 1])
    plt.show()


    Now for the actual calculation. First, you can use itertools.combinations_with_replacement to get all pairs of points. Then you can insert them directly into the correct row of an array:



    from itertools import combinations_with_replacement

    def main():

    points = read_two_column_file(file_name)

    adj_matrix = np.empty(len(points)**2)
    for i, (point1, point2) in enumerate(combinations_with_replacement(points, 2)):
    adj_matrix[i] = haversine(point1, point2)
    adj_matrix.reshape((len(points), len(points))

    print(adj_matrix)
    display_points(points)


    This can probably be further improved by using numpy.meshgrid to get the combinations of points and using a vectorized version of the haversine function.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 '18 at 17:34









    GraipherGraipher

    27k54497




    27k54497











    • $begingroup$
      Thanks for the constructive feedback. Will get back after I look into all suggestions and try to implement.
      $endgroup$
      – Rrz0
      Nov 12 '18 at 17:51






    • 1




      $begingroup$
      For some reason I'm getting an error on Line 28 being: adj_matrix[i] = haversine(point1, point2). ValueError: not enough values to unpack (expected 2, got 1) I haven't yet found what's wrong.
      $endgroup$
      – Rrz0
      Nov 12 '18 at 18:36











    • $begingroup$
      @Rrz0 That sounds odd. Will also investigate when I get home later.
      $endgroup$
      – Graipher
      Nov 12 '18 at 18:43










    • $begingroup$
      I suspect tuple unpacking gone wrong, somewhere.
      $endgroup$
      – Mast
      Nov 13 '18 at 10:29










    • $begingroup$
      @Rrz0: I just tested it again, it works on my machine with Python 3.6.3. Did you maybe not replace the read_two_column_file function?
      $endgroup$
      – Graipher
      Nov 13 '18 at 13:22
















    • $begingroup$
      Thanks for the constructive feedback. Will get back after I look into all suggestions and try to implement.
      $endgroup$
      – Rrz0
      Nov 12 '18 at 17:51






    • 1




      $begingroup$
      For some reason I'm getting an error on Line 28 being: adj_matrix[i] = haversine(point1, point2). ValueError: not enough values to unpack (expected 2, got 1) I haven't yet found what's wrong.
      $endgroup$
      – Rrz0
      Nov 12 '18 at 18:36











    • $begingroup$
      @Rrz0 That sounds odd. Will also investigate when I get home later.
      $endgroup$
      – Graipher
      Nov 12 '18 at 18:43










    • $begingroup$
      I suspect tuple unpacking gone wrong, somewhere.
      $endgroup$
      – Mast
      Nov 13 '18 at 10:29










    • $begingroup$
      @Rrz0: I just tested it again, it works on my machine with Python 3.6.3. Did you maybe not replace the read_two_column_file function?
      $endgroup$
      – Graipher
      Nov 13 '18 at 13:22















    $begingroup$
    Thanks for the constructive feedback. Will get back after I look into all suggestions and try to implement.
    $endgroup$
    – Rrz0
    Nov 12 '18 at 17:51




    $begingroup$
    Thanks for the constructive feedback. Will get back after I look into all suggestions and try to implement.
    $endgroup$
    – Rrz0
    Nov 12 '18 at 17:51




    1




    1




    $begingroup$
    For some reason I'm getting an error on Line 28 being: adj_matrix[i] = haversine(point1, point2). ValueError: not enough values to unpack (expected 2, got 1) I haven't yet found what's wrong.
    $endgroup$
    – Rrz0
    Nov 12 '18 at 18:36





    $begingroup$
    For some reason I'm getting an error on Line 28 being: adj_matrix[i] = haversine(point1, point2). ValueError: not enough values to unpack (expected 2, got 1) I haven't yet found what's wrong.
    $endgroup$
    – Rrz0
    Nov 12 '18 at 18:36













    $begingroup$
    @Rrz0 That sounds odd. Will also investigate when I get home later.
    $endgroup$
    – Graipher
    Nov 12 '18 at 18:43




    $begingroup$
    @Rrz0 That sounds odd. Will also investigate when I get home later.
    $endgroup$
    – Graipher
    Nov 12 '18 at 18:43












    $begingroup$
    I suspect tuple unpacking gone wrong, somewhere.
    $endgroup$
    – Mast
    Nov 13 '18 at 10:29




    $begingroup$
    I suspect tuple unpacking gone wrong, somewhere.
    $endgroup$
    – Mast
    Nov 13 '18 at 10:29












    $begingroup$
    @Rrz0: I just tested it again, it works on my machine with Python 3.6.3. Did you maybe not replace the read_two_column_file function?
    $endgroup$
    – Graipher
    Nov 13 '18 at 13:22




    $begingroup$
    @Rrz0: I just tested it again, it works on my machine with Python 3.6.3. Did you maybe not replace the read_two_column_file function?
    $endgroup$
    – Graipher
    Nov 13 '18 at 13:22













    0












    $begingroup$

    I am trying to Extract an adjacency matrix distance from points on a map (latitude and longitude) in Python, can you share the optimized code?






    share|improve this answer








    New contributor




    diego Marshall is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    $endgroup$

















      0












      $begingroup$

      I am trying to Extract an adjacency matrix distance from points on a map (latitude and longitude) in Python, can you share the optimized code?






      share|improve this answer








      New contributor




      diego Marshall is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      $endgroup$















        0












        0








        0





        $begingroup$

        I am trying to Extract an adjacency matrix distance from points on a map (latitude and longitude) in Python, can you share the optimized code?






        share|improve this answer








        New contributor




        diego Marshall is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        $endgroup$



        I am trying to Extract an adjacency matrix distance from points on a map (latitude and longitude) in Python, can you share the optimized code?







        share|improve this answer








        New contributor




        diego Marshall is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        diego Marshall is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 13 mins ago









        diego Marshalldiego Marshall

        1




        1




        New contributor




        diego Marshall is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        diego Marshall is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        diego Marshall is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207461%2fextracting-an-adjaceny-matrix-containing-haversine-distance-from-points-on-map%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

            名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

            Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar