Created
January 30, 2019 20:30
-
-
Save dazzag24/6c3b96d72598ef9fe18b1d06e49df7dc to your computer and use it in GitHub Desktop.
Rendering GPS traces from OSM
This file contains 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
import requests | |
import xml.etree.ElementTree as ET | |
from PIL import Image | |
import numpy as np | |
# Area format is left, bottom, right, top | |
#AREA = [-1.4853, 53.3730, -1.4557, 53.3893] | |
AREA = [0.0422, 52.1648, 0.2204, 52.2532] | |
WIDTH = 1280 | |
HEIGHT = 720 | |
sess = requests.session() | |
def get_points(area, page=0): | |
bbox = ','.join(map(str, area)) | |
xml = sess.get('https://api.openstreetmap.org/api/0.6/trackpoints', | |
params={'bbox': bbox, 'page': page}).text | |
root = ET.fromstring(xml) | |
for trkpt in root.findall('.//{http://www.topografix.com/GPX/1/0}trkpt'): | |
yield trkpt.attrib['lat'], trkpt.attrib['lon'] | |
img = Image.new('L', (WIDTH, HEIGHT), color='white') | |
#for page in range(15): | |
for page in range(30): | |
for point in get_points(AREA, page): | |
y, x = point | |
x = int(np.interp(float(x), [AREA[0], AREA[2]], [0, WIDTH])) | |
y = int(np.interp(float(y), [AREA[1], AREA[3]], [HEIGHT, 0])) | |
try: | |
img.putpixel((x, y), 1) | |
except: | |
pass | |
img.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment