Skip to content

Conversation

brianherbert
Copy link
Contributor

This change checks if a geometry/ies in GeoJSON are set as null, in which case it will skip it.

I tried to follow the coding style for conditionals but I couldn't find any long if statements like the one I'm contributing. Should there be a better way to format this?

Related to issue #1235

This change checks if a geometry/ies in GeoJSON are set as null, in
which case it will skip it.
@brianherbert
Copy link
Contributor Author

Here's some sample JSON in case it's needed for testing or demonstration. The first feature has a single point, second is a collection of geometries and the third is null.

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    126.982812881,
                    37.5944097607
                ]
            },
            "properties": {
                "popupContent": "I'm a single point."
            }
        },
        {
            "type": "GeometryCollection",
            "geometries": [
                {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                -33.3984375,
                                49.8379824531
                            ],
                            [
                                -33.837890625,
                                43.0046471278
                            ],
                            [
                                -23.466796875,
                                42.6177914328
                            ],
                            [
                                -33.3984375,
                                49.8379824531
                            ]
                        ]
                    ]
                },
                {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                -27.158203125,
                                49.8946343957
                            ],
                            [
                                -19.423828125,
                                44.9647979303
                            ],
                            [
                                -11.6015625,
                                41.3768085657
                            ],
                            [
                                -4.04296875,
                                44.715513732
                            ],
                            [
                                -7.3828125,
                                50.2331518325
                            ],
                            [
                                -17.05078125,
                                51.6725551484
                            ],
                            [
                                -27.158203125,
                                49.8946343957
                            ]
                        ]
                    ]
                },
                {
                    "type": "LineString",
                    "coordinates": [
                        [
                            0,
                            50.5134265263
                        ],
                        [
                            4.04296875,
                            42.4883019796
                        ],
                        [
                            0,
                            50.5134265263
                        ]
                    ]
                },
                {
                    "type": "Point",
                    "coordinates": [
                        5.361328125,
                        50.5134265263
                    ]
                },
                {
                    "type": "LineString",
                    "coordinates": [
                        [
                            -33.310546875,
                            51.1793429793
                        ],
                        [
                            -13.359375,
                            38.8225909762
                        ],
                        [
                            -33.310546875,
                            51.1793429793
                        ]
                    ]
                }
            ],
            "properties": {
                "popupContent": "I should have multiple geometries showing."
            }
        },
        {
            "type": "Feature",
            "geometry": null,
            "properties": {
                "popupContent": "This is some content in a null geometry which shouldn't show up."
            }
        }
    ]
}

this.addData(features[i]);
// Only add this if geometry or geometries are set and not null
if ((typeof features[i].geometries !== undefined && features[i].geometries !== null) &&
(typeof features[i].geometry !== undefined && features[i].geometry !== null)) {
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 just if (features[i].geometries || features[i].geometry) will be enough. It will coerce to false if not present or null and to true otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes, that's much more elegant. Let me make the changes.

@abierbaum
Copy link

Just ran into this bug today as well. Can't wait to see a solution merged.

Used feedback from @mourner to make the change from the pull request
Leaflet#1240
@brianherbert
Copy link
Contributor Author

I made the changes and it seems to check out fine on my end. Everything looks good?

@mourner
Copy link
Member

mourner commented Dec 28, 2012

Yep!

mourner added a commit that referenced this pull request Dec 28, 2012
@mourner mourner merged commit 733666b into Leaflet:master Dec 28, 2012
emartinez-usgs pushed a commit to emartinez-usgs/Leaflet that referenced this pull request Feb 27, 2013
Used feedback from @mourner to make the change from the pull request
Leaflet#1240
emartinez-usgs pushed a commit to emartinez-usgs/Leaflet that referenced this pull request Aug 15, 2013
Used feedback from @mourner to make the change from the pull request
Leaflet#1240
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.

3 participants