address: geocode language, objects for mapimages
authorFredrik Unger <fred@tree.se>
Wed, 19 Dec 2012 09:45:28 +0000 (10:45 +0100)
committerFredrik Unger <fred@tree.se>
Wed, 19 Dec 2012 09:45:28 +0000 (10:45 +0100)
Added a accept-language header to the nominatim requests.
Started to use Coord objects in mapimages, but still need more work.

xinclude/address.py

index be9930f98136f8b899cbf32164c4c41b32d67ede..30e8a0713efcfd546e1fc6fa20d5860a7a55b498 100755 (executable)
@@ -150,12 +150,14 @@ class Address(object):
         self.name = None
         self.coord = None
 
-    def geocode(self):
+
+    def geocode(self, language='en'):
         base_url = 'http://nominatim.openstreetmap.org/search?%s'
         params = { 'addressdetails': 1,
                    'limit': 1,
                    'format': 'xml',
-                   'polygon': 0 }
+                   'polygon': 0,
+                   'accept-language': language}
 
         if self.country:
             t = etree.parse('/usr/share/xml/iso-codes/iso_3166.xml')
@@ -218,16 +220,17 @@ def mark(image, coord):
     bbox = (x-r, y-r, x+r, y+r)
     draw.ellipse(bbox, outline="red")
 
+def box(image,box):
+    draw = ImageDraw.Draw(image)
+    draw.rectangle(box, outline="red")
+
 def mapimages(coords, zoom=15,size=(TS,TS)):
-        minlat = 1000
-        maxlat = 0
-        minlon = 1000
-        maxlon = 0
-        for c in coords:
-            minlat = min(minlat,c[0])
-            maxlat = max(maxlat,c[0])
-            minlon = min(minlon,c[1])
-            maxlon = max(maxlon,c[1])
+
+        minlat = min(coords,key=attrgetter('latitude'))
+        maxlat = max(coords,key=attrgetter('latitude'))
+        minlon = min(coords,key=attrgetter('longitude'))
+        maxlon = max(coords,key=attrgetter('longitude'))
+
         # Find minimal bounding box and expand it 5%
         hyp = distance((maxlat,minlon),(minlat,maxlon))
         hyp = hyp*0.05
@@ -236,6 +239,8 @@ def mapimages(coords, zoom=15,size=(TS,TS)):
         lld =  Geodesic.WGS84.Direct(minlat, minlon, 225, hyp)
         lrd =  Geodesic.WGS84.Direct(minlat, maxlon, 135, hyp)
 
+        ul = Coord(maxlat,minlon).direct(315, hyp)
+
         ul = (uld['lat2'],uld['lon2'])
         ur = (urd['lat2'],urd['lon2'])
         ll = (lld['lat2'],lld['lon2'])