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
Conversation
* @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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- JSDocs
@returns
instead of@return
- 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Dissolving by property name should be optional.
- 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>}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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++) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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++) { |
There was a problem hiding this comment.
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) {
...
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", |
There was a problem hiding this comment.
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'), |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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);
});
There was a problem hiding this comment.
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'), |
There was a problem hiding this comment.
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.
@rowanwins Looks good, just have a look at those recent comments I made and we should be good to merge and publish this module.
|
FYI @rowanwins That last commit you sent 3 hours ago didn't have any updates. |
@rowanwins This module is Published! 👍 🎆 Fixed some minor things: 97c458e |
npm test
at the sub modules where changes have occured.npm run lint
to ensure code style at the turf module level.Gday @DenisCarriere
First commit of turf-dissolve module.