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):
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')
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
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
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'])