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.jsonso 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:mysqlormssqldatabase.connectionDetails:- For
mysqldatabases, follow the format here (either as a connection string or JSON option object): https://www.npmjs.com/package/mysql#connection-options - For
mssqldatabases, 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: ASELECTstatement that only selects a column namedpostcode(use an alias if necessary). All postcodes selected will be geocoded. It is recommended you limit yourSELECTstatement to 5000 at a time for performance reasons.database.setPostcodeSql: AnINSERTorUPDATEstatement 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, @postcodeformssql).
- If using a
mysqldatabase. The script will not exit until you pressCtrl+C. - Example
settings.jsonfiles use a database with a singlepostalcodetable with three columns:postcodeVARCHAR(6)latitudeFLOATlongitudeFLOAT
To run the script, run node app.js.