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

Rhumb line modules #728

Merged
merged 18 commits into from
May 11, 2017
Merged

Rhumb line modules #728

merged 18 commits into from
May 11, 2017

Conversation

stebogit
Copy link
Collaborator

Added
@turf/rhumb-bearing
@turf/rhumb-destination
@turf/rhumb-distance

Solves #714 and #681
Ref: #684

@stebogit
Copy link
Collaborator Author

stebogit commented May 10, 2017

@DenisCarriere I don't understand why the tests fail, on my computer everything passes... 😞

@DenisCarriere
Copy link
Member

DenisCarriere commented May 10, 2017

This is the error, I'll have a look at it.

Errored while running npm script 'test' in '@turf/along'
Error: Command failed: npm run test 
/home/travis/build/Turfjs/turf/packages/turf-rhumb-destination/index.js:43
    if (distance === undefined || distance === null || distance < 0) throw new Error('distance is required');

@@ -0,0 +1,50 @@
{
"name": "@turf/destination",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stebogit Here's your problem 🤦‍♂️ You've named it @turf/destination and when lerna boostrap creates the symbolic links it overwrites the "real" @turf/destination with the rhumb module. I'll fix this and the tests should pass.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@@ -0,0 +1,50 @@
{
"name": "@turf/destination",
"version": "4.3.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For new modules, define them as 4.0.0 (base number of major release) because whenever I publish with Lerna it will automatically bump up the appropriate version (using the same version number as the next release will cause a version conflict with Lerna).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it 👍

@@ -0,0 +1,60 @@
// https://en.wikipedia.org/wiki/Rhumb_line
// http://www.movable-type.co.uk/scripts/latlong.html#rhumblines
var getCoords = require('@turf/invariant').getCoords;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're only dealing with points you can use getCoord which has some extra point validation built-in.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't understood the difference between the two yet... Maybe some additional explanation on the doc would help?
I'll use getCoord anyway 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes more documentation is always better 👍
The Gist is getCoord if you want to retrieve a Point Array (with point validation), getCoords for the entire coordinates as an Array (limited validation).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why then not renaming them getCoords and getCoordsArray (I guess this would be suitable for LineString, Polygon, etc. right?); I believe that would make it more clear and intuitive.

*
* @name rhumbBearing
* @param {Feature<Point>} start starting Point
* @param {Feature<Point>} end ending Point
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding Geometry Object & Array<number> as valid input (input type supported when using getCoord()).

* @param {Geometry|Feature<Point>|Array<number>} start starting Point

* origin Point with the (constant) given bearing.
*
* @name rhumbDestination
* @param {Point|Array<number>} origin starting point
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same JSDocs as above Geometry|Feature<Point>|Array<number>

* }
* };
* var distance = 50;
* var bearing = 90;// function (from, distance, bearing, units) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// function (from, distance, bearing, units) { ?


units = units || 'kilometers';

var distanceInMeters;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth having distanceInMeters as a method in @turf/helpers

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I was thinking about it; maybe distanceInKm (just * 1000 to have meters) as that is Turf's default in many cases?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes many libraries use meters as the unit of measurement.

I solved this using:

var distanceInMeters = radiansToDistance(distanceToRadians(distance, units), 'meters');

Might be worth having a method that wraps this behavior. Doesn't have to be to Meters it can be to any distance unit type.

convertDistance(1000, 'miles', 'meters')

I dunno about the method name, but something like that.

if (!origin) throw new Error('origin is required');
if (distance === undefined || distance === null) throw new Error('distance is required');
if (bearing === undefined || bearing === null) throw new Error('bearing is required');
if (!(distance >= 0)) throw new Error('distance must be greater than 0');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this if statement from (distance < 0). If your input is text, then the result would be false since text cannot be less than 0.

> -4 < 0
true
> 5 < 0
false
> 'miles' < 0
false


units = units || 'kilometers';

var distanceInMeters;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire switch statement can be replaced with only:

var distanceInMeters = radiansToDistance(distanceToRadians(distance, units), 'meters');

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very simple and elegant! 👍 👍

if (process.env.REGEN) write.sync(directories.out + name +'.json', result);
t.deepEqual(load.sync(directories.out + name + '.json'), result, name);

// TODO adopt the following graphical output once rhumbDestination is published
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Sounds like a plan!

t.deepEqual(distances, load.sync(directories.out + name + '.json'), name);

// Removed since (point1 = true & point2 = false)
// t.ok(distances.kilometers < distances.greatCircleDistance, name + ' distance comparison');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stebogit Can you have a look at this failing test case, I don't know if the GreatCircle distance is always greater than Rhumb Line Distance (I would assume that your test statement is correct, however the output says otherwise)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The correct test is reverse, as the GreatCircle line is the shortest path:
distances.kilometers < distances.greatCircleDistance
However it still fails now because of the approximation error due to the incorrect earth radius. I ran the corrected test with 6371km radius and it passes for both cases.

@DenisCarriere
Copy link
Member

@stebogit Ok I'm done with commits 🤓 Have a glance at it again and give me a 👍 if you're good to merge this, looks good to me!

@DenisCarriere DenisCarriere self-assigned this May 10, 2017
Copy link
Member

@DenisCarriere DenisCarriere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Looks great!

@stebogit
Copy link
Collaborator Author

Looks good to me 😃

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

Successfully merging this pull request may close these issues.

2 participants