web/hdalab/management/commands/import_insee_csv.py
changeset 271 8f77cf71ab02
parent 265 73f19fa4f997
child 272 1c774f7a0341
equal deleted inserted replaced
265:73f19fa4f997 271:8f77cf71ab02
     1 # -*- coding: utf-8 -*-
       
     2 '''
       
     3 @author: raphv
       
     4 '''
       
     5 from django.core.management.base import BaseCommand, CommandError
       
     6 import django.utils.simplejson as json
       
     7 from SPARQLWrapper import SPARQLWrapper, JSON
       
     8 from hdalab.models import InseeCoords
       
     9 import csv
       
    10 import re
       
    11 import sys
       
    12 
       
    13 class Command(BaseCommand):
       
    14     '''
       
    15     Command to export tags
       
    16     '''
       
    17     args = '<path_to_csv_file>'
       
    18     options = ''
       
    19     help = """Imports Insee codes and geographic coordinates from a csv file"""
       
    20         
       
    21     def handle(self, *args, **options):
       
    22 
       
    23         if len(args) == 0 or not args[0]:
       
    24             raise CommandError("Give a CSV File to import")
       
    25         
       
    26         filename = args[0]
       
    27         
       
    28         csvfile = open(filename, "rb")
       
    29         dialect = csv.Sniffer().sniff(csvfile.read(1024))
       
    30         csvfile.seek(0)
       
    31         reader = csv.reader(csvfile, dialect)
       
    32         fieldstoget = [ 'ville', 'insee', 'latitude', 'longitude' ]
       
    33         
       
    34         for i,line in enumerate(reader):
       
    35             if i == 0:
       
    36                 fields = {}
       
    37                 minlength = 0
       
    38                 for j,field in enumerate(line):
       
    39                     for fieldname in fieldstoget:
       
    40                         if re.search('(?i)%s' % fieldname, field):
       
    41                             fields[fieldname] = j
       
    42                             minlength = max(j,minlength)
       
    43             else:
       
    44                 if len(line) > minlength:
       
    45                     rawdata = dict([(k,line[v].strip()) for k,v in fields.iteritems()])
       
    46                     #print "Processing line %d" % i
       
    47                     #print rawdata
       
    48                     try:
       
    49                         latitude = float(rawdata['latitude'].replace(',','.'))
       
    50                         longitude = float(rawdata['longitude'].replace(',','.')) if rawdata['longitude'] != '-' else 0
       
    51                         insee = int(rawdata['insee'])
       
    52                         ville = unicode(rawdata['ville'], 'iso-8859-1')
       
    53                         
       
    54                         InseeCoords.objects.get_or_create(insee=insee, city_name=ville, latitude=latitude, longitude=longitude)
       
    55                         
       
    56                     except:
       
    57                         print line, "Error :", sys.exc_info()[1]
       
    58         
       
    59         csvfile.close()