diff options
author | Étienne Loks <etienne.loks@peacefrogs.net> | 2012-03-16 20:00:55 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@peacefrogs.net> | 2012-03-16 20:00:55 +0100 |
commit | 9a794edbffbb3a178138fac3ded0b516c4ced9a7 (patch) | |
tree | f10e09c1b8a820dc341c067fae589feee784d30a /chimere/utils.py | |
parent | 50278ab6a6b2972920629a3983328b0a49af970f (diff) | |
download | Chimère-9a794edbffbb3a178138fac3ded0b516c4ced9a7.tar.bz2 Chimère-9a794edbffbb3a178138fac3ded0b516c4ced9a7.zip |
Work on KML import
Diffstat (limited to 'chimere/utils.py')
-rw-r--r-- | chimere/utils.py | 77 |
1 files changed, 72 insertions, 5 deletions
diff --git a/chimere/utils.py b/chimere/utils.py index 40bc5ad..4e78220 100644 --- a/chimere/utils.py +++ b/chimere/utils.py @@ -21,7 +21,10 @@ Utilitaries """ -from urllib2 import urlopen, URLError +import urllib2 +from lxml import etree + +from django.core.exceptions import ObjectDoesNotExist class ImportManager: u""" @@ -39,16 +42,80 @@ class ImportManager: class KMLManager(ImportManager): u""" KML importer/exporter + The filtr argument has to be defined as a correct Xpath string pointing + to Placemark nodes. + A typical XPath string looks like: + "//kml:Folder/kml:name[text()='Subcategory 1']/../kml:Placemark" """ + def __init__(self, importer_instance, ns=''): + self.importer_instance = importer_instance + self.ns = ns + def get(self): u""" Get data from the source - Return a tuple with number of item imported and the detail + Return a tuple with: + - number of new item ; + - number of item updated ; + - detail """ + from models import Marker + new_item, updated_item, msg = 0, 0, '' try: - source = urlopen(self.importer_instance.source_url) - except URLError as error: - return (0, error.reason) + source = urllib2.urlopen(self.importer_instance.source_url) + 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.reason) + tree = etree.parse(source) + # try to get default namespace + if not self.ns: + self.ns = tree.getroot().nsmap[None] + for placemark in tree.xpath(self.importer_instance.filtr, + namespaces={'kml':self.ns}): + name, point, linestring = None, None, None + pl_id = placemark.attrib.get('id') + ns = '{%s}' % self.ns + for item in placemark: + if item.tag == ns + 'name': + name = item.text + elif item.tag == ns + 'description': + description = item.text + elif item.tag == ns + 'Point': + for coord in item: + if coord.tag == ns + 'coordinates': + x, y, z = coord.text.split(',') + point = 'SRID=4326;POINT(%s %s)' % (x, y) + if point: + dct = {'point':point, + 'description':description, + 'name':name,} + m = None + if pl_id: + dct_import = { + 'import_key':pl_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) class OSMManager(ImportManager): pass |