Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeggiCH committed Nov 1, 2023
2 parents ebeb7ad + f776099 commit d8a7609
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 20 deletions.
19 changes: 17 additions & 2 deletions ImportSchweizmobil.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import pyproj
from os.path import isfile,exists,expanduser
from datetime import datetime, timedelta
import PySimpleGUI as sg

# The following variables are made global and are set prior to
# exececuting ImportSchweizmobil()
Expand Down Expand Up @@ -118,6 +119,14 @@ def Import_Schweizmobil():
print ("No cache file - giving up !")
return

# setup progress bar
# layout the form
layout = [[sg.Text(f'Processing {len(tracks)} tracks')],
[sg.ProgressBar(max_value=len(tracks), orientation='h', size=(20, 20), key='progress')]]

window = sg.Window('Progress', layout, finalize=True)
progress_bar = window['progress']

# setup transformer object to transform
# Swiss LV03 coordinates used by schweizmobil.ch to WGS84
trafo=pyproj.Transformer.from_crs(21781,4326)
Expand All @@ -128,12 +137,13 @@ def Import_Schweizmobil():
}

k=0
included=0
# process each track in the tracks reponse
if debug>0: print("Start fetching track details")
for i in tracks:
if ((debug>1) and (k==20)): break
if debug>1: k=k+1

k=k+1
progress_bar.update_bar(k)
iid=i['id']
ts=i['modified_at'].replace(":", "" )
trkn=f'{outfp}{SchweizmobCacheDir}track {iid}-{ts}.geojson'
Expand Down Expand Up @@ -346,6 +356,10 @@ def Import_Schweizmobil():
# append the feature dict (current track) to the geo dict (collection of tracks)
if (opo==0 or opo==1 or opo==2):
geo['features'].append(feature)
included+=1

# close the progress bar window
window.close()

if geo['features']!=[]:
if debug>0:
Expand All @@ -354,6 +368,7 @@ def Import_Schweizmobil():
case 1: print ("Output includes via coordinates")
case 2: print ("Output includes full track coordinates")
case _: print ("Unsupported opo parameter value - no track/features written")
print (f"Included {included} of {len(tracks)} tracks")

f=open(f'{outfn}',mode="w")
if debug<2:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ So, this project ..
* allows users to filter tracks based on a number of criteria, such as the hike/bike length in kilometers, duration, meters uphill etc
* converts tracks into WGS84 coordinates and a GeoJSON FeatureCollection
* renders the FeatureCollection in a browser-viewable map (using python folium and leaflet.js)

Please note that I am not affiliated in any way with the schweizmobil.ch foundation and this repo is not an official publication of schweizmobil.ch. I created this repo for my training and because I was unable to find another solution for my need.
53 changes: 35 additions & 18 deletions main.py → TrackMapper.pyw
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
"""
schweizmobil.ch is a service to discover and plan (mostly) hiking/biking tracks in Switzerland. Viewing tracks does not require a paid subscription, drawing own tracks does. The schweizmobil.ch UI is great but unfortunately can only show one of the user-created tracks at a time. I have 300+ tracks and struggle to keep the overview of what I planned/did in a given region.
So, this project ..
- uses the schweizmobil.ch API described at https://github.com/JoeggiCH/schweizmobil.ch-API to download all tracks of a given user (with a paid subscription)
- allows users to filter tracks based on a number of criteria, such as the hike/bike length in kilometers, duration, meters uphill etc
- converts tracks into WGS84 coordinates and a GeoJSON FeatureCollection
- renders the FeatureCollection in a browser-viewable map (using python folium and leaflet.js)
"""
import PySimpleGUI as sg
from datetime import datetime
import ImportSchweizmobil as IS
Expand Down Expand Up @@ -70,7 +81,7 @@ def showFilter(fparam):
except:
print ("Failed to initialize window"); sys.exit()

# The SimpleGUI event loop
# The Filter window event loop
while True:
event, v = window.read()

