Skip to content
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
4 changes: 2 additions & 2 deletions src/Traits/ParsesCoordinates.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected function parsePointCoordinates(string $coordinates): array
return [
'longitude' => (float) $parts[0],
'latitude' => (float) ($parts[1] ?? 0),
'altitude' => isset($parts[2]) ? (float) $parts[2] : 0,
'altitude' => isset($parts[2]) ? (float) $parts[2] : 0.0,
];
}

Expand All @@ -35,7 +35,7 @@ protected function parseLineStringCoordinates(string $coordinates): array
$coords[] = [
'longitude' => (float) $parts[0],
'latitude' => (float) $parts[1],
'altitude' => isset($parts[2]) ? (float) $parts[2] : 0,
'altitude' => isset($parts[2]) ? (float) $parts[2] : 0.0,
];
}
}
Expand Down
47 changes: 47 additions & 0 deletions tests/AltitudeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

use PlinCode\KmlParser\KmlParser;

function kmlWithCoordinates(string $coordinates): string
{
return <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<Point>
<coordinates>{$coordinates}</coordinates>
</Point>
</Placemark>
<Placemark>
<LineString>
<coordinates>{$coordinates} 7.9,45.9</coordinates>
</LineString>
</Placemark>
</Document>
</kml>
XML;
}

it('returns a float altitude when the coordinate omits it', function () {
$placemarks = (new KmlParser)->loadFromString(kmlWithCoordinates('7.7,45.8'))->getPlacemarks();

expect($placemarks[0]['coordinates']['altitude'])->toBeFloat()
->and($placemarks[1]['coordinates'][0]['altitude'])->toBeFloat()
->and($placemarks[1]['coordinates'][1]['altitude'])->toBeFloat();
});

it('returns a float altitude when the coordinate declares it', function () {
$placemark = (new KmlParser)->loadFromString(kmlWithCoordinates('7.7,45.8,12'))->getPlacemarks()[0];

expect($placemark['coordinates']['altitude'])->toBeFloat()
->and($placemark['coordinates']['altitude'])->toBe(12.0);
});

it('keeps the altitude a float through to GeoJSON', function () {
$geometry = (new KmlParser)->loadFromString(kmlWithCoordinates('7.7,45.8'))
->toGeoJson()['features'][0]['geometry'];

expect($geometry['coordinates'][2])->toBeFloat()
->and($geometry['coordinates'])->toBe([7.7, 45.8, 0.0]);
});