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

Commit cd19db2

Browse files
authored
Merge pull request #173 from scottlimmer/fix-167-coordinate-precision
Fix coordinate precision
2 parents 68e522e + a0755be commit cd19db2

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,22 @@ Available geometry classes:
233233
* Polygon
234234
* MultiPolygon
235235
* GeometryCollection
236+
237+
## Publishing config
238+
A configuration file exists for overriding default values. To add to your project, run:
239+
240+
```sh
241+
php artisan vendor:publish --provider="MStaack\LaravelPostgis\DatabaseServiceProvider" --tag="postgis"
242+
```
243+
### Coordinate precision
244+
The precision of stored/displayed coordinated can be customised in the config.
245+
246+
247+
```php
248+
# config/postgis.php
249+
return [
250+
...
251+
'precision' => 6,
252+
];
253+
```
254+
See http://wiki.gis.com/wiki/index.php/Decimal_degrees#Accuracy for more information

config/postgis.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

33
return [
4-
'schema' => 'public' // Schema for the Postgis extension
4+
'schema' => 'public', // Schema for the Postgis extension,
5+
'precision' => 6, // Control precision of floats in stringifyFloat
56
];

src/Geometries/Point.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,26 @@ class Point extends Geometry
77
protected $lat;
88
protected $lng;
99
protected $alt;
10+
protected $precision;
1011

1112
public function __construct($lat, $lng, $alt = null)
1213
{
1314
$this->lat = (float)$lat;
1415
$this->lng = (float)$lng;
1516
$this->alt = isset($alt) ? (float)$alt : null;
17+
$this->setPrecision(
18+
function_exists('config') ? config('postgis.precision') : 6
19+
);
20+
}
21+
22+
public function setPrecision($precision)
23+
{
24+
$precision = filter_var($precision, FILTER_VALIDATE_INT);
25+
if (!is_int($precision)) {
26+
throw new \UnexpectedValueException('Precision must be an integer');
27+
}
28+
29+
$this->precision = $precision;
1630
}
1731

1832
public function getLat()
@@ -52,17 +66,18 @@ public function is3d()
5266

5367
public function toPair()
5468
{
55-
$pair = self::stringifyFloat($this->getLng()) . ' ' . self::stringifyFloat($this->getLat());
69+
$pair = $this->stringifyFloat($this->getLng()) . ' ' . $this->stringifyFloat($this->getLat());
5670
if ($this->is3d()) {
57-
$pair .= ' ' . self::stringifyFloat($this->getAlt());
71+
$pair .= ' ' . $this->stringifyFloat($this->getAlt());
5872
}
5973
return $pair;
6074
}
6175

62-
private static function stringifyFloat($float)
76+
private function stringifyFloat($float)
6377
{
6478
// normalized output among locales
65-
return rtrim(rtrim(sprintf('%F', $float), '0'), '.');
79+
80+
return rtrim(rtrim(sprintf("%.{$this->precision}F", $float), '0'), '.');
6681
}
6782

6883
public static function fromPair($pair)

tests/Geometries/PointTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,20 @@ public function testJsonSerialize3d()
126126
$this->assertInstanceOf(\GeoJson\Geometry\Point::class, $point->jsonSerialize());
127127
$this->assertSame('{"type":"Point","coordinates":[3.4,1.2,5.6]}', json_encode($point));
128128
}
129+
130+
public function testPointPrecisionDefault()
131+
{
132+
$point = new Point(-37.8745505,144.9102885,12.38);
133+
134+
$this->assertSame('144.910289 -37.87455 12.38', $point->toPair());
135+
136+
}
137+
138+
public function testPointPrecision10()
139+
{
140+
$point = new Point(-37.87455051578,144.91028850798,7.38257341563);
141+
$point->setPrecision(10);
142+
143+
$this->assertSame('144.910288508 -37.8745505158 7.3825734156', $point->toPair());
144+
}
129145
}

0 commit comments

Comments
 (0)