Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Fix coordinate precision #173

Merged
merged 4 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,22 @@ Available geometry classes:
* Polygon
* MultiPolygon
* GeometryCollection

## Publishing config
A configuration file exists for overriding default values. To add to your project, run:

```sh
php artisan vendor:publish --provider="MStaack\LaravelPostgis\DatabaseServiceProvider" --tag="postgis"
```
### Coordinate precision
The precision of stored/displayed coordinated can be customised in the config.


```php
# config/postgis.php
return [
...
'precision' => 6,
];
```
See http://wiki.gis.com/wiki/index.php/Decimal_degrees#Accuracy for more information
3 changes: 2 additions & 1 deletion config/postgis.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

return [
'schema' => 'public' // Schema for the Postgis extension
'schema' => 'public', // Schema for the Postgis extension,
'precision' => 6, // Control precision of floats in stringifyFloat
];
23 changes: 19 additions & 4 deletions src/Geometries/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,26 @@ class Point extends Geometry
protected $lat;
protected $lng;
protected $alt;
protected $precision;

public function __construct($lat, $lng, $alt = null)
{
$this->lat = (float)$lat;
$this->lng = (float)$lng;
$this->alt = isset($alt) ? (float)$alt : null;
$this->setPrecision(
function_exists('config') ? config('postgis.precision') : 6
);
}

public function setPrecision($precision)
{
$precision = filter_var($precision, FILTER_VALIDATE_INT);
if (!is_int($precision)) {
throw new \UnexpectedValueException('Precision must be an integer');
}

$this->precision = $precision;
}

public function getLat()
Expand Down Expand Up @@ -52,17 +66,18 @@ public function is3d()

public function toPair()
{
$pair = self::stringifyFloat($this->getLng()) . ' ' . self::stringifyFloat($this->getLat());
$pair = $this->stringifyFloat($this->getLng()) . ' ' . $this->stringifyFloat($this->getLat());
if ($this->is3d()) {
$pair .= ' ' . self::stringifyFloat($this->getAlt());
$pair .= ' ' . $this->stringifyFloat($this->getAlt());
}
return $pair;
}

private static function stringifyFloat($float)
private function stringifyFloat($float)
{
// normalized output among locales
return rtrim(rtrim(sprintf('%F', $float), '0'), '.');

return rtrim(rtrim(sprintf("%.{$this->precision}F", $float), '0'), '.');
}

public static function fromPair($pair)
Expand Down
16 changes: 16 additions & 0 deletions tests/Geometries/PointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,20 @@ public function testJsonSerialize3d()
$this->assertInstanceOf(\GeoJson\Geometry\Point::class, $point->jsonSerialize());
$this->assertSame('{"type":"Point","coordinates":[3.4,1.2,5.6]}', json_encode($point));
}

public function testPointPrecisionDefault()
{
$point = new Point(-37.8745505,144.9102885,12.38);

$this->assertSame('144.910289 -37.87455 12.38', $point->toPair());

}

public function testPointPrecision10()
{
$point = new Point(-37.87455051578,144.91028850798,7.38257341563);
$point->setPrecision(10);

$this->assertSame('144.910288508 -37.8745505158 7.3825734156', $point->toPair());
}
}