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

Support Null geometries #866

Merged
merged 16 commits into from
Jul 27, 2017
Merged

Support Null geometries #866

merged 16 commits into from
Jul 27, 2017

Conversation

DenisCarriere
Copy link
Member

@DenisCarriere DenisCarriere commented Jul 26, 2017

Adds null geometry support.

Ref: #853

Modified

  • @turf/invariant
    • getGeom
    • getGeomType
    • getCoord
    • getCoords
  • @turf/helpers
    • feature
    • featureCollection
  • @turf/meta
    • geomEach
    • geomReduce
    • featureEach
    • featureReduce
    • flattenEach
    • flattenReduce
    • coordEach
    • coordReduce

Implementation Discussions

Callbacks Feature/Geometry with null geometry

  • geomEach
  • flattenEach
  • featureEach

Does NOT return any Coordinate/Segment

  • coordEach
  • segmentEach

Tested

@DenisCarriere DenisCarriere added this to the 4.6.0 milestone Jul 26, 2017
@DenisCarriere DenisCarriere self-assigned this Jul 26, 2017
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 I believe we should allow/hande only the geometry: null case; a type member must be defined to have a valid GeoJSON object (see specs), so null or an object with type:null are not valid inputs.

properties: {},
geometry: null
}
t.equal(invariant.getGeom(null), null);
Copy link
Collaborator

Choose a reason for hiding this comment

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

@DenisCarriere why supporting null values? Reading the specs it seems the GeoJSON objects is an object and has to have a type member (which in my understanding is the only requirement to be a valid GeoJSON object)


if (geometry.type === 'Point') {
switch (geomType) {
case null:
Copy link
Collaborator

Choose a reason for hiding this comment

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

@DenisCarriere same here, from the specs:

  • A GeoJSON object has a member with the name "type". The value of
    the member MUST be one of the GeoJSON types.

I guess the geometry attribute might be null, but the type must be a valid string

Copy link
Member Author

@DenisCarriere DenisCarriere Jul 26, 2017

Choose a reason for hiding this comment

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

This section is more internal and doesn't need to reflect the exact spec. Only way to iterate over a FeatureCollection (containing valid null geometries) with coordEach is to not throw an error Unknown Geometry Type since we know that null is a valid geometry.

var geomType = (geometry === null) ? null : geometry.type;
switch (geomType) {
case null:
    return;

Simply breaks the loop and moves on to the next valid geometry.

@DenisCarriere
Copy link
Member Author

Thanks for looking to this @stebogit , I wasn't too sure how to handle this for the invariant module.

I believe we should allow/hande only the geometry: null case; a type member must be defined to have a valid GeoJSON object (see specs), so null or an object with type:null are not valid inputs.

👍 Yep, we should only handle the geometry: null case.

@DenisCarriere
Copy link
Member Author

@stebogit ❓ Question for you: When we're using geomEach or flattenEach and a FeatureCollection contains Features with null geometry, these should be excluded from the iterating process, correct?

@stebogit
Copy link
Collaborator

When we're using geomEach or flattenEach and a FeatureCollection contains Features with null geometry, these should be excluded from the iterating process

Good question @DenisCarriere... 🤔
I would not exclude them, for we confirmed they are 100% valid GeoJSON features.
What if you actually need to iterate through them to actually check and populate the geometry field?

@DenisCarriere
Copy link
Member Author

DenisCarriere commented Jul 27, 2017

we confirmed they are 100% valid GeoJSON features.

Agreed they are valid GeoJSON features, however geomEach is meant to return valid GeometryObjects which null isn't since it doesn't have a type (that's my reason why we should exclude them?).

We could return a null Feature for flattenEach since that is "valid" GeoJSON Feature.

🤔 still thinking how we should approach this one...

@DenisCarriere
Copy link
Member Author

🤔 flattenEach uses geomEach...

function flattenEach(geojson, callback) {
    geomEach(geojson, function (geometry, index, properties) {

So the options are, we callback both null geometries, or we exclude both...

@DenisCarriere
Copy link
Member Author

@stebogit Chose to preserve null geometries for flattenEach & geomEach.

What if you actually need to iterate through them to actually check and populate the geometry field?

👍 Good point.

@DenisCarriere
Copy link
Member Author

@stebogit Have a glance at this one last time, I'm 👍 to merge this.

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 I added a couple of comments here, hopefully helpful

@@ -248,6 +248,7 @@ function multiPolygon(coordinates, properties) {
*/
function geometryCollection(geometries, properties) {
if (!geometries) throw new Error('geometries is required');
Copy link
Collaborator

Choose a reason for hiding this comment

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

@DenisCarriere just being very fussy here, but from specs geometryCollections can have an empty geometries array.
But (!geometries) would exclude that, right?

Same goes actually for FeatureCollection, which I'm noticing has the same check.

Copy link
Member Author

@DenisCarriere DenisCarriere Jul 27, 2017

Choose a reason for hiding this comment

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

We can add those in the tests, but !geometries would not exclude empty geometries inside, it would only exclude geometries: null or undefined.

lets add them to the tests and see what happens from there :) lol

Copy link
Member Author

Choose a reason for hiding this comment

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

(!geometries) would not throw an error if you provide an empty Array.

$ node
> ![1,2,3]
false
> ![]
false
> !undefined
true
> !null
true

Copy link
Member Author

Choose a reason for hiding this comment

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

Added tests for this case: c1edd39

Copy link
Collaborator

Choose a reason for hiding this comment

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

ah! 👍 sorry, PHP confusion! 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

lol! Everything language has their quirks

@@ -32,64 +32,73 @@ function coordEach(geojson, callback, excludeWrapCoord) {
wrapShrink = 0,
currentIndex = 0,
isGeometryCollection,
isFeatureCollection = geojson.type === 'FeatureCollection',
isFeature = geojson.type === 'Feature',
type = (geojson) ? geojson.type : null,
Copy link
Collaborator

Choose a reason for hiding this comment

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

@DenisCarriere isn't geojson required here? I mean, shouldn't this actually throw an error if !geojson?
Then type = geojson.type any time, as geojson.type can be null
Sorry, type can NOT be null, it would be an invalid GeoJSON.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well since a Geometry can be passed to coordEach, we need to handle null somehow.

Maybe its' best to add the null handling at the very top of the function.

if (geojson === null) return;

That way the rest of the code can remain unchanged.

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

Successfully merging this pull request may close these issues.

2 participants