DEV Community

Cover image for Distance Type Provider
Paweł Stadnicki
Paweł Stadnicki

Posted on

Distance Type Provider

I'm presenting my Distance Type Provider: a little bit different, literacy-close type provider, created for interactive, notebook-driven geospatial explorations.

If you threaten the geospatial domain as far from your interest, I encourage you to read the post even more, as I'm using tangible, life inspired examples.

Distance Type Provider plays a crucial role in my other, not yet released endeavor Florence, but can be used alone.

In short, DTP helps to find distances and travel times for different movement profiles just by typing:

Image description

You can find some of the examples I present here.

Type Providers

Type Providers have many applications and are a fabulous concept on their own. Almost any F# developer had intrinsic desire to create one at some point!

They are especially useful in data explorations: you get a local or remote data source and query it.

Most of the existing type providers have a generic purpose to traverse through well established schemas: json, swagger. Other serve DSL purpose like Regex. World Bank type provider is instead one that is very close to the literate programming. It collects development indicators and other data about countries in the world:

Image description

Literate programming is about writing code, that is meant to be clear to a human being, not a developer itself. In data explorations, any burden distracts and we should document what we already know. Literate code communicates data as information. It is very useful.

Distance Type Provider

If you follow me on Twitter, you know that I'm interested in smart city and open data domains. They in turn are strictly connected with geospatial, and geospatial is all about distances.

You may ask:

Wait! Distance is something you measure, it is a value or variable but not a type! Ok, it can be int or float (or unit of measure) but it already exists...

It is all about data literacy. The goal is to not only provide a distance itself, but ask about it in plain English.

Let's start with an example, which describes our life without such a provider.

Ordinary code

Assuming we have a set of places (with names and positions):
how can we calculate distances between some wanted places ?

Image description

Every developer wrote similar code hundreds of times and it is very easy to follow, even for newbies and people outside IT.
However, we need to filter out wanted entities first. If we want to compare many of them, the code will consist mostly of middle steps everyone has to read (and potentially, likely be distracted from the program goal). Also, with more and more data, data discoverability shrinks.

So how we could write code with data literacy?
In other words, how we would transform the previous code to a question in English ?

Image description

With Distance Type Provider you can write code exactly like above.

Geojson everywhere

To make the provider as generic as possible, it expects a geosjon as an input.
If we look at F1 dataset, that contains F1 tracks, we can find some properties that determine these tracks:

Image description

The default geojson property that provider expects is "name", so if we run provider without additional parameters, we can check distances based on track names:

Image description

If we want to use other attribute name, we should pass it as a static parameter:

Image description

What about two different data sources ?

Image description

MyLife geojson is very interesting, as you can define it once and use it in many municipal analyses.

Florence even comes with special editor based on Mapbox autofill that you can use directly from the notebooks:

Image description
and/or continue data analyses in the very next cell:

Image description

With such a life space data ready for our disposal, we can rank city places with life-inspired function:

Image description

Spatial distance vs actual distance

The easiest way to measure the distance between 2 points is to provide spatial distance, a straight line length. It can be calculated with this formula. Although it is not 100% accurate for larger distances (
due to possible differences in height above sea level and the fact that the Earth is not perfectly round ) it is fair enough for most analysis.

So spatial distance is the default one.

Image description

But there are situations when we need a distance between 2 or more points, that is measured through the exact road or path. Moreover, the road will be different depending on the means of transport used: car, bike, bus. We can always decide to just walk.

And all of these can return different values on different hours due to the current traffic level.

Fortunately, there are data providers that do that for almost any area in the World.

Distance Type Provider leverages Mapbox for this purpose.
Despite the fact Mapbox offers generous free plans, the provider user must use his own token.

In return you get 3 new profiles: driving, cycling and walking:

Image description

Image description

That is nice! What about checking more data at once? Not a problem at all, you can use any place from the geojson combined with any other, even at hoc location:

Image description

Just in case you haven't noticed it:

Image description

Mapbox direction-matrix API return also expected duration of the travel. That is a nice bonus!

What next

To be honest, I added the Mapbox support just 2 days ago so it is all very fresh. The provider itself is still experimental but will evolve ratehr rapidly, / I definately want to add many more capabilities of the Mapbox API.

I hope it will serve some purpose outside of my own experiments!
Stay tuned for more geospatial stuff related to F#.

Top comments (0)