a2g_maps/Classes/Utility/MapConfigUtility.php

387 lines
11 KiB
PHP
Raw Permalink Normal View History

2023-12-04 16:36:56 +01:00
<?php
declare(strict_types=1);
namespace A2G\A2gMaps\Utility;
use TYPO3\CMS\Extbase\Annotation as Extbase;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use A2G\A2gMaps\Domain\Model\MapEntry;
use GuzzleHttp\Client;
/**
* Description of Canonical
*
* @author Raphael Martin raphael@web-crossing.com
*/
class MapConfigUtility {
/**
* uidParamName
*
* @var string
*/
static private $mapLayerConfigs = [
1 => [
"layer" => "osm",
"config" => [
],
"label" => "osm_map_layer"
],
10 => [
"layer" => "stamen",
"config" => [
"layer" => "terrain"
],
"label" => "stamen_map_terrain"
],
11 => [
"layer" => "stamen",
"config" => [
"layer" => "watercolor"
],
"label" => "stamen_map_watercolor"
],
12 => [
"layer" => "stamen",
"config" => [
"layer" => "toner"
],
"label" => "stamen_map_toner"
],
100 => [
"layer" => "thunderforest",
"config" => [
"layer" => "cycle"
],
"label" => "thunderforest_map_cycle"
],
101 => [
"layer" => "thunderforest",
"config" => [
"layer" => "transport"
],
"label" => "thunderforest_map_transport"
],
102 => [
"layer" => "thunderforest",
"config" => [
"layer" => "landscape"
],
"label" => "thunderforest_map_landscape"
],
103 => [
"layer" => "thunderforest",
"config" => [
"layer" => "outdoors"
],
"label" => "thunderforest_map_outdoors"
],
104 => [
"layer" => "thunderforest",
"config" => [
"layer" => "atlas"
],
"label" => "thunderforest_map_atlas"
],
105 => [
"layer" => "thunderforest",
"config" => [
"layer" => "transport-dark"
],
"label" => "thunderforest_map_transport-dark"
],
106 => [
"layer" => "thunderforest",
"config" => [
"layer" => "spinal-map"
],
"label" => "thunderforest_map_spinal"
],
107 => [
"layer" => "thunderforest",
"config" => [
"layer" => "pioneer"
],
"label" => "thunderforest_map_pioneer"
],
108 => [
"layer" => "thunderforest",
"config" => [
"layer" => "neighbourhood"
],
"label" => "thunderforest_map_neighbourhood"
],
109 => [
"layer" => "thunderforest",
"config" => [
"layer" => "mobile-atlas"
],
"label" => "thunderforest_map_mobile-atlas"
],
200 => [
"layer" => "maptiler",
"config" => [
"layer" => "basic"
],
"label" => "maptiler_map_basic"
],
201 => [
"layer" => "maptiler",
"config" => [
"layer" => "basic-4326"
],
"label" => "maptiler_map_basic-4326"
],
202 => [
"layer" => "maptiler",
"config" => [
"layer" => "bright"
],
"label" => "maptiler_map_bright"
],
203 => [
"layer" => "maptiler",
"config" => [
"layer" => "openstreetmap"
],
"label" => "maptiler_map_openstreetmap"
],
204 => [
"layer" => "maptiler",
"config" => [
"layer" => "outdoor"
],
"label" => "maptiler_map_outdoor"
],
205 => [
"layer" => "maptiler",
"config" => [
"layer" => "pastel"
],
"label" => "maptiler_map_pastel"
],
206 => [
"layer" => "maptiler",
"config" => [
"layer" => "hybrid"
],
"label" => "maptiler_map_hybrid"
],
207 => [
"layer" => "maptiler",
"config" => [
"layer" => "streets"
],
"label" => "maptiler_map_streets"
],
208 => [
"layer" => "maptiler",
"config" => [
"layer" => "toner"
],
"label" => "maptiler_map_toner"
],
209 => [
"layer" => "maptiler",
"config" => [
"layer" => "topo"
],
"label" => "maptiler_map_topo"
],
210 => [
"layer" => "maptiler",
"config" => [
"layer" => "topographique"
],
"label" => "maptiler_map_topographique"
],
211 => [
"layer" => "maptiler",
"config" => [
"layer" => "voyager"
],
"label" => "maptiler_map_voyager"
],
212 => [
"layer" => "maptiler",
"config" => [
"layer" => "winter"
],
"label" => "maptiler_map_winter"
],
];
/**
* setCanonical
*
* @param type $href
*/
public static function getMapLayerConfig(array $mapLayerConfigIds) {
$out = [];
foreach ($mapLayerConfigIds as $id) {
$out[] = self::$mapLayerConfigs[$id];
}
return $out;
}
public static function getMapConfig(array $mapLayerConfigIds, ?string $gpxSource = null, ?string $markerSource = null,
?string $defaultMarkerIcon = null,
int $zoom = 1, int $maxZoom = 18, float $centerLon = 0.0, float $centerLat = 0.0,
bool $zoomSlider = false, bool $fullScreen = false, bool $useLayerSwitcher = false): array {
$out = [
'zoom' => $zoom,
'maxZoom' => $maxZoom,
'centerLon' => $centerLon,
'centerLat' => $centerLat
];
$out['mapLayers'] = self::getMapLayerConfig($mapLayerConfigIds);
if ($useLayerSwitcher) {
for ($i = 0; $i < count($out['mapLayers']); $i++) {
$out['mapLayers'][$i]['active'] = true;
}
}
if ($defaultMarkerIcon !== null) {
$out['defaultMarkerIcon'] = $defaultMarkerIcon;
}
if ($gpxSource !== null) {
$out['gpxSource'] = $gpxSource;
}
if ($markerSource !== null) {
$out['markerSource'] = $markerSource;
}
if ($zoomSlider) {
$out['zoomSlider'] = $zoomSlider;
}
if ($fullScreen) {
$out['fullScreen'] = $fullScreen;
}
return $out;
}
static public function getMarkerCsr() {
return [
"type" => "name",
"properties" => [
"name" => "urn:ogc:def:crs:EPSG::4326"
]
];
}
static public function getMarker(MapEntry $marker, ?string $icon, ?string $popupContent) {
$out = [
"type" => "Feature",
"id" => $marker->getUid(),
"geometry" => [
"type" => "Point",
"coordinates" => [
$marker->getLongitude(),
$marker->getLatitude()
]
],
"geometry_name" => "SHAPE",
"properties" => [
'icon' => $icon
]
];
if ($popupContent !== null) {
$out['properties']['popup'] = $popupContent;
}
return $out;
}
static private $countryCoordinates = null;
private static function getCountryGeoJson(string $twoIsoCode = ''): ?array {
if (self::$countryCoordinates === null) {
self::$countryCoordinates = \GuzzleHttp\json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/typo3conf/ext/a2g_travel_blog/Resources/Public/sampleData/countries.json'), true);
}
if (self::$countryCoordinates[strtoupper($twoIsoCode)]) {
return self::$countryCoordinates[strtoupper($twoIsoCode)][0];
} else {
return null;
}
}
static protected $nominationBaseUrl = 'https://nominatim.openstreetmap.org';
static protected $nominationSufixUrl = '&format=json&polygon=1&addressdetails=1';
static protected $client = null;
static protected function initClient(){
if(self::$client===null){
self::$client = new Client([
// Base URI is used with relative requests
'base_uri' => self::$nominationBaseUrl,
// You can set any number of default request options.
'timeout' => 2.0,
]);
}
}
static public function getLatLonFrom(string $addressline, string $countryline, string $state = ''): ?array {
self::initClient();
if ($state === '') {
$url = urlencode($addressline . ',' . $countryline);
} else {
$url = urlencode($addressline . ',' . $countryline . ',' . $state);
}
$response = self::$client->request('GET', self::$nominationBaseUrl.'/search?q='.$url.self::$nominationSufixUrl);
if($response->getStatusCode() === 200){
return \GuzzleHttp\json_decode($response->getBody()->getContents(), true)[0];
}
return null;
}
public static function getNavToLink(string $addressline, string $countryline, string $state = ''): string{
if ($state === '') {
$url = str_replace(' ', '+', $addressline . ',' . $countryline);
} else {
$url = str_replace(' ', '+', $addressline . ',' . $countryline . ',' . $state);
}
return 'https://www.google.com/maps/dir//'. urlencode($url);
}
public static function getCountryFeatureCollectionsFromIsoCodeList(array $isoCodes = ['DE' => 'popupcontent de', 'AT' => 'popupcontent at']) {
$out = [
'type' => 'FeatureCollection',
'crs' => [
'type' => 'name',
'properties' => [
'name' => 'urn:ogc:def:crs:OGC:1.3:CRS84'
]
],
'features' => []
];
foreach ($isoCodes as $twoIsoCode => $isoCodePopupcontent) {
if ($isoCodePopupcontent === null) {
$out['features'][] = [
'type' => 'Feature',
'properties' => [
],
'geometry' => self::getCountryGeoJson($twoIsoCode)
];
} else {
$out['features'][] = [
'type' => 'Feature',
'properties' => [
'popup' => $isoCodePopupcontent
],
'geometry' => self::getCountryGeoJson($twoIsoCode)
];
}
}
return $out;
}
}