forked from Snck3rs/contao-leaflet-maps
Refactor overpass support using own implementation of overpass layer.
This commit is contained in:
190
src/Netzmacht/Contao/Leaflet/Definition/Layer/OverpassLayer.php
Normal file
190
src/Netzmacht/Contao/Leaflet/Definition/Layer/OverpassLayer.php
Normal file
@@ -0,0 +1,190 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package netzmacht
|
||||
* @author David Molineus <david.molineus@netzmacht.de>
|
||||
* @copyright 2016 netzmacht David Molineus. All rights reserved.
|
||||
* @filesource
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Netzmacht\Contao\Leaflet\Definition\Layer;
|
||||
|
||||
use Netzmacht\JavascriptBuilder\Encoder;
|
||||
use Netzmacht\JavascriptBuilder\Type\AnonymousFunction;
|
||||
use Netzmacht\JavascriptBuilder\Type\ConvertsToJavascript;
|
||||
use Netzmacht\JavascriptBuilder\Type\Expression;
|
||||
use Netzmacht\LeafletPHP\Definition\AbstractLayer;
|
||||
use Netzmacht\LeafletPHP\Definition\HasOptions;
|
||||
use Netzmacht\LeafletPHP\Encoder\EncodeHelperTrait;
|
||||
|
||||
/**
|
||||
* Class OverpassLayer provides implementation of https://github.com/kartenkarsten/leaflet-layer-overpass.
|
||||
*
|
||||
* @package Netzmacht\LeafletPHP\Plugins\OverpassLayer
|
||||
*/
|
||||
class OverpassLayer extends AbstractLayer implements HasOptions, ConvertsToJavascript
|
||||
{
|
||||
use EncodeHelperTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getType()
|
||||
{
|
||||
return 'OverpassLayer';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getRequiredLibraries()
|
||||
{
|
||||
$libs = parent::getRequiredLibraries();
|
||||
$libs[] = 'osmtogeojson';
|
||||
|
||||
return $libs;
|
||||
}
|
||||
|
||||
/**
|
||||
* OverpassLayer constructor.
|
||||
*
|
||||
* @param string $identifier Indicator of the layer.
|
||||
* @param array $options Options.
|
||||
*/
|
||||
public function __construct($identifier, array $options = [])
|
||||
{
|
||||
parent::__construct($identifier);
|
||||
|
||||
$this->setOptions($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the debug mode.
|
||||
*
|
||||
* @param bool $debug Debug mode.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setDebug($debug)
|
||||
{
|
||||
return $this->setOption('debug', (bool) $debug);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get debug mode.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getDebug()
|
||||
{
|
||||
return $this->getOption('debug', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the query.
|
||||
*
|
||||
* @param string $query Query.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setQuery($query)
|
||||
{
|
||||
return $this->setOption('query', $query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get query.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getQuery()
|
||||
{
|
||||
return $this->getOption('query', '(node(BBOX)[organic];node(BBOX)[second_hand];);out qt;');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the endpoint.
|
||||
*
|
||||
* @param string $endpoint Endpoint.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setEndpoint($endpoint)
|
||||
{
|
||||
return $this->setOption('endpoint', $endpoint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get endpoint.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getEndpoint()
|
||||
{
|
||||
return $this->getOption('endpoint', '//overpass-api.de/api/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set point to layer function.
|
||||
*
|
||||
* @param Expression|AnonymousFunction $function The function callback.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setPointToLayer($function)
|
||||
{
|
||||
return $this->setOption('pointToLayer', $function);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set on each feature function.
|
||||
*
|
||||
* @param Expression|AnonymousFunction $function The function callback.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setOnEachFeature($function)
|
||||
{
|
||||
return $this->setOption('onEachFeature', $function);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the minZoom.
|
||||
*
|
||||
* @param int $minZoom MinZoom.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setMinZoom($minZoom)
|
||||
{
|
||||
return $this->setOption('minZoom', (int) $minZoom);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get minZoom.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getMinZoom()
|
||||
{
|
||||
return $this->getOption('minZoom', 15);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function encode(Encoder $encoder, $flags = null)
|
||||
{
|
||||
$buffer = sprintf (
|
||||
'%s = new L.OverPassLayer(%s)%s',
|
||||
$encoder->encodeReference($this),
|
||||
$encoder->encodeArray($this->getOptions(), JSON_FORCE_OBJECT),
|
||||
$encoder->close($flags)
|
||||
);
|
||||
|
||||
$buffer .= $this->encodeMethodCalls($this->getMethodCalls(), $encoder, $flags);
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
}
|
||||
@@ -10,12 +10,12 @@
|
||||
|
||||
namespace Netzmacht\Contao\Leaflet\Mapper\Layer;
|
||||
|
||||
use Model;
|
||||
use Netzmacht\Contao\Leaflet\Definition\Layer\OverpassLayer;
|
||||
use Netzmacht\Contao\Leaflet\Filter\Filter;
|
||||
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
|
||||
use Netzmacht\Contao\Leaflet\Mapper\OptionsBuilder;
|
||||
use Netzmacht\JavascriptBuilder\Type\Expression;
|
||||
use Netzmacht\LeafletPHP\Definition;
|
||||
use Netzmacht\LeafletPHP\Plugins\OverpassLayer\OverpassLayer;
|
||||
|
||||
/**
|
||||
* Class OverpassLayerMapper
|
||||
@@ -36,7 +36,7 @@ class OverpassLayerMapper extends AbstractLayerMapper
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $definitionClass = 'Netzmacht\LeafletPHP\Plugins\OverpassLayer\OverpassLayer';
|
||||
protected static $definitionClass = 'Netzmacht\Contao\Leaflet\Definition\Layer\OverpassLayer';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
@@ -47,8 +47,8 @@ class OverpassLayerMapper extends AbstractLayerMapper
|
||||
|
||||
$this->optionsBuilder
|
||||
->addOption('query', 'overpassQuery')
|
||||
->addOption('minzoom', 'minZoom')
|
||||
->addOption('debug')
|
||||
->addOption('minZoom')
|
||||
->addOption('boundsMode')
|
||||
->addOption('overpassEndpoint', 'endpoint');
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ class OverpassLayerMapper extends AbstractLayerMapper
|
||||
*/
|
||||
protected function build(
|
||||
Definition $definition,
|
||||
\Model $model,
|
||||
Model $model,
|
||||
DefinitionMapper $mapper,
|
||||
Filter $filter = null,
|
||||
Definition $parent = null
|
||||
@@ -66,17 +66,12 @@ class OverpassLayerMapper extends AbstractLayerMapper
|
||||
return;
|
||||
}
|
||||
|
||||
$minZoomIndicatorOptions = $definition->getMinZoomIndicatorOptions();
|
||||
$minZoomIndicatorOptionsBuilder = new OptionsBuilder();
|
||||
$minZoomIndicatorOptionsBuilder
|
||||
->addOption('position', 'minZoomIndicatorPosition')
|
||||
->addOption('minZoomMessageNoLayer', 'minZoomIndicatorMessageNoLayer')
|
||||
->addOption('minZoomMessage', 'minZoomIndicatorMessage');
|
||||
if ($model->pointToLayer) {
|
||||
$definition->setPointToLayer(new Expression($model->pointToLayer));
|
||||
}
|
||||
|
||||
$minZoomIndicatorOptionsBuilder->build($minZoomIndicatorOptions, $model);
|
||||
|
||||
if ($model->overpassCallback) {
|
||||
$definition->setCallback(new Expression($model->overpassCallback));
|
||||
if ($model->onEachFeature) {
|
||||
$definition->setOnEachFeature(new Expression($model->onEachFeature));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user