2

I have about 300 polygons delineating ski areas. The GeoJSON is called aa_winter_sports_polygons.geojson. I have thousands of ski runs (aka pistes) that either intersect the polygon or are contained within the polygon. Ultimately, I want to sum the length of the lines but before I do that I have to get the script recognizing lines in/intersecting the polygon. I have entered "Bingo!" for now to state whether the line in/intersected a polygon but no "bingo!" at all.

Here is my script:

require 'vendor/autoload.php';
$aa_winter_sports_polygonsGeoJSON = json_decode(file_get_contents('aa_winter_sports_polygons.geojson'));
$aa_pistesGeoJSON  = json_decode(file_get_contents('aa_pistes.geojson'));

$i = 0;

// Loop through each feature (polygon) in the first collection
foreach ($aa_winter_sports_polygonsGeoJSON->features as $polygonFeature) {
    // Convert each polygon feature to a GeoPHP geometry object
    $polygon = geoPHP::load(json_encode($polygonFeature->geometry), 'json');


    // Loop through each feature (line) in the second collection
    foreach ($aa_pistesGeoJSON->features as $lineFeature) {
        
        // Convert each line feature to a GeoPHP geometry object
        $line = geoPHP::load(json_encode($lineFeature->geometry), 'json');

        // Check if the line is either contained within the polygon or intersects with it
        if ($polygon->contains($line) || $polygon->intersects($line)) {
            echo "bingo!</br>";

        }
        echo $i++.'</br>';
    }
}

When I sum up the lines in QGIS, every polygon has pistes within it so I am doing something wrong.

1 Answer 1

1

I found the solution. As a matter of fact "contains" is a feature from GEOS. I installed GEOS and now it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.