i have around 100 list of gps coordinates , want draw line each of lists make.
one of lists plotted using scatter, this:

clearly there's line there;
i tried several methods sort gps positions , plot them:
lats = [] lngs = [] open(filename) f: line in f: lat, lng = line.split("\t") lats.append(float(lat)) lngs.append(float(lng)) def sort_positions(position): return position[0]+position[1] positions= zip(lngs, lats) positions = sorted(poss, key=sort_positions) i, positionin enumerate(positions): lng, lat = position #plt.scatter(lng, lat) try: n_lng, n_lat = positions[i+1] plt.plot((lng, n_lng),(lat, n_lat), "b") except indexerror: pass plt.show() sorting longitude
def sort_positions(position): return position[0] 
sorting latitude
def sort_positions(position): return position[1] 
sorting summing both
def sort_positions(position): return position[0]+position[1] 
if line straight in 1 of axis(latitude/longitude), plots fine (some minor bumps still showing)
here's 1 of lists plot fine, sorting latitude.

i plot if distance between 2 points lesser 200~500 meters, endup holes in lines, since there's missing data.

probably i'm doing wrong. know how plot lines properly?
edit:
in response rth answer:

the blue line using approach in questions, red 1 using method in other answer.
ignoring fact red closing loop.
there limitations in both, first, red 1 can't handle many points, had to use 1/90 of points both, blue 1 generates weird sharp turns when points clustered (the dark spots in image), while red 1 weird curves in places.
the easiest thing here figure out why gps coordinates mixed in first place , correct that.
if that's not possible, thing can think of iterative algorithm, takes xy point , decides based on criteria (e.g. distance, direction of consecutive points, etc) point should go next.
something along these lines,
import numpy np scipy.spatial.distance import pdist, squareform def find_gps_sorted(xy_coord, k0=0): """find iteratively continuous path given points xy_coord, starting point indexes k0 """ n = len(xy_coord) distance_matrix = squareform(pdist(xy_coord, metric='euclidean')) mask = np.ones(n, dtype='bool') sorted_order = np.zeros(n, dtype=np.int) indices = np.arange(n) = 0 k = k0 while true: sorted_order[i] = k mask[k] = false dist_k = distance_matrix[k][mask] indices_k = indices[mask] if not len(indices_k): break # find next unused closest point k = indices_k[np.argmin(dist_k)] # add criterion here on direction between consecutive points etc. += 1 return sorted_order, xy_coord[sorted_order] that use as,
xy_coord = np.random.randn(10, 2) sorted_order, xy_coord_sorted = find_gps_sorted(xy_coord, k0=0) 
although may need extended.
for instance in addition nearest distance criteria, don't want trajectory turn more 90° (if that's makes sense buses) , ignore points @ every iteration. essentially, can add enough constraints in algorithm result looking for.
edit: since gps coordinates has uncertainty, final step smooth resulting trace b-spline interpolation using scipy.interpolate.splprep , playing s argument ( see related questions (1), (2))
Comments
Post a Comment