mirror of
https://github.com/netzmacht/contao-leaflet-maps.git
synced 2025-12-01 04:24:02 +01:00
Extract geo json creation to an event subscriber.
This commit is contained in:
@@ -11,4 +11,5 @@
|
|||||||
return array(
|
return array(
|
||||||
'Netzmacht\Contao\Leaflet\Subscriber\BootSubscriber',
|
'Netzmacht\Contao\Leaflet\Subscriber\BootSubscriber',
|
||||||
'Netzmacht\Contao\Leaflet\Subscriber\HashSubscriber',
|
'Netzmacht\Contao\Leaflet\Subscriber\HashSubscriber',
|
||||||
|
'Netzmacht\Contao\Leaflet\Subscriber\GeoJsonSubscriber',
|
||||||
);
|
);
|
||||||
|
|||||||
91
src/Netzmacht/Contao/Leaflet/Event/ConvertToGeoJsonEvent.php
Normal file
91
src/Netzmacht/Contao/Leaflet/Event/ConvertToGeoJsonEvent.php
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @package dev
|
||||||
|
* @author David Molineus <david.molineus@netzmacht.de>
|
||||||
|
* @copyright 2015 netzmacht creative David Molineus
|
||||||
|
* @license LGPL 3.0
|
||||||
|
* @filesource
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Netzmacht\Contao\Leaflet\Event;
|
||||||
|
|
||||||
|
use Netzmacht\LeafletPHP\Definition;
|
||||||
|
use Netzmacht\LeafletPHP\Definition\GeoJson\GeoJsonObject;
|
||||||
|
use Symfony\Component\EventDispatcher\Event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ConvertToGeoJsonEvent is emitted when the DefinitionMapper converts a definition to a geo json feature.
|
||||||
|
*
|
||||||
|
* @package Netzmacht\Contao\Leaflet\Event
|
||||||
|
*/
|
||||||
|
class ConvertToGeoJsonEvent extends Event
|
||||||
|
{
|
||||||
|
const NAME = 'leaflet.mapper.convert-to-geojson';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The definition.
|
||||||
|
*
|
||||||
|
* @var Definition
|
||||||
|
*/
|
||||||
|
private $definition;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The GeoJSON object.
|
||||||
|
*
|
||||||
|
* @var GeoJsonObject
|
||||||
|
*/
|
||||||
|
private $geoJson;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The definition model.
|
||||||
|
*
|
||||||
|
* @var mixed
|
||||||
|
*/
|
||||||
|
private $model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct.
|
||||||
|
*
|
||||||
|
* @param Definition $definition The definition.
|
||||||
|
* @param GeoJsonObject $geoJson The GeoJSON object.
|
||||||
|
* @param mixed $model The corresponding model. Usually a \Model but could be everything.
|
||||||
|
*/
|
||||||
|
public function __construct(Definition $definition, GeoJsonObject $geoJson, $model)
|
||||||
|
{
|
||||||
|
$this->definition = $definition;
|
||||||
|
$this->geoJson = $geoJson;
|
||||||
|
$this->model = $model;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the definition.
|
||||||
|
*
|
||||||
|
* @return Definition
|
||||||
|
*/
|
||||||
|
public function getDefinition()
|
||||||
|
{
|
||||||
|
return $this->definition;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the geoJson representation.
|
||||||
|
*
|
||||||
|
* @return GeoJsonObject
|
||||||
|
*/
|
||||||
|
public function getGeoJson()
|
||||||
|
{
|
||||||
|
return $this->geoJson;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the model.
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getModel()
|
||||||
|
{
|
||||||
|
return $this->model;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,10 +12,13 @@
|
|||||||
namespace Netzmacht\Contao\Leaflet\Mapper;
|
namespace Netzmacht\Contao\Leaflet\Mapper;
|
||||||
|
|
||||||
use Netzmacht\Contao\Leaflet\Event\BuildDefinitionEvent;
|
use Netzmacht\Contao\Leaflet\Event\BuildDefinitionEvent;
|
||||||
|
use Netzmacht\Contao\Leaflet\Event\ConvertToGeoJsonEvent;
|
||||||
use Netzmacht\Contao\Leaflet\Event\GetHashEvent;
|
use Netzmacht\Contao\Leaflet\Event\GetHashEvent;
|
||||||
use Netzmacht\LeafletPHP\Definition;
|
use Netzmacht\LeafletPHP\Definition;
|
||||||
|
use Netzmacht\LeafletPHP\Definition\GeoJson\ConvertsToGeoJsonFeature;
|
||||||
use Netzmacht\LeafletPHP\Definition\GeoJson\Feature;
|
use Netzmacht\LeafletPHP\Definition\GeoJson\Feature;
|
||||||
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
|
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
|
||||||
|
use Netzmacht\LeafletPHP\Definition\GeoJson\GeoJsonFeature;
|
||||||
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
|
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
|
||||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface as EventDispatcher;
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface as EventDispatcher;
|
||||||
|
|
||||||
@@ -132,6 +135,30 @@ class DefinitionMapper
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a definition to a geo json feature.
|
||||||
|
*
|
||||||
|
* @param Definition $definition The leaflet definition object.
|
||||||
|
* @param mixed $model The corresponding definition model.
|
||||||
|
*
|
||||||
|
* @return GeoJsonFeature
|
||||||
|
*/
|
||||||
|
public function convertToGeoJsonFeature(Definition $definition, $model)
|
||||||
|
{
|
||||||
|
if ($definition instanceof GeoJsonFeature) {
|
||||||
|
$feature = $definition;
|
||||||
|
} elseif ($definition instanceof ConvertsToGeoJsonFeature) {
|
||||||
|
$feature = $definition->toGeoJsonFeature();
|
||||||
|
} else {
|
||||||
|
throw new \RuntimeException('Unsupported definition');
|
||||||
|
}
|
||||||
|
|
||||||
|
$event = new ConvertToGeoJsonEvent($definition, $feature, $model);
|
||||||
|
$this->eventDispatcher->dispatch($event::NAME, $event);
|
||||||
|
|
||||||
|
return $feature;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the hash of a model.
|
* Get the hash of a model.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ use Netzmacht\Contao\Leaflet\Model\MarkerModel;
|
|||||||
use Netzmacht\Contao\Leaflet\Frontend\RequestUrl;
|
use Netzmacht\Contao\Leaflet\Frontend\RequestUrl;
|
||||||
use Netzmacht\JavascriptBuilder\Type\Expression;
|
use Netzmacht\JavascriptBuilder\Type\Expression;
|
||||||
use Netzmacht\LeafletPHP\Definition;
|
use Netzmacht\LeafletPHP\Definition;
|
||||||
use Netzmacht\LeafletPHP\Definition\GeoJson\Feature;
|
|
||||||
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
|
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
|
||||||
use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
|
use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
|
||||||
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
|
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
|
||||||
@@ -59,7 +58,6 @@ class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
|
|||||||
$elementId = null
|
$elementId = null
|
||||||
) {
|
) {
|
||||||
if ($model->deferred) {
|
if ($model->deferred) {
|
||||||
|
|
||||||
if ($model->pointToLayer || $model->affectBounds) {
|
if ($model->pointToLayer || $model->affectBounds) {
|
||||||
$layer = new GeoJson($this->getElementId($model, $elementId));
|
$layer = new GeoJson($this->getElementId($model, $elementId));
|
||||||
|
|
||||||
@@ -100,15 +98,10 @@ class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
|
|||||||
if ($collection) {
|
if ($collection) {
|
||||||
foreach ($collection as $item) {
|
foreach ($collection as $item) {
|
||||||
$marker = $mapper->handle($item);
|
$marker = $mapper->handle($item);
|
||||||
|
$point = $mapper->convertToGeoJsonFeature($marker, $item);
|
||||||
|
|
||||||
if ($marker instanceof Marker) {
|
if ($point) {
|
||||||
$feature = $marker->toGeoJsonFeature();
|
$definition->addData($point);
|
||||||
|
|
||||||
if ($item->ignoreForBounds || !$model->affectBounds) {
|
|
||||||
$feature->setProperty('ignoreForBounds', true);
|
|
||||||
}
|
|
||||||
|
|
||||||
$definition->addData($feature, true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -130,14 +123,9 @@ class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
|
|||||||
if ($collection) {
|
if ($collection) {
|
||||||
foreach ($collection as $item) {
|
foreach ($collection as $item) {
|
||||||
$marker = $mapper->handle($item);
|
$marker = $mapper->handle($item);
|
||||||
|
$point = $mapper->convertToGeoJsonFeature($marker, $item);
|
||||||
|
|
||||||
if ($marker instanceof Marker) {
|
if ($point) {
|
||||||
$point = $marker->toGeoJsonFeature();
|
|
||||||
|
|
||||||
if ($point instanceof Feature && ($item->ignoreForBounds || !$model->affectBounds)) {
|
|
||||||
$point->setProperty('ignoreForBounds', true);
|
|
||||||
}
|
|
||||||
|
|
||||||
$feature->addFeature($point);
|
$feature->addFeature($point);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,8 +22,6 @@ use Netzmacht\LeafletPHP\Definition\GeoJson\Feature;
|
|||||||
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
|
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
|
||||||
use Netzmacht\LeafletPHP\Definition\GeoJson\GeoJsonFeature;
|
use Netzmacht\LeafletPHP\Definition\GeoJson\GeoJsonFeature;
|
||||||
use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
|
use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
|
||||||
use Netzmacht\LeafletPHP\Definition\Group\LayerGroup;
|
|
||||||
use Netzmacht\LeafletPHP\Definition\Layer;
|
|
||||||
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
|
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
|
||||||
use Netzmacht\LeafletPHP\Definition\Vector;
|
use Netzmacht\LeafletPHP\Definition\Vector;
|
||||||
|
|
||||||
@@ -109,15 +107,10 @@ class VectorsLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
|
|||||||
|
|
||||||
if ($collection) {
|
if ($collection) {
|
||||||
foreach ($collection as $item) {
|
foreach ($collection as $item) {
|
||||||
$vector = $mapper->handle($item);
|
$vector = $mapper->handle($item);
|
||||||
|
$feature = $mapper->convertToGeoJsonFeature($vector, $item);
|
||||||
if ($vector instanceof ConvertsToGeoJsonFeature) {
|
|
||||||
$feature = $vector->toGeoJsonFeature();
|
|
||||||
|
|
||||||
if ($feature instanceof Feature && ($item->ignoreForBounds || !$model->affectBounds)) {
|
|
||||||
$feature->setProperty('ignoreForBounds', true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if ($feature) {
|
||||||
$definition->addData($feature, true);
|
$definition->addData($feature, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -132,28 +125,21 @@ class VectorsLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
|
|||||||
*/
|
*/
|
||||||
public function handleGeoJson(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
|
public function handleGeoJson(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
|
||||||
{
|
{
|
||||||
$feature = new FeatureCollection();
|
$definition = new FeatureCollection();
|
||||||
$collection = $this->loadVectorModels($model);
|
$collection = $this->loadVectorModels($model);
|
||||||
|
|
||||||
if ($collection) {
|
if ($collection) {
|
||||||
foreach ($collection as $item) {
|
foreach ($collection as $item) {
|
||||||
$vector = $mapper->handle($item);
|
$vector = $mapper->handle($item);
|
||||||
|
$feature = $mapper->convertToGeoJsonFeature($vector, $item);
|
||||||
|
|
||||||
if ($vector instanceof ConvertsToGeoJsonFeature) {
|
if ($feature) {
|
||||||
$vector = $vector->toGeoJsonFeature();
|
$definition->addFeature($feature, true);
|
||||||
}
|
|
||||||
|
|
||||||
if ($vector instanceof GeoJsonFeature) {
|
|
||||||
if ($vector instanceof Feature && ($item->ignoreForBounds || !$model->affectBounds)) {
|
|
||||||
$vector->setProperty('ignoreForBounds', true);
|
|
||||||
}
|
|
||||||
|
|
||||||
$feature->addFeature($vector);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $feature;
|
return $definition;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
133
src/Netzmacht/Contao/Leaflet/Subscriber/GeoJsonSubscriber.php
Normal file
133
src/Netzmacht/Contao/Leaflet/Subscriber/GeoJsonSubscriber.php
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @package dev
|
||||||
|
* @author David Molineus <david.molineus@netzmacht.de>
|
||||||
|
* @copyright 2015 netzmacht creative David Molineus
|
||||||
|
* @license LGPL 3.0
|
||||||
|
* @filesource
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Netzmacht\Contao\Leaflet\Subscriber;
|
||||||
|
|
||||||
|
use Netzmacht\Contao\Leaflet\Dca\Vector;
|
||||||
|
use Netzmacht\Contao\Leaflet\Event\ConvertToGeoJsonEvent;
|
||||||
|
use Netzmacht\Contao\Leaflet\Model\LayerModel;
|
||||||
|
use Netzmacht\Contao\Leaflet\Model\MarkerModel;
|
||||||
|
use Netzmacht\Contao\Leaflet\Model\VectorModel;
|
||||||
|
use Netzmacht\LeafletPHP\Definition\GeoJson\Feature;
|
||||||
|
use Netzmacht\LeafletPHP\Definition\HasPopup;
|
||||||
|
use Netzmacht\LeafletPHP\Definition\UI\Marker;
|
||||||
|
use Netzmacht\LeafletPHP\Definition\Vector\Circle;
|
||||||
|
use Netzmacht\LeafletPHP\Definition\Vector\CircleMarker;
|
||||||
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
|
|
||||||
|
class GeoJsonSubscriber implements EventSubscriberInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public static function getSubscribedEvents()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
ConvertToGeoJsonEvent::NAME => array(
|
||||||
|
array('addPopup'),
|
||||||
|
array('enrichMarker'),
|
||||||
|
array('enrichVector'),
|
||||||
|
array('enrichCircle')
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add popup property for definitions with an popup.
|
||||||
|
*
|
||||||
|
* @param ConvertToGeoJsonEvent $event The subscribed event.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function addPopup(ConvertToGeoJsonEvent $event)
|
||||||
|
{
|
||||||
|
$feature = $event->getGeoJson();
|
||||||
|
$definition = $event->getDefinition();
|
||||||
|
|
||||||
|
if ($definition instanceof HasPopup && $feature instanceof Feature) {
|
||||||
|
if ($definition->getPopup()) {
|
||||||
|
$feature->setProperty('popup', $definition->getPopup());
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($definition->getPopupContent()) {
|
||||||
|
$feature->setProperty('popupContent', $definition->getPopupContent());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ConvertToGeoJsonEvent $event
|
||||||
|
*/
|
||||||
|
public function enrichMarker(ConvertToGeoJsonEvent $event)
|
||||||
|
{
|
||||||
|
$feature = $event->getGeoJson();
|
||||||
|
$definition = $event->getDefinition();
|
||||||
|
$model = $event->getModel();
|
||||||
|
|
||||||
|
if ($definition instanceof Marker && $model instanceof MarkerModel && $feature instanceof Feature) {
|
||||||
|
if ($model->featureData) {
|
||||||
|
$feature->setProperty('data', json_decode($model->featureData, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($model->ignoreForBounds) {
|
||||||
|
$feature->setProperty('ignoreForBounds', true);
|
||||||
|
} else {
|
||||||
|
$parent = LayerModel::findByPk($model->pid);
|
||||||
|
|
||||||
|
if ($parent && !$parent->affectBounds) {
|
||||||
|
$feature->setProperty('ignoreForBounds', true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ConvertToGeoJsonEvent $event
|
||||||
|
*/
|
||||||
|
public function enrichVector(ConvertToGeoJsonEvent $event)
|
||||||
|
{
|
||||||
|
$feature = $event->getGeoJson();
|
||||||
|
$definition = $event->getDefinition();
|
||||||
|
$model = $event->getModel();
|
||||||
|
|
||||||
|
if ($definition instanceof Vector && $model instanceof VectorModel && $feature instanceof Feature) {
|
||||||
|
if ($model->featureData) {
|
||||||
|
$feature->setProperty('data', json_decode($model->featureData, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($model->ignoreForBounds) {
|
||||||
|
$feature->setProperty('ignoreForBounds', true);
|
||||||
|
} else {
|
||||||
|
$parent = LayerModel::findByPk($model->pid);
|
||||||
|
|
||||||
|
if ($parent && !$parent->affectBounds) {
|
||||||
|
$feature->setProperty('ignoreForBounds', true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enrich the the circle with constructor arguments.
|
||||||
|
*
|
||||||
|
* @param ConvertToGeoJsonEvent $event The subscribed events.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function enrichCircle(ConvertToGeoJsonEvent $event)
|
||||||
|
{
|
||||||
|
$feature = $event->getGeoJson();
|
||||||
|
$definition = $event->getDefinition();
|
||||||
|
|
||||||
|
if ($definition instanceof Circle && !$definition instanceof CircleMarker && $feature instanceof Feature) {
|
||||||
|
$feature->setProperty('arguments', array($definition->getLatLng(), $definition->getRadius()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user