dartclub / turf_dart

A turf.js-like geospatial analysis library working with GeoJSON, written in pure Dart.
https://pub.dev/packages/turf
MIT License
68 stars 31 forks source link

Invalid conversion from radians to yards #193

Closed dimonkomaron closed 2 weeks ago

dimonkomaron commented 2 weeks ago

Seems like conversion from radians to length if using Unit.yards calculates wrong because 1 meter = 1.09361 yards, but in factors earthRadius is divided on 1.0936 but there we should multiply.

/// Convert a distance measurement (assuming a spherical Earth) from radians to a more friendly unit.
/// Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
num radiansToLength(num radians, [Unit unit = Unit.kilometers]) {
  var factor = factors[unit];
  if (factor == null) {
    throw Exception("$unit units is invalid");
  }
  return radians * factor;
}
/// Earth Radius used with the Harvesine formula and approximates using a spherical (non-ellipsoid) Earth.
const earthRadius = 6371008.8;

/// Unit of measurement factors using a spherical (non-ellipsoid) earth radius.
/// Keys are the name of the unit, values are the number of that unit in a single radian
const factors = <Unit, num>{
  Unit.centimeters: earthRadius * 100,
  Unit.degrees: earthRadius / 111325,
  Unit.feet: earthRadius * 3.28084,
  Unit.inches: earthRadius * 39.370,
  Unit.kilometers: earthRadius / 1000,
  Unit.meters: earthRadius,
  Unit.miles: earthRadius / 1609.344,
  Unit.millimeters: earthRadius * 1000,
  Unit.nauticalmiles: earthRadius / 1852,
  Unit.radians: 1,
  Unit.yards: earthRadius / 1.0936,
};
lukas-h commented 2 weeks ago

Hi @dimonkomaron, thank you for bringing this issue to our attention. 👍 👍

Would you be open to contribute and create a pull request to fix this problem?

Otherwise it might take longer until this bug is fixed.

dimonkomaron commented 2 weeks ago

@lukas-h Yes, ok, will try :)

Also have a question is there reason why spherical radius is used as it can loss precision near the equator or ellipsoidal radius has it's own flaws?

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

P.S. Also seems I have no rights to push in the repository

lukas-h commented 2 weeks ago

@dimonkomaron

P.S. Also seems I have no rights to push in the repository

just start by

  1. forking this repository
  2. committing onto the main branch of your fork
  3. create a pull request with a clear name so it is identifiable
  4. then I'll review and merge

Also have a question is there reason why spherical radius is used as it can loss precision near the equator or ellipsoidal radius has it's own flaws?

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

we didn't decide on that ourselves, rather it was a decision made by TurfJS, the original javascript library that turf_dart is very much influenced by.

Perplexity gave me this answer: https://www.perplexity.ai/search/in-turfjs-is-there-reason-why-DDd.UpGSRo6Awus4iot7iA

If you have an idea/concept for ellipsoidal calculations how to do it, let me know, I'd be happy to discuss and add it to the library 😄

dimonkomaron commented 2 weeks ago

@lukas-h PR is done. You can find it here: https://github.com/dartclub/turf_dart/pull/194

lukas-h commented 2 weeks ago

@dimonkomaron It is merged. Until the next turf release on pub.dev you can just use the package like this in your pubspec.yaml:

dependencies:
  turf:
    git:
      url: https://github.com/dartclub/turf_dart.git
      ref: main

I will mark this issue as done.