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

Add FeatureCollection mutate tests (not failing) #897

Merged
merged 7 commits into from
Aug 14, 2017

Conversation

DenisCarriere
Copy link
Member

@DenisCarriere DenisCarriere commented Aug 11, 2017

Add test cases for @turf/transform-scale

Issue

On (Multi)Polygon the first & last coordinate gets repeated when using coordEach (not sure if this was intentional in coordEach).

Before

image

After

image

@DenisCarriere DenisCarriere added this to the 4.7.0 milestone Aug 11, 2017
coordEach(geojson, function (coord) {
coordEach(geojson, function (coord, coordIndex, featureIndex, featureSubIndex) {
// Ignore scaling on first coordinate of Polygons (Issue #895)
if (isPolygon && mutate === true && featureSubIndex === 0) return;
Copy link
Member Author

Choose a reason for hiding this comment

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

@stebogit Might want to add this type of behavior in a few of the transform modules.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@DenisCarriere it looks ok, and clearly it does work. However I can not understand why we don't want to scale all the points of the ring(s) of a polygon, whether we clone it or not.
I really can't wrap my head around it... 🤔

Copy link
Collaborator

@stebogit stebogit left a comment

Choose a reason for hiding this comment

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

@DenisCarriere it clearly works, but the idea we need to skip a point if we clone geojson (and only for polygons?) seems so weird to me... 🤔

origin = defineOrigin(geojson, origin);

// Shortcut no-scaling
if (factor === 1 || isPoint) return geojson;

// Scale each coordinate
coordEach(geojson, function (coord) {
coordEach(geojson, function (coord, coordIndex, featureIndex, featureSubIndex) {
Copy link
Collaborator

@stebogit stebogit Aug 12, 2017

Choose a reason for hiding this comment

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

I noticed coordIndex and featureSubIndex are always equals, throughout the entire test suite, and featureIndex is always 0.
Inside coordEach this never logs:
if (coordIndex !== featureSubIndex || featureIndex > 0) console.log('Woah!');
Shouldn't the indices change at least for multi features?

EDIT: Got it, geojson is a polygon with one ring, i.e. featureIndex = 0 and featureSubIndex = coordIndex

coordEach(geojson, function (coord) {
coordEach(geojson, function (coord, coordIndex, featureIndex, featureSubIndex) {
// Ignore scaling on first coordinate of Polygons (Issue #895)
if (isPolygon && mutate === true && featureSubIndex === 0) return;
Copy link
Collaborator

Choose a reason for hiding this comment

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

@DenisCarriere it looks ok, and clearly it does work. However I can not understand why we don't want to scale all the points of the ring(s) of a polygon, whether we clone it or not.
I really can't wrap my head around it... 🤔

@@ -59,18 +61,24 @@ module.exports = function (geojson, factor, origin, mutate) {
* @param {Feature|Geometry} geojson GeoJSON Feature/Geometry
* @param {number} factor of scaling, positive or negative values greater than 0
* @param {string|Geometry|Feature<Point>|Array<number>} [origin="centroid"] Point from which the scaling will occur (string options: sw/se/nw/ne/center/centroid)
* @param {boolean} [mutate=false] allows GeoJSON input to be mutated
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here mutate does not actually define if the feature is going to be mutated or not, that was handled before.
I can't find a good description though 😅

cloned last vertex in turf-hex-grid;
reverted changes in transform-scale;
fixed JDoc types;
@stebogit
Copy link
Collaborator

stebogit commented Aug 13, 2017

@DenisCarriere I found out the real issue: as you said

The scaling happens twice on the first & last coordinate position (for some reason...)

because when @turf/hex-grid created the polygons the last vertex of the ring was set as reference to the first one, instead of cloning it.
screen shot 2017-08-13 at 1 58 58 am

I modified (locally) that line and all seems to be good with the original transform-scale code.

@DenisCarriere
Copy link
Member Author

because when @turf/hex-grid created the polygons the last vertex of the ring was set as reference to the first one, instead of cloning it.

Ahh... that makes sense! Was wondering where that issue came from.

};

/**
* Scale Feature/Geometry
*
* @private
* @param {Feature|Geometry} geojson GeoJSON Feature/Geometry
* @param {GeoJSON} geojson GeoJSON Feature/Geometry
Copy link
Member Author

@DenisCarriere DenisCarriere Aug 14, 2017

Choose a reason for hiding this comment

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

GeoJSON means it also handles FeatureCollection & GeometryCollection.

This doesn't? The main method handles that part.

* @param {number} factor of scaling, positive or negative values greater than 0
* @param {string|Geometry|Feature<Point>|Array<number>} [origin="centroid"] Point from which the scaling will occur (string options: sw/se/nw/ne/center/centroid)
* @returns {Feature|Geometry} scaled GeoJSON Feature/Geometry
* @returns {GeoJSON} scaled GeoJSON object
Copy link
Member Author

Choose a reason for hiding this comment

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

If the input is Geometry it outputs a scaled Geometry, we know the outputs/inputs can ONLY be Feature or Geometry.

@DenisCarriere DenisCarriere merged commit bf7be9d into master Aug 14, 2017
@DenisCarriere DenisCarriere deleted the transform-scale-#895 branch August 14, 2017 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants