forked from pjhartzell/GPIV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
show_functions.py
152 lines (123 loc) · 6.59 KB
/
show_functions.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import rasterio
import rasterio.plot
import matplotlib.pyplot as plt
import numpy as np
import math
from matplotlib.patches import FancyArrow
from matplotlib.patches import Ellipse
from matplotlib.patches import Rectangle
import json
def show(image_file, vector_file, ellipse_file, vector_scale_factor, ellipse_scale_factor):
(image_array,image_geo_extents, image_geo_transform) = get_image_array(image_file)
(figure, axes) = plot_image(image_array, image_geo_extents)
if vector_file is not None:
if vector_scale_factor is None:
vector_scale_factor = 1
plot_vectors(axes, image_geo_extents, vector_file, vector_scale_factor)
if ellipse_file is not None:
if ellipse_scale_factor is None:
ellipse_scale_factor = 1
plot_ellipses(axes, image_geo_extents, ellipse_file, ellipse_scale_factor)
plt.show()
def get_image_array(image_file):
image_source = rasterio.open(image_file)
image_array = image_source.read(1)
image_geo_transform = np.reshape(np.asarray(image_source.transform), (3,3))
image_geo_extents = list(rasterio.plot.plotting_extent(image_source)) # [left, right, bottom, top]
image_source.close()
return image_array, image_geo_extents, image_geo_transform
def plot_image(image_array, image_geo_extents):
figure = plt.figure(figsize=(6,6))
axes = plt.gca()
image_data_min = min(np.percentile(image_array, 1), np.percentile(image_array, 1))
image_data_max = max(np.percentile(image_array, 99), np.percentile(image_array, 99))
plt.imshow(image_array,
cmap=plt.cm.gray,
extent=image_geo_extents,
vmin=image_data_min,
vmax=image_data_max)
return figure, axes
def plot_vectors(axes, image_geo_extents, vector_file, user_scale_factor):
with open(vector_file) as json_file:
origins_vectors = json.load(json_file)
origins_vectors_numpy = np.asarray(origins_vectors)
plot_width_in_pixels = axes.get_window_extent().width
plot_width_in_ground_units = image_geo_extents[1] - image_geo_extents[0]
pixels_per_ground_unit = plot_width_in_pixels / plot_width_in_ground_units
ground_units_per_pixel = plot_width_in_ground_units / plot_width_in_pixels
vector_lengths_ground = np.linalg.norm(origins_vectors_numpy[:,2:], axis=1)
vector_lengths_pixels = vector_lengths_ground * pixels_per_ground_unit
arrow_scale_factor = (30*ground_units_per_pixel) / np.median(vector_lengths_ground)
arrow_head_scale_factor = 8*ground_units_per_pixel
for i in range(len(origins_vectors)):
arrow = FancyArrow(
origins_vectors[i][0],
origins_vectors[i][1],
origins_vectors[i][2] * arrow_scale_factor * user_scale_factor,
-origins_vectors[i][3] * arrow_scale_factor * user_scale_factor, # Negative sign converts from dV (positive down) to dY (positive up)
length_includes_head=True,
head_width=arrow_head_scale_factor,
overhang=0.8,
fc='yellow',
ec = 'yellow'
)
axes.add_artist(arrow)
geo_height = image_geo_extents[3] - image_geo_extents[2]
legend_background = Rectangle((image_geo_extents[0] + geo_height/50, image_geo_extents[2] + geo_height/50),
geo_height/7, geo_height/7,
fc='silver', clip_on=False, alpha=0.75)
axes.add_artist(legend_background)
plt.text(image_geo_extents[0] + geo_height/50 + geo_height/14,
image_geo_extents[2] + geo_height/7,
'{0:.3f}'.format(np.median(vector_lengths_ground)/user_scale_factor),
horizontalalignment='center', verticalalignment='top')
arrow = FancyArrow(
image_geo_extents[0] + geo_height/50 + (geo_height/7 - 30*ground_units_per_pixel)/2,
image_geo_extents[2] + geo_height/14,
30*ground_units_per_pixel, 0,
length_includes_head=True, head_width=arrow_head_scale_factor,
overhang=0.8, fc='yellow', ec = 'yellow')
axes.add_artist(arrow)
def plot_ellipses(axes, image_geo_extents, ellipse_file, user_scale_factor):
with open(ellipse_file) as json_file:
locations_covariances = json.load(json_file)
plot_width_in_pixels = axes.get_window_extent().width
plot_width_in_ground_units = image_geo_extents[1] - image_geo_extents[0]
pixels_per_ground_unit = plot_width_in_pixels / plot_width_in_ground_units
ground_units_per_pixel = plot_width_in_ground_units / plot_width_in_pixels
semimajor_lengths_ground = []
semimajor_lengths_pixels = []
for i in range(len(locations_covariances)):
eigenvalues, eigenvectors = np.linalg.eig(locations_covariances[i][1])
max_index = np.argmax(eigenvalues)
semimajor_lengths_ground.append(math.sqrt(2.298*eigenvalues[max_index]))
ellipse_scale_factor = (20*ground_units_per_pixel) / np.median(semimajor_lengths_ground)
for i in range(len(locations_covariances)):
eigenvalues, eigenvectors = np.linalg.eig(locations_covariances[i][1])
max_index = np.argmax(eigenvalues)
min_index = np.argmin(eigenvalues)
semimajor = math.sqrt(2.298*eigenvalues[max_index])
semiminor = math.sqrt(2.298*eigenvalues[min_index])
my_angle = np.degrees(np.arctan(eigenvectors[max_index][1]/eigenvectors[max_index][0]))
ellipse = Ellipse(
(locations_covariances[i][0][0], locations_covariances[i][0][1]),
semimajor * ellipse_scale_factor * user_scale_factor,
semiminor * ellipse_scale_factor * user_scale_factor,
angle=my_angle,
fc='None',
ec='red'
)
axes.add_artist(ellipse)
geo_height = image_geo_extents[3] - image_geo_extents[2]
legend_background = Rectangle((image_geo_extents[0] + geo_height/50 + geo_height/7 + geo_height/50,
image_geo_extents[2] + geo_height/50),
geo_height/7, geo_height/7, fc='silver', clip_on=False, alpha=0.75)
axes.add_artist(legend_background)
plt.text(image_geo_extents[0] + geo_height/50 + geo_height/7 + geo_height/50 + geo_height/14,
image_geo_extents[2] + geo_height/7,
'{0:.3f}'.format(np.median(semimajor_lengths_ground)/user_scale_factor),
horizontalalignment='center', verticalalignment='top')
ell = Ellipse((image_geo_extents[0] + geo_height/50 + geo_height/7 + geo_height/50 + geo_height/14, image_geo_extents[2] + geo_height/14),
20*ground_units_per_pixel, 20*ground_units_per_pixel,
ec='red', fc='none', clip_on=False)
axes.add_artist(ell)