TSP GPX

Posted on Sat 21 April 2018 in Projects

In [3]:
import os
import pandas as pd
import gpxpy.gpx
import altair as alt
from bokeh.models import GMapPlot, GMapOptions, ColumnDataSource, Circle, Range1d, PanTool, WheelZoomTool, ResetTool, SaveTool
from bokeh.io import show, output_notebook
In [ ]:
gpx = gpxpy.parse(open('../images/TSPdata/allTSP/ladypackTSP.gpx'))
In [3]:
track_coords = [[point.latitude, point.longitude, point.elevation] 
                                for track in gpx.tracks 
                                    for segment in track.segments 
                                        for point in segment.points]
                       
locs = pd.DataFrame(track_coords)
locs.columns = ['Latitude', 'Longitude', 'Altitude']
locs
Out[3]:
Latitude Longitude Altitude
0 34.011080 -118.495300 16.0
1 34.011081 -118.495298 16.0
2 34.011190 -118.495150 16.0
3 34.011325 -118.494971 17.0
4 34.011352 -118.494953 17.0
5 34.011382 -118.494946 17.0
6 34.011522 -118.495025 17.0
7 34.011555 -118.495056 17.0
8 34.011588 -118.495087 17.0
9 34.011622 -118.495118 18.0
10 34.011654 -118.495150 18.0
11 34.011743 -118.495250 18.0
12 34.011773 -118.495282 18.0
13 34.011802 -118.495311 18.0
14 34.011888 -118.495404 18.0
15 34.011919 -118.495434 18.0
16 34.012005 -118.495530 18.0
17 34.012035 -118.495559 18.0
18 34.012176 -118.495718 18.0
19 34.012309 -118.495870 18.0
20 34.012337 -118.495897 18.0
21 34.012450 -118.496019 19.0
22 34.012480 -118.496048 19.0
23 34.012541 -118.496107 19.0
24 34.012646 -118.496222 20.0
25 34.012778 -118.496361 20.0
26 34.012804 -118.496389 20.0
27 34.012905 -118.496509 20.0
28 34.012976 -118.496600 20.0
29 34.013042 -118.496699 20.0
... ... ... ...
28730 36.076427 -115.172643 667.0
28731 36.076620 -115.172652 668.0
28732 36.076817 -115.172647 668.0
28733 36.077023 -115.172636 667.0
28734 36.077207 -115.172642 667.0
28735 36.077401 -115.172644 667.0
28736 36.077595 -115.172644 667.0
28737 36.077794 -115.172653 667.0
28738 36.078002 -115.172638 666.0
28739 36.078203 -115.172645 666.0
28740 36.078395 -115.172642 666.0
28741 36.078585 -115.172653 666.0
28742 36.078785 -115.172680 666.0
28743 36.078986 -115.172688 665.0
28744 36.079191 -115.172706 665.0
28745 36.079383 -115.172720 665.0
28746 36.079585 -115.172711 665.0
28747 36.079781 -115.172715 665.0
28748 36.079988 -115.172717 665.0
28749 36.080169 -115.172703 664.0
28750 36.080366 -115.172698 664.0
28751 36.080560 -115.172767 664.0
28752 36.080763 -115.172797 664.0
28753 36.080948 -115.172808 664.0
28754 36.081135 -115.172769 664.0
28755 36.081335 -115.172756 664.0
28756 36.081530 -115.172736 663.0
28757 36.081737 -115.172747 663.0
28758 36.081933 -115.172776 663.0
28759 36.082065 -115.172788 663.0

28760 rows × 3 columns

In [4]:
alt.data_transformers.enable('default', max_rows=None)
Out[4]:
DataTransformerRegistry.enable('default')
In [5]:
states = alt.topo_feature(data.us_10m.url, 'states')

# US states background
background = alt.Chart(states).mark_geoshape(
    fill='lightgray',
    stroke='white'
).properties(
    title='US State Capitols',
    width=700,
    height=400
).project('albersUsa')

# GPS Track
GPS = alt.Chart(locs).mark_point().encode(
    longitude='Longitude:Q',
    latitude='Latitude:Q',
    color='Altitude:Q'
)

background + GPS
Out[5]:
In [22]:
midlat = locs['Latitude'].min()+((locs['Latitude'].max()-locs['Latitude'].min())/2)
midlon = locs['Longitude'].min()+((locs['Longitude'].max()-locs['Longitude'].min())/2)
    
map_options = GMapOptions(lat=midlat, lng=midlon, map_type="roadmap", zoom=7)
activitymap = GMapPlot(x_range=Range1d(), y_range=Range1d(), map_options=map_options)
activitymap.api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
source = ColumnDataSource(data=locs)
circle = Circle(x="Longitude", y="Latitude", size=3, fill_color="blue", fill_alpha=0.9, line_color=None)
activitymap.add_glyph(source, circle)
activitymap.add_tools(PanTool(), WheelZoomTool(), ResetTool())
In [25]:
show(activitymap)