Expand Down Expand Up @@ -138,7 +149,7 @@ def showSettings():
npw=passw
nfolder=settings['data_path']

# The SimpleGUI event Loop
# The Settings window event Loop
while True:
event, v = window.read()

Expand Down Expand Up @@ -171,12 +182,20 @@ def showSettings():
window.close()

def DoSettings():
#assuming write perms
if not isdir(settings['data_path']):
#assuming write perms
os.mkdir(settings['data_path'])

if not isdir(settings['data_path']+'cache'):
os.mkdir(settings['data_path']+'cache')

if not isdir(settings['data_path']+'cache/schweizmobil.ch/'):
os.mkdir(settings['data_path']+'cache/schweizmobil.ch/')

if not isdir(settings['data_path']+'GeoJSON'):
os.mkdir(settings['data_path']+'GeoJSON')

if not isdir(settings['data_path']+'html'):
os.mkdir(settings['data_path']+'html')

# derive the module settings from the main settings
Expand All @@ -199,36 +218,31 @@ def showMain():
insize=(12,1)

frame1=[
[sg.Button('Filter',key="FilterSM"),sg.Text(" => "),
sg.Button('Fetch',key="FetchSM"),sg.Text(" => "),
sg.Button('Publish',key="Publish")],
[sg.Button('Set Filter',key="FilterSM"),sg.Text(" => "),
sg.Button('Fetch Data & Apply Filter',key="FetchSM"),sg.Text(" => "),
sg.Button('Open',key="OpenLocal")],
[sg.Radio("Bounding Box",group_id='opo'),
sg.Radio("Vias",group_id='opo'),
sg.Radio("Detailed tracks",group_id='opo',default=True)]]

frame2=[
[sg.Button('Local',key="WriteLocal"),
sg.Button('Local & Web',key="WriteWeb")]]

frame3=[
[sg.Button('Local',key="OpenLocal"),
sg.Button('Web',key="OpenWeb")]]

[sg.Button('Publish',key="Publish"),
sg.Button('Open',key="OpenWeb")]]

layout = [
[sg.Frame ("schweizmobil.ch",frame1,expand_x=True)],
[sg.Frame ("Open Map in browser",frame3,expand_x=True)],
[sg.Frame ("On Web",frame2,expand_x=True)],
[sg.Output(size=(80, 10))],
[sg.Button('Settings',key='set'),sg.Push(),sg.Button('Exit',key="Exit")]]

try:
window = sg.Window('Show Tracks',
window = sg.Window('Track Mapper',
layout, default_button_element_size=(4,2),
use_default_focus=False,finalize=True)
except:
print ("Failed to initialize window"); sys.exit()

# The SimpleGUI event Loop
# The Main window event Loop
while True:
event, v = window.read()

Expand All @@ -253,8 +267,11 @@ def showMain():
if event=="Publish":
os.chdir(IS.outfp)
script_dir = os.path.abspath( os.path.dirname( __file__ ) )
os.system(f"{script_dir}\\update_web.bat {IS.outfp} >>{IS.outfp}update_web.log")
#os.system(IS.outfp+"update_web.bat >>update_web.log")
winpath=IS.outfp.replace("/","\\")
cmd=f"\"\"{script_dir}\\update web.bat\" \"{winpath}\" >>\"{winpath}update web.log\"\""
if debug>0: print (cmd)
os.system(cmd)

continue

if event=="OpenLocal":
Expand Down
6 changes: 6 additions & 0 deletions encryption.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""
encryption.py uses the fernet module to encrypt the configuration file - including
the API passwords - in TrackMapper
"""

# https://cryptography.io/en/latest/
# https://www.pythoninformer.com/python-libraries/cryptography/fernet/

Expand Down
2 changes: 2 additions & 0 deletions update_web.bat → update web.bat
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
REM this is an example of a batch to publish html\map.html on a webpage
REM please adjust it to your needs!

echo ===========================================================
echo new pub request
date /t
time /t
cd %1
Expand Down

0 comments on commit d8a7609

Please sign in to comment.