Skip to content

Commit

Permalink
Merge pull request #503 from JeroenDeDauw/kml
Browse files Browse the repository at this point in the history
 Simplify KML formtter
  • Loading branch information
JeroenDeDauw authored Nov 20, 2018
2 parents 34239d0 + afd8e17 commit d4d96aa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 78 deletions.
99 changes: 27 additions & 72 deletions src/Presentation/KmlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,18 @@
namespace Maps\Presentation;

use Maps\Elements\Location;
use Xml;

/**
* Class to format geographical data to KML.
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < [email protected] >
*/
class KmlFormatter {

/**
* @var array
*/
private $params;

/**
* @var Location[]
*/
private $placemarks;

public function __construct( array $params = [] ) {
$this->params = $params;
$this->clearElements();
}

/**
* Clears all set geographical objects.
*/
public function clearElements() {
$this->clearPlacemarks();
}

/**
* Clears all set placemarks.
*/
public function clearPlacemarks() {
$this->placemarks = [];
}

/**
* Builds and returns KML representing the set geographical objects.
*/
public function getKML(): string {
$elements = $this->getKMLElements();
public function formatLocationsAsKml( Location ...$locations ): string {
$elements = $this->getKmlForLocations( $locations );

// http://earth.google.com/kml/2.2
return <<<EOT
Expand All @@ -59,44 +27,29 @@ public function getKML(): string {
EOT;
}

/**
* Returns the KML for all set geographical objects.
*/
private function getKMLElements(): string {
$elements = [];

$elements = array_merge( $elements, $this->getPlaceMarks() );

return implode( "\n", $elements );
private function getKmlForLocations( array $locations ): string {
return implode(
"\n",
array_map(
function( Location $location ) {
return $this->locationToKmlPlacemark( $location );
},
$locations
)
);
}

/**
* Returns KML for all set placemarks in a list, where each element is
* a KML node representing a placemark.
*/
private function getPlaceMarks(): array {
$placeMarks = [];

foreach ( $this->placemarks as $location ) {
$placeMarks[] = $this->getKMLForLocation( $location );
}

return $placeMarks;
}

private function getKMLForLocation( Location $location ): string {
private function locationToKmlPlacemark( Location $location ): string {
// TODO: escaping?
$name = '<name><![CDATA[ ' . $location->getTitle() . ']]></name>';

// TODO: escaping?
$description = '<description><![CDATA[ ' . $location->getText() . ']]></description>';

$coordinates = $location->getCoordinates();

// lon,lat[,alt]
$coordinates = Xml::element(
'coordinates',
[],
$coordinates->getLongitude() . ',' . $coordinates->getLatitude() . ',0'
);
$coordinates = '<coordinates>'
. $this->escapeValue( $this->getCoordinateString( $location ) )
. '</coordinates>';

return <<<EOT
<Placemark>
Expand All @@ -110,13 +63,15 @@ private function getKMLForLocation( Location $location ): string {
EOT;
}

/**
* @param Location[] $placemarks
*/
public function addPlacemarks( array $placemarks ) {
foreach ( $placemarks as $placemark ) {
$this->placemarks[] = $placemark;
}
private function getCoordinateString( Location $location ): string {
// lon,lat[,alt]
return $location->getCoordinates()->getLongitude()
. ',' . $location->getCoordinates()->getLatitude()
. ',0';
}

private function escapeValue( string $value ): string {
return htmlspecialchars( $value, ENT_NOQUOTES );
}

}
8 changes: 2 additions & 6 deletions src/SemanticMW/ResultPrinters/KmlPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,8 @@ private function getKML( SMWQueryResult $res, $outputmode ) {
$queryHandler->setSubjectSeparator( '' );
$queryHandler->setPageLinkText( $this->params['pagelinktext'] );

$formatter = new KmlFormatter( $this->params );

$shapes = $queryHandler->getShapes();
$formatter->addPlacemarks( $shapes['locations'] );

return $formatter->getKML();
$formatter = new KmlFormatter();
return $formatter->formatLocationsAsKml( $queryHandler->getShapes()['locations'] );
}

/**
Expand Down

0 comments on commit d4d96aa

Please sign in to comment.