1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# See the file COPYING for details.
"""
Utilitaries
"""
import datetime
import os
import re
import StringIO
import tempfile
import urllib2
import unicodedata
import zipfile
from lxml import etree
from django.conf import settings
from django.contrib.gis.gdal import DataSource, Driver, OGRGeometry, \
OGRGeomType, SpatialReference, check_err
from django.contrib.gis.gdal.libgdal import lgdal
from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import render_to_response
from django.utils.translation import ugettext_lazy as _
from chimere import get_version
from external_utils import OsmApi
def unicode_normalize(string):
return ''.join(
(c for c in unicodedata.normalize('NFD', string)
if unicodedata.category(c) != 'Mn'))
class ImportManager:
u"""
Generic class for specific importers
"""
default_source = None
def __init__(self, importer_instance):
self.importer_instance = importer_instance
self.default_name = " - ".join([cat.name
for cat in self.importer_instance.categories.order_by('name').all()])
def get(self):
pass
def put(self):
pass
def create_or_update_item(self, cls, values, import_key, version=None):
updated, created, item = False, False, None
if import_key:
dct_import = {
'import_key__icontains':'%s:%s;' % (
self.importer_instance.importer_type,
import_key),
'import_source':self.importer_instance.source}
try:
item = cls.objects.get(**dct_import)
if version and item.import_version == int(version):
# no update since the last import
return item, None, None
for k in values:
setattr(item, k, values[k])
item.save()
updated = True
except ObjectDoesNotExist:
pass
if not item:
values.update({
'import_source':self.importer_instance.source})
values['status'] = 'I'
item = cls.objects.create(**values)
created = True
if import_key:
item.set_key(self.importer_instance.importer_type,
import_key)
item.categories.clear()
for cat in self.importer_instance.categories.all():
item.categories.add(cat)
return item, updated, created
@classmethod
def get_files_inside_zip(cls, zippedfile, suffixes, dest_dir=None):
try:
flz = zipfile.ZipFile(zippedfile)
except zipfile.BadZipfile:
return [], _(u"Bad zip file")
namelist = flz.namelist()
filenames = []
for suffix in suffixes:
current_file_name = None
for name in namelist:
if name.endswith(suffix) \
or name.endswith(suffix.lower()) \
or name.endswith(suffix.upper()):
current_file_name = name
filenames.append(current_file_name)
files = []
for filename in filenames:
if filename:
if dest_dir:
files.append(filename)
flz.extract(filename, dest_dir)
else:
files.append(flz.open(filename))
else:
files.append(None)
return files
def get_source_file(self, source, suffixes, dest_dir=None,
extra_url=None):
if not hasattr(source, 'read'):
if not source:
source = self.importer_instance.source \
if self.importer_instance.source else self.default_source
try:
url = source
if extra_url:
url += extra_url
remotehandle = urllib2.urlopen(url)
source = StringIO.StringIO(remotehandle.read())
remotehandle.close()
except ValueError:
# assume it is a local file
try:
source = open(source)
except IOError, msg:
return (None, msg)
except urllib2.URLError as error:
return (None, error.message)
if self.importer_instance.zipped:
try:
files = self.get_files_inside_zip(source, suffixes, dest_dir)
except zipfile.BadZipfile:
return (None, _(u"Bad zip file"))
if not files or None in files:
return (None,
_(u"Missing file(s) inside the zip file"))
source = files[0] if len(suffixes) == 1 else files
return (source, None)
class KMLManager(ImportManager):
u"""
KML importer
The filtr argument has to be defined as the exact name of the folder to be
imported
"""
XPATH = '//kml:Folder/kml:name[text()="%s"]/../kml:Placemark'
DEFAULT_XPATH = '//kml:Placemark'
def __init__(self, importer_instance, ns=''):
self.importer_instance = importer_instance
self.ns = ns
def get(self, source=None):
u"""
Get data from the source
Args:
- source (None): input file if not provided get it from the distant
source provided in the importer instance.
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, ''
source, msg = self.get_source_file(source, ['.kml'])
if msg:
return (0, 0, msg)
doc = source
# remove empty lines before declaration (bad XML file)
if hasattr(source, 'getvalue'):
splitted = source.getvalue().split('\n')
for idx, line in enumerate(splitted):
if line.strip():
break
doc = StringIO.StringIO("\n".join(splitted[idx:]))
tree = etree.parse(doc)
# try to get default namespace
if not self.ns:
self.ns = tree.getroot().nsmap[None]
xpath = self.XPATH % self.importer_instance.filtr \
if self.importer_instance.filtr else self.DEFAULT_XPATH
for placemark in tree.xpath(xpath,
namespaces={'kml':self.ns}):
name, point = None, None
pl_id = placemark.attrib.get('id')
pl_key = 'kml-%d' % self.importer_instance.pk
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__icontains':'%s:%s;' % (pl_key, pl_id),
'import_source':self.importer_instance.source}
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({
'import_source':self.importer_instance.source})
if not m:
dct['status'] = 'I'
m = Marker.objects.create(**dct)
new_item += 1
if pl_id:
m.set_key(pl_key, pl_id)
m.categories.clear()
for cat in self.importer_instance.categories.all():
m.categories.add(cat)
return (new_item, updated_item, msg)
@classmethod
def export(cls, queryset):
dct = {'name':settings.PROJECT_NAME,
'description':unicode(datetime.date.today()),
'locations':queryset.all()
}
filename = unicode_normalize(settings.PROJECT_NAME + dct['description']\
+ '.kml')
result = render_to_response('chimere/export.kml', dct)
return filename, result
class ShapefileManager(ImportManager):
u"""
Shapefile importer
"""
def get(self, source=None):
u"""
Get data from the source
Args:
- source (None): input file if not provided get it from the distant
source provided in the importer instance.
Return a tuple with:
- number of new item ;
- number of item updated ;
- error detail on error
"""
from models import Marker, Route
new_item, updated_item, msg = 0, 0, ''
tmpdir = tempfile.mkdtemp()
sources, msg = self.get_source_file(source,
['.shp', '.dbf', '.prj', '.shx'],
dest_dir=tmpdir)
if msg:
return (0, 0, msg)
if not sources:
return (0, 0, _(u"Error while reading the data source."))
# get the srid
srid = self.importer_instance.srid
if not srid:
prjfilename = tmpdir + os.sep + sources[2]
try:
from osgeo import osr
with open(prjfilename, 'r') as prj_file:
prj_txt = prj_file.read()
srs = osr.SpatialReference()
srs.ImportFromESRI([prj_txt])
srs.AutoIdentifyEPSG()
srid = srs.GetAuthorityCode(None)
except ImportError:
pass
if not srid:
# try with the default projection
srid = settings.CHIMERE_EPSG_DISPLAY_PROJECTION
shapefilename = tmpdir + os.sep + sources[0]
ds = DataSource(shapefilename)
lyr = ds[0]
# for this first version it is assumed that the first field is a
# id name and the second field is the name
id_name = lyr.fields[0] if len(lyr.fields) > 0 else None
# test if id_name is well guess
if id_name:
ids = lyr.get_fields(id_name)
if len(ids) != len(set(ids)):
id_name = None
lbl_name = None
if len(lyr.fields) > 1:
lbl_name = lyr.fields[1]
elif id_name:
lbl_name = id_name
if lyr.geom_type not in ('Point', 'LineString'):
return (0, 0, _(u"Type of geographic item of this shapefile "
u"is not managed by Chimère."))
geom_key = 'point' if lyr.geom_type == 'Point' else 'route'
geom_cls = Marker if lyr.geom_type == 'Point' else Route
indexes = []
for idx, feat in enumerate(lyr):
name = unicode(idx)
if lbl_name:
name = feat.get(lbl_name)
try:
name = unicode(name)
except UnicodeDecodeError:
try:
name = unicode(
name.decode(settings.CHIMERE_SHAPEFILE_ENCODING))
except:
continue
geom = feat.geom.wkt
dct = {geom_key:'SRID=%s;%s' % (srid, feat.geom.wkt),
'name':name
}
import_key = feat.get(id_name) if id_name else ''
item, updated, created = self.create_or_update_item(
geom_cls, dct, import_key)
if updated:
updated_item += 1
if created:
new_item += 1
"""
m = None
if id_name:
c_id = feat.get(id_name)
dct_import = {
'import_key__icontains':'%s:%s;' % (id_name, c_id),
'import_source':self.importer_instance.source}
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({
'import_source':self.importer_instance.source})
if not m:
dct['status'] = 'I'
m = Marker.objects.create(**dct)
new_item += 1
if id_name:
m.set_key(id_name, c_id)
m.categories.clear()
for cat in self.importer_instance.categories.all():
m.categories.add(cat)"""
# clean up
tmpdirs = set()
for src in sources:
dirs = os.sep.join(src.split(os.sep)[:-1])
if dirs:
tmpdirs.add(tmpdir + os.sep + dirs)
os.remove(tmpdir + os.sep + src)
for dr in tmpdirs:
os.removedirs(dr)
return (new_item, updated_item, msg)
@classmethod
def export(cls, queryset):
date = unicode(datetime.date.today())
tmp = tempfile.NamedTemporaryFile(suffix='.shp', mode='w+b')
tmp.close()
tmp_name = tmp.name
field_names = [field.name for field in queryset.model._meta.fields]
geo_field = getattr(queryset.model,
'point' if 'point' in field_names else 'route')._field
dr = Driver('ESRI Shapefile')
ds = lgdal.OGR_Dr_CreateDataSource(dr._ptr, tmp_name, None)
if ds is None:
raise Exception(_(u'Could not create file!'))
ogr_type = OGRGeomType(geo_field.geom_type).num
srs = SpatialReference(geo_field.srid)
layer = lgdal.OGR_DS_CreateLayer(ds, 'lyr', srs._ptr, ogr_type, None)
for field_name in ('name', 'category'):
fld = lgdal.OGR_Fld_Create(field_name, 4)
added = lgdal.OGR_L_CreateField(layer, fld, 0)
check_err(added)
feature_def = lgdal.OGR_L_GetLayerDefn(layer)
for item in queryset:
# duplicate items when in several categories
for category in item.categories.all():
feat = lgdal.OGR_F_Create(feature_def)
lgdal.OGR_F_SetFieldString(feat, 0,
str(unicode_normalize(item.name)[:80]))
lgdal.OGR_F_SetFieldString(feat, 1,
str(unicode_normalize(category.name)[:80]))
geom = getattr(item, geo_field.name)
if not geom:
continue
ogr_geom = OGRGeometry(geom.wkt, srs)
check_err(lgdal.OGR_F_SetGeometry(feat, ogr_geom._ptr))
check_err(lgdal.OGR_L_SetFeature(layer, feat))
# Cleaning up
check_err(lgdal.OGR_L_SyncToDisk(layer))
lgdal.OGR_DS_Destroy(ds)
lgdal.OGRCleanupAll()
# writing to a zip file
filename = unicode_normalize(settings.PROJECT_NAME) + '-' + date
buff = StringIO.StringIO()
zip_file = zipfile.ZipFile(buff, 'w', zipfile.ZIP_DEFLATED)
suffixes = ['shp', 'shx', 'prj', 'dbf']
for suffix in suffixes:
name = tmp_name.replace('.shp', '.' + suffix)
arcname = '.'.join((filename, suffix))
zip_file.write(name, arcname=arcname)
zip_file.close()
buff.flush()
zip_stream = buff.getvalue()
buff.close()
return filename, zip_stream
RE_NODE = re.compile('node\[([^\]]*)\]')
# manage deleted item from OSM
class OSMManager(ImportManager):
u"""
OSM importer/exporter
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.
"""
default_source = settings.CHIMERE_XAPI_URL
def get(self, source=None):
u"""
Get data from the source
Args:
- source (None): input file if not provided get it from the distant
source provided in the importer instance.
Return a tuple with:
- new items;
- updated items;
- error detail on error.
"""
source, msg = self.get_source_file(source, ['.osm'],
extra_url=self.importer_instance.filtr)
if not source:
return (0, 0, msg)
tree = etree.parse(source)
# only import node or ways
if tree.xpath('count(//way)') and tree.xpath('count(//node)'):
return self.import_ways(tree)
elif tree.xpath('count(//node)'):
return self.import_nodes(tree)
return 0, 0, _(u"Nothing to import")
def import_ways(self, tree):
from chimere.models import Marker, Route
msg, items, new_item, updated_item = "", [], 0 , 0
nodes = {}
for node in tree.xpath('//node'):
node_id = node.attrib.get('id')
for item in node:
k = item.attrib.get('k')
if node_id:
nodes[node_id] = '%s %s' % (node.get('lon'),
node.get('lat'))
for way in tree.xpath('//way'):
name = None
points = []
node_id = way.attrib.get('id')
version = way.attrib.get('version')
for item in way:
k = item.attrib.get('k')
if k == 'name':
name = item.attrib.get('v')
if item.tag == 'nd':
points.append(item.get('ref'))
if not name:
name = self.default_name
if not points:
continue
wkt = 'SRID=4326;LINESTRING(%s)' % ",".join([nodes[point_id]
for point_id in points if point_id in nodes])
dct = {'route':wkt,
'name':name,
'import_version':version}
item, updated, created = self.create_or_update_item(
Route, dct, node_id, version)
if updated:
updated_item += 1
if created:
new_item += 1
items.append(item)
return new_item, updated_item, msg
def import_nodes(self, tree):
from chimere.models import Marker
msg, items, new_item, updated_item = "", [], 0 , 0
for node in tree.xpath('//node'):
name = None
node_id = node.attrib.get('id')
if not node_id:
continue
version = node.attrib.get('version')
for item in node:
k = item.attrib.get('k')
if k == 'name':
name = item.attrib.get('v')
if not name:
name = self.default_name
point = 'SRID=4326;POINT(%s %s)' % (node.get('lon'),
node.get('lat'))
dct = {'point':point,
'name':name,
'import_version':version}
item, updated, created = self.create_or_update_item(
Marker, dct, node_id, version)
if updated:
updated_item += 1
if created:
new_item += 1
items.append(item)
return (new_item, updated_item, msg)
def put(self):
# first of all: reimport in order to verify that no changes has been
# made since the last import
from models import Marker
new_item, updated_item, msg = self.get()
# check if import is possible
if msg:
return 0, msg
if new_item:
return 0, _(u"New items imported - validate them before exporting")
if Marker.objects.filter(status='I').count():
return 0, _(u"There are items from a former import not yet "
u"validated - validate them before exporting")
# start import
api = OsmApi.OsmApi(api=settings.CHIMERE_OSM_API_URL,
username=settings.CHIMERE_OSM_USER,
password=settings.CHIMERE_OSM_PASSWORD)
api.ChangesetCreate({u"comment": u"Import from Chimère %s" % \
get_version()})
tag = RE_NODE.finddall(self.importer_instance.filtr)
if not tag:
return 0, _(u"Bad param")
tag = tag[0].split('=')
default_dct = {'tag':{tag[0]:tag[1]},
'import_source':self.importer_instance.source}
for idx, item in Marker.objects.filter(status='A',
categories=self.importer_instance.categories.all()):
dct = default_dct.update({
'name':item.name,
'lon':item.point.lon,
'lat':item.point.lat})
node = None
import_key = marker.get_key('OSM')
if not import_key:
node = OsmApi.NodeCreate(dct)
item.set_key('OSM', node['id'])
else:
dct['id'] = import_key
node = OsmApi.NodeUpdate(dct)
item.import_version = node['version']
item.save()
api.ChangesetClose()
return idx+1, None
|