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
# 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))
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
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 '''
-<uri type="location">
- <link xlink:href=
- "http://www.openstreetmap.org/?mlat=%s&mlon=%s&zoom=18&layers=M">
- <inlinemediaobject>
- <imageobject condition="web">
- <imagedata fileref="%s.png" format="PNG"/>
- </imageobject>
- <textobject>
- <phrase>geo:%s,%s</phrase>
- </textobject>
- </inlinemediaobject>
- <para>%s</para>
- </link>
-</uri>
-''' % (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":
if al[0] == "xptr":
argument = al[1]
-
(cards,query) = argument.split('?')
(key, name) = query.split(':')
with open(cards, 'r') as f: