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
Conversation
290b71e
to
5116d98
Compare
This introduces the GeometryUtil including these methods: - splitByLine - addBuffer - mergeGeometries - union Co-authored-by: dnlkoch <koch@terrestris.de>
5116d98
to
448c6a4
Compare
ebb3ad5
to
04e60ca
Compare
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.
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. |
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.
Will this also work for multi-geometries?
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.
Does this work with (Multi-)polygons with holes?
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.
Yes it does. I'll add a test.
return features; | ||
} else { | ||
return features.map(f => f.getGeometry()); | ||
} |
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.
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. |
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.
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. |
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.
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); |
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 think I like one return type better. but this is not blocking this.
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.
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.'); |
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.
Hmm. why not warn? And why don't we iterate over the single parts of the multipolygon in that case?
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.
Are holes in (Multi)polygons also covered here?
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.
@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(); |
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 get it now. the different return types seem to be everywhere.
* | | ||
* + | ||
*/ | ||
it('splits the given convex polygon geometry with a straight line', () => { |
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.
Indentation is of one space I think. Otherwise impressive ascii art 😎
* | | | ||
* | | | ||
* +-------------+ | ||
*/ |
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.
indentation
* | | | ||
* +-------------------+ | ||
* | ||
*/ |
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.
indentation off. probably some more occurences.
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.
Nice enhancement @KaiVolland and @dnlkoch 👏
@@ -69,6 +69,7 @@ | |||
"react": "~16.0" | |||
}, | |||
"dependencies": { | |||
"@turf/turf": "5.1.6", |
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.
Does turf
have an integrated validation method, or is it possible to use the one of jsts
(which is a dependency of turf
AFAIK)
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 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. |
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.
Does this work with (Multi-)polygons with holes?
} | ||
}); | ||
if (mixedGeometryTypes) { | ||
// Logger.warn('Can not merge mixed geometries into one multigeometry.'); |
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.
Can this logger be removed / activated?
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'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.'); |
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.
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') { |
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.
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 ...
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.
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') { |
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.
See above comment regarding generality.
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.
turf.js
only supports difference for intersect.
}); | ||
}); | ||
}); | ||
}); |
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 extracted the geometries to a seperate file to increase the readability of the spec file and added some more docs. |
FEATURE
Description:
This introduces the GeometryUtil including these methods:
💡 great job @dnlkoch
This introduces Turf as a dependency
💡 Splitting a geometry with a hole was not yet tested and might be failing.