Skip to content

Instantly share code, notes, and snippets.

@lukasmartinelli
Last active May 4, 2016 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukasmartinelli/b89ed70143db1bcd5616ec7ad046bfb4 to your computer and use it in GitHub Desktop.
Save lukasmartinelli/b89ed70143db1bcd5616ec7ad046bfb4 to your computer and use it in GitHub Desktop.
Filter coords whether they are in Switzerland
#!/usr/bin/env python
import csv
import sys
import os
import mercantile
DELIMITER = os.getenv('DELIMITER', ' ')
def calculate_center(x, y, zoom):
bounds = mercantile.bounds(x, y, zoom)
height = bounds.north - bounds.south
width = bounds.east - bounds.west
center = (bounds.north + height / 2, bounds.west + width / 2)
return center
if __name__ == '__main__':
reader = csv.reader(sys.stdin, delimiter=DELIMITER)
writer = csv.writer(sys.stdout, delimiter=DELIMITER)
for row in reader:
zoom = int(row[0])
x = int(row[1])
y = int(row[2])
requests = int(row[3])
center = calculate_center(x, y, zoom)
writer.writerow([zoom, x, y, requests] + list(center))
#!/usr/bin/env python
"""
Filter out all points in Switzerland
"""
import csv
import sys
import os
from decimal import *
DELIMITER = os.getenv('DELIMITER', ' ')
NORTH = Decimal('47.9922193487799')
WEST = Decimal('5.99235534667969')
EAST = Decimal('11.1243438720703')
SOUTH = Decimal('45.6769214851596')
def in_switzerland(coords):
lat, lng = coords
return lat < NORTH and lat > SOUTH and lng > WEST and lng < EAST
if __name__ == '__main__':
reader = csv.reader(sys.stdin, delimiter=DELIMITER)
writer = csv.writer(sys.stdout, delimiter=DELIMITER)
for row in reader:
requests = int(row[0])
lat = Decimal(row[1])
lng = Decimal(row[2])
if in_switzerland((lat, lng)):
writer.writerow([requests, lat, lng])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment