size: printing size of style
[treecutter.git] / xinclude / address.py
index be9930f98136f8b899cbf32164c4c41b32d67ede..6a30d7accc13006f17d337ff30ddc6a2c86f8c18 100755 (executable)
@@ -135,7 +135,9 @@ def c(s):
     return s or ''
 
 def s(s,n):
-    return s or n
+    if n is not None:
+        return s or n.text
+    return s
 
 
 class Address(object):
@@ -150,12 +152,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')
@@ -179,10 +183,11 @@ class Address(object):
             places = int(root.xpath('count(//place[@place_id])'))
             if places == 1:
                 place = root.find("place")
-                self.postcode = s(self.postcode,place.find("boundary").text)
-                self.city = s(self.city,place.find("city").text)
-                self.country = s(self.country,place.find("country").text)
-                self.country_code = s(self.country_code,place.find("country_code").text)
+#                print etree.tostring(place,encoding='UTF-8',pretty_print=True)
+                self.postcode = s(self.postcode,place.find("postcode"))
+                self.city = s(self.city,place.find("city"))
+                self.country = s(self.country,place.find("country"))
+                self.country_code = s(self.country_code,place.find("country_code"))
                 self.coord=Coord(place.get("lat"),place.get("lon"))
                 return
 
@@ -218,16 +223,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 +242,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'])