Skip to content
Browse files

generate map from template

  • Loading branch information...
1 parent fd9398a commit 53efbaa1c13ab58c4c2807fb4355fec5bec2f8ec @alonswartz alonswartz committed Dec 27, 2011
Showing with 45 additions and 45 deletions.
  1. +17 −45 generate.py
  2. +28 −0 input/map.html.tmpl
View
62 generate.py
@@ -10,27 +10,6 @@
from string import Template
from math import *
-MAP_HEAD="""
-<html>
-<head>
-<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
-<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
-<title>GeoIP AWS Regions Map</title>
-<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
-<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
-<script type="text/javascript">
-
- function initialize() {
- var myLatLng = new google.maps.LatLng(25, 0);
- var myOptions = {
- zoom: 2,
- center: myLatLng,
- mapTypeId: google.maps.MapTypeId.ROADMAP
- };
-
- var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
-"""
-
MAP_MARKER="""
var M$num = new google.maps.Marker({
position: new google.maps.LatLng($latlon),
@@ -46,16 +25,6 @@
P$num.setMap(map);
"""
-MAP_TAIL="""
- }
-</script>
-</head>
-<body onload="initialize()">
- <div id="map_canvas"></div>
-</body>
-</html>
-"""
-
def haversine(lat1, lon1, lat2, lon2):
"""calculate the great circle distance between two points on the earth"""
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
@@ -122,31 +91,33 @@ def write_index(self, tag, filepath):
fd.close()
- def write_map(self, filepath):
- """generate map html at filepath"""
- fd = open(filepath, 'w')
-
- print >>fd, MAP_HEAD
+ def write_map(self, template, output):
+ """generate map from template"""
+ t = Template(file(template).read())
+ # markers
n = 0
- t = Template(MAP_MARKER)
+ markers = []
+ marker = Template(MAP_MARKER)
for r in self.regions.values():
n += 1
title = "%s (%s)" % (r.name, r.code)
- print >>fd, t.substitute(num=n, latlon=r.latlon, title=title)
+ markers.append(marker.substitute(num=n, latlon=r.latlon, title=title))
+ # lines
n = 0
- t = Template(MAP_LINE)
+ lines = []
+ line = Template(MAP_LINE)
for e in self.values():
- if not e.lon:
- continue
-
+ if not e.lon: continue
n += 1
r = self.regions[e.region]
- print >>fd, t.substitute(num=n, e_ll=e.latlon, r_ll=r.latlon)
+ lines.append(line.substitute(num=n, e_ll=e.latlon, r_ll=r.latlon))
- print >>fd, MAP_TAIL
+ html = t.substitute(MARKERS="\n".join(markers), LINES="\n".join(lines) )
+ fd = open(output, 'w')
+ fd.write(html)
fd.close()
def __iter__(self):
@@ -170,9 +141,10 @@ def main():
code, tag, name, region = line.rstrip().split(";")
entries.override_entry(code, name, tag, region)
- entries.write_map("output/map.html")
entries.write_index("usa", "output/usa.index")
entries.write_index("countries", "output/countries.index")
+ entries.write_map("input/map.html.tmpl", "output/map.html")
+
if __name__ == "__main__":
main()
View
28 input/map.html.tmpl
@@ -0,0 +1,28 @@
+<html>
+<head>
+<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
+<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
+<title>GeoIP AWS Regions Map</title>
+<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
+<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
+<script type="text/javascript">
+
+ function initialize() {
+ var myLatLng = new google.maps.LatLng(25, 0);
+ var myOptions = {
+ zoom: 2,
+ center: myLatLng,
+ mapTypeId: google.maps.MapTypeId.ROADMAP
+ };
+
+ var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
+
+ ${MARKERS}
+ ${LINES}
+ }
+</script>
+</head>
+<body onload="initialize()">
+ <div id="map_canvas"></div>
+</body>
+</html>

0 comments on commit 53efbaa

Please sign in to comment.
Something went wrong with that request. Please try again.