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

Introduces GeometryUtil #463

Merged
merged 6 commits into from Mar 9, 2018
Merged

Conversation

KaiVolland
Copy link
Member

@KaiVolland KaiVolland commented Mar 8, 2018

FEATURE

Description:

This introduces the GeometryUtil including these methods:

  • splitByLine: Splits an Polygon-Feature or -Geometry into multiple Features or Geometries
    💡 great job @dnlkoch
  • addBuffer: Adds a Buffer to an Feature or Geometry
  • mergeGeometries: Merges mutliple Geometries into on MutliGeometry
  • union: Unions multiple Features or Geometries into one Feature or Geometry
  • difference: Finds the difference between two polygons by clipping the second polygon from the first.
  • intersection: Takes two polygons and finds their intersection.

This introduces Turf as a dependency

💡 Splitting a geometry with a hole was not yet tested and might be failing.

@KaiVolland KaiVolland changed the title Introduces GeometryUtil [WIP] Introduces GeometryUtil Mar 8, 2018
This introduces the GeometryUtil including these methods:
- splitByLine
- addBuffer
- mergeGeometries
- union

Co-authored-by: dnlkoch <koch@terrestris.de>
@KaiVolland KaiVolland changed the title [WIP] Introduces GeometryUtil Introduces GeometryUtil Mar 8, 2018
Copy link
Member

@marcjansen marcjansen left a comment

Choose a reason for hiding this comment

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

Very nice work. I was quite chatty in my review but really like this. Take from my comments what you agree with and then please merge. Thanks guys!

*
* @param {ol.feature | ol.geom.Polygon} polygon The polygon geometry to split.
* @param {ol.feature | ol.geom.LineString} lineFeat The line geometry to split the polygon
* geometry with.
Copy link
Member

@marcjansen marcjansen Mar 8, 2018

Choose a reason for hiding this comment

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

Will this also work for multi-geometries?

Copy link
Member

Choose a reason for hiding this comment

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

Does this work with (Multi-)polygons with holes?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes it does. I'll add a test.

return features;
} else {
return features.map(f => f.getGeometry());
}
Copy link
Member

@marcjansen marcjansen Mar 8, 2018

Choose a reason for hiding this comment

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

We should more clearly document that the return value is dependent on the input values. Thinking again I would rather always return one thing, possibly the features. I think this is a better API. Non-blocking.

*
* @param {ol.geom.Geometry | ol.Feature} geometry The geometry.
* @param {Integer} buffer The buffer to add in meters.
* @param {String} projection A projection as EPSG code.
Copy link
Member

@marcjansen marcjansen Mar 8, 2018

Choose a reason for hiding this comment

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

Why would I need that? Documentation enhancement welcome.

* Adds a buffer to a given geometry.
*
* @param {ol.geom.Geometry | ol.Feature} geometry The geometry.
* @param {Integer} buffer The buffer to add in meters.
Copy link
Member

@marcjansen marcjansen Mar 8, 2018

Choose a reason for hiding this comment

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

Integer is not a valid type I guess. What happens if a float like number is passed? Can we use this to dissolve a feature with a negative value? Will buffering consider holes in polygons, and how?

