initial commit
This commit is contained in:
95
Classes/Utility/EvalcoordinatesUtility.php
Executable file
95
Classes/Utility/EvalcoordinatesUtility.php
Executable file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace A2G\A2gMaps\Utility;
|
||||
|
||||
/**
|
||||
* This file is part of the "tt_address" Extension for TYPO3 CMS.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class EvalcoordinatesUtility
|
||||
*/
|
||||
class EvalcoordinatesUtility
|
||||
{
|
||||
const LATITUDE_UPPER = '90.00000000';
|
||||
const LONGITUDE_UPPER = '180.00000000';
|
||||
|
||||
/**
|
||||
* @param float $coordinate
|
||||
* @return float evaluated and well-formed coordinate
|
||||
*/
|
||||
public static function formatLongitude(float $coordinate)
|
||||
{
|
||||
return self::validate((string)$coordinate, self::LONGITUDE_UPPER);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $coordinate
|
||||
* @return float evaluated and well-formed coordinate
|
||||
*/
|
||||
public static function formatLatitude(float $coordinate)
|
||||
{
|
||||
return self::validate((string)$coordinate, self::LATITUDE_UPPER);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $coordinate
|
||||
* @param string $upperRange
|
||||
* @return string
|
||||
*/
|
||||
protected static function validate($coordinate, string $upperRange): string
|
||||
{
|
||||
if ($coordinate === '') {
|
||||
return '.00000000';
|
||||
}
|
||||
|
||||
// test if value is negative
|
||||
$negative = '';
|
||||
if ($coordinate[0] === '-') {
|
||||
$negative = '-';
|
||||
}
|
||||
// remove all chars not being digits and point
|
||||
// therefore we will get a number
|
||||
$coordinate = preg_replace("/[^\d\.]/", '', $coordinate);
|
||||
|
||||
// split up string at first occurrence decimal point without losing data
|
||||
$integerPart = strstr($coordinate, '.', true);
|
||||
$decimalPart = strstr($coordinate, '.');
|
||||
|
||||
// if coordinate is given as integer (no decimal point)
|
||||
if ($integerPart === false) {
|
||||
$integerPart = $coordinate;
|
||||
}
|
||||
if ($decimalPart === false) {
|
||||
$decimalPart = '00';
|
||||
}
|
||||
|
||||
// remove all points from decimal-part
|
||||
$decimalPart = preg_replace("/[^\d]/", '', $decimalPart);
|
||||
|
||||
// fill up with zeros or shorten to match our goal of decimal(latitude: 10,8 and longitude: 11,8) in DB
|
||||
if (\strlen($decimalPart) >= 8) {
|
||||
$decimalPart = substr($decimalPart, 0, 8);
|
||||
} else {
|
||||
$decimalPart = str_pad($decimalPart, 8, '0', STR_PAD_RIGHT);
|
||||
}
|
||||
|
||||
// concatenate the whole string to a well-formed longitude and return
|
||||
$coordinate = $integerPart . '.' . $decimalPart;
|
||||
|
||||
// test if value is in the possible range. longitude can be -180 to +180.
|
||||
// latitude can be -90 to +90
|
||||
// At this point, our minus, if there, is stored to 'negative'
|
||||
// therefore we just test if integerpart is bigger than 90
|
||||
if ($coordinate > $upperRange) {
|
||||
$coordinate = $upperRange;
|
||||
}
|
||||
|
||||
// reapply signed/unsigned and return
|
||||
return $negative . $coordinate;
|
||||
}
|
||||
}
|
386
Classes/Utility/MapConfigUtility.php
Executable file
386
Classes/Utility/MapConfigUtility.php
Executable file
@ -0,0 +1,386 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user