Mark Your Turf
Tags: turf, turfjs, geojson
Published: 2019-10-13 20:15:56

We need to get cozy with spatial data. So invite it over, light a fire, pour a glass of the good wine, and make space for...space.
 
Why share your good wine with it? Spatial data plays a big role in augmented reality, big data, and the desperate need to protect privacy and civil liberties in the age of drones, facial recognition registering and recording an individual's location, "crime cameras" (aka:  Jeremy Bentham/Michel Foucault's Panopticon, come to unfortunate fruition), and the like. This kind of space isn't exactly the final frontier, but it is one of the newer landscapes we need to learn to navigate.

Here's some vocab to start us off:
1. GeoJSON: "a format for encoding a variety of geographic data structures" according to [geojson.org](geojson.org). 
Or you could say it's a ["geospatial data interchange format based on JavaScript Object Notation (JSON](https://tools.ietf.org/html/rfc7946).

The GISt of it is (that's an amazing pun, that you'll get in just in a minute, when I tell you about GIS) geoJSON is a few data types, including Point, LineString, Polygon, MultiPoint, MultiLineString, and MultiPolygon, all used for doing geometry with geography. Those data types can be stored in objects that are called Feature objects, and Feature objects can be stored with other Feature objects in sets called FeatureCollection objects. All these things, of course, are based on JavaScript Object Notation (JSON), so if you've used JavaScript, they'll be super easy to embrace.

Here's an example of a geoJSON point, which is the barest-of-bones sort of data format:
{% replit @CarinChapman/SeashellConventionalFraction %}

That's pretty straightforward syntax. You just make it an object (because it's JSON), you give it a "type" (which defines whether it's going to be a single point in space, a line of connected points, a polygon shape made from the connections of lines between multiple points, or etc), and you give it some "coordinates" (which are latitude, longitude in my example, but the order of lat/lng or lng/lat depends on the context in which you're using the geoJSON), and that's it. You've got yourself a slick new geoJSON object.

It gets a little more complicated from there, but not too much. For example, you can make a "point" into a "Feature" if, say, that point represents something in the physical world, like "A place we should support":
{% replit @CarinChapman/SalmonAridProtools %}

In so doing, you add data about the spot on the map that your geoJSON point is referring to, and give the point some context.

2. GIS: this stands for "Geographic Information System," and it is "a framework for gathering, managing, and analyzing data...[and] organiz[ing] layers of information into visualizations...[to reveal] insights into data, such as patterns, relationships, and situations—helping users make smarter decisions," as per the folks at [esri] (https://www.esri.com/en-us/what-is-gis/overview). (FYI esri is a really nifty platform for mapping analytics, and I'd love to explore it in more detail in a blog post another day.) Put more simply, it's a way to use info, like geoJSON data, to make meaning and put geography into a context that a user can experience, understand, appreciate. 

Enough vocab. The cool stuff is how you can use geo spatial data. There's a whole slew of related software, platforms, and so. Some names you'll run across that are worth looking into include:
[QGIS] (https://www.qgis.org/en/site/)
[PostGIS] (https://postgis.net/documentation/)
[Leaflet] (https://leafletjs.com/)
And especially Tom MacWright's aptly named ["awesome-geojson"] (https://github.com/tmcw/awesome-geojson), which is a list of utilities on their Github (bless you, Tom. You're a life saver).

But the one I want to briefly touch on here is [Turf.js] (https://turfjs.org/) which has come in really handy for me lately. 
Turf.js is a pretty self-explanatory library of functions that will help you turn simple geoJSON objects into data you can use to create cool stuff. Specifically, I recently helped build an app that gets people safe routes in flash flood scenarios, and Turf.js was indispensable. Here's a bit of how/why:

{% replit @CarinChapman/TallFaithfulFormat %}

In that snipped of code, as the comments point out, I used Turf.js to turn a simple set coordinates into a geoJSON object using turf.point(); surrounded that single point with a radius that turned the point into a polygon of space with turf.buffer(); then found a user the shortest safe path by naming the buffered polygon of space as an off-limits "obstacle" along the route using turf.shortestPath(). That gave me back some much needed data that I went on to use to construct flood-free routes for users. 
Turf.js made it quick and easy to implement their functions, and besides the couple I'm showing you here, there a ton more that give you lots of power with your data, by supplying the algorithms you need, so you can focus on what it is you want to create. 

There's so, so much more to explore and learn about geoJSON data, all of which is way beyond the scope of this little blurb. I hope, though, that this entices you to consider using the data in the future and looking into it more.