size: printing size of style
[treecutter.git] / xinclude / address.py
index c99f51ad0e86bca424f21a1d613f77a7d26e268b..6a30d7accc13006f17d337ff30ddc6a2c86f8c18 100755 (executable)
@@ -131,36 +131,48 @@ class Coord(object):
                 **{const.XLINK+"href": self.osmlink()}))
         return uri
 
+def c(s):
+    return s or ''
+
+def s(s,n):
+    if n is not None:
+        return s or n.text
+    return s
+
 
 class Address(object):
     """Address object to contain everything known about an address"""
     def __init__(self,street=None,postcode=None,city=None,country=None):
-        self.name = None
         self.street = street
         self.postcode = postcode
         self.city = city
         self.country = country
-        self.phone = ''
+        self.country_code = None
+        self.phone = None
+        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')
             r = t.xpath('//iso_3166_entry[@name="'+self.country+'"]')
             if len(r)==1:
-                params['countrycodes'] = r[0].get("alpha_2_code")
+                self.country_code = r[0].get("alpha_2_code")
+        if self.country_code:
+            params['countrycodes'] = self.country_code
 
         addrlist=[]
-        if self.name and len(self.name)>0:
-            addrlist.append(u''+self.name+', '+self.street+', '+self.city)
-        addrlist.append(u''+self.street+', '+self.postcode+', '+self.city)
-        addrlist.append(u''+self.street+', '+self.city)
+        addrlist.append(u''+c(self.name)+', '+c(self.street)+', '+c(self.city))
+        addrlist.append(u''+c(self.street)+', '+c(self.postcode)+', '+c(self.city))
+        addrlist.append(u''+c(self.street)+', '+c(self.city))
 
         for addr in addrlist:
             params['q'] = addr.encode('utf-8')
@@ -171,11 +183,13 @@ class Address(object):
             places = int(root.xpath('count(//place[@place_id])'))
             if places == 1:
                 place = root.find("place")
+#                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
-        sys.stderr.write(u'FAILURE: Did not find:\n')
-        sys.stderr.write(addrlist[0].encode('utf-8'))
-        sys.stderr.write(url)
 
     def add_phone(self, phone):
         self.phone = phone
@@ -185,12 +199,15 @@ class Address(object):
 
     def db_xml(self):
         db = ElementMaker(namespace=const.DB_NS, nsmap=const.NSMAP)
-        adr = db.address(db.street(self.street),
-                         db.postcode(self.postcode),
-                         db.city(self.city),
-                         db.country(self.country),
-                         db.phone(self.phone),
-                         self.coord.db_xml())
+        co = ''
+        if self.coord:
+            co = self.coord.db_xml()
+        adr = db.address(db.street(c(self.street)),
+                         db.postcode(c(self.postcode)),
+                         db.city(c(self.city)),
+                         db.country(c(self.country)),
+                         db.phone(c(self.phone)),
+                         co)
 #                       type=self.type,
         return adr
 
@@ -206,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
@@ -224,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'])