phayes / geoPHP

Advanced geometry operations in PHP
https://geophp.net
Other
861 stars 262 forks source link

Cannot construct Point. x and y should be numeric: trim coordinates #181

Open jbogdani opened 3 years ago

jbogdani commented 3 years ago

Hello and thank you for this library.

I'm having an issue on loading WKT with extra spaces, for instance:

$geo = \geoPHP::load('POINT ( 1 1 )', 'wkt');

As I can see at Point.class.php the basic validation is failing due to the trailing space on the first coordinate. I know I can clean my data by removing these spaces, but the point is that POINT ( 1 1 ) is valid WKT, and I guess geoPHP should parse it correctly.

I guess that trimming the input could solve this issue: Point.class.php:

if (!is_numeric(trim($x)) || !is_numeric(trim($y))) {
    throw new Exception("Cannot construct Point. x and y should be numeric");
}
// Check to see if this is a 3D point
if ($z !== NULL) {
    if (!is_numeric(trim($z))) {
        throw new Exception("Cannot construct Point. z should be numeric");
    }
    $this->dimension = 3;
}

The floatval below also trims the values, so there is no need to repeat the trim after the validation.

BathoryPeter commented 3 years ago

This has already been solved in several fork, for example look at mine.

jbogdani commented 3 years ago

Thank you @BathoryPeter! I guess the main repository is not actively maintained. I'm going to point at yours, thank you.