turfjs

Convert output of turfjs turf.lineChunk to turf.points


Just wondering if there is a way to easily convert the output of turf.lineChunk to turf.points?

     testLineString = {
        "type": "Feature",
        "properties": {},
        "geometry": {
            "type": "LineString",
            "coordinates": [                    
                [
                    155.9668,
                    -2.9402
                ],
                [
                    162.959225,
                    -2.94504
                ],
            ]
        }                   
    };

var chunk = turf.lineChunk(testLineString, 25, { units: "kilometers" });

var points = turf.points([ [], [], [], ]);

Hoping for something simple like this (but this doesn't work)

var points = turf.points(chunk.features); 

I would like the points to be able to be used in turf.pointsWithinPolygon()

var inside = turf.pointsWithinPolygon(points ,turfpolygon);

Thank you.


Solution

  • Does not seem to be an easy way to do this other than a loop like below:

    for (var x = 0; x < chunkedLine.features.length; x++) {
      if (foundWithinPolygon == false) {
                                        points = turf.points([
                                            [chunkedLine.features[x].geometry.coordinates[0][0], chunkedLine.features[x].geometry.coordinates[0][1]],
                                            [chunkedLine.features[x].geometry.coordinates[1][0], chunkedLine.features[x].geometry.coordinates[1][1]],
                                        ]);
                                        var ptsWithin = turf.pointsWithinPolygon(points, turfpolygon);
                                        if (ptsWithin.features.length > 0) {
                                            foundWithinPolygon = true;
                                            break;
                                        }
                        }
        }