PHP code example of willvincent / turf-php

1. Go to this page and download the library: Download willvincent/turf-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

willvincent / turf-php example snippets


use GeoJson\GeoJson;
use Turf\Turf;

$featureCollection = GeoJson::jsonUnserialize(json_decode('
    {"type":"FeatureCollection","features":[{"type":"Feature","properties":{"name":"LAX"},
     "geometry":{"coordinates":[[[-118.43130558364376,33.94968604365768],[-118.4012727129631,33.952569329158464],
     [-118.40029702645336,33.951330030271265],[-118.39709555509219,33.95155765795056],
     [-118.39679065305795,33.949812496849574],[-118.39974820279133,33.948750207354195],
     [-118.39996163421532,33.945689727778216],[-118.40846840097439,33.94498150194964],
     [-118.40798055771938,33.9421738344311],[-118.39718702570275,33.9431350283305],
     [-118.39419898576588,33.94450091678105],[-118.39133290664276,33.944804444571744],
     [-118.39079018485972,33.94308612928623],[-118.37905145653627,33.94394613617513],
     [-118.37889900551932,33.93582631317177],[-118.37908199117742,33.93213293103197],
     [-118.39511983818524,33.932006442427294],[-118.41701182370598,33.93172817593698],
     [-118.42484782241374,33.9317281792144],[-118.42856762723284,33.93134871103278],
     [-118.4328667459175,33.93914011860488],[-118.43561083211533,33.94422979717065],
     [-118.43536691048782,33.94683505044485],[-118.43152514485466,33.94772031253221],
     [-118.43130558364376,33.94968604365768]]],"type":"Polygon"}}]}
'));

// Default is Square Meters, but you can easily
// request a different unit of measure with a second parameter
$area = Turf::area($featureCollection);

$square_miles = Turf::area($featureCollection, 'miles');

use GeoJson\Geometry\Point;
use GeoJson\Geometry\Polygon;
use Turf\Turf;

$point = new Point([-77, 44]);
$polygon = new Polygon([
    [[-81, 41], [-81, 47], [-72, 47], [-72, 41], [-81, 41]]
]);

echo Turf::booleanPointInPolygon($point, $polygon) ? 'true' : 'false'; // Output: true

use GeoJson\Feature\Feature;
use GeoJson\Feature\FeatureCollection;
use GeoJson\Geometry\Polygon;
use Turf\Turf;

$polygon1 = new Polygon([
  [
    [-87.62411094338104, 41.880714036293114],
    [-87.62411094338104, 41.87843046431638],
    [-87.6209051112661, 41.87843046431638],
    [-87.6209051112661, 41.880714036293114],
    [-87.62411094338104, 41.880714036293114]
  ]
]);
$polygon2 = new Polygon([
  [
    [-87.61655715285198, 41.884051046913896],
    [-87.61541915184196, 41.884164700799886],
    [-87.61534976153654, 41.88380307409719],
    [-87.61648776254657, 41.88369975180643],
    [-87.61655715285198, 41.884051046913896]
  ]
]);

$featureCollection = new FeatureCollection([
  new Feature($polygon1),
  new Feature($polygon2)
]);

// Draw a bounding box around our feature collection
$bbox = Turf::bbox($featureCollection);

// Generate a grid from the bounding box
$grid = Turf::squareGrid($bbox, 0.25, 'kilometers');