Subversion Repositories Misc

[/] [dslmap/] [forgottenislanderbot/] [fib-cadb2sql.py] - Blame information for rev 15

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 15 art
#!/usr/bin/env python
2
 
3
# forgottenislanderbot - makes a DSL coverage map from Bell Aliant's website.
4
# Copyright (c) 2010 Art Ortenburger
5
#
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU General Public License
8
# as published by the Free Software Foundation; either version 2
9
# of the License, or any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
#
20
 
21
# fib-cadb2sql.py
22
# by Art Ortenburger IV <utrrrongeeb a users , sf , net>
23
# written 2010-01-13 -
24
#
25
# forgottenislanderbot -- TSV db -> SQlite db
26
#
27
# Converts PEI Government civic address databases
28
# from tab-separated-values text to Python SQlite.
29
# Removes and adds columns in the process.
30
#
31
 
32
 
33
import sys
34
import csv
35
import sqlite3
36
import fibmod
37
 
38
print sys.argv[0],"""(forgottenislanderbot TSV db -> sqlite db)    Version 0.1
39
Copyright (c) 2010 Art Ortenburger. Licensed under the GNU GPL; no warranty."""
40
 
41
 
42
def processFile(inFilename):
43
    """ processes one tab-separated-values PEI gov't civic address database,
44
        adding it to the new sqlite database.
45
        I suspect duplicates are handled very poorly.
46
        inFilename: string; TSV to open."""
47
    try:
48
        ftr = open(inFilename,"rb") # file to read
49
        tdp = csv.reader(ftr,"excel-tab") # tsv data parser
50
        # loop through list of values, adding each one in turn.
51
        for ccar in tdp: # current civic address
52
            qdbC.execute("insert into dsl values (?,?,?,?,?,?)",
53
                         [ int(ccar[0]),ccar[1],ccar[2],
54
                           float(ccar[5]),float(ccar[6]),0 ])
55
    except Exception, errdet:
56
        print "error parsing input: ",errdet
57
    finally:
58
        try:
59
            ftr.close()
60
        except:
61
            pass
62
 
63
 
64
# list of files to add to new sqlite
65
filesToParse = []
66
 
67
# minimum two arguments, of course
68
if len(sys.argv)<2:
69
    print """
70
Usage:  fib-cadb2sql.py (outputFile.sqlite) (inputFile1.tsv)...
71
"""
72
    exit(0)
73
else:
74
    filesToParse = sys.argv[2:]
75
 
76
qdbCon = sqlite3.connect(sys.argv[1]) # output SQlite database connection
77
qdbC = qdbCon.cursor() # output SQlite database cursor
78
 
79
# create db tables
80
# probably handles existing database poorly 
81
qdbC.execute("create table dsl (\
82
civic integer, road text, town text, lat real, long real, dsl integer)")
83
 
84
# loop through input TSV list, parsing and adding to database
85
for curFile in filesToParse:
86
    processFile(curFile)
87
 
88
try:
89
    qdbC.close()
90
except:
91
    pass
92
try:
93
    qdbCon.commit()
94
    qdbCon.close()
95
except Exception, errdet:
96
    print "Error saving database:",errdet
97
 
98
print "done"
99
 
100
# TSV format is
101
# [#]   [RD]    [CMTY]  [CNTY]  [LAT]   [LON]   [COP]   [FIRE]  [MED]
102
# SQlite format is
103
# [#]   [RD]    [CMTY]  [LAT]   [LON]   [DSL]
104
# DSL options: [ 0: unknown, 1: no, 2: basic, 3: ultra ]