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

New module @turf/ellipse - Create an Ellipse submodule based on Circle #1087

Merged
merged 35 commits into from
Nov 18, 2017

Conversation

muziejus
Copy link
Collaborator

Comments:

  • I haven't changed the index.d.ts file because I don't know what it does.

Please fill in this template.

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Have read How To Contribute.
  • Run npm test at the sub modules where changes have occurred.
  • Run npm run lint to ensure code style at the turf module level.

Submitting a new TurfJS Module.

  • Overview description of proposed module.
  • Include JSDocs with a basic example.
  • Execute ./scripts/generate-readmes to create README.md.
  • Add yourself to contributors in package.json using "Full Name <@github Username>".

@DenisCarriere
Copy link
Member

Nice! Thanks for the contributions! Going to have a look at this more closely later on.

I'll help out to make the TravisCI tests pass.

@muziejus
Copy link
Collaborator Author

Thanks. My goal, if it's not obvious, is to add Standard Deviational Ellipse functionality to turf. The potential for geostatistical packages in interactive maps (in leaflet, say)… 💥

@DenisCarriere
Copy link
Member

When creating an Ellipse, would it also be possible points (Coordinates) to define xRadius & yRadius... 🤔 might be tricky, but I think that would be possible using @turf/distance.

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 good, I've left a few comments, I can help with any final touches.

