Question

I am calculating satellite coverage footprints on the surface of an ellipsoid and my function returns a list of Lat / Lons that looks like below. It is a python list of dictionaries. Now if I scatter plot this in Matlab or Matplotlib I get a nice 3D footprint of disconnected points.. However, I want to be able to use the plot (not scatter) command to create a smooth 3D circle to represent the footprint. In order to do this I need to sort them somehow. I've tried Haversine (greatest circle distance) to find the nearest neighbor for each point but this still gives me the occasional disconnected line when I have a larger set of values (GEO sats). I've also attempted to split the values into N and S latitudes when the footprint straddles the equator, and then sort by longitude. I'm probably missing something - Does anyone have a better/faster idea to sort a list like this so that if I connect all sequential points in the list I would get a complete, ordered, circle?

latLons = [{'lat': -33.783781327, 'lon': 137.47747747700001}, {'lat': -33.783781326899998, 'lon': 139.63963964000001}, {'lat': -33.603601166200001, 'lon': 136.03603603600001}, {'lat': -33.423421005500003, 'lon': 134.59459459499999}, {'lat': -32.882880523399997, 'lon': 132.43243243200001}, {'lat': -32.522520202199999, 'lon': 131.71171171200001}, {'lat': -32.342340041600004, 'lon': 145.40540540500001}, {'lat': -31.261259078399998, 'lon': 147.56756756799999}, {'lat': -31.081078917799999, 'lon': 128.828828829}, {'lat': -29.459457473099999, 'lon': 126.666666667}, {'lat': -28.558556670200002, 'lon': 125.94594594599999}, {'lat': -27.657655866700001, 'lon': 125.225225225}, {'lat': -26.936935223300001, 'lon': 151.89189189199999}, {'lat': -26.7567550624, 'lon': 124.504504504}, {'lat': -25.6756740961, 'lon': 152.61261261300001}, {'lat': -25.3153137736, 'lon': 123.78378378399999}, {'lat': -23.873872481599999, 'lon': 153.33333333300001}, {'lat': -23.333331995999998, 'lon': 123.063063063}, {'lat': -19.3693684138, 'lon': 154.05405405400001}, {'lat': -15.765765115600001, 'lon': 123.063063063}, {'lat': -15.2252246167, 'lon': 153.33333333300001}, {'lat': -13.243242777300001, 'lon': 152.61261261300001}, {'lat': -12.162161767000001, 'lon': 124.504504505}, {'lat': -11.801801428999999, 'lon': 151.89189189199999}, {'lat': -10.9009005815, 'lon': 125.225225225}, {'lat': -8.1981980155999992, 'lon': 149.00900900900001}, {'lat': -6.9369368056800003, 'lon': 147.56756756799999}, {'lat': -6.5765764584799999, 'lon': 129.54954954999999}, {'lat': -6.5765764584799999, 'lon': 146.84684684699999}, {'lat': -5.6756755875199998, 'lon': 130.99099099099999}, {'lat': -4.7747747122700002, 'lon': 143.24324324299999}, {'lat': -4.23423418502, 'lon': 141.08108108100001}, {'lat': -3.8738738326600002, 'lon': 138.198198198}]
Was it helpful?

Solution

One approach would be representing your points in polar coordinates with respect to the center point and using the angle as the sorting key.

Here is a simple implementation:

import matplotlib.pyplot as plt
import math

def polar_sort(l):
    x, y = zip(*((c['lat'], c['lon']) for c in l))
    ave_x = float(sum(x))/len(x)
    ave_y = float(sum(y))/len(y)

    return sorted(l, key=lambda c: math.atan2(c['lat']-ave_x, c['lon']-ave_y))

latLons = [{'lat': -33.783781327, 'lon': 137.47747747700001}, {'lat': -33.783781326899998, 'lon': 139.63963964000001}, {'lat': -33.603601166200001, 'lon': 136.03603603600001}, {'lat': -33.423421005500003, 'lon': 134.59459459499999}, {'lat': -32.882880523399997, 'lon': 132.43243243200001}, {'lat': -32.522520202199999, 'lon': 131.71171171200001}, {'lat': -32.342340041600004, 'lon': 145.40540540500001}, {'lat': -31.261259078399998, 'lon': 147.56756756799999}, {'lat': -31.081078917799999, 'lon': 128.828828829}, {'lat': -29.459457473099999, 'lon': 126.666666667}, {'lat': -28.558556670200002, 'lon': 125.94594594599999}, {'lat': -27.657655866700001, 'lon': 125.225225225}, {'lat': -26.936935223300001, 'lon': 151.89189189199999}, {'lat': -26.7567550624, 'lon': 124.504504504}, {'lat': -25.6756740961, 'lon': 152.61261261300001}, {'lat': -25.3153137736, 'lon': 123.78378378399999}, {'lat': -23.873872481599999, 'lon': 153.33333333300001}, {'lat': -23.333331995999998, 'lon': 123.063063063}, {'lat': -19.3693684138, 'lon': 154.05405405400001}, {'lat': -15.765765115600001, 'lon': 123.063063063}, {'lat': -15.2252246167, 'lon': 153.33333333300001}, {'lat': -13.243242777300001, 'lon': 152.61261261300001}, {'lat': -12.162161767000001, 'lon': 124.504504505}, {'lat': -11.801801428999999, 'lon': 151.89189189199999}, {'lat': -10.9009005815, 'lon': 125.225225225}, {'lat': -8.1981980155999992, 'lon': 149.00900900900001}, {'lat': -6.9369368056800003, 'lon': 147.56756756799999}, {'lat': -6.5765764584799999, 'lon': 129.54954954999999}, {'lat': -6.5765764584799999, 'lon': 146.84684684699999}, {'lat': -5.6756755875199998, 'lon': 130.99099099099999}, {'lat': -4.7747747122700002, 'lon': 143.24324324299999}, {'lat': -4.23423418502, 'lon': 141.08108108100001}, {'lat': -3.8738738326600002, 'lon': 138.198198198}]

x,y = zip(*((c['lat'], c['lon']) for c in polar_sort(latLons)))

plt.plot(x,y)
plt.show()

enter image description here

OTHER TIPS

I don't think a regular sort is what you need. In a regular sort you use an absolute order between items. Here you don't have an absolute order (what's the "first" coordinate?), only a relative one.

First I'd get the data out of dictionaries and into a list of tuples:

latlonslist = [ (x['lat'],x['lon']) for x in latLons ]

Then import scipy.spatial and use a distance of your choice for finding the nearest neighbour to each point. You can also use euclidean distance without resorting to scipy, of course.

Compute all possible distances (should be n^2 ops) with something like this:

distances = {}  
for n1 in latlonslist:
  for n2 in latlonslist:
    if n1 == n2:
      continue
    thisdist = scipy.spatial.distance.euclidean(n1,n2)
    distances[n1,n2] = thisdist

Then walk the node list starting from an arbitrary node, looking for the nearest node in each step.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top