diff options
Diffstat (limited to 'chimere/utils.py')
| -rw-r--r-- | chimere/utils.py | 67 | 
1 files changed, 64 insertions, 3 deletions
| diff --git a/chimere/utils.py b/chimere/utils.py index d631fe7..e243c34 100644 --- a/chimere/utils.py +++ b/chimere/utils.py @@ -41,7 +41,7 @@ class ImportManager:  class KMLManager(ImportManager):      u""" -    KML importer/exporter +    KML importer      The filtr argument has to be defined as a correct Xpath string pointing      to Placemark nodes.      A typical XPath string looks like: @@ -70,7 +70,7 @@ class KMLManager(ImportManager):              except IOError, msg:                  return (new_item, updated_item, msg)          except urllib2.URLError as error: -            return (new_item, updated_item, error.reason) +            return (new_item, updated_item, error.message)          tree = etree.parse(source)          # try to get default namespace          if not self.ns: @@ -118,4 +118,65 @@ class KMLManager(ImportManager):          return (new_item, updated_item, msg)  class OSMManager(ImportManager): -    pass +    u""" +    OSM importer +    The source url is a path to an OSM file or a XAPI url +    The filtr argument is XAPI args or empty if it is an OSM file. +    """ + +    def get(self): +        u""" +        Get data from the source +        Return a tuple with: +        - number of new item ; +        - number of item updated ; +        - error detail on error +        """ +        from models import Marker +        new_item, updated_item, msg = 0, 0, '' +        try: +            source = urllib2.urlopen(self.importer_instance.source_url+ +                                     self.importer_instance.filtr) +        except ValueError: +            # assume it is a local file +            try: +                source = open(self.importer_instance.source_url) +            except IOError, msg: +                return (new_item, updated_item, msg) +        except urllib2.URLError as error: +            return (new_item, updated_item, error.message) +        tree = etree.parse(source) +        for node in tree.xpath('//node'): +            name, point, linestring = None, None, None +            node_id = node.attrib.get('id') +            for item in node: +                k = item.attrib.get('k') +                if k == 'name': +                    name = item.attrib.get('v') +            point = 'SRID=4326;POINT(%s %s)' % (node.get('lon'), +                                                node.get('lat')) +            if point: +                dct = {'point':point, +                       'name':name,} +                m = None +                if node_id: +                    dct_import = { +                        'import_key':node_id, +                        'import_source':self.importer_instance.source_url} +                    try: +                        m = Marker.objects.get(**dct_import) +                        for k in dct: +                            setattr(m, k, dct[k]) +                        m.save() +                        updated_item += 1 +                    except ObjectDoesNotExist: +                        m = None +                        dct.update(dct_import) +                if not m: +                    dct['status'] = 'I' +                    m = Marker.objects.create(**dct) +                    new_item += 1 +                m.categories.clear() +                for cat in self.importer_instance.categories.all(): +                    m.categories.add(cat) +        return (new_item, updated_item, msg) | 
