Switching to address based map generation. Divided to subroutines.
authorFredrik Unger <fred@tree.se>
Wed, 16 May 2012 09:44:41 +0000 (11:44 +0200)
committerFredrik Unger <fred@tree.se>
Wed, 16 May 2012 09:44:41 +0000 (11:44 +0200)
The generation was geo: tag based but as there is only one tag per
vcard I switched to address lookup instead.
There are problems with CZ village addresses, and some missing
address tags that has to be added to openstreetmap, but it is a
better longterm solution.

xinclude/contact.py

index 879fd66fc8ef05a903dfdcead8405501f8348d0a..379a33607fff3e10a1da373c831600ab1474f5c8 100755 (executable)
@@ -3,6 +3,38 @@
 
 from vobject import readComponents
 import sys
+from addr import mapimage, geocode
+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&amp;mlon=%s&amp;zoom=18&amp;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("=")
@@ -11,6 +43,7 @@ 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:
@@ -71,6 +104,10 @@ if 'adr' in found.contents.keys():
     if t.value != '':
       ad += '<phone>'+t.value+'</phone>'
     ad += '</address>'
+    geostr = u''+a.value.street+', '+a.value.city+', '+a.value.country
+    (lat,lon) = geocode(geostr.encode('utf-8'))
+    mapimage([(float(lat),float(lon))])
+    ad += maplink(lat,lon)
 
 o = ''
 if 'org' in found.contents.keys():
@@ -85,10 +122,12 @@ url = ''
 if 'url' in found.contents.keys():
   url += '<uri type="website"><link xlink:href="'+found.url.value+'"/></uri>'
 geo = ''
-if 'geo' in found.contents.keys():
-  (lat,lon) = found.geo.value.split(';')
-  geo += '<uri type="location"><link xlink:href="http://www.openstreetmap.org/'+ \
-      '?mlat='+lat+'&amp;mlon='+lon+'&amp;zoom=18&amp;layers=M">geo:'+lat+','+lon+'</link></uri>'
+#if 'geo' in found.contents.keys():
+#  (lat,lon) = found.geo.value.split(';')
+#  mapimage([(float(lat),float(lon))])
+#  # create picture
+#  geo += maplink(lat,lon)
+
 
 # Turn off email for now
 email = ''