if (geometry instanceof OlFeature) {
return geoJsonFormat.readFeature(buffered);
} else {
return geoJsonFormat.readGeometry(buffered.geometry);
Copy link
Member

Choose a reason for hiding this comment

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

I think I like one return type better. but this is not blocking this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Docs are enhanced to clearify why we two different return types.

return geoJsonFormat.writeFeatureObject(feature);
});
if (invalid) {
// Logger.warn('Can only create union of polygons.');
Copy link
Member

@marcjansen marcjansen Mar 8, 2018

Choose a reason for hiding this comment

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

Hmm. why not warn? And why don't we iterate over the single parts of the multipolygon in that case?

Copy link
Member

Choose a reason for hiding this comment

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

Are holes in (Multi)polygons also covered here?

Copy link
Member Author

Choose a reason for hiding this comment

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

@marcjansen I don't think we do log anywhere in react-geo... We just have loglevel as dependency. 🙈

@ahennr Yes. I won't add anymore test as these would be more or less just tests agains turf.js. But it tested it once in a sandbox.

if (polygons[0] instanceof OlFeature) {
return feature;
} else {
return feature.getGeometry();
Copy link
Member

Choose a reason for hiding this comment

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

I get it now. the different return types seem to be everywhere.

* |
* +
*/
it('splits the given convex polygon geometry with a straight line', () => {
Copy link
Member

@marcjansen marcjansen Mar 8, 2018

Choose a reason for hiding this comment

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

Indentation is of one space I think. Otherwise impressive ascii art 😎

* | |
* | |
* +-------------+
*/
Copy link
Member

Choose a reason for hiding this comment

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

indentation

* | |
* +-------------------+
*
*/
Copy link
Member

Choose a reason for hiding this comment

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

indentation off. probably some more occurences.

Copy link
Member

@ahennr ahennr left a comment

Choose a reason for hiding this comment

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

Nice enhancement @KaiVolland and @dnlkoch 👏

@@ -69,6 +69,7 @@
"react": "~16.0"
},
"dependencies": {
"@turf/turf": "5.1.6",
Copy link
Member

Choose a reason for hiding this comment

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

Does turf have an integrated validation method, or is it possible to use the one of jsts (which is a dependency of turf AFAIK)

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 think turf.js has a validation method. We could a jsts as an extra dependency if we want to have validation in react-geo. We can't rely on the existence of jsts by using turf.js as the dependency might be remove.

*
* @param {ol.feature | ol.geom.Polygon} polygon The polygon geometry to split.
* @param {ol.feature | ol.geom.LineString} lineFeat The line geometry to split the polygon
* geometry with.
Copy link
Member

Choose a reason for hiding this comment

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

Does this work with (Multi-)polygons with holes?

}
});
if (mixedGeometryTypes) {
// Logger.warn('Can not merge mixed geometries into one multigeometry.');
Copy link
Member

Choose a reason for hiding this comment

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

Can this logger be removed / activated?

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'd like to leave it as is. We may wan't make use of a logger in the near future and this way we don't forget the loggers here.

return geoJsonFormat.writeFeatureObject(feature);
});
if (invalid) {
// Logger.warn('Can only create union of polygons.');
Copy link
Member

Choose a reason for hiding this comment

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

Are holes in (Multi)polygons also covered here?

* @returns {ol.geom.Geometry | ol.Feature} A Feature or Geometry with the area
* of polygon1 excluding the area of polygon2.
*/
static difference(polygon1, polygon2, projection = 'EPSG:3857') {
Copy link
Member

Choose a reason for hiding this comment

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

It would be possible to pass linestring geometries (e.g.) as well here, wouldn't it? If true, one could be more general here, e.g. ... Find the difference between two geometries ...

Copy link
Member Author

Choose a reason for hiding this comment

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

turf.js only supports difference for polygons.

* @returns {ol.geom.Geometry | ol.Feature} A Feature or Geometry with the
* shared area of the two polygons or null if the polygons don't intersect.
*/
static intersection(polygon1, polygon2, projection = 'EPSG:3857') {
Copy link
Member

Choose a reason for hiding this comment

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

See above comment regarding generality.

Copy link
Member Author

Choose a reason for hiding this comment

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

turf.js only supports difference for intersect.

});
});
});
});
Copy link
Member

Choose a reason for hiding this comment

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

🔝

@KaiVolland
Copy link
Member Author

I extracted the geometries to a seperate file to increase the readability of the spec file and added some more docs.

@KaiVolland KaiVolland merged commit 894260b into terrestris:master Mar 9, 2018
@KaiVolland KaiVolland deleted the geometry-util branch April 3, 2018 08:28
hblitza pushed a commit that referenced this pull request Oct 19, 2022
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

4 participants