* //addToMap
* const addToMap = [turf.point(center), ellipse]
*/
module.exports = function (center, xRadius, yRadius, options) {
Copy link
Member

Choose a reason for hiding this comment

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

We are using ES Modules now + this type of module should also be using export default.

function ellipse(center, xRadius, yRadius, options) {
   ...
}
export default ellipse


/**
* Takes a {@link Point} and calculates the ellipse polygon given two axes in degrees and steps for precision.
*
Copy link
Member

Choose a reason for hiding this comment

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

Most modules will contain a @name JSDocs tag.

/**
 * @name ellipse
 */

@@ -0,0 +1,68 @@
const polygon = require('@turf/helpers').polygon;
const lengthToDegrees = require('@turf/helpers').lengthToDegrees;
const getCoord = require('@turf/invariant').getCoord;
Copy link
Member

Choose a reason for hiding this comment

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

Since Turf now uses ES Modules, you should be using the import syntax.

import { polygon, lengthToDegrees } from '@turf/helpers';
import { getCoord } from '@turf/invariant';

/**
* http://turfjs.org/docs/#ellipse
*/
declare function ellipse(feature1: Feature, feature2: Feature): boolean;
Copy link
Member

Choose a reason for hiding this comment

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

... 🤔 I know Typescript is new for a lot of folks, but you could simply copy the Typescript definition from @turf/circle which will be very similar to the new ellipse module.

https://github.com/Turfjs/turf/blob/master/packages/turf-circle/index.d.ts

if (!center) throw new Error('center is required');
if (!xRadius) throw new Error('xRadius is required');
if (!yRadius) throw new Error('yRadius is required');
if (typeof options !== 'object') throw new Error('options must be an object');
Copy link
Member

Choose a reason for hiding this comment

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

There's a helper function isObject in @turf/helpers instead of using typeof.

if (typeof options !== 'object') throw new Error('options must be an object');
if (typeof steps !== 'number') throw new Error('steps must be a number');

const centerCoords = getCoord(center);
Copy link
Member

Choose a reason for hiding this comment

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

At the moment TurfJS is only using ES5 code, ES6 is not being compiled to ES5 for the final build (at the moment).

  • Replace const with var

xRadius = lengthToDegrees(xRadius, units);
yRadius = lengthToDegrees(yRadius, units);

let coordinates = [];
Copy link
Member

Choose a reason for hiding this comment

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

Same as above, replace let with var

@@ -0,0 +1,43 @@
{
"name": "@turf/ellipse",
"version": "5.0.4",
Copy link
Member

Choose a reason for hiding this comment

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

Could you change the version to 5.0.0 for the first publish, Lerna will decide the patch version.

"types": "index.d.ts",
"files": [
"index.js",
"index.d.ts"
Copy link
Member

Choose a reason for hiding this comment

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

Missing main.js that will be the CommonJS distributed file from Rollup

"index.d.ts"
],
"scripts": {
"test": "node test.js",
Copy link
Member

Choose a reason for hiding this comment

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

Use the same scripts as all the other modules.

  "scripts": {
    "pretest": "rollup -c ../../rollup.config.js",
    "test": "node -r @std/esm test.js",
    "bench": "node -r @std/esm bench.js"
  },

Copy link
Member

Choose a reason for hiding this comment

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

You'll also need to install @std/esm with the following config:

  "@std/esm": {
    "esm": "js",
    "cjs": true
  }

@DenisCarriere
Copy link
Member

I'm looking at the ellipse code base, why don't we simply add xRadius & yRadius as options to @turf/circle?

@DenisCarriere
Copy link
Member

My goal, if it's not obvious, is to add Standard Deviational Ellipse functionality to turf. The potential for geostatistical packages in interactive maps (in leaflet, say)…

👍 Super! 🚀 Lets definitely get this done, I think expanding the @turf/circle module will be our best bet. I'll set up a PR to get us started.

@muziejus
Copy link
Collaborator Author

@DenisCarriere Thanks for making these changes. A few notes:

Abstractly:

  1. I don't think adding a second radius to circle will do the trick, because circle uses distance to plot the points on the circle, but the distance is variable in an ellipse, relying on the interplay between the two radii. I think the more traditional means of measuring w/ tangents works great. Whether that should be abstracted to its own helper, on the other hand…
  2. I don't think we can use points instead of distances for the radii because the radii have to be perpendicular to each other.

Concretely:

  1. Some of the lack of packages, etc., was a function of how the default new submodule script works. I'm not very familiar w/ node, so this required some guess work.
  2. I stayed away from using import, etc., because they were simply breaking my tests:
/Users/moacir/Projects/turf/packages/turf-ellipse/test.js:1
(function (exports, require, module, __filename, __dirname) { import test from 'tape';
                                                              ^^^^^^

SyntaxError: Unexpected token import

Switching to require() syntax fixed that.

Copy link
Collaborator Author

@muziejus muziejus 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. Thanks! 👍 I left a comment addressing come of your questions.

@stebogit
Copy link
Collaborator

@muziejus @DenisCarriere nice addition here. 👍

Are the radii required by the module actually the semi-minor and the semi-major axes of the ellipse? If so I'd describe that way in the docs.

May I suggest to include also an optional tilt parameter related to the major axis of the ellipse for example? You could probably just apply @turf/transform-rotate at the end if the parameter (angle) is defined.

I don't think having this as a separate module would be a terrible idea, it would make it "easier to find" or like more explicit that Turf has that functionality. Just a thought.

@muziejus
Copy link
Collaborator Author

@stebogit You're right; they are the semi-minor and semi-major axes. My terminology is bad. But we can't use terms like "major" and "minor" because they actually relate to the x and y--it's very easy to send, say, ellipse(place, 2, 10); and end up with an ellipse where the semi-major axis is along the y-axis.

Maybe xSemiAxis and ySemiAxis?

@muziejus
Copy link
Collaborator Author

@stebogit As for tilting, I considered that, since rotation is a vital aspect of the standard deviational ellipse, but given there's already a diff utility, I don't think adding another parameter is necessary.

If I were to add a tilt option, why not also add a translate or scale option, etc.?

@stebogit
Copy link
Collaborator

@muziejus looking at the picture in my understanding we are passing O (as a point), a and b (as lengths); with those and a tilt angle (𝛼), you can describe the ellipse in the picture. In fact the center defines the position (no need for a translate parameter) and the axes define the dimensions (no need for a scale param). However without a tilt you would actually define the gray ellipse in the picture (where a=c and b=d).
Unless we always want to create ellipses with axes parallel to the x and y axes (is this actually the case?), I'd say we need a tilt.

ellipse

Maybe xSemiAxis and ySemiAxis?

We could easily use major and minor axes and internally divide them by 2

there's already a diff utility

What do you mean?

@muziejus
Copy link
Collaborator Author

@stebogit You're right. I meant "separate utility" in that the user could always use transformRotate() on their own after the fact, but I don't see why I should be adamant about not adding the functionality into this submodule.

@DenisCarriere
Copy link
Member

✅ 👍 for adding tilt as an option to this module.

✅ 👍 using major & minor.

✅ 👍 Creating it's own module @turf/ellipse

❌ 👎 add a translate or scale option (doesn't seem necessary given there's already 3 parameters that will allow to control that)

It is best to create it's own dedicated module @turf/ellipse like @stebogit mentioned, it would be easier to find and it would make it easier to understand the context of the major & minor options given it belongs to a module called ellipse.

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.

Approved once the module supports at least the following 3 params & 2 options:

Params

  • center
  • major
  • minor

Options

  • tilt
  • units

@DenisCarriere
Copy link
Member

🤔 Seems like the build is failing because of a linting issue.

/home/travis/build/Turfjs/turf/packages/turf-ellipse/index.js
  50:13  error  'angle' is already defined                    no-redeclare
  66:1   error  Expected indentation of 4 spaces but found 0  indent
✖ 2 problems (2 errors, 0 warnings)
  1 error, 0 warnings potentially fixable with the `--fix` option.

@DenisCarriere
Copy link
Member

DenisCarriere commented Nov 17, 2017

Cannot find module @turf/transform-rotate

Missing dependency 😄 The joys... Thank you TravisCI for being there for us.

lerna ERR! Error [ERR_MISSING_MODULE]: Cannot find module @turf/transform-rotate
lerna ERR!     at Object.<anonymous> (/home/travis/build/Turfjs/turf/packages/turf-ellipse/main.js:5:23)
lerna ERR!     at Object.<anonymous> (/home/travis/build/Turfjs/turf/packages/turf-ellipse/main.js)
lerna ERR!     at Module._compile (module.js:641:30)
lerna ERR! npm ERR! code ELIFECYCLE

@DenisCarriere
Copy link
Member

👍 Awesome stuff @muziejus!

@DenisCarriere DenisCarriere merged commit 04480be into Turfjs:master Nov 18, 2017
@DenisCarriere DenisCarriere changed the title Create an Ellipse submodule based on Circle New module @turf/ellipse - Create an Ellipse submodule based on Circle Nov 27, 2017
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.

3 participants