File: geocode_addresses.py

package info (click to toggle)
mapserver 6.0.1-3.2%2Bdeb7u2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 17,940 kB
  • sloc: ansic: 209,190; cpp: 53,949; cs: 12,101; python: 5,323; perl: 3,332; makefile: 841; lex: 706; yacc: 609; java: 466; xml: 379; sh: 253; tcl: 158; ruby: 53
file content (88 lines) | stat: -rwxr-xr-x 2,146 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/python

# Script : geocode_addresses.py
#
# Purpose: simple script to read a csv file, geocode the addresses
# with the Geocoder API and write to a new shapefile
#
# $Id: geocode_addresses.py 9921 2010-03-06 17:08:20Z tomkralidis $
#

import sys
import csv
import mapscript
import urllib
import urllib2
from lxml import etree

# uses dbfpy http://pypi.python.org/pypi/dbfpy/2.2.1
from dbfpy.dbf import *

# example invocation
# ./reproj.py ./foo.csv address_colnum, city_colnum, stateprov_colnum ./bar.csv

# check input parameters
if (len(sys.argv) != 6):
    print sys.argv[0] + \
    " <csvfile> <address_colnum> <city_colnum> <stateprov_colnum> <outfile>"
    sys.exit(1)

# geocoder base request URL
sGeocoderUrl = "http://www.geocoder.ca/?geoit=xml&locate="

# set csv record indices
sAddress   = int(sys.argv[2])
sCity      = int(sys.argv[3])
sStateProv = int(sys.argv[4])

# open file
fCsv = open(sys.argv[1], 'r')

# read csv 
csvIn = csv.reader(fCsv)

# create output shp/shx
msSFOut = mapscript.shapefileObj(sys.argv[-1], 1)

# create output dbf
dbfOut = Dbf(sys.argv[-1]+".dbf", new=True)

# add fields
dbfOut.addField(
    ("address", 'C', 255),
    ("city", 'C', 255),
    ("stateprov", 'C', 255),
    ("x", 'N', 6,2),
    ("y", 'N', 7,2)
)

for aRow in csvIn:
    # concatenate request params
    sRequest = aRow[sAddress] + "," + aRow[sCity] + "," + aRow[sStateProv]

    # contatenate request params (escaped) and base URL  
    sRequest = sGeocoderUrl + urllib.quote(sRequest)

    # make the HTTP request
    ul2Response = urllib2.urlopen(sRequest)

    # serialize into etree XML object
    etTree = etree.parse(ul2Response)
    sY = float(etTree.find('latt').text)
    sX = float(etTree.find('longt').text)

    # serialize mapscript pointObj and add to shapefile
    msPoint = mapscript.pointObj(sX, sY)
    msSFOut.addPoint(msPoint)

    # add dbf record
    dRec=dbfOut.newRecord()
    dRec['address']   = aRow[sAddress]
    dRec['city']      = aRow[sCity]
    dRec['StateProv'] = aRow[sStateProv]
    dRec['x']         = sX
    dRec['y']         = sY
    dRec.store()
# close files
fCsv.close()
dbfOut.close()