summaryrefslogtreecommitdiff
path: root/ishtar_common/management/commands/import_geofla_csv.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2018-02-18 12:35:13 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2018-02-18 12:35:13 +0100
commitac36f95a1688f1fc63541e61166ae1c00f650b94 (patch)
treece2da2b76df5117ed47bacbbd917316cd762c22c /ishtar_common/management/commands/import_geofla_csv.py
parent7430e6cbfb76ee2ccb6675e63b88a954117dd31f (diff)
downloadIshtar-ac36f95a1688f1fc63541e61166ae1c00f650b94.tar.bz2
Ishtar-ac36f95a1688f1fc63541e61166ae1c00f650b94.zip
Import geofla: continue import when the limit is not available
Diffstat (limited to 'ishtar_common/management/commands/import_geofla_csv.py')
-rw-r--r--ishtar_common/management/commands/import_geofla_csv.py56
1 files changed, 37 insertions, 19 deletions
diff --git a/ishtar_common/management/commands/import_geofla_csv.py b/ishtar_common/management/commands/import_geofla_csv.py
index 642d59a5f..5a4d00be1 100644
--- a/ishtar_common/management/commands/import_geofla_csv.py
+++ b/ishtar_common/management/commands/import_geofla_csv.py
@@ -22,6 +22,7 @@ import sys
from django.core.management.base import BaseCommand
from django.contrib.gis.geos import GEOSGeometry, Point
+from django.db import transaction
from django.db.utils import DataError
from ishtar_common.models import Town
@@ -42,6 +43,21 @@ class Command(BaseCommand):
'--srid', type=int, default=2154, dest='srid',
help='SRID uses. Default: 2154.')
+ def get_town(self, num_insee, name, default_year):
+ q = Town.objects.filter(numero_insee=num_insee)
+ created = False
+ if q.count():
+ if q.filter(year=default_year).count():
+ town = q.filter(year=default_year).all()[0]
+ else:
+ town = q.order_by('-year').all()[0]
+ else:
+ created = True
+ town = Town(name=name,
+ numero_insee=num_insee)
+ return town, created
+
+ @transaction.atomic
def handle(self, *args, **options):
csv_file = options['csv_file']
default_year = options['year']
@@ -61,44 +77,46 @@ class Command(BaseCommand):
num_insee = row['INSEE_COM']
if len(num_insee) < 5:
num_insee = '0' + num_insee
- q = Town.objects.filter(numero_insee=num_insee)
- created = False
- if q.count():
- if q.filter(year=default_year).count():
- town = q.filter(year=default_year).all()[0]
- else:
- town = q.order_by('-year').all()[0]
- else:
- created = True
+ name = row['NOM_COM_M']
+ town, created = self.get_town(num_insee, name, default_year)
+ if created:
nb_created += 1
- town = Town(name=row['NOM_COM_M'],
- numero_insee=num_insee)
geom = row['wkt_geom'].upper()
if 'MULTI' not in geom:
geom = geom.replace('POLYGON', 'MULTIPOLYGON(') + ')'
- town.limit = GEOSGeometry(geom, srid=srid)
+ values = {'limit': GEOSGeometry(geom, srid=srid)}
if 'X_CENTROID' in row:
- town.center = Point(float(row['X_CENTROID']),
- float(row['Y_CENTROID']), srid=srid)
+ values['center'] = Point(
+ float(row['X_CENTROID']), float(row['Y_CENTROID']),
+ srid=srid)
else:
- town.center = None
+ values['center'] = None
if not town.year and default_year:
- town.year = default_year
+ values['year'] = default_year
if 'SUPERFICIE' in row:
- town.surface = row['SUPERFICIE']
+ values['surface'] = row['SUPERFICIE']
else:
- town.surface = None
+ values['surface'] = None
if not created:
nb_changed += 1
+ for k in values:
+ setattr(town, k, values[k])
try:
- town.save()
+ with transaction.atomic():
+ town.save()
except DataError:
+ """
new_limit = str(GEOSGeometry(geom, srid=srid).simplify(
preserve_topology=True))
if 'MULTI' not in new_limit:
new_limit = new_limit.replace(
'POLYGON', 'MULTIPOLYGON(') + ')'
town.limit = new_limit
+ """
+ town, created = self.get_town(num_insee, name, default_year)
+ values['limit'] = None
+ for k in values:
+ setattr(town, k, values[k])
town.save()
if quiet:
return