summaryrefslogtreecommitdiff
path: root/ishtar_common/management
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2017-10-26 20:58:21 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2017-10-26 20:58:21 +0200
commitb6676aa6ad94739743ac613e2f19a8b29b9705eb (patch)
treea3d98f35885d9a5d20d9eb4004eb16b131edfd20 /ishtar_common/management
parent5fb235649207a15f54c69f3ac9c2f0659fc2436d (diff)
downloadIshtar-b6676aa6ad94739743ac613e2f19a8b29b9705eb.tar.bz2
Ishtar-b6676aa6ad94739743ac613e2f19a8b29b9705eb.zip
Command: import geofla csv
Diffstat (limited to 'ishtar_common/management')
-rw-r--r--ishtar_common/management/commands/import_geofla_csv.py85
1 files changed, 85 insertions, 0 deletions
diff --git a/ishtar_common/management/commands/import_geofla_csv.py b/ishtar_common/management/commands/import_geofla_csv.py
new file mode 100644
index 000000000..b7cb2b604
--- /dev/null
+++ b/ishtar_common/management/commands/import_geofla_csv.py
@@ -0,0 +1,85 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Copyright (C) 2017 É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 Affero 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 Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# See the file COPYING for details.
+
+import csv
+
+from django.core.management.base import BaseCommand
+from django.contrib.gis.geos import GEOSGeometry, Point
+
+from ishtar_common.models import Town
+
+
+class Command(BaseCommand):
+ help = 'Import GEOFLA csv'
+
+ def add_arguments(self, parser):
+ parser.add_argument('csv_file')
+ parser.add_argument(
+ '--year', type=int, default=2014, dest='year',
+ help='Default year to affect to the town')
+
+ def handle(self, *args, **options):
+ csv_file = options['csv_file']
+ self.stdout.write('* Opening file {}\n'.format(csv_file))
+ default_year = options['year']
+ nb_created, nb_changed = 0, 0
+ with open(csv_file, 'rb') as csvfile:
+ reader = csv.DictReader(csvfile)
+ for idx, row in enumerate(reader):
+ self.stdout.write('Processing town %d.\r' % (idx + 1))
+ self.stdout.flush()
+ num_insee = row['INSEE_COM']
+ if len(num_insee) < 5:
+ num_insee = '0' + num_insee
+ q = Town.objects.filter(numero_insee=num_insee)
+ changed, created = False, False
+ if q.count():
+ town = q.all()[0]
+ else:
+ changed = True
+ created = True
+ nb_created += 1
+ town = Town(name=row['NOM_COM'],
+ numero_insee=num_insee)
+ if not town.limit:
+ changed = True
+ geom = row['wkt_geom'].upper()
+ if 'MULTI' not in geom:
+ geom = geom.replace('POLYGON', 'MULTIPOLYGON(') + ')'
+ town.limit = GEOSGeometry(geom, srid=2154)
+ if not town.center:
+ changed = True
+ town.center = Point(float(row['X_CENTROID']),
+ float(row['Y_CENTROID']), srid=2154)
+ if not town.year and default_year:
+ changed = True
+ town.year = default_year
+ if not town.surface:
+ changed = True
+ town.surface = row['SUPERFICIE']
+ if changed:
+ if not created:
+ nb_changed += 1
+ town.save()
+ self.stdout.write('\n* {} town created'.format(nb_created))
+ self.stdout.write('\n* {} town changed\n'.format(nb_changed))
+ self.stdout.flush()
+
+
+