Turfjs / turf

A modular geospatial engine written in JavaScript and TypeScript

Home Page:https://turfjs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add configuration module to specify user preferences in a central location

smallsaucepan opened this issue · comments

Turf currently uses great circle calculations in several places (e.g. distance) and it would be helpful to provide an ellipsoid option as well. Rather than pass that as an option to every call though we could add a global configuration object where the user can specify it once only. For example in the app setup:

// Optionally in app.ts ...
import turf from "@turf"

turf.config.configure({
  geodesicEllipsoid: turf.config.WGS_84,
})


// In distance.ts
import { config } from "@turf/config"
...
function distance(...) {
  if (config.geodesicEllipsoid) {
    // do ellipsoidal calculation using config.geodesicEllipsoid as a parameter
  } else {
    // do traditional great circle calculation
  }
}

// In application file ...
import { distance } from "@turf/distance"

// distance(x, y) returns WGS_84 ellipsoid result, rather than default great circle based result

This seems cleaner than passing { ellipsoid="WGS_84 } through several layers of function calls e.g. lineSlice -> nearestPointOnLine -> distance.

Could potentially use this for other preferences such as:

  • speed vs accuracy
  • indexing optimisation preferences
  • default measurement units
  • customising earth radius or other constants - #1176

Thoughts? Comments?

One thing to know is that turf publishes individual modules to npm for each of its functions. This lets you import just the pieces you need. In fact, that's how I usually use it. I think this was originally by design, to keep it from being a monolithic library, before things like tree shaking existed.

I'm not sure how your suggestion would work with that in mind.

Ah, thanks. You're right I think. The turf-config bundled with the distance package wouldn't appear as the same "instance" as the turf-config bundled with nearest-point-on-line. Changing one wouldn't change the other.

Let me see if there is another clean way to do this.

Not sure there is a nice way to do this. Will reopen if a suitable approach springs to mind.