#!/usr/bin/env python # -*-python-*- import sys import os.path import pwd import posix user = pwd.getpwuid(posix.geteuid()) home = os.path.abspath(user[5]) pylib = home + "/lib/python" sys.path.insert(0, pylib + "/ModestMaps/py/") sys.path.insert(0, pylib + "/modestMMarkers/") import optparse import Flickr.API import elementtree.ElementTree as ET import ModestMaps from modestMMarkers import polylines from modestMMarkers import utils import cairo def munge_place (res) : polys = [] allpoints = [] et = ET.parse(res) for poly in et.findall(".//polyline") : points = [] if not poly.text : continue for coord in poly.text.split(" ") : pair = coord.split(",") lat = float(pair[0]) lon = float(pair[1]) points.append({'latitude':lat, 'longitude':lon}) allpoints.append({'lat':lat, 'lon':lon}) polys.append(points) return (polys, allpoints) def draw (opts) : api = Flickr.API.API(opts.apikey, None) req = Flickr.API.Request(method='flickr.places.getInfo', woe_id=opts.woeid, sign=False) res = api.execute_request(req) (polys, allpoints) = munge_place(res) # req = Flickr.API.Request(method='flickr.places.getShapeHistory', woe_id=opts.woeid, sign=False) res = api.execute_request(req) et = ET.parse(res) # hey look! donuts!! donuts = [] for shp in et.findall(".//shape") : if shp.attrib['is_donuthole'] == '1' : for poly in shp.findall(".//polyline") : dpoints = [] for coord in poly.text.split(" ") : pair = coord.split(",") lat = float(pair[0]) lon = float(pair[1]) dpoints.append({'latitude':lat, 'longitude':lon}) donuts.append(dpoints) break # req = Flickr.API.Request(method='flickr.places.getChildrenWithPhotosPublic', woe_id=opts.woeid, sign=False) res = api.execute_request(req) others = [] if int(opts.children) != 0 : et = ET.parse(res) for pl in et.findall(".//place") : woeid = pl.attrib['woeid'] subreq = Flickr.API.Request(method='flickr.places.getInfo', woe_id=woeid) subres = api.execute_request(subreq) (child_polys, child_points) = munge_place(subres) if len(child_polys) > 0 : others.append(child_polys) bbox = utils.calculate_bbox_for_points(allpoints) # Draw the map dims = ModestMaps.Core.Point(int(opts.width), int(opts.height)) sw = ModestMaps.Geo.Location(bbox[0], bbox[1]) ne = ModestMaps.Geo.Location(bbox[2], bbox[3]) provider = ModestMaps.builtinProviders['MICROSOFT_AERIAL']() mm_obj = ModestMaps.mapByExtent(provider, sw, ne, dims) mm_img = mm_obj.draw() # Draw the shapes (First the parent WOE ID) poly = polylines.polyline(mm_obj) mm_img = poly.draw_polylines(mm_img, polys) # mm_img = poly.draw_polylines(mm_img, donuts, colour=(.005, 0, 1)) # Then more shapes the children for p in others : mm_img = poly.draw_polylines(mm_img, p, colour=(255,255,255), no_fill=True) mm_img.save(opts.out) # if __name__ == '__main__' : parser = optparse.OptionParser() parser.add_option("-o", "--out", dest="out", help="The path where the final PNG image should be created") parser.add_option("-H", "--height", dest="height", help="Page height (in pixels)", default=1024) parser.add_option("-W", "--width", dest="width", help="Page width (in pixels)", default=1024) parser.add_option("-a", "--apikey", dest="apikey", help="Your Flickr API key") parser.add_option("-w", "--woeid", dest="woeid", help="The WOE ID to fetch a shape file for") parser.add_option("-c", "--children", dest="children", help="", default=0) (opts, args) = parser.parse_args() draw(opts)