geo icon indicating copy to clipboard operation
geo copied to clipboard

WKTReader throws Exception

Open treehousetim opened this issue 3 years ago • 1 comments

Code

<?php
define( 'DEFAULT_SRID', 4326 );

function getGeometry( string $wkt ) : Geometry
{
	return (new WKTReader())
		->read( $wkt, DEFAULT_SRID );
}

getGeometry( 'MULTIPOLYGON(((-96.5198207634025 39.0656352587354,-96.5337852030667 39.0657393639196,-96.5337039197601 39.0730173561199,-96.5198057421105 39.0728878509279,-96.5198207634025 39.0656352587354)))' );

Output

Expected word but encountered '('

#0 /opt/site/vendor/brick/geo/src/IO/AbstractWKTReader.php(35): Brick\\Geo\\IO\\WKTParser->getNextWord()
#1 /opt/site/vendor/brick/geo/src/IO/WKTReader.php(24): Brick\\Geo\\IO\\AbstractWKTReader->readGeometry()
#2 /opt/site/app/myCode.php(7): Brick\\Geo\\IO\\WKTReader->read()

Thoughts

I believe the format is correct - it has been inserted into a multi polygon column in a PostGIS enabled database and is coming back out as text. I'm trying to take the string and convert it back into a brick/geo object.

I can take my field value and plot it out on this tool:

https://clydedacruz.github.io/openstreetmap-wkt-playground/

and that parses it just fine.

Any help would be appreciated - thanks!

treehousetim avatar Mar 12 '22 00:03 treehousetim

Hi, your example works fine for me:

<?php

define( 'DEFAULT_SRID', 4326 );

require 'vendor/autoload.php';

use Brick\Geo\IO\WKTReader;
use Brick\Geo\Geometry;

function getGeometry( string $wkt ) : Geometry
{
        return (new WKTReader())
                ->read( $wkt, DEFAULT_SRID );
}

$g = getGeometry( 'MULTIPOLYGON(((-96.5198207634025 39.0656352587354,-96.5337852030667 39.0657393639196,-96.5337039197601 39.0730173561199,-96.5198057421105 39.0728878509279,-96.51982076340>
echo $g->asText();

Please make sure that you don't have control chars (null byte, etc.) at the beginning of your string.

BenMorel avatar Mar 12 '22 10:03 BenMorel

Closing due to lack of feedback.

BenMorel avatar Oct 26 '22 21:10 BenMorel