A script to retrieve the latitude and longitude for a set of Canadian postal codes using batch geocoding from the MapQuest API. Inserts the mapping into the database. Script could easily be modified to support any batch geocoding, if desired (simply change the URL request).
Supports connecting a MySQL or MSSQL database (specified in the settings.json file, see below).
-
A MapQuest API key: https://developer.mapquest.com/user/me/profile
-
Copy the correct settings file to
settings.json
. Example files are included. -
Update
settings.json
so that the values match your database configuration. See details below. -
Ensure your postal codes are cleansed (correct format), with no space between characters (script expects
A#A#A#
format) -
Ensure your SQL query syntax is correct, you may want to only geocode 1 postal code at a time until you are done debugging to preserve your MapQuest quota.
-
Install node modules
npm install
You must use the following settings:
apikey
: The API key obtained from the MapQuest developer portal.database.type
:mysql
ormssql
database.connectionDetails
:- For
mysql
databases, follow the format here (either as a connection string or JSON option object): https://www.npmjs.com/package/mysql#connection-options - For
mssql
databases, follow the format here (either as a connection string or JSON option object): https://www.npmjs.com/package/mssql#general-same-for-all-drivers
- For
database.selectAllPostcodesSql
: ASELECT
statement that only selects a column namedpostcode
(use an alias if necessary). All postcodes selected will be geocoded. It is recommended you limit yourSELECT
statement to 5000 at a time for performance reasons.database.setPostcodeSql
: AnINSERT
orUPDATE
statement to set the latitude and longitude values. The statement will be passed 3 variables (latitude
,longitude
, andpostalcode
), and so must contain three parameterized-variables (?
s in the previous order formysql
,@latitude, @longitude, @postcode
formssql
).
- If using a
mysql
database. The script will not exit until you pressCtrl+C
. - Example
settings.json
files use a database with a singlepostalcode
table with three columns:postcode
VARCHAR(6)latitude
FLOATlongitude
FLOAT
To run the script, run node app.js
.