1 # -*- coding: utf-8 -*- |
1 # -*- coding: utf-8 -*- |
2 ''' |
2 ''' |
3 @author: raphv |
3 Importe des codes INSEE et les coordonnées géographiques d'un fichier CSV. |
|
4 Crée des objects :class:`hdalab.models.dataviz.InseeCoords`. |
|
5 |
|
6 Attention cette commande de remplace pas ni n'efface de données existante dans la base (la clef étant le numéro INSEE). |
|
7 |
|
8 **Usage**: ``django-admin import_insee_csv <chemin_vers_fichier_csv>`` |
|
9 |
4 ''' |
10 ''' |
5 from django.core.management.base import BaseCommand, CommandError |
11 from django.core.management.base import BaseCommand, CommandError |
6 from SPARQLWrapper import SPARQLWrapper, JSON |
12 from SPARQLWrapper import SPARQLWrapper, JSON |
7 from hdalab.models import InseeCoords |
13 from hdalab.models import InseeCoords |
8 import json |
14 import json |
15 Command to export tags |
21 Command to export tags |
16 ''' |
22 ''' |
17 args = '<path_to_csv_file>' |
23 args = '<path_to_csv_file>' |
18 options = '' |
24 options = '' |
19 help = """Imports Insee codes and geographic coordinates from a csv file""" |
25 help = """Imports Insee codes and geographic coordinates from a csv file""" |
20 |
26 |
21 def handle(self, *args, **options): |
27 def handle(self, *args, **options): |
22 |
28 |
23 if len(args) == 0 or not args[0]: |
29 if len(args) == 0 or not args[0]: |
24 raise CommandError("Give a CSV File to import") |
30 raise CommandError("Give a CSV File to import") |
25 |
31 |
26 filename = args[0] |
32 filename = args[0] |
27 |
33 |
28 csvfile = open(filename, "rb") |
34 csvfile = open(filename, "rb") |
29 dialect = csv.Sniffer().sniff(csvfile.read(1024)) |
35 dialect = csv.Sniffer().sniff(csvfile.read(1024)) |
30 csvfile.seek(0) |
36 csvfile.seek(0) |
31 reader = csv.reader(csvfile, dialect) |
37 reader = csv.reader(csvfile, dialect) |
32 fieldstoget = [ 'ville', 'insee', 'latitude', 'longitude' ] |
38 fieldstoget = [ 'ville', 'insee', 'latitude', 'longitude' ] |
33 |
39 |
34 for i,line in enumerate(reader): |
40 for i,line in enumerate(reader): |
35 if i == 0: |
41 if i == 0: |
36 fields = {} |
42 fields = {} |
37 minlength = 0 |
43 minlength = 0 |
38 for j,field in enumerate(line): |
44 for j,field in enumerate(line): |
48 try: |
54 try: |
49 latitude = float(rawdata['latitude'].replace(',','.')) |
55 latitude = float(rawdata['latitude'].replace(',','.')) |
50 longitude = float(rawdata['longitude'].replace(',','.')) if rawdata['longitude'] != '-' else 0 |
56 longitude = float(rawdata['longitude'].replace(',','.')) if rawdata['longitude'] != '-' else 0 |
51 insee = int(rawdata['insee']) |
57 insee = int(rawdata['insee']) |
52 ville = unicode(rawdata['ville'], 'iso-8859-1') |
58 ville = unicode(rawdata['ville'], 'iso-8859-1') |
53 |
59 |
54 InseeCoords.objects.get_or_create(insee=insee, city_name=ville, latitude=latitude, longitude=longitude) |
60 InseeCoords.objects.get_or_create(insee=insee, city_name=ville, latitude=latitude, longitude=longitude) |
55 |
61 |
56 except: |
62 except: |
57 print line, "Error :", sys.exc_info()[1] |
63 print line, "Error :", sys.exc_info()[1] |
58 |
64 |
59 csvfile.close() |
65 csvfile.close() |