source: src/django_gheat/gheat/management/commands/import_csv.py@ 9142

Last change on this file since 9142 was 9142, checked in by dennisw, 14 years ago

Figuring out the tile generation & data flow.

  • Property svn:executable set to *
File size: 3.5 KB
Line 
1#!/usr/bin/env python
2#
3###########################################
4#
5# Script for importing .csv files
6#
7# In theory, only the -f option is needed, but for overview's sake, please use the others aswell.
8# -f = location of the .netxml, e.g. '/home/test.csv'
9# -m = name of the dataset, e.g. 'Walk in park' or 'Trip with boat'
10# -g = your name
11# -e = your email address
12#
13# (Run from project root)
14# ./manage.py import_csv -f <file location> -m <dataset name> -g <username> -e <email>
15#
16# Make sure the variables in this script match the column numbers in your file e.g.;
17# Lat is read from the first column [0], if the lat in your file is in the 4th column, change
18# 'lat = row[0]' to 'lat = row[3]'.
19# Also, take note of the 'replace()' and 'strip()' functions. These should probably be edited aswell.
20#
21# Dennis Wagenaar
22# d.wagenaar@gmail.com
23#
24###########################################
25
26from django.core.management import setup_environ
27from django.core.management.base import BaseCommand
28from optparse import OptionParser, make_option
29import settings
30setup_environ(settings)
31from gheat.models import *
32import datetime
33import csv
34
35# Function that imports a csv and processes it.
36def import_file(location, meetrondje, gebruiker, email, antenne, kaart):
37
38 # Creating some objects that have to be created only once. Uses 'get_or_create' to rule out duplicates.
39 # Creating user object. Depends on basecommand options.
40 g, created = Gebruiker.objects.get_or_create(naam=gebruiker , email=email)
41 # Creating equipment object. Depends on basecomand options.
42 a, created = Apparatuur.objects.get_or_create(antenne=antenne , kaart=kaart)
43 # Creating dataset object. Depends on baseommand options.
44 mr = MeetRondje.objects.create(datum=datetime.datetime.now() , naam=meetrondje , gebruiker=g , apparatuur=a)
45
46 # Open the csv.
47 csvfile = csv.reader(open(location, 'rb'), delimiter='\t')
48 # Read every row individually and extract the required data.
49 for row in csvfile:
50 # '.replace' & '.strip' are for replacing and stripping some unwished characters. Edit where necessary.
51 lat = row[0].replace('N ', '')
52 lon = row[1].replace('E ', '')
53 ssid = row[2].strip('( )')
54 bssid = row[4].strip('( )')
55 enc = row[8]
56 if enc[2] == '1': enc = True
57 else: enc = False
58 sig = 100 # Get's fix soon
59 print lat, lon, ssid, bssid, enc, sig
60
61 # Creating accespoint objects. Avoiding duplicates.
62 ap, created = Accespoint.objects.get_or_create(mac=bssid, ssid=ssid, encryptie=enc)
63 # Creating the measurement objects.
64 m = Meting.objects.create(meetrondje=mr, accespoint=ap, latitude=lat, longitude=lon, signaal=sig)
65
66# Basecommand with options. This gives the user some control over the execution of this script.
67class Command(BaseCommand):
68 option_list = BaseCommand.option_list + (
69 make_option('-f', '--location', dest='location', default='location'),
70 make_option('-m', '--meetrondje', dest='meetrondje', default='rondje'),
71 make_option('-g', '--gebruiker', dest='gebruiker', default='username'),
72 make_option('-e', '--email', dest='email', default='foo@bar.org'),
73 make_option('-a', '--antenne', dest='antenne', default='geen'),
74 make_option('-k', '--kaart', dest='kaart', default='interne kaart'),
75 )
76
77 # The function 'import_file' will be executed with the default option values, or the values specified by the user.
78 def handle(self, *args, **options):
79 import_file(options['location'],options['meetrondje'],options['gebruiker'],options['email'],options['antenne'],options['kaart'])
Note: See TracBrowser for help on using the repository browser.