Learnings for Mar 6–7: a command-line script to modify a CSV file, fetch geolocation data, and convert to JSON.
Node has a file system module for reading and writing files.
There's an npm package written by @wyattdanger called Geocoder that can call Google's geocoding. As explained in his blog post after requiring the module, you can use two methods: geocode
and reverseGeocode
. For the purpose of this project, we use geocode
to get the latitude and longitude of the directors' addresses.
An example of the geocode
method is as follows.
var geocoder = require('geocoder');
geocoder.geocode("Atlanta, GA", function ( err, data ) {
// do stuff with data
});
- I ran into trouble attaching the lat and lng coordinate to the appropriate array, since the call to geocode is asynchronous. Reading articles like this one helped me understand callback functions better. I ended up just chaining all the asynchronous calls.
- At first, I manually wrote JSON-style braces and string quotes to the output file. But I later realized I could use
JSON.stringify()
, which does it automatically (not to mention better abstraction). - There were multiple options when choosing a data type to work with. I was initally using parallel arrays to match the CSV header labels to the appropriate value but moved to dictionaries (in reality, they're objects in JavaScript). This was better not only because it made it easy to write to JSON, but was more semantic and easier to add values.
The script is pretty robust. It reads a CSV file formatted
name; address
Scott Stanfield; Berkeley, CA
Tim; Carlsbad, CA
Grant Skinner; Edmonton, AB CA
Justin; Eiffel Tower
and converts it to JSON with geolocation formatted like
[
{
"name": "Scott Stanfield",
"address": "Berkeley, CA",
"lat": 37.8715926,
"lng": -122.272747
},
{
"name": "Tim",
"address": "Carlsbad, CA",
"lat": 33.1580933,
"lng": -117.3505939
},
{
"name": "Grant Skinner",
"address": "Edmonton, AB CA",
"lat": 53.544389,
"lng": -113.4909267
},
{
"name": "Justin",
"address": "Eiffel Tower",
"lat": 48.8582222,
"lng": 2.2945
}
]
.
To use the script, just install the Geocoder package via $ npm install
and run $ npm geocode.js
.