| author | cavaliet |
| Tue, 24 Jun 2014 16:58:34 +0200 | |
| changeset 281 | bc0f26b1acc2 |
| parent 272 | 1c774f7a0341 |
| child 693 | 09e00f38d177 |
| permissions | -rw-r--r-- |
| 123 | 1 |
# -*- coding: utf-8 -*- |
2 |
''' |
|
3 |
@author: raphv |
|
4 |
''' |
|
5 |
from django.core.management.base import BaseCommand, CommandError |
|
6 |
from SPARQLWrapper import SPARQLWrapper, JSON |
|
7 |
from hdalab.models import InseeCoords |
|
|
281
bc0f26b1acc2
Hdalab : commands now work after update. Requests update with a dbpedia url from settings.
cavaliet
parents:
272
diff
changeset
|
8 |
import json |
| 123 | 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: |
|
| 231 | 45 |
rawdata = dict([(k,line[v].strip()) for k,v in fields.iteritems()]) |
| 123 | 46 |
#print "Processing line %d" % i |
47 |
#print rawdata |
|
48 |
try: |
|
49 |
latitude = float(rawdata['latitude'].replace(',','.')) |
|
| 124 | 50 |
longitude = float(rawdata['longitude'].replace(',','.')) if rawdata['longitude'] != '-' else 0 |
| 123 | 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: |
|
| 124 | 57 |
print line, "Error :", sys.exc_info()[1] |
| 123 | 58 |
|
59 |
csvfile.close() |