Commits

Anonymous committed 8f6d151

Support geonames places

Comments (0)

Files changed (1)

 import time
 import urllib
 import logging
-import lxml
+from lxml import etree
 import feedparser
 
 q_url = "http://zcologia.com:8081/features.atom"
         
         this_geom = entry_geom(this)
         if this_geom is None:
-            loc_links = [x for x in this.links if x['rel'] == 'http://atlantides.org/relations/geo/location']
+            loc_links = [x for x in this.links if x['rel'] == 'http://www.georss.org/relation/location']
             
             if len(loc_links) == 0:
                 continue
+
+            loc_url = loc_links[0]['href']
+            resource = urllib.urlopen(loc_url)
+            loc_type = resource.headers.get('content-type')
+
+            if loc_type.startswith('application/atom+xml'):
+                try:
+                    loc_feed = feedparser.parse(resource)
+                    this['where'] = loc_feed['entries'][0]['where']
+                    if this['where']['type'] == 'Point':
+                        coords = this['where']['coordinates']
+                        this['pos'] = "%f %f" % (coords[1], coords[0])
+                    elif this['where']['type'] == 'LineString':
+                        this['pos_list'] = " ".join(["%f %f" % (coords[1], coords[0]) for coords in this['where']['coordinates']])
+                    elif this['where']['type'] == 'Polygon':
+                        this['pos_list'] = " ".join(["%f %f" % (coords[1], coords[0]) for coords in this['where']['coordinates'][0]])
+                except:
+                    log.debug("Undereferenceable resource at %s." % loc_url)
             
-            # We only dereference Atom entries
-            try:
-                loc_url = loc_links[0]['href']
-                loc_type = loc_links[0].get('type')
-                loc_feed = feedparser.parse(loc_url)
-                this['where'] = loc_feed['entries'][0]['where']
-                if this['where']['type'] == 'Point':
-                    coords = this['where']['coordinates']
-                    this['pos'] = "%f %f" % (coords[1], coords[0])
-                elif this['where']['type'] == 'LineString':
-                    this['pos_list'] = " ".join(["%f %f" % (coords[1], coords[0]) for coords in this['where']['coordinates']])
-                elif this['where']['type'] == 'Polygon':
-                    this['pos_list'] = " ".join(["%f %f" % (coords[1], coords[0]) for coords in this['where']['coordinates'][0]])
-                    
-            except:
-                log.debug("Undereferenceable resource at %s." % loc_url)
+            elif loc_type.startswith('application/rdf+xml'):
+                namespaces={'g': 'http://www.w3.org/2003/01/geo/wgs84_pos#'}
+                try:
+                    r = etree.parse(resource)
+                    lat = r.xpath('//g:lat', namespaces=namespaces)[0].text
+                    long = r.xpath('//g:long', namespaces=namespaces)[0].text
+                    this['where'] = {'type': 'Point'}
+                    this['where']['coordinates'] = (float(long), float(lat))
+                    this['pos'] = "%f %f" % (float(lat), float(long))
+                except:
+                    log.debug("Undereferenceable resource at %s." % loc_url)
+
+            else:
+                log.debug("Unsupported content type %s." % loc_type)
 
         result['entries'].append(this)