How to bulk geocode addresses in a database


Write an Xbasic script that loops over all data and updates fields for DBF and SQL tables.

The Xbasic function called geocode_address() that allows you to convert addresses to latitude and longitude. See geocode_address Function for a full description. The function does a web service call to accomplish the conversion, which can take a good fraction of a second.

To bulk geocode a table, you could write an Xbasic script that looped over all of the data and updated fields.

Here is an Xbasic loop for .DBF tables:

dim t as p
t ="addresses")
dim pa as p
while .not. t.fetch_eof()
    pa = geocode_address(t.address,"Google")
    t.latitude =
    t.longitude = pa.lon
end while

For a SQL table, the code is similar in spirit.

dim cn as sql::Connection
flag ="::Name::<MyConnectionString>")
dim sql as c 
'execute a query to get the primary key of each record you want to update
sql = "select id from table1"
rs = cn.ResultSet
dim flag as l 
'check to see if any rows in resultset
flag = rs.nextRow()
delete args 
dim args as sql::Arguments
dim sql1 as c 
'this assumes that the address field has the full address -- 
'if not, modify the sql query appropriately
sql1 = "select address from table1 where id = :primaryKey" 
sql2 = "update table1 set latitude = :lat, longitude = :lon where id = :primaryKey"
dim pa as p
dim rs2 as sql::ResultSet
while flag 
    flag = cn.Execute(sql1,args)
    rs2 = cn.ResultSet
    pa = geocode_address("address"),"Google")
    flag = cn.execute(sql2,args)
    flag = rs.nextRow()
end flag

If you also want to update a Geography field named Location, the sql2 query would change to:

UPDATE table1 SET latitude = :lat, longitude = :lon, 
    location = GeogCreateLocation(:lon, :lat) WHERE id = :primaryKey
GeogCreateLocation is a Portable SQL function. To make it work in code, you may have to set your connection to use Portable SQL, e.g. cn.PortableSQLEnabled = .t.

Another way to geocode addresses would be to use