PHP code example of brick / geo

1. Go to this page and download the library: Download brick/geo 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/ */

    

brick / geo example snippets


use Brick\Geo\LineString;
use Brick\Geo\Point;
use Brick\Geo\Polygon;

// Building geometries from coordinates

$lineString = LineString::of(
    Point::xy(1, 2),
    Point::xy(3, 4),
);

echo $lineString->asText(); // LINESTRING (1 2, 3 4)

// Importing geometries

$point = Point::fromText('POINT (1 2)');

echo $point->x(); // 1
echo $point->y(); // 2

// Using advanced calculations from a GeometryEngine
// (see the Configuration section)

$polygon = Polygon::fromText('POLYGON ((0 0, 0 3, 3 3, 0 0))');
echo $geometryEngine->area($polygon); // 4.5

$centroid = $geometryEngine->centroid($polygon);
echo $centroid->asText(); // POINT (1 2)

    use Brick\Geo\Engine\PDOEngine;
    
    $pdo = new PDO('mysql:host=localhost', 'root', '');
    $geometryEngine = new PDOEngine($pdo);
    

    use Brick\Geo\Engine\PDOEngine;
    
    $pdo = new PDO('pgsql:host=localhost', 'postgres', '');
    $geometryEngine = new PDOEngine($pdo);
    

    use Brick\Geo\Engine\SQLite3Engine;
    
    $sqlite3 = new SQLite3(':memory:');
    $sqlite3->loadExtension('mod_spatialite.so');
    $geometryEngine = new SQLite3Engine($sqlite3);
    

    use Brick\Geo\Engine\GEOSEngine;
    
    $geometryEngine = new GEOSEngine();
    

use Brick\Geo\Point;

$point = Point::fromText('POINT (1.5 2.5)', 4326);

echo $point->asText(); // POINT (1.5 2.5)

use Brick\Geo\Point;

$point = Point::fromBinary(hex2bin('0101000000000000000000f83f0000000000000440'), 4326);

echo $point->asText(); // POINT (1.5 2.5)
echo $point->SRID(); // 4326

echo bin2hex($point->asBinary()); // 0101000000000000000000f83f0000000000000440

use Brick\Geo\Point;
use Brick\Geo\IO\EWKTReader;
use Brick\Geo\IO\EWKTWriter;

$reader = new EWKTReader();
$point = $reader->read('SRID=4326; POINT (1.5 2.5)');

echo $point->asText(); // POINT (1.5 2.5)
echo $point->SRID(); // 4326

$writer = new EWKTWriter();
echo $writer->write($point); // SRID=4326; POINT (1.5 2.5)

use Brick\Geo\Point;
use Brick\Geo\IO\EWKBReader;
use Brick\Geo\IO\EWKBWriter;

$reader = new EWKBReader();
$point = $reader->read(hex2bin('0101000020e6100000000000000000f83f0000000000000440'));

echo $point->asText(); // POINT (1.5 2.5)
echo $point->SRID(); // 4326

$writer = new EWKBWriter();
echo bin2hex($writer->write($point)); // 0101000020e6100000000000000000f83f0000000000000440

use Brick\Geo\Point;
use Brick\Geo\IO\GeoJSONReader;
use Brick\Geo\IO\GeoJSONWriter;

$reader = new GeoJSONReader();
$point = $reader->read('{ "type": "Point", "coordinates": [1, 2] }');

echo $point->asText(); // POINT (1 2)
echo $point->SRID(); // 4326

$writer = new GeoJSONWriter();
echo $writer->write($point); // {"type":"Point","coordinates":[1,2]}

use Brick\Geo\Point;
use Brick\Geo\Projector\RoundCoordinatesProjector;

$roundProjector = new RoundCoordinatesProjector(2);

$point = Point::xy(1.2345678, 2.3456789);
echo $point->asText(); // POINT (1.2345678 2.3456789)

$roundedPoint = $point->project($roundProjector);
echo $roundedPoint->asText(); // POINT (1.23 2.35)