-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_display.py
More file actions
119 lines (90 loc) · 3.55 KB
/
map_display.py
File metadata and controls
119 lines (90 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from scipy.spatial import ConvexHull
from matplotlib.path import Path
import matplotlib.patches as patches
def stuff(readings, refs, points, anchor_to):
angle = np.pi * readings['azim'].values / 180
dist = readings['hdist'].values
all_points = pd.concat((refs, points))
ns = np.hstack((refs['ns'], points['ns']))
ew = np.hstack((refs['ew'], points['ew']))
from_readings = all_points.loc[readings['from']]
nsf = from_readings['ns']
ewf = from_readings['ew']
to_readings = all_points.loc[readings['to']]
nst = to_readings['ns']
ewt = to_readings['ew']
dns = dist * np.cos(angle)
dew = dist * np.sin(angle)
if anchor_to:
return (nst - dns, ewt - dew, nst, ewt)
else:
return (nsf, ewf, nsf + dns, ewf + dew)
def unique_rows(array):
"""
Taken blindly from http://stackoverflow.com/questions/16970982/find-unique-rows-in-numpy-array
"""
b = np.ascontiguousarray(array).view(np.dtype((np.void, array.dtype.itemsize * array.shape[1])))
_, index = np.unique(b, return_index=True)
return array[index]
def show_hull(points, ax):
"""Add a polygon to a plot that contains all the points.
This seems like a lot of work...
"""
try:
hull = ConvexHull(points)
vertices = hull.vertices
vertices = np.append(vertices, vertices[0])
codes = [Path.LINETO] * len(vertices)
codes[0] = Path.MOVETO
codes[-1] = Path.CLOSEPOLY
path = Path(points[vertices], codes)
patch = patches.PathPatch(path, facecolor='orange', lw=0, alpha=0.3)
ax.add_patch(patch)
except:
print "oh well ..."
def show_map(fig, points, readings=None, focus_points=None):
if focus_points is None:
focus_points = []
refs = points[points['type'] == 'anchor']
est = points[points['type'] != 'anchor']
ax = fig.add_subplot(111, aspect='equal')
ax.set_xlabel('meters west of origin')
ax.set_ylabel('meters north of origin')
if readings is not None:
for name in est.index:
pts = est.loc[name]
rf = readings[readings['from'] == name]
rt = readings[readings['to'] == name]
nsf, ewf, nsd, ewd = stuff(rf, refs, est, anchor_to=True)
nso, ewo, nst, ewt = stuff(rt, refs, est, anchor_to=False)
f_points = np.array([ewf, nsf]).T
t_points = np.array([ewt, nst]).T
a_points = np.append(f_points, t_points, axis=0)
if len(a_points) > 2:
show_hull(a_points, ax)
ax.scatter(ewf, nsf, marker='.', c='g', linewidths=0)
ax.scatter(ewt, nst, marker='.', c='r', linewidths=0)
if name in focus_points:
plt.plot([ewf, ewd], [nsf, nsd], 'y-')
plt.plot([ewo, ewt], [nso, nst], 'b-')
plt.scatter(refs['ew'], refs['ns'], marker='^', c='g', s=100)
ns = refs['ns']
ew = refs['ew']
names = refs.index
for i in range(len(ns)):
plt.annotate(
names[i], xytext = (0, -20),
textcoords = 'offset points', ha = 'center', va = 'bottom',
xy = (ew[i], ns[i]))
plt.scatter(est['ew'], est['ns'], marker='o', c='r', s = 20)
ns = est['ns']
ew = est['ew']
names = est.index
for i in range(len(ns)):
plt.annotate(
names[i], xytext = (0, -20),
textcoords = 'offset points', ha = 'center', va = 'bottom',
xy = (ew[i], ns[i]))