-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpsjsonplot-pos.py
More file actions
executable file
·137 lines (98 loc) · 4.25 KB
/
gpsjsonplot-pos.py
File metadata and controls
executable file
·137 lines (98 loc) · 4.25 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import matplotlib.pyplot as plt
import numpy as np
# from datetime import datetime
import seaborn as sns
import argparse
from datetime import datetime
# -------------------------------------------------------- #
parser = argparse.ArgumentParser(description="Process some integers.")
parser.add_argument("--json", dest="jsonfile", required=True, help="input json file")
parser.add_argument("--outfile", dest="outfile", required=True, help="output image file")
parser.add_argument("--n", dest="n", default=50, type=int, help="sample n points; n=0 - get all points")
parser.add_argument("--contour", dest="contour", default=6, type=int, help="number of contour lines, 0-disables")
args = parser.parse_args()
jsonfile = args.jsonfile
outfile = args.outfile
n = args.n
contour = args.contour
# -------------------------------------------------------- #
def read_uniform_sample(jsonfile, n):
data = []
with open(jsonfile, "r") as file:
# Read all lines from the file
all_lines = file.readlines()
if n == 0:
# If n is 0, read all lines
n = len(all_lines)
step_size = max(len(all_lines) // n, 1)
for i in range(0, len(all_lines), step_size):
line = all_lines[i]
try:
json_data = json.loads(line)
# Check if the "class" is "TPV"
if json_data.get("class") == "TPV":
data.append(json_data)
except json.decoder.JSONDecodeError as e:
print(f"Error decoding JSON at line {i + 1}: {e}")
# print("Problematic JSON:", line.strip())
return data
data = read_uniform_sample(jsonfile, n)
# -------------------------------------------------------- #
# Extract relevant data
latitude = [entry["lat"] for entry in data]
longitude = [entry["lon"] for entry in data]
altitude = [entry["alt"] for entry in data]
points = len(latitude)
# Calculate mean latitude and longitude
mean_latitude = np.mean(latitude)
mean_longitude = np.mean(longitude)
# Convert latitude and longitude to meters
latitude_meters = (latitude - mean_latitude) * 111000 # Approximate 1 degree = 111000 meters
longitude_meters = (longitude - mean_longitude) * 111000 * np.cos(np.radians(mean_latitude))
# mean_altitude = np.mean(altitude)
std_altitude = np.std(altitude)
std_latitude = np.std(latitude_meters)
std_longitude = np.std(longitude_meters)
# Calculate maximum absolute values for latitude and longitude
max_abs_latitude = max(np.abs(latitude_meters))
max_abs_longitude = max(np.abs(longitude_meters))
# Set explicit limits for the axes based on maximum absolute values
lim = np.ceil(max(max_abs_longitude, max_abs_latitude))
# Convert mean latitude and longitude to meters
mean_latitude_meters = 0
mean_longitude_meters = 0
# ------------------------------------------------------------------ #
# Create a joint plot with Seaborn
# sns.set(style="white", color_codes=True)
g = sns.jointplot(
x=longitude_meters,
y=latitude_meters,
kind="scatter",
color="silver",
edgecolor="black", # for edge color
linewidth=0.3,
s=10,
xlim=(-lim, lim),
ylim=(-lim, lim),
height=4.6,
ratio=4,
marginal_kws=dict(fill=True, log_scale=False, color="silver", stat="density"),
)
g.plot_joint(sns.kdeplot, color="r", zorder=5, levels=contour)
# g.plot_marginals(sns.rugplot, color="r", height=-.15, clip_on=False)
g.plot_marginals(sns.kdeplot, color="silver", fill=True)
g.set_axis_labels("Longitude (meters from mean)", "Latitude (meters from mean)")
# Plotting mean location as a red dot
plt.scatter(0, 0, color="red", marker="x", s=100, label="Mean Location")
# Display mean and std in the top right corner
text_str = f"{points:} points. STD: alt: {std_altitude:.2f}m lat: {std_latitude:.2f}m, lon: {std_longitude:.2f}m"
# Add current date and time outside the plotting area (bottom-left)
current_datetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
footnote = f"{current_datetime:} | {text_str:}"
plt.text(0.9, -0.05, footnote, fontsize=6, family='monospace', horizontalalignment='right', transform=plt.gcf().transFigure)
# -------------------------------------------------------- #
# Save the plot to a file
plt.savefig(outfile, bbox_inches="tight")