Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration module to specify user preferences in a central location #2472

Closed
smallsaucepan opened this issue Aug 19, 2023 · 4 comments

Comments

@smallsaucepan
Copy link
Member

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 - Custom earth radius #1176

Thoughts? Comments?

@twelch
Copy link
Collaborator

twelch commented Aug 19, 2023

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.

@smallsaucepan
Copy link
Member Author

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.

@smallsaucepan
Copy link
Member Author

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

@neelduttahere
Copy link

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 - Custom earth radius #1176

Thoughts? Comments?

Hey @smallsaucepan,
is there any way right now, to change calculations from great circle to ellipsoid for a particular function/package, lets say the area function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants