Home > front end >  How to connect the points in such a way that a parallelogram is created
How to connect the points in such a way that a parallelogram is created

Time:09-17

I have four coordinates in my df table:

lat1 lon1 lat2 lon2 lat3 lon3 lat4 lon4
51.071833 6.237204 51.071836 6.237195 51.071833 6.237195 51.071836 6.237204

Based on this data, I try to connect the points in such a way that a parallelogram is created.

Scatter plot:

# selecting columns start with 'lat'
xx = df[[col for col in test if col.startswith('lat')]].stack().to_list() 
# selecting columns start with 'lon'
yy = df[[col for col in test if col.startswith('lon')]].stack().to_list() 

plt.scatter(xx,yy)

enter image description here

Unfortunatelly, when I try to connect points with lines, it doesn't work:

plt.scatter(xx,yy)
plt.plot(xx,yy)

enter image description here

Expected result:

enter image description here

I guess it's because the points are not ordered. I also tried this:

points = np.c_[xx, yy]

from sklearn.neighbors import NearestNeighbors
clf = NearestNeighbors(2).fit(points)
G = clf.kneighbors_graph()

import networkx as nx
T = nx.from_scipy_sparse_matrix(G)

order = list(nx.dfs_preorder_nodes(T, 0))

xx = xx[order]
yy = yy[order]

plt.plot(xx, yy)
plt.show()

But it is still incorrect.

Do you have idea where is the issue?

Thanks

CodePudding user response:

You can calculate the convex hull

  • Related