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

Custom earth radius #1176

Open
dhivehi opened this issue Dec 12, 2017 · 6 comments
Open

Custom earth radius #1176

dhivehi opened this issue Dec 12, 2017 · 6 comments

Comments

@dhivehi
Copy link
Collaborator

dhivehi commented Dec 12, 2017

I think it would be better to be able to set earth's radius as a global parameter in turf. Because many people like me would be using google earth's radius which is also accepted by many professional organaisations. As for now, for every update, i download turf js and edit radius and use.

@stebogit
Copy link
Collaborator

Hey @dhivehi, not sure if this is what you need but you can get the earth radius from @turf/helpers.earthRadius :

/**
 * Earth Radius used with the Harvesine formula and approximates using a spherical (non-ellipsoid) Earth.
 */
export var earthRadius = 6371008.8;

which I believe is also used internally as global/constant value, isn't it @DenisCarriere ?

@stebogit
Copy link
Collaborator

stebogit commented Dec 12, 2017

Sorry, just realized you said set and not get... 😅

I guess now that we adopted the options object in each module it would be quite easy to add an optional earthRadius where needed.
@dhivehi would you please list all the modules you think would require that parameter?

@dhivehi
Copy link
Collaborator Author

dhivehi commented Dec 13, 2017

I want that feature mostly in distance calculation modules. But would be better if can be used in all modules.
turf.distance(from, to, {units: 'meters', earthRadius: 6378137});
Adding support for extra parameter "earthRadius" would be great i guess.

@dhivehi
Copy link
Collaborator Author

dhivehi commented Dec 13, 2017

Attached screenshot shows what troubles me for every turf update.
capture
(Web application made with openlayers & TURF)

@smallsaucepan
Copy link
Member

Perhaps reconsider using options to override the radius. That might work ok for using distance directly. However any other modules that in turn use distance would need to also accept earthRadius as an option so everything matches up. For example, the midpoint function.

It may be better to pass the new radius from the inside out globally, rather than outside in on a case by case basis. Something like having @turf/helpers check for an environment variable override which applies the desired radius globally (at least within turf).

@twelch
Copy link
Collaborator

twelch commented Aug 7, 2024

I agree with your approach @smallsaucepan if it can work. I think you brought it up in a discussion.

What does this mechanism look like? Needs to work in browser and node/other runtimes, and also if user isn't importing and using the big turf library, but rather individual turf packages e.g. @turf/distance and @turf/area.

It could be a global variable that helpers looks for on init? Falling back to a default value of mean radius.

export const earthRadius = TURF_EARTH_RADIUS || 6371008.8;

How the global variable gets set in user space code would be up to the user. Simple browser app it can be set in the base script, or top of a module, before using Turf. If user bundles their app with Vite, they can pass in the value via an environment variable at build time and set the variable - https://vitejs.dev/guide/env-and-mode.

[EDIT] I guess turf would be checking for the global variable right when it is imported, so the global variable would need to be set before import.

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

4 participants