```
> Epistemic status: A brief tutorial on Turf.js,
trying to be the resource I would have wanted.
(There are not many turf.js tutorials out there, which is tedious...)
```

While I for a while enjoyed tools such as Leaflet.js or MapBox, only recently have I been able to get spatial analysis libraries installed in Python. Thanks to Unofficial Windows Binaries for Python Extension Packages website, geospatial analysis libraries that were once a trouble to install now work on my computer.

This has proven helpful because practicing those libraries meant Turf.js, a JavaScript library devoted to geospatial analysis, makes a lot more sense now, and is pretty useful actually!

For example, imagine if one was a city planner, and wanted to track the locations of electric bikes, let us assume a data set which is a part of one I am working with:

```
const bikes = [
{ lat: 38.9043176051402, lon: -77.0160190957944, time: '2018-12-26T15:31:32.770277'},
// and so on...
```

With leaflet, while points and other geoJSON features can be made and added with ease, geospatial analysis requires other tools.

For example, to see the distance between points, one can do this with turf.js:

```
for(let i = 0; i < bikes.length; i++){
if (i === 0){
continue // This will prove important
} else {
let from = turf.point([bikes[i].lon,bikes[i].lat])
let to = turf.point([bikes[i - 1].lon,bikes[i - 1].lat])
// Reason why it does not happen on i == 0 is to prevent a crash if one was doing the i + 1 method
// ^ or that there is no point before [0] to compare to.
let options = {units: 'miles'};
let distance = turf.distance(from, to, options); // <= Compute the distance.
console.log(`The distance is ${distance}`)
}
}
```

By doing this, one can easily check the distance between two points. In my case, I am currently looking into the average location of where Lime Bike and Scooters are located, so this lets me see how far the average parked location is moving during a set period of time.

Next, what if one wants to see what is the general area where the scooters are contained in? With Turf.js, this is also possible:

```
let enveloped = turf.envelope(features)
L.geoJSON(enveloped).addTo(mymap)
// ^ Create a GeoJSON object consisting of all points, and via leaflet add it to the map
let area = turf.area(enveloped)
console.log(area)
// ^ Calculate the area and then log it.
```

Beyond these tools just highlighted, their are other useful tools in turf.js as well.

```
let pointA = turf.point([-77.566406, 32.421008]);
let pointB = turf.flip(pointA)
// ^ Flip points!
let points = [turf.point([-77.566406, 32.421008])...]
let nClusters = {numberOfClusters: 5};
let clustered = turf.clustersKmeans(points, nClusters);
// ^ Use k-means clustering to find where points are clustered!
let line = turf.lineString([[5, 5], [5, 6], [5, 7], [5, 8]]);
let point = turf.point([5, 5]);
turf.booleanContains(line, point);
// ^ See if one geoJSON object is contained within another!
```

### An additional note:

With some basic stats, geospatial analysis can be made even more useful. For example, add in some calculation of the standard deviation, the average change in location can be made even more useful!

```
let bikeList = []
for (let i = 0; i < bikes.length; i++){
bikeList.push(bikes[i].lon)
}
// ^ Store the values in array
const Mean = (Array) => {
const m = Array.reduce((accumulator, currentValue, currentIndex, _) => accumulator + currentValue);
return m / Array.length
}
// ^ Mean function using JavaScript reduce syntax instead of a for loop and a sum variable.
const StandardDeviation = (Array) => {
let sum = 0
for(let i = 0; i < Array.length; i++){
sum += (Array[i] - Mean(Array))**2
}
const std = Math.sqrt(sum / Array.length)
return std
}
// ^ Calculate the standard deviation
let bike_StandardDeviation = StandardDeviation(bikeSTDList)
console.log(bike_StandardDeviation)
// ^ Calculate standard deviation of the change in longitude.
```

While there is a lot more to doing geospatial analysis by programming then these various examples, they are still interesting and worth taking a look at.