Ghif Aliy
Ghif Aliy

Reputation: 21

*pointsWithinPolygon* on Trufjs

I'm trying to get some point inside multiple polygon using pointsWithinPolygon in turfjs, but the result is unexpected.

Is there any chance that pointsWithinPolygon isn't compatible with FeatureCollection?

Here is the example of my usage.

let points = {
    "type": "Point",
    "coordinates": [
        106.866995, -6.261513
    ]
}

let filter = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "IdArea": 4
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            106.314674,
                            -6.6348689124
                        ],
                        [
                            107.5781628906,
                            -6.6348689124
                        ],
                        [
                            107.5781628906,
                            -5.9742195859
                        ],
                        [
                            106.314674,
                            -5.9742195859
                        ],
                        [
                            106.314674,
                            -6.6348689124
                        ]
                    ]
                ]
            }
        }
    ]
}

let result = turf.pointsWithinPolygon(points, filter);
console.log(result); 

Upvotes: 2

Views: 2386

Answers (1)

stephanie
stephanie

Reputation: 51

You are trying to use geoJsons as inputs to turf's pointsWithinPolygons function.

The function takes a turf.points array of points, and a turf.polygon array of polygon vertices.

Based on the variables you established, you would need to call:

let result = turf.pointsWithinPolygon(turf.points([points.coordinates]), turf.polygon(filter.features.geometry.coordinates));

Upvotes: 3

Related Questions