mirror of
https://github.com/netzmacht/contao-leaflet-maps.git
synced 2025-12-02 13:03:43 +01:00
Extract geo json creation to an event subscriber.
This commit is contained in:
@@ -12,10 +12,13 @@
|
||||
namespace Netzmacht\Contao\Leaflet\Mapper;
|
||||
|
||||
use Netzmacht\Contao\Leaflet\Event\BuildDefinitionEvent;
|
||||
use Netzmacht\Contao\Leaflet\Event\ConvertToGeoJsonEvent;
|
||||
use Netzmacht\Contao\Leaflet\Event\GetHashEvent;
|
||||
use Netzmacht\LeafletPHP\Definition;
|
||||
use Netzmacht\LeafletPHP\Definition\GeoJson\ConvertsToGeoJsonFeature;
|
||||
use Netzmacht\LeafletPHP\Definition\GeoJson\Feature;
|
||||
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
|
||||
use Netzmacht\LeafletPHP\Definition\GeoJson\GeoJsonFeature;
|
||||
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
|
||||
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.
|
||||
*
|
||||
|
||||
@@ -17,7 +17,6 @@ use Netzmacht\Contao\Leaflet\Model\MarkerModel;
|
||||
use Netzmacht\Contao\Leaflet\Frontend\RequestUrl;
|
||||
use Netzmacht\JavascriptBuilder\Type\Expression;
|
||||
use Netzmacht\LeafletPHP\Definition;
|
||||
use Netzmacht\LeafletPHP\Definition\GeoJson\Feature;
|
||||
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
|
||||
use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
|
||||
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
|
||||
@@ -59,7 +58,6 @@ class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
|
||||
$elementId = null
|
||||
) {
|
||||
if ($model->deferred) {
|
||||
|
||||
if ($model->pointToLayer || $model->affectBounds) {
|
||||
$layer = new GeoJson($this->getElementId($model, $elementId));
|
||||
|
||||
@@ -100,15 +98,10 @@ class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
|
||||
if ($collection) {
|
||||
foreach ($collection as $item) {
|
||||
$marker = $mapper->handle($item);
|
||||
$point = $mapper->convertToGeoJsonFeature($marker, $item);
|
||||
|
||||
if ($marker instanceof Marker) {
|
||||
$feature = $marker->toGeoJsonFeature();
|
||||
|
||||
if ($item->ignoreForBounds || !$model->affectBounds) {
|
||||
$feature->setProperty('ignoreForBounds', true);
|
||||
}
|
||||
|
||||
$definition->addData($feature, true);
|
||||
if ($point) {
|
||||
$definition->addData($point);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -130,14 +123,9 @@ class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
|
||||
if ($collection) {
|
||||
foreach ($collection as $item) {
|
||||
$marker = $mapper->handle($item);
|
||||
$point = $mapper->convertToGeoJsonFeature($marker, $item);
|
||||
|
||||
if ($marker instanceof Marker) {
|
||||
$point = $marker->toGeoJsonFeature();
|
||||
|
||||
if ($point instanceof Feature && ($item->ignoreForBounds || !$model->affectBounds)) {
|
||||
$point->setProperty('ignoreForBounds', true);
|
||||
}
|
||||
|
||||
if ($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\GeoJsonFeature;
|
||||
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\Vector;
|
||||
|
||||
@@ -109,15 +107,10 @@ class VectorsLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
|
||||
|
||||
if ($collection) {
|
||||
foreach ($collection as $item) {
|
||||
$vector = $mapper->handle($item);
|
||||
|
||||
if ($vector instanceof ConvertsToGeoJsonFeature) {
|
||||
$feature = $vector->toGeoJsonFeature();
|
||||
|
||||
if ($feature instanceof Feature && ($item->ignoreForBounds || !$model->affectBounds)) {
|
||||
$feature->setProperty('ignoreForBounds', true);
|
||||
}
|
||||
$vector = $mapper->handle($item);
|
||||
$feature = $mapper->convertToGeoJsonFeature($vector, $item);
|
||||
|
||||
if ($feature) {
|
||||
$definition->addData($feature, true);
|
||||
}
|
||||
}
|
||||
@@ -132,28 +125,21 @@ class VectorsLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
|
||||
*/
|
||||
public function handleGeoJson(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
|
||||
{
|
||||
$feature = new FeatureCollection();
|
||||
$definition = new FeatureCollection();
|
||||
$collection = $this->loadVectorModels($model);
|
||||
|
||||
if ($collection) {
|
||||
foreach ($collection as $item) {
|
||||
$vector = $mapper->handle($item);
|
||||
$vector = $mapper->handle($item);
|
||||
$feature = $mapper->convertToGeoJsonFeature($vector, $item);
|
||||
|
||||
if ($vector instanceof ConvertsToGeoJsonFeature) {
|
||||
$vector = $vector->toGeoJsonFeature();
|
||||
}
|
||||
|
||||
if ($vector instanceof GeoJsonFeature) {
|
||||
if ($vector instanceof Feature && ($item->ignoreForBounds || !$model->affectBounds)) {
|
||||
$vector->setProperty('ignoreForBounds', true);
|
||||
}
|
||||
|
||||
$feature->addFeature($vector);
|
||||
if ($feature) {
|
||||
$definition->addFeature($feature, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $feature;
|
||||
return $definition;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user