Created
October 9, 2017 04:29
-
-
Save youminkim/bec86a062d8c5fdfbfc22b2e149e02b0 to your computer and use it in GitHub Desktop.
Dengue helper
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/bin/python | |
# -*- coding: utf-8 -*- | |
import urllib, urllib2, zipfile, StringIO, json, pprint, logging | |
import xml.etree.ElementTree as ET | |
from pykml import parser | |
import googlemaps | |
from math import radians, cos, sin, asin, sqrt | |
GOOGLE_GEO_KEY = "" | |
gmaps = googlemaps.Client(key=GOOGLE_GEO_KEY) | |
def haversine(lon1, lat1, lon2, lat2): | |
""" | |
Calculate the great circle distance between two points | |
on the earth (specified in decimal degrees) | |
""" | |
# convert decimal degrees to radians | |
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2]) | |
# haversine formula | |
dlon = lon2 - lon1 | |
dlat = lat2 - lat1 | |
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2 | |
c = 2 * asin(sqrt(a)) | |
km = 6367 * c | |
return km | |
def simple_map(center_lat, center_lon, points): | |
points_desc = "markers=color:blue|label:Y|%f,%f&markers=" % (center_lat, center_lon) | |
for p in points: | |
points_desc = points_desc + str(p.lat) + "," + str(p.lon) + "|" | |
u = "https://maps.googleapis.com/maps/api/staticmap?center=%s,%s&scale=2&size=600x600&maptype=roadmap&key=%s&%s" % (center_lat, center_lon, GOOGLE_GEO_KEY, points_desc) | |
# return urllib.quote(u, safe="://?&=") | |
return u | |
def geo_lookup(query): | |
geocode_result = gmaps.geocode(query, region='sg', components={"country":"SG"}) | |
if len(geocode_result) > 0: | |
loc = geocode_result[0]['geometry']['location'] | |
lat, lon = loc['lat'], loc['lng'] | |
town = '' | |
addr = geocode_result[0]['formatted_address'] | |
return True, lat, lon, addr | |
return False, 0, 0, '' | |
def readDengue(): | |
url = 'https://data.gov.sg/api/action/package_metadata_show?id=dengue-cases' | |
response = urllib2.urlopen(url) | |
if response.code != 200: | |
return None | |
# Use the json module to load CKAN's response into a dictionary. | |
response_dict = json.loads(response.read()) | |
# Check the contents of the response. | |
if response_dict['success'] is not True: | |
return None | |
ret = [] | |
result = response_dict['result'] | |
title = result['name'] | |
last_updated = result['last_updated'] | |
identifier = result['identifier'] | |
kml_files = [] | |
for i in result['resources']: | |
if i['format'] != 'KML': | |
continue | |
u = i['url'] | |
req = urllib2.Request(u, headers={'User-Agent': 'Hello World'}) | |
f = urllib2.urlopen(req) | |
z = zipfile.ZipFile(StringIO.StringIO(f.read())) | |
for name in z.namelist(): | |
a = z.open(name).read() | |
root = parser.fromstring(a) | |
folder = root.Document.Folder | |
for p in folder.iterchildren(): | |
if hasattr(p, 'MultiGeometry'): | |
co = p.MultiGeometry.Polygon.outerBoundaryIs.LinearRing.coordinates | |
co = [[float(j) for j in a.split(",")[:-1]] for a in str(co).strip().split(" ")][:-1] | |
longitude, lat = 0,0 | |
for c in co: | |
longitude += c[0] | |
lat += c[1] | |
longitude = longitude / len(co) | |
lat = lat / len(co) | |
ret.append((lat, longitude)) | |
return ret, last_updated |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment