Creating GeoJSON in client-side JS
2014-05-27
Often times I find myself with spatial data in non-spatial formats. Equally as often, those data cannot be permanently changed into a spatial format since they are being continually updated in their original format by someone else. Rendering spatial data requires GeoJSON. GeoJSON is derived from JSON (JavaScript Object Notation) and adds a spatial key/value pair into the mix. The data type has a required format, which can be found at geojson.org and seen below:
It’s effectively a javascript object with a standardized format, including type, geometry, and any number of properties (metadata). Much of the time I’m able to convert the data on the client side into a useable geojson format, which can then be mapped. The loop structure is consistent, but how I obtain the data can vary (ajax, jQuery, php, etc.). The use case for this loop is extremely app-specific, so it may not be the thing you need. I just tend to use it a lot for development purposes.
Basic Loop
Tabletop.js
Typically I see data come in .csv formats, which is great for keeping things under control and organized - but difficult for the browser. Lately I’ve seen most .csv on Google Spreadsheets - which can work to our benefit.
Introducing tabletop.js, a slick javascript library that grabs data from Google Spreadsheets and returns a data object for you to manipulate. Two things need to happen here:
- Tabletop makes a call to the Spreadsheets API and returns a nice JSON object
- You loop through that returned data JSON object and create a new GeoJSON object
Things start to get redundant, but for development and a reasonable number of points this works out great.