25 Mar 2008, 00:11
Generic-user-small

lcp (2 posts)

I hope that someone can help me with the Groovy script presented in
the “Geocoding Your Data” section. I was able to use the first part
successfully to load comma-delimited data into my postgress database, but
I’m having trouble with the instructions on building in the geocoder.

I have attached my code below – I have worked up to the point
just before 9.3 “Adding PostGIS fields”.

Currently, I get the error:
startup failed, notworking_fixer.groovy: 100: unexpected token: } @
line 100, column 1.
1 error

I think I am misunderstanding where to put the blocks of code within
my script after adding the expanded ddl statement.
I’m not sure if class Addr and public Addr go right after the ddl
statement. Then, I’m not sure what } ... } means written in the book
after the public Addr statement. Also I don’t know if I have the
geocode method or the Fixer statement in the right places.

Any help is much appreciated!

Lela

Groovy Script Pg 222

outputFile = new File(“college.sql”)
if(outputFile.exists()){ outputFile.delete() }
ddl = ”””
BEGIN;
CREATE TABLE college (
“id” numeric PRIMARY KEY,
“name” varchar(255),
“address” varchar(255),
“city” varchar(255),
“state” varchar(255),
“zip” varchar(255),
“lat” varchar(255),
“lon” varchar(255),
“address_n” varchar(255),
“city_n” varchar(255),
“state_n” varchar(255),
“zip_n” varchar(255));
””“

class Addr{
String id
String name
String address
String city
String state
String zip
String lat
String lon
String addressNormalized
String cityNormalized
String stateNormalized
String zipNormalized

public Addr(String[] tokens){
id = tokens0.noQuote()
name = tokens1.noQuote()
address = tokens2.noQuote()
city = tokens3.noQuote()
state = tokens4.noQuote()
zip = tokens5.noQuote()

}}

public boolean geocode(){
def urlStart = “http://rpc.geocoder.us/service/csv?address=
def urlBody = ”${address},${city},${state},${zip}”
def urlEncoded = urlStart + URLEncoder.encode(urlBody, “UTF-8”)
new URL.eachLine{ line ->
println ”\t${line}”
if(line.startsWith(“2”)){
addressNormalized = “NOT FOUND
}
else{
def tokens = line.getNext(6)
lat = tokens0
lon = tokens1
addressNormalized = tokens2.fixQuote()
cityNormalized = tokens3.fixQuote()
stateNormalized = tokens4.fixQuote()
zipNormalized = tokens5.fixQuote()
}
}
return addressNormalized != “NOT FOUND
}

outputFile.append(ddl)
// new code
// ED: how do I make this bold?
insertStart = ””“insert into college (“id”, “name”, “address”,
“city”, “state”, “zip”, “lat”, “lon”, “address_n”, “city_n”, “state_n”, “zip_n”) values(”””
insertEnd = “);”
counter = 0
inputFile = new File(“sample.csv”)

use(Fixer){
inputFile.eachLine{ line ->
String[] tokens = line.getNext(6)
if(counter == 0) {
/* skip the headers */
counter++
}
else{
println ”${counter+} ${tokens1.fixQuote()}” //show what’s going on
addr = new Addr(tokens)
addr.geocode() ? found
+ : notFound++
insertMiddle = ””
for(i in 0..5){
insertMiddle += ”${tokens[i].fixQuote()},”
}
insertMiddle += addr.toSql()
//insertMiddle = insertMiddle[0..-2] //strip off trailing comma
outputFile.append(”${insertStart}${insertMiddle}${insertEnd}\n”)

//write out current status
statusFile = new File(“status.txt”)
statusFile.append(new Status(counter, found, notFound).toString()
}
}
}

outputFile.append(“END;”)

class Fixer{
static String fixQuote(String self){
self = self0 + self[1..-2].replaceAll(”\’”, ”\’\’”) + self[-1]
if(self.startsWith(”\”“)){
return ”’” + self[1..-2] + ”’”
}
else{
return self
}
}

static String[] getNext(String self, int numberOf){
def list = []
def st = new StringTokenizer(self, ”,”)
numberOf.times{
def thisToken = st.nextToken()
while(thisToken.startsWith(”\”“) && !thisToken.endsWith(”\”“) ){
thisToken += ”,” + st.nextToken()
}
list << thisToken
}
return list
}
}

26 Mar 2008, 18:13
Generic-user-small

Greg Cermak (1 post)

In chapter 9: Geocoding your data of ‘GIS for Web Developers’ the source for the following methods which should be in class Addr are missing

  • noQuote()
  • toSQL()

This chapter was poorly editted. Also there are no source code listings available to check for download.

Thanks

Greg Cermak

31 Mar 2008, 23:45
Generic-user-small

lcp (2 posts)

Thanks very much, I’ll write to Scott Davis, the author, and see if he has the code or could post it here.

Lela

  You must be logged in to comment