Implement a file layer for gpx,kml,wkt support.

This commit is contained in:
David Molineus
2017-10-17 16:50:29 +02:00
parent 31a5cc40a5
commit d1f26056e9
9 changed files with 170 additions and 1 deletions

View File

@@ -26,7 +26,7 @@
"contao/core-bundle": "~4.4",
"netzmacht/contao-leaflet-libraries": "^1.0",
"netzmacht/php-javascript-builder": "^1.0",
"netzmacht/php-leaflet": "^1.0.1",
"netzmacht/php-leaflet": "^1.0.2",
"netzmacht/contao-toolkit": "^3.0@dev",
"contao-community-alliance/meta-palettes": "^1.5",
"menatwork/contao-multicolumnwizard": "^3.2",

View File

@@ -45,6 +45,10 @@ parameters:
extend: true
fit: true
file:
children: false
icon: 'bundles/netzmachtcontaoleaflet/img/file.png'
services:
_defaults:
public: false

View File

@@ -58,6 +58,10 @@ services:
tags:
- { name: netzmacht.contao_leflet_maps.mapper }
netzmacht.contao_leaflet.mapper.file_layer:
class: Netzmacht\Contao\Leaflet\Mapper\Layer\FileLayerMapper
tags:
- { name: netzmacht.contao_leaflet.mapper }
# Control mappers
netzmacht.contao_leaflet.mapper.zoom_control:

View File

@@ -217,6 +217,14 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = [
'pointToLayer',
],
],
'file extends default' => [
'+config' => ['file'],
'+expert' => [
'onEachFeature',
'pointToLayer',
],
],
],
'metasubselectpalettes' => [
@@ -853,5 +861,18 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = [
],
'sql' => 'mediumtext NULL',
],
'file' => [
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['file'],
'exclude' => true,
'inputType' => 'fileTree',
'eval' => [
'filesOnly' => true,
'fieldType' => 'radio',
'mandatory' => true,
'extensions' => 'gpx,kml,wkt',
'tl_class' => 'clr',
],
'sql' => 'binary(16) NULL',
],
],
];

View File

@@ -39,6 +39,8 @@ $GLOBALS['TL_LANG']['leaflet_layer']['tile'][0] = 'Tile layer';
$GLOBALS['TL_LANG']['leaflet_layer']['tile'][1] = 'Tile layer with full config options.';
$GLOBALS['TL_LANG']['leaflet_layer']['overpass'][0] = 'Overpass API';
$GLOBALS['TL_LANG']['leaflet_layer']['overpass'][1] = 'Overpass API data layer.';
$GLOBALS['TL_LANG']['leaflet_layer']['file'][0] = 'File';
$GLOBALS['TL_LANG']['leaflet_layer']['file'][1] = 'Geo data from a file (gpx,kml,wkt).';
$GLOBALS['TL_LANG']['leaflet_vector']['polyline'][0] = 'Polyline';
$GLOBALS['TL_LANG']['leaflet_vector']['polyline'][1] = 'Polyline overlay. For more details read the <a href="http://leafletjs.com/reference.html#polyline" target="_blank">Leaflet documentation</a>.';

View File

@@ -145,6 +145,8 @@ $GLOBALS['TL_LANG']['tl_leaflet_layer']['amenity'][0] = 'Amen
$GLOBALS['TL_LANG']['tl_leaflet_layer']['amenity'][1] = 'OSM amenity.';
$GLOBALS['TL_LANG']['tl_leaflet_layer']['amenityIcon'][1] = 'Icon';
$GLOBALS['TL_LANG']['tl_leaflet_layer']['amenityIcon'][0] = 'Icon style';
$GLOBALS['TL_LANG']['tl_leaflet_layer']['file'][0] = 'File';
$GLOBALS['TL_LANG']['tl_leaflet_layer']['file'][1] = 'Choose a file containing geodata. Supported formats are gpx,kml and wkt.';
$GLOBALS['TL_LANG']['tl_leaflet_layer']['groupTypes']['layer'][0] = 'Layer group';
$GLOBALS['TL_LANG']['tl_leaflet_layer']['groupTypes']['layer'][1] = 'Basic layer group. <br> See <a href="http://leafletjs.com/reference.html#layergroup" target="_blank">http://leafletjs.com/reference.html#layergroup</a>';

Binary file not shown.

After

Width:  |  Height:  |  Size: 619 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 546 B

View File

@@ -0,0 +1,136 @@
<?php
/**
* Leaflet maps for Contao CMS.
*
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2017 netzmacht David Molineus. All rights reserved.
* @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE
* @filesource
*/
declare(strict_types=1);
namespace Netzmacht\Contao\Leaflet\Mapper\Layer;
use Contao\FilesModel;
use Contao\Model;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Request\Request;
use Netzmacht\JavascriptBuilder\Type\Expression;
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\Group\FeatureGroup;
use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
use Netzmacht\LeafletPHP\Plugins\Omnivore\Gpx;
use Netzmacht\LeafletPHP\Plugins\Omnivore\Kml;
use Netzmacht\LeafletPHP\Plugins\Omnivore\OmnivoreLayer;
use Netzmacht\LeafletPHP\Plugins\Omnivore\Wkt;
/**
* Class FileLayerMapper.
*
* @package Netzmacht\Contao\Leaflet\Mapper\Layer
*/
class FileLayerMapper extends AbstractLayerMapper
{
/**
* The definition type.
*
* @var string
*/
protected static $type = 'file';
/**
* Class of the model being build.
*
* @var string
*/
protected static $definitionClass = FeatureGroup::class;
/**
* {@inheritDoc}
*/
public function handle(
$model,
DefinitionMapper $mapper,
Request $request = null,
$elementId = null,
Definition $parent = null
) {
$fileModel = FilesModel::findByPk($model->file);
$definition = $this->createInstance($model, $mapper, $request, $elementId, $fileModel);
$this->optionsBuilder->build($definition, $model);
$this->build($definition, $model, $mapper, $request, $parent);
return $definition;
}
/**
* {@inheritDoc}
*/
protected function createInstance(
Model $model,
DefinitionMapper $mapper,
Request $request = null,
$elementId = null,
FilesModel $fileModel = null
) {
$layerId = $this->getElementId($model, $elementId);
if ($fileModel instanceof FilesModel && $fileModel->type === 'file') {
switch ($fileModel->extension) {
case 'gpx':
$layer = new Gpx($layerId, $fileModel->path, []);
break;
case 'kml':
$layer = new Kml($layerId, $fileModel->path, []);
break;
case 'wkt':
$layer = new Wkt($layerId, $fileModel->path, []);
break;
default:
return parent::createInstance($model, $mapper, $request, $elementId);
}
$customId = $layerId . '_data';
$customLayer = new GeoJson($customId);
$layer->setCustomLayer($customLayer);
return $layer;
}
return parent::createInstance($model, $mapper, $request, $elementId);
}
/**
* {@inheritDoc}
*/
protected function build(
Definition $definition,
Model $model,
DefinitionMapper $mapper,
Request $request = null,
Definition $parent = null
) {
if (!$definition instanceof OmnivoreLayer) {
return;
}
$customLayer = $definition->getCustomLayer();
if ($customLayer instanceof GeoJson) {
if ($model->pointToLayer) {
$customLayer->setPointToLayer(new Expression($model->pointToLayer));
}
if ($model->onEachFeature) {
$customLayer->setOnEachFeature(new Expression($model->onEachFeature));
}
}
}
}