Created
December 28, 2015 19:28
-
-
Save timokoola/a66c6a2845f9957b29da to your computer and use it in GitHub Desktop.
Create a static map of nearby public transport stops using Helsinki area Reittiopas API and Google Static Maps API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/env/bin/python | |
| # Create a static map of nearby public transport stops using Helsinki area Reittiopas API and Google Static Maps API | |
| # pip install requests if needed | |
| import requests | |
| import sys | |
| import urllib | |
| import string | |
| googleurl = "https://maps.googleapis.com/maps/api/staticmap?center=%s,%s&zoom=%d&scale=2&size=640x640&maptype=roadmap" | |
| # Get API key for Google Static Maps API and documentation at | |
| # https://developers.google.com/maps/documentation/static-maps/?hl=en | |
| googlekey = "YOUR-OWN-API-KEY" | |
| markertemplate = "&markers=color:green%%7Clabel:%s%%7C%s,%s" | |
| # HSL Reittiopas API credentials and instructions at | |
| # http://developer.reittiopas.fi/pages/en/home.php | |
| hsluser = "YOUR-USERNAME" | |
| hslpass = "YOUR-PASSWORd" | |
| class HslUrls(object): | |
| """Helper class to generate HSL API urls""" | |
| def __init__(self,user,password): | |
| self.user = user | |
| self.password = password | |
| self.baseurl = "http://api.reittiopas.fi/hsl/prod/?request=" | |
| def nearby_stops(self,longitude,latitude): | |
| """Nearby stops API, not epsg_out and epsg_in to change to coordinate system Google uses | |
| note also latitude and longitude are in different order for these two APIs""" | |
| url = "%sstops_area&epsg_in=4326&epsg_out=4326¢er_coordinate=%s,%s&user=%s&pass=%s" % (self.baseurl,latitude,longitude,self.user,self.password) | |
| return url | |
| if __name__ == '__main__': | |
| # example has Helsinki railway station coordinates | |
| # change it to something more interesting/relevant | |
| longitude = "60.1719" | |
| latitude = "24.9414" | |
| # zoom level 15 | |
| zoomlevel = 15 | |
| hu = HslUrls(hsluser,hslpass) | |
| # make a request to Reittiopas API | |
| r = requests.get(hu.nearby_stops(longitude,latitude)) | |
| stops = r.json() | |
| # handle the resulting json, turn it into dictionary {"bus stop code": <coordinates>} | |
| # remember to reverse coordinates received from Reittiopas API | |
| l = dict([(s["codeShort"],s["coords"].split(",")[::-1]) for s in stops if len(s["codeShort"]) > 0]) | |
| baseurl = googleurl % (longitude,latitude,zoomlevel) | |
| # create markers, add an uppercase label to everyone as a marker and the coordinates | |
| markers = "".join([markertemplate % (k[0], l[k[1]][0],l[k[1]][1]) for k in zip(string.uppercase,l.keys())]) | |
| keystr = "&key=%s" % googlekey | |
| # print out the resulting url | |
| print baseurl + markers + keystr | |
| # run with ```$ python busstops.py| open``` OS X to show resulting map on browser |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment