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

Initial commit of turf-dissolve #556

Merged
merged 4 commits into from Jan 25, 2017
Merged

Initial commit of turf-dissolve #556

merged 4 commits into from Jan 25, 2017

Conversation

rowanwins
Copy link
Member

  • 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 occured.
  • Run npm run lint to ensure code style at the turf module level.

Gday @DenisCarriere

First commit of turf-dissolve module.

* @name dissolve
* @param {FeatureCollection<Polygon>} featureCollection input feature collection to be dissolved
* @param {string} propertyName property name on which to dissolve features
* @return {FeatureCollection} a FeatureCollection containing the dissolved polygons
Copy link
Member

Choose a reason for hiding this comment

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

  1. JSDocs @returns instead of @return
  2. Wouldn't it return {FeatureCollection<Polygon>}?

*
* @name dissolve
* @param {FeatureCollection<Polygon>} featureCollection input feature collection to be dissolved
* @param {string} propertyName property name on which to dissolve features
Copy link
Member

Choose a reason for hiding this comment

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

  1. Dissolving by property name should be optional.
  2. Maybe we should introduce this option as a list of strings instead? That way this gives the ability to dissolve from multiple properties. {string|Array<string>} or only handle {Array<string>}

Copy link
Member Author

Choose a reason for hiding this comment

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

Happy to implement number 1, can't believe that didn't cross my mind!
Number 2 might be a bit trickier so for version 1 I'd prefer to stick to a single property.

Copy link
Member

Choose a reason for hiding this comment

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

That works for me, I just know that QGIS & ArcGIS can dissolve using multiple attributes, just something to keep in mind in the future.

Copy link
Member

Choose a reason for hiding this comment

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

@rowanwins Optional JSDocs are defined with square brackets.

 * @param {string} [propertyName] ...

}
rtree.load(treeItems);

for (var i = 0; i < featureCollection.features.length; i++) {
Copy link
Member

Choose a reason for hiding this comment

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

This next comment is a personal preference, but you could make this for loop like the following:

for (var polygon of featureCollection.features) {
  ...

Which eliminates of these lines:

for (var i = 0; i < featureCollection.features.length; i++) {
  var polygon = featureCollection.features[i];

This does work in the browser, I've already tested this using Webpack & Browserify

Copy link
Member Author

Choose a reason for hiding this comment

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

I need to hold onto the index variables unfortunately so can't really do this sorry.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Another alternative is to use the array's forEach method. I find it to be expressive like the former but offers the option to pass in the index like the latter. Since you are not breaking out of the loop and only using continue shouldn't be an issue using forEach.

Copy link
Member

Choose a reason for hiding this comment

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

@rowanwins If you need to keep the index, you can use in instead of of.

for (var i in featureCollection.features) {
  var polygon = featureCollection.features[i];

I dunno... personal taste I guess, I just hate seeing a for loop with i++ it feels like the jQuery days.


var featureChanged = false;

for (var searchIndex = 0; searchIndex < potentialMatchingFeatures.length; searchIndex++) {
Copy link
Member

Choose a reason for hiding this comment

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

Same for loop comment from earlier

for (var polygon of potentialMatchingFeatures) {
  ...

@DenisCarriere
Copy link
Member

DenisCarriere commented Jan 23, 2017

  • Also, could you add a Typescript definition index.d.ts in the root directory of the dissolve module.

Use this as an example

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

The definition should be pretty simple & straightforward.

"name": "@turf/dissolve",
"version": "3.7.5",
"description": "Dissolves polygons based on an attribute",
"main": "index.js",
Copy link
Member

Choose a reason for hiding this comment

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

  • Add types: "index.d.ts" once theindex.d.ts is created.

Use this module as an example:
https://github.com/Turfjs/turf/tree/master/packages/turf-bearing

@@ -16,6 +16,7 @@ var turf = {
within: require('@turf/within'),
concave: require('@turf/concave'),
difference: require('@turf/difference'),
dissolve: require('@turf/dissolve'),
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 remove this until all of the TravisCI tests pass.

We will re-add this afterwards once everything is good to go.

https://travis-ci.org/Turfjs/turf/builds/194420002#L780

Also, we will need to add the module to turf/package.json

https://github.com/Turfjs/turf/blob/master/packages/turf/package.json

@@ -106,8 +106,10 @@ module.exports = function (featureCollection, propertyName) {
}
var matchFeature = featureCollection.features[matchFeaturePosition];

if (matchFeature.properties[propertyName] !== polygon.properties[propertyName]) {
continue;
if (propertyName !== null) {
Copy link
Member

Choose a reason for hiding this comment

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

Should be undefined instead of null. An empty param will result as undefined`.

Question: Did this get picked up in your tests?

Copy link
Member Author

Choose a reason for hiding this comment

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

happy to change it to undefined. The existing tests don't pick up any errors even though one of the polys does not have the required property.

/**
* http://turfjs.org/docs.html#dissolve
*/
declare function dissolve(featureCollection: Polygons, propertyName: string): Polygons;
Copy link
Member

Choose a reason for hiding this comment

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

Include ? to make it optional.

propertyName?: string

@@ -1,13 +1,18 @@
var turfDissolve = require('./index');
var test = require('tape');
var polys = require('./test/polys.json');
var expectedOutput = require('./test/outputPolys.json');
var expectedOutput = require('./test/outputPolysByProperty.json');
var expectedOutput2 = require('./test/outputPolysWithNoProperty.json');
Copy link
Member

Choose a reason for hiding this comment

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

Instead of hardcoding your expected results, add a fs.writeSync function using an environment variable REGEN=true to output your expected results.

Here's an example of a test case using this:

https://github.com/Turfjs/turf/blob/master/packages/turf-buffer/test/test.js#L34-L38

    buffered.features = buffered.features.concat(normalize(fixture).features);
    if (process.env.REGEN) {
      fs.writeFileSync(
          __dirname+'/fixtures/out/'+path.split('/')[path.split('/').length-1],
          JSON.stringify(buffered, null, 2));
    }
    var expected = JSON.parse(fs.readFileSync(
        __dirname+'/fixtures/out/'+path.split('/')[path.split('/').length-1]));
    t.deepEqual(expected, buffered);
  });

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't follow on this? What is the point? It seems much trickier than the current approach...

@@ -16,7 +16,7 @@ var turf = {
within: require('@turf/within'),
concave: require('@turf/concave'),
difference: require('@turf/difference'),
dissolve: require('@turf/dissolve'),
// dissolve: require('@turf/dissolve'),
Copy link
Member

Choose a reason for hiding this comment

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

Damn.. still can't make the tests pass since it really needs that @turf/dissolve module to be present for @turf/turf, once the package is published once it will be ok.

https://travis-ci.org/Turfjs/turf/builds/194629215#L2380-L2383

You can uncomment this since the tests has passed all the way to the end.

@DenisCarriere
Copy link
Member

DenisCarriere commented Jan 24, 2017

@rowanwins Looks good, just have a look at those recent comments I made and we should be good to merge and publish this module.

  • Include REGEN for expected fixtures in tests.
  • Typescript definition & JSDocs propertyName defined as optional (?)
  • Uncomment dissolve in turf/index.js
  • Catch propertyName as undefined instead of null.

@DenisCarriere
Copy link
Member

FYI @rowanwins That last commit you sent 3 hours ago didn't have any updates.

@DenisCarriere DenisCarriere merged commit 35c87dd into Turfjs:master Jan 25, 2017
@DenisCarriere
Copy link
Member

@rowanwins This module is Published! 👍 🎆

Fixed some minor things: 97c458e

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

Successfully merging this pull request may close these issues.

None yet

3 participants