--- a/src/hdalab/management/commands/import_insee_csv.py Thu Apr 12 01:27:16 2018 +0200
+++ b/src/hdalab/management/commands/import_insee_csv.py Wed Apr 11 12:19:47 2018 +0200
@@ -1,59 +1,65 @@
-# -*- coding: utf-8 -*-
-'''
-@author: raphv
-'''
-from django.core.management.base import BaseCommand, CommandError
-from SPARQLWrapper import SPARQLWrapper, JSON
-from hdalab.models import InseeCoords
-import json
-import csv
-import re
-import sys
-
-class Command(BaseCommand):
- '''
- Command to export tags
- '''
- args = '<path_to_csv_file>'
- options = ''
- help = """Imports Insee codes and geographic coordinates from a csv file"""
-
- def handle(self, *args, **options):
-
- if len(args) == 0 or not args[0]:
- raise CommandError("Give a CSV File to import")
-
- filename = args[0]
-
- csvfile = open(filename, "rb")
- dialect = csv.Sniffer().sniff(csvfile.read(1024))
- csvfile.seek(0)
- reader = csv.reader(csvfile, dialect)
- fieldstoget = [ 'ville', 'insee', 'latitude', 'longitude' ]
-
- for i,line in enumerate(reader):
- if i == 0:
- fields = {}
- minlength = 0
- for j,field in enumerate(line):
- for fieldname in fieldstoget:
- if re.search('(?i)%s' % fieldname, field):
- fields[fieldname] = j
- minlength = max(j,minlength)
- else:
- if len(line) > minlength:
- rawdata = dict([(k,line[v].strip()) for k,v in fields.iteritems()])
- #print "Processing line %d" % i
- #print rawdata
- try:
- latitude = float(rawdata['latitude'].replace(',','.'))
- longitude = float(rawdata['longitude'].replace(',','.')) if rawdata['longitude'] != '-' else 0
- insee = int(rawdata['insee'])
- ville = unicode(rawdata['ville'], 'iso-8859-1')
-
- InseeCoords.objects.get_or_create(insee=insee, city_name=ville, latitude=latitude, longitude=longitude)
-
- except:
- print line, "Error :", sys.exc_info()[1]
-
- csvfile.close()
\ No newline at end of file
+# -*- coding: utf-8 -*-
+'''
+Importe des codes INSEE et les coordonnées géographiques d'un fichier CSV.
+Crée des objects :class:`hdalab.models.dataviz.InseeCoords`.
+
+Attention cette commande de remplace pas ni n'efface de données existante dans la base (la clef étant le numéro INSEE).
+
+**Usage**: ``django-admin import_insee_csv <chemin_vers_fichier_csv>``
+
+'''
+from django.core.management.base import BaseCommand, CommandError
+from SPARQLWrapper import SPARQLWrapper, JSON
+from hdalab.models import InseeCoords
+import json
+import csv
+import re
+import sys
+
+class Command(BaseCommand):
+ '''
+ Command to export tags
+ '''
+ args = '<path_to_csv_file>'
+ options = ''
+ help = """Imports Insee codes and geographic coordinates from a csv file"""
+
+ def handle(self, *args, **options):
+
+ if len(args) == 0 or not args[0]:
+ raise CommandError("Give a CSV File to import")
+
+ filename = args[0]
+
+ csvfile = open(filename, "rb")
+ dialect = csv.Sniffer().sniff(csvfile.read(1024))
+ csvfile.seek(0)
+ reader = csv.reader(csvfile, dialect)
+ fieldstoget = [ 'ville', 'insee', 'latitude', 'longitude' ]
+
+ for i,line in enumerate(reader):
+ if i == 0:
+ fields = {}
+ minlength = 0
+ for j,field in enumerate(line):
+ for fieldname in fieldstoget:
+ if re.search('(?i)%s' % fieldname, field):
+ fields[fieldname] = j
+ minlength = max(j,minlength)
+ else:
+ if len(line) > minlength:
+ rawdata = dict([(k,line[v].strip()) for k,v in fields.iteritems()])
+ #print "Processing line %d" % i
+ #print rawdata
+ try:
+ latitude = float(rawdata['latitude'].replace(',','.'))
+ longitude = float(rawdata['longitude'].replace(',','.')) if rawdata['longitude'] != '-' else 0
+ insee = int(rawdata['insee'])
+ ville = unicode(rawdata['ville'], 'iso-8859-1')
+
+ InseeCoords.objects.get_or_create(insee=insee, city_name=ville, latitude=latitude, longitude=longitude)
+
+ except:
+ print line, "Error :", sys.exc_info()[1]
+
+ csvfile.close()