-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtripmap.py
76 lines (69 loc) · 2.99 KB
/
tripmap.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
import argparse
import sys
import pandas as pd
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from loguru import logger
from datamodels import Torqlogs, TorqFile
import matplotlib.pyplot as plt
from plotutils import MAP_CACHE, PLOT_DIR
from plotutils import plot_trip, combine_map_plot, download_maps
# x = latitude y = longitude !
def cli_main(args):
dburl = args.dburl # 'sqlite:///torqfiskur.db'
engine = create_engine(dburl, echo=False, connect_args={'check_same_thread': False})
Session = sessionmaker(bind=engine)
session = Session()
print(args)
if args.plotid: # plot a single trip
print(f'plotting {args.plotid}')
plot_trip(args.plotid, session)
sys.exit(0)
elif args.combine:
print(f'combiner {args.combine}')
combine_map_plot(args.combine[0], args.combine[1], args.combine[2])
# combine_map_plot(f'{MAP_CACHE}/tripmap-0001.png', f'{PLOT_DIR}/testplot1.png')
elif args.dlmaps: # download all maps from mapbox
download_maps(args, session)
sys.exit(0)
elif args.plotall: # make a plot of all trips - no maps
trips = [k.fileid for k in session.query(TorqFile.fileid).all()]
for idx,trip in enumerate(trips):
logger.debug(f'[{idx}/{len(trips)}] plotting {trip}')
pltfilename = f'{PLOT_DIR}/tripmap-{trip:04d}-plotly.png' # padding
# fileid = str(trips.iloc[0].values[0])
df = pd.DataFrame([k for k in session.query(Torqlogs.latitude, Torqlogs.longitude).filter(Torqlogs.fileid == trip).all()])
px = 1/plt.rcParams['figure.dpi'] # pixel in inches
fig,ax1 = plt.subplots(figsize=(800*px,600*px))
plt.axis('off')
df.plot(x="latitude", y="longitude", kind="scatter", ax=ax1, marker='.')
plt.savefig(pltfilename, transparent=True, dpi=100)
# plt.gcf()
# plt.show()
plt.close()
# fig = make_plt(df)
# plt.show()
# plt.savefig(pltfilename, transparent=True)
# plt.close()
logger.debug(f'[{idx}/{len(trips)}] saved {pltfilename}')
# fig = make_plotly(df)
# fig.write_image(pltfilename)
# fig.canvas.draw()
# buf = fig.canvas.buffer_rgba()
# width, height = fig.canvas.get_width_height()
# pil_image = Image.frombytes("RGB", (width, height), buf)
# Save PIL Image as PNG
# pil_image.save(pltfilename)
# cv2.imwrite(pltfilename, plt)
#
# plt.show(hold=False)
# plt.close(fig)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="plotmap")
parser.add_argument('-pi', '--plot-id', help="fileid to plot", action="store", dest='plotid')
parser.add_argument('-c', '--combine', help="combiner mapfile plotfile outfile", action="store", dest='combine', default="", nargs=3)
parser.add_argument('-pa', '--plot-all', help="plot all", action="store_true", dest='plotall', default=False)
parser.add_argument('-d', '--download-maps', help="download all maps from mapbox", action="store_true", default=False, dest='dlmaps')
parser.add_argument('-db', '--dburl', help="database url", action="store", default='sqlite:///torqfiskur.db', dest='dburl')
args = parser.parse_args()
cli_main(args)