Skip to content

Commit 6188949

Browse files
fix(parser): always return altitude as a float
A coordinate declaring an altitude produced a float, one omitting it produced the integer 0, so the type of the same key depended on the input. The array shape documented on parsePointCoordinates() claims float in both cases, and PHPStan believed it. Callers comparing strictly, or encoding to JSON and diffing the result, saw 0 where they had been told to expect 0.0.
1 parent 6add266 commit 6188949

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

src/Traits/ParsesCoordinates.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ protected function parsePointCoordinates(string $coordinates): array
1616
return [
1717
'longitude' => (float) $parts[0],
1818
'latitude' => (float) ($parts[1] ?? 0),
19-
'altitude' => isset($parts[2]) ? (float) $parts[2] : 0,
19+
'altitude' => isset($parts[2]) ? (float) $parts[2] : 0.0,
2020
];
2121
}
2222

@@ -35,7 +35,7 @@ protected function parseLineStringCoordinates(string $coordinates): array
3535
$coords[] = [
3636
'longitude' => (float) $parts[0],
3737
'latitude' => (float) $parts[1],
38-
'altitude' => isset($parts[2]) ? (float) $parts[2] : 0,
38+
'altitude' => isset($parts[2]) ? (float) $parts[2] : 0.0,
3939
];
4040
}
4141
}

tests/AltitudeTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
use PlinCode\KmlParser\KmlParser;
4+
5+
function kmlWithCoordinates(string $coordinates): string
6+
{
7+
return <<<XML
8+
<?xml version="1.0" encoding="UTF-8"?>
9+
<kml xmlns="http://www.opengis.net/kml/2.2">
10+
<Document>
11+
<Placemark>
12+
<Point>
13+
<coordinates>{$coordinates}</coordinates>
14+
</Point>
15+
</Placemark>
16+
<Placemark>
17+
<LineString>
18+
<coordinates>{$coordinates} 7.9,45.9</coordinates>
19+
</LineString>
20+
</Placemark>
21+
</Document>
22+
</kml>
23+
XML;
24+
}
25+
26+
it('returns a float altitude when the coordinate omits it', function () {
27+
$placemarks = (new KmlParser)->loadFromString(kmlWithCoordinates('7.7,45.8'))->getPlacemarks();
28+
29+
expect($placemarks[0]['coordinates']['altitude'])->toBeFloat()
30+
->and($placemarks[1]['coordinates'][0]['altitude'])->toBeFloat()
31+
->and($placemarks[1]['coordinates'][1]['altitude'])->toBeFloat();
32+
});
33+
34+
it('returns a float altitude when the coordinate declares it', function () {
35+
$placemark = (new KmlParser)->loadFromString(kmlWithCoordinates('7.7,45.8,12'))->getPlacemarks()[0];
36+
37+
expect($placemark['coordinates']['altitude'])->toBeFloat()
38+
->and($placemark['coordinates']['altitude'])->toBe(12.0);
39+
});
40+
41+
it('keeps the altitude a float through to GeoJSON', function () {
42+
$geometry = (new KmlParser)->loadFromString(kmlWithCoordinates('7.7,45.8'))
43+
->toGeoJson()['features'][0]['geometry'];
44+
45+
expect($geometry['coordinates'][2])->toBeFloat()
46+
->and($geometry['coordinates'])->toBe([7.7, 45.8, 0.0]);
47+
});

0 commit comments

Comments
 (0)