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/polygon-tangents #708

Merged
merged 12 commits into from
May 5, 2017
Merged

New module @turf/polygon-tangents #708

merged 12 commits into from
May 5, 2017

Conversation

rowanwins
Copy link
Member

@rowanwins rowanwins commented May 2, 2017

New Module @turf/polygon-tangents

Finds the tangents of a (Multi)Polygon from a Point.

Initial commit of a new @turf/polygonTangents module as per #648

The only downside at the moment is that it doesnt handle a multipolygon input as @turf/meta.coordsEach function is a bit limited. coordsEach will loop through a multipoly but there isn't doesnt provide much useful information in the currentIndex as to what depth you're at etc so I just need to do some refactoring.

If there is anything else you notice let me know, otherwise I'll get the multipoly thing sorted and recommit.

Cheers
Rowan

var suite = new Benchmark.Suite('turf-point-on-surface');
suite
.add('turf-point-on-surface',function () {
centroid(fc);
Copy link
Member

Choose a reason for hiding this comment

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

@rowanwins Might want to change this 🤦‍♂️ to @turf/tangents

@@ -0,0 +1,12 @@
/// <reference types="geojson" />

type Point = GeoJSON.Feature<GeoJSON.Point>;
Copy link
Member

Choose a reason for hiding this comment

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

Started including Geometry Object as a valid input.

type Point = GeoJSON.Feature<GeoJSON.Point> | GeoJSON.Point;
type Polygon = GeoJSON.Feature<GeoJSON.Polygon> | GeoJSON.Polygon;

* @name polygonTangents
* @param {Feature<Point>} point to calculate the tangent points from
* @param {Feature<Polygon>} polygon to get tangents from
* @returns {FeatureCollection|Feature<Point>} Feature Collection containing the two tangent points
Copy link
Member

Choose a reason for hiding this comment

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

Should only return FeatureCollection<Point>

var rtan = 0;
var ltan = 0;

coordEach(polygon, function (currentCoords, currentIndex) {
Copy link
Member

Choose a reason for hiding this comment

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

You might need to look into using coordReduce, seems like this is the type of operation you are doing with eprev & enext.


coordEach(polygon, function (currentCoords, currentIndex) {
if (currentIndex === 0) {
eprev = isLeft(polygon.geometry.coordinates[0][0], polygon.geometry.coordinates[0][1], point.geometry.coordinates);
Copy link
Member

Choose a reason for hiding this comment

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

Might want to consider using a shorter variable to declare your coordinates instead of always having to use polygon.geometry.coordinates (hard to read).

var polyCoords = getCoords(polygon);
var ptCoords = getCoords(point);

Also this normalizes retrieving coordinates from either Feature or Geometry Object.

return turfFc([turfPoint(polygon.geometry.coordinates[0][rtan]), turfPoint(polygon.geometry.coordinates[0][ltan])]);
};

function isAbove(Point1, Point2, Point3) {
Copy link
Member

Choose a reason for hiding this comment

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

Normally capitalized Javascript variables are used for Class objects.
Point1 should be lowercased, we can look into including this as a warning in the ESLinter.

"tangent",
"polygon"
],
"author": "Turf Authors",
Copy link
Member

Choose a reason for hiding this comment

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

Add yourself in contributors

"contributors": [
  "Rowan Winsemius <@rowanwins>"
]

rowanwins and others added 4 commits May 5, 2017 08:57
- Added types test `tsc test/types.ts` to execute (if nothing happens, then it passes)
- Added MultiPolygon to Types & JSDocs
- Updated bench name :)
CC: @rowanwins This one is for you :)
@DenisCarriere
Copy link
Member

Supporting Geometry Objects

You might find getCoords() in @turf/invariant a useful to method to support this.

Error

/Users/mac/Github/turf-polygon-tangents/packages/turf-polygon-tangents/index.js:37
    var rtan = polygon.geometry.coordinates[0][0].slice(0, 2);
                               ^
TypeError: Cannot read property 'coordinates' of undefined

Feature

{
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "Point",
    "coordinates": [132, -26]
  }
}

Geometry

{
  "type": "Point",
  "coordinates": [132, -26]
}

Using getCoords()

var getCoords = require('@turf/invariant').getCoords;
// pt => Geometry or Feature
getCoords(pt);
//= [132, -26]

CC: @rowanwins

@DenisCarriere DenisCarriere merged commit 4bd1387 into master May 5, 2017
@DenisCarriere
Copy link
Member

@rowanwins 👍 Looks good to publish now. If you have anymore improvements just send over a new PR or simply push directly to master if they are minor changes (if npm test passes).

@rowanwins rowanwins deleted the polygon-tangents branch May 6, 2017 12:05
@DenisCarriere DenisCarriere changed the title Polygon tangents module New module @turf/polygon-tangents May 12, 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.

2 participants