From 25ce3f9f366b09975a5f23b589dc71c0ee550aa5 Mon Sep 17 00:00:00 2001 From: Fredrik Unger Date: Wed, 17 Oct 2012 15:31:08 +0200 Subject: [PATCH] Adding classes Address and Coord, moving functions into the classes. Creating two classes to deal with Coorinates and Addresses. An address has a coordinate but a coordinate can also be used by it self. Some helper functions were also moved from contact as a contact also has addresses and coordinates. --- xinclude/address.py | 188 ++++++++++++++++++++++++++------------------ xinclude/contact.py | 33 +------- 2 files changed, 114 insertions(+), 107 deletions(-) diff --git a/xinclude/address.py b/xinclude/address.py index fd5fe96..3b7ff38 100755 --- a/xinclude/address.py +++ b/xinclude/address.py @@ -24,82 +24,58 @@ TS = 256 h = Http(".cache") -def lontile(lon, zoom): - tile = ((lon + 180) / 360) * (2**zoom) - return tile - -def lattile(lat, zoom): - tile = (1 - log(tan(lat * pi/180) + 1 / cos(lat* pi/180)) / pi) /2 * 2**zoom -# tile = (1-log(tan(radians(lat))+1/cos(radians(lat)))/pi)/2*2**zoom - return tile - -def coordtile(coord, zoom): - x = lontile(coord[1],zoom) - y = lattile(coord[0],zoom) - return (y,x) - -def tile(x,y,z): - return 'http://tile.openstreetmap.org/%s/%s/%s.png' % (z,x,y) - -def link(coord,zoom): - z = zoom - x = int(floor(lontile(coord[1],z))) - y = int(floor(lattile(coord[0],z))) - return tile(x,y,z) - -def offset(coord, zoom): - z = zoom - x = lontile(coord[1],z) - y = lattile(coord[0],z) - xo = int(floor((x-floor(x))*TS)) - yo = int(floor((y-floor(y))*TS)) - return (xo, yo) +class Coord(object): + def __init__(self, lat, lon): + self.latitude = float(lat) + self.longitude = float(lon) + self.image = None -def distance(p1, p2): - res = Geodesic.WGS84.Inverse(p1[0], p1[1], p2[0], p2[1]) - return res['s12'] + def osmlink(self): + return "http://www.openstreetmap.org/?mlat=%s&mlon=%s&zoom=18&layers=M"\ + % (self.latitude,self.longitude) -def geocode(address,country=None): - params = { 'q': address, - 'addressdetails': 1, - 'limit': 1, - 'format': 'xml', - 'polygon': 0 } - time.sleep(1) - if country: - params['countrycodes'] = country - - base_url = 'http://nominatim.openstreetmap.org/search?%s' - url = base_url % urllib.urlencode(params) - resp, content = h.request(url) -# print resp -# print content - root = etree.fromstring(content) - etree.tostring(root) -# print "%s" % (address) - lat = None - lon = None - for element in root.iter("place"): - lat = element.get("lat") - lon = element.get("lon") -# print(" : %s,%s" % (lat, lon)) - break - if not lat or not lon: - print resp - print content - return (float(lat),float(lon)) + def dms(self): + ns = self.latitude + ew = self.longitude + mnt,sec = divmod(ns*3600,60) + deg,mnt = divmod(mnt,60) + out = u'''%d°%2d'%5.2f"%s''' % ( deg,mnt,sec,'N') + mnt,sec = divmod(ew*3600,60) + deg,mnt = divmod(mnt,60) + out += u''' %d°%2d'%05.2f"%s''' % ( deg,mnt,sec,'E') + return out -def mark(image, coord): - draw = ImageDraw.Draw(image) - x, y = coord - r = 5 - bbox = (x-r, y-r, x+r, y+r) - draw.ellipse(bbox, outline="red") + def lontile(lon, zoom): + tile = ((lon + 180) / 360) * (2**zoom) + return tile -def mapimage(coords, zoom=15,size=(TS,TS)): - if len(coords) == 1: - co = coords[0] - filename = encode(co[0],co[1])+'.png' + def lattile(lat, zoom): + tile = (1 - log(tan(lat * pi/180) + 1 / cos(lat* pi/180)) / pi) /2 * 2**zoom + # tile = (1-log(tan(radians(lat))+1/cos(radians(lat)))/pi)/2*2**zoom + return tile + + def coordtile(coord, zoom): + x = lontile(coord[1],zoom) + y = lattile(coord[0],zoom) + return (y,x) + + def tile(x,y,z): + return 'http://tile.openstreetmap.org/%s/%s/%s.png' % (z,x,y) + + def link(coord,zoom): + (x, y) = coordtile(coord,zoom) + x = int(floor(x)) + y = int(floor(y)) + return tile(x,y,zoom) + + def offset(coord, zoom): + (x, y) = coordtile(coord,zoom) + xo = int(floor((x-floor(x))*TS)) + yo = int(floor((y-floor(y))*TS)) + return (xo, yo) + + def png(self,zoom=15,size=(TS,TS)): + filename = encode(self.latitude, self.longitude)+'.png' if path.isfile(filename): if path.getctime(filename) > time.time() - 60*60*24*2: return @@ -128,11 +104,9 @@ def mapimage(coords, zoom=15,size=(TS,TS)): # url = link(c,zoom) size = (len(lontiles)*TS,len(lattiles)*TS) grid = Image.new("RGB", size, None) - img = [] for yi, y in enumerate(lattiles): for xi, x in enumerate(lontiles): url = tile(x,y,zoom) -# print url time.sleep(1) request, content = h.request(url) img = Image.open(StringIO(content)) @@ -145,7 +119,71 @@ def mapimage(coords, zoom=15,size=(TS,TS)): mark(grid, (xp,yp)) gridc = grid.crop((xp-TS/2,yp-TS/2,xp+TS/2,yp+TS/2)) gridc.save(filename) - else: + + + def db_xml(self): + uri = etree.Element(DB+'uri',nsmap=NSMAP) + ln = etree.SubElement(uri, DB+'link') + ln.set(XLINK+'href',self.osmlink()) + imo = etree.SubElement(ln, DB+'inlinemediaobject') + io = etree.SubElement(imo, DB+'imageobject', condition="web") + idat = etree.SubElement(io , DB+'imagedata', + fileref=encode(self.latitude, self.longitude)+'.png', + format='PNG') + to = etree.SubElement(imo, DB+'textobject') + ph = etree.SubElement(to, DB+'phrase') + ph.text = "geo:"+str(self.latitude)+","+str(self.longitude) + para = etree.SubElement(ln, DB+'para') + para.text = self.dms() + return uri + +class Address(object): + """Address object to contain everything known about an address""" + def __init__(self,address): + self._address_string = address + self._root = etree.Element(DB+'address',nsmap=NSMAP) + self._coord = None + + def geocode(self,country=None): + params = { 'q': self._address_string, + 'addressdetails': 1, + 'limit': 1, + 'format': 'xml', + 'polygon': 0 } + + if country: + params['countrycodes'] = country + + base_url = 'http://nominatim.openstreetmap.org/search?%s' + url = base_url % urllib.urlencode(params) + resp, content = h.request(url) + root = etree.fromstring(content) + place = root.find("place") + if place is not None: + print (etree.tostring(root, pretty_print=True)) + self._coord=Coord(place.get("lat"),place.get("lon")) + return 1 + else: + print resp + print content + return 0 + + def db_xml(self): + return self._coord.db_xml() + + +def distance(p1, p2): + res = Geodesic.WGS84.Inverse(p1[0], p1[1], p2[0], p2[1]) + return res['s12'] + +def mark(image, coord): + draw = ImageDraw.Draw(image) + x, y = coord + r = 5 + bbox = (x-r, y-r, x+r, y+r) + draw.ellipse(bbox, outline="red") + +def mapimages(coords, zoom=15,size=(TS,TS)): minlat = 1000 maxlat = 0 minlon = 1000 diff --git a/xinclude/contact.py b/xinclude/contact.py index 74d9700..e7e0ab3 100755 --- a/xinclude/contact.py +++ b/xinclude/contact.py @@ -3,39 +3,9 @@ from vobject import readComponents import sys -from addr import mapimage, geocode +from address import Address from geohash import encode -def coord2dms(coord): - ns = coord[0] - ew = coord[1] - mnt,sec = divmod(ns*3600,60) - deg,mnt = divmod(mnt,60) - out = u'''%d°%2d'%5.2f"%s''' % ( deg,mnt,sec,'N') - mnt,sec = divmod(ew*3600,60) - deg,mnt = divmod(mnt,60) - out += u''' %d°%2d'%05.2f"%s''' % ( deg,mnt,sec,'E') - return out - - -def maplink(lat,lon): - return ''' - - - - - - - - geo:%s,%s - - - %s - - -''' % (lat, lon, encode(float(lat), float(lon)), lat, lon, coord2dms((lat, lon))) - for arg in sys.argv[1:]: al = arg.split("=") if al[0] == "lang": @@ -43,7 +13,6 @@ for arg in sys.argv[1:]: if al[0] == "xptr": argument = al[1] - (cards,query) = argument.split('?') (key, name) = query.split(':') with open(cards, 'r') as f: -- 2.30.2