Browse Source

Passe de phpcbf

pull/34/head
b_b 7 months ago
parent
commit
029f07f00a
  1. 42
      gis_fonctions.php
  2. 224
      inc/staticmap.php

42
gis_fonctions.php

@ -518,11 +518,13 @@ function gis_icon_properties($img = '') {
}
if (!defined('_TAILLE_MAX_GIS_STATIC')) define('_TAILLE_MAX_GIS_STATIC',1024);
if (!defined('_TAILLE_MAX_GIS_STATIC')) {
define('_TAILLE_MAX_GIS_STATIC', 1024);
}
/**
* Retourne la balise img d'une carte statique
*
*
* @param int $id_gis
* ID du point à afficher
* @param int $width
@ -542,36 +544,38 @@ if (!defined('_TAILLE_MAX_GIS_STATIC')) define('_TAILLE_MAX_GIS_STATIC',1024);
* @return string
* Le code HTML de l'image de la carte
**/
function gis_static_map($id_gis=null, $width, $height, $maptype=false, $zoom=null, $markers=null, $lat=0, $lon=0) {
function gis_static_map($id_gis = null, $width, $height, $maptype = false, $zoom = null, $markers = null, $lat = 0, $lon = 0) {
$gis = [];
if ($id_gis) {
$gis = sql_fetsel('lat,lon,zoom', 'spip_gis', 'id_gis=' . intval($id_gis));
}
$gis = array();
if($id_gis)
$gis = sql_fetsel('lat,lon,zoom','spip_gis','id_gis='.intval($id_gis));
$zoom = $zoom ? $zoom : $gis['zoom'];
$lat = $gis['lat'] ? $gis['lat'] : $lat;
$lon = $gis['lon'] ? $gis['lon'] : $lon;
if (intval($id_gis) AND !$markers)
$markers_[] = array('lat' => $lat, 'lon' => $lon);
else {
if (intval($id_gis) and !$markers) {
$markers_[] = ['lat' => $lat, 'lon' => $lon];
} else {
$markers = explode('|', $markers);
foreach ($markers as $marker) {
list($markerLat, $markerLon, $markerUrl) = explode(';', $marker);
$markers_[] = array('lat' => $markerLat, 'lon' => $markerLon, 'url' => $markerUrl);
$markers_[] = ['lat' => $markerLat, 'lon' => $markerLon, 'url' => $markerUrl];
}
}
if ($width > _TAILLE_MAX_GIS_STATIC)
if ($width > _TAILLE_MAX_GIS_STATIC) {
$width = _TAILLE_MAX_GIS_STATIC;
if ($height > _TAILLE_MAX_GIS_STATIC)
}
if ($height > _TAILLE_MAX_GIS_STATIC) {
$height = _TAILLE_MAX_GIS_STATIC;
}
include_spip('inc/staticmap');
$map = new staticMapGis($lat, $lon, $zoom, $width, $height, $maptype, $markers_);
$balise_img = charger_filtre('balise_img');
return $balise_img($map->showMap());
}

224
inc/staticmap.php

@ -25,75 +25,75 @@
*
*/
Class staticMapGis
class staticMapGis
{
protected $maxWidth = _TAILLE_MAX_GIS_STATIC;
protected $maxHeight = _TAILLE_MAX_GIS_STATIC;
protected $tileSize = 256;
protected $tileSrcUrl = array(
'openstreetmap_mapnik' => array(
protected $tileSrcUrl = [
'openstreetmap_mapnik' => [
'url' => 'https://tile.openstreetmap.org/{Z}/{X}/{Y}.png',
'credits' => '© OpenStreetMap contributors'
),
'openstreetmap_de' => array(
],
'openstreetmap_de' => [
'url' => 'https://tile.openstreetmap.de/{Z}/{X}/{Y}.png',
'credits' => '© OpenStreetMap contributors'
),
'openstreetmap_fr' => array(
],
'openstreetmap_fr' => [
'url' => 'https://a.tile.openstreetmap.fr/osmfr/{Z}/{X}/{Y}.png',
'credits' => '© Openstreetmap France | © OpenStreetMap contributors'
),
'openstreetmap_hot' => array(
],
'openstreetmap_hot' => [
'url' => 'http://a.tile.openstreetmap.fr/hot/{Z}/{X}/{Y}.png',
'credits' => '© OpenStreetMap contributors, Tiles style by Humanitarian OpenStreetMap Team hosted by OpenStreetMap France'
),
'opentopomap' => array(
],
'opentopomap' => [
'url' => 'https://tile.opentopomap.org/{Z}/{X}/{Y}.png',
'credits' => '© OpenStreetMap contributorsn, SRTM | Map style: © OpenTopoMap (CC-BY-SA)'
),
'cartodb_positron' => array(
],
'cartodb_positron' => [
'url' => 'https://basemaps.cartocdn.com/light_all/{Z}/{X}/{Y}.png',
'credits' => '© OpenStreetMap contributors © CARTO'
),
'cartodb_positron_base' => array(
],
'cartodb_positron_base' => [
'url' => 'https://basemaps.cartocdn.com/light_nolabels/{Z}/{X}/{Y}.png',
'credits' => '© OpenStreetMap contributors © CARTO'
),
'cartodb_darkmatter' => array(
],
'cartodb_darkmatter' => [
'url' => 'https://basemaps.cartocdn.com/dark_all/{Z}/{X}/{Y}.png',
'credits' => '© OpenStreetMap contributors © CARTO'
),
'cartodb_darkmatter_base' => array(
],
'cartodb_darkmatter_base' => [
'url' => 'https://basemaps.cartocdn.com/dark_nolabels/{Z}/{X}/{Y}.png',
'credits' => '© OpenStreetMap contributors © CARTO'
),
'cartodb_voyager' => array(
],
'cartodb_voyager' => [
'url' => 'https://basemaps.cartocdn.com/rastertiles/voyager/{Z}/{X}/{Y}.png',
'credits' => '© OpenStreetMap contributors © CARTO'
),
'cartodb_voyager_base' => array(
],
'cartodb_voyager_base' => [
'url' => 'https://basemaps.cartocdn.com/rastertiles/voyager_nolabels/{Z}/{X}/{Y}.png',
'credits' => '© OpenStreetMap contributors © CARTO'
),
'stamen_toner' => array(
],
'stamen_toner' => [
'url' => 'https://stamen-tiles-c.a.ssl.fastly.net/toner/{Z}/{X}/{Y}.png',
'credits' => 'Map tiles by Stamen Design, CC BY 3.0 — Map data © OpenStreetMap contributors'
),
'stamen_tonerlite' => array(
],
'stamen_tonerlite' => [
'url' => 'https://stamen-tiles-c.a.ssl.fastly.net/toner-lite/{Z}/{X}/{Y}.png',
'credits' => 'Map tiles by Stamen Design, CC BY 3.0 — Map data © OpenStreetMap contributors'
),
'stamen_terrain' => array(
],
'stamen_terrain' => [
'url' => 'https://stamen-tiles-c.a.ssl.fastly.net/terrain/{Z}/{X}/{Y}.png',
'credits' => 'Map tiles by Stamen Design, CC BY 3.0 — Map data © OpenStreetMap contributors'
),
'stamen_watercolor' => array(
],
'stamen_watercolor' => [
'url' => 'https://stamen-tiles-c.a.ssl.fastly.net/watercolor/{Z}/{X}/{Y}.jpg',
'credits' => 'Map tiles by Stamen Design, CC BY 3.0 — Map data © OpenStreetMap contributors'
)
);
]
];
protected $tileDefaultSrc = 'openstreetmap_mapnik';
@ -109,12 +109,12 @@ Class staticMapGis
protected $zoom, $lat, $lon, $width, $height, $markers, $image, $maptype;
protected $centerX, $centerY, $offsetX, $offsetY;
public function __construct($lat=null, $lon=null, $zoom=null, $width, $height, $maptype, $markers)
{
public function __construct($lat = null, $lon = null, $zoom = null, $width, $height, $maptype, $markers) {
$this->width = intval($width);
$this->height = intval($height);
if ($zoom)
if ($zoom) {
$this->zoom = intval($zoom);
}
$this->lat = floatval($lat);
$this->lon = floatval($lon);
$this->markers = $markers;
@ -122,57 +122,58 @@ Class staticMapGis
$base = sous_repertoire(_DIR_VAR, 'cache-gis');
$this->tileCacheBaseDir = sous_repertoire($base, 'tiles');
$this->mapCacheBaseDir = sous_repertoire($base, 'maps');
if ($maptype AND array_key_exists($maptype, $this->tileSrcUrl))
if ($maptype and array_key_exists($maptype, $this->tileSrcUrl)) {
$this->maptype = $maptype;
}
}
public function lonToTile($long, $zoom)
{
public function lonToTile($long, $zoom) {
return (($long + 180) / 360) * pow(2, $zoom);
}
public function latToTile($lat, $zoom)
{
public function latToTile($lat, $zoom) {
return (1 - log(tan($lat * pi() / 180) + 1 / cos($lat * pi() / 180)) / pi()) / 2 * pow(2, $zoom);
}
public function latLngToPixels($lat, $lon, $zoom)
{
return array(
'x' => $this->lonToTile($lon,$zoom) * $this->tileSize,
public function latLngToPixels($lat, $lon, $zoom) {
return [
'x' => $this->lonToTile($lon, $zoom) * $this->tileSize,
'y' => $this->latToTile($lat, $zoom) * $this->tileSize
);
];
}
public function getBoundsZoom()
{
public function getBoundsZoom() {
// thx to https://github.com/esripdx/Static-Maps-API-PHP/blob/master/img.php#L166
$bounds = array('minLat' => 90, 'maxLat' => -90, 'minLon' => 180, 'maxLon' => -180);
$bounds = ['minLat' => 90, 'maxLat' => -90, 'minLon' => 180, 'maxLon' => -180];
foreach ($this->markers as $marker) {
if($marker['lat'] < $bounds['minLat'])
if ($marker['lat'] < $bounds['minLat']) {
$bounds['minLat'] = $marker['lat'];
if($marker['lat'] > $bounds['maxLat'])
}
if ($marker['lat'] > $bounds['maxLat']) {
$bounds['maxLat'] = $marker['lat'];
if($marker['lon'] < $bounds['minLon'])
}
if ($marker['lon'] < $bounds['minLon']) {
$bounds['minLon'] = $marker['lon'];
if($marker['lon'] > $bounds['maxLon'])
}
if ($marker['lon'] > $bounds['maxLon']) {
$bounds['maxLon'] = $marker['lon'];
}
}
$this->lat = $bounds['minLat'] + (($bounds['maxLat'] - $bounds['minLat']) / 2);
$this->lon = $bounds['minLon'] + (($bounds['maxLon'] - $bounds['minLon']) / 2);
if (!intval($this->zoom)) {
$fitZoom = 21;
$doesNotFit = true;
while($fitZoom > 1 && $doesNotFit) {
while ($fitZoom > 1 && $doesNotFit) {
$center = $this->latLngToPixels($this->lat, $this->lon, $fitZoom);
$leftEdge = $center['x'] - $this->width/2;
$topEdge = $center['y'] - $this->height/2;
$leftEdge = $center['x'] - $this->width / 2;
$topEdge = $center['y'] - $this->height / 2;
// check if the bounding rectangle fits within width/height
$sw = $this->latLngToPixels($bounds['minLat'], $bounds['minLon'], $fitZoom);
$ne = $this->latLngToPixels($bounds['maxLat'], $bounds['maxLon'], $fitZoom);
$fitHeight = abs($ne['y'] - $sw['y']);
$fitWidth = abs($ne['x'] - $sw['x']);
if($fitHeight <= $this->height && $fitWidth <= $this->width) {
if ($fitHeight <= $this->height && $fitWidth <= $this->width) {
$doesNotFit = false;
}
$fitZoom--;
@ -180,17 +181,15 @@ Class staticMapGis
$this->zoom = $fitZoom;
}
}
public function initCoords()
{
public function initCoords() {
$this->centerX = $this->lonToTile($this->lon, $this->zoom);
$this->centerY = $this->latToTile($this->lat, $this->zoom);
$this->offsetX = floor((floor($this->centerX) - $this->centerX) * $this->tileSize);
$this->offsetY = floor((floor($this->centerY) - $this->centerY) * $this->tileSize);
}
public function createBaseMap()
{
public function createBaseMap() {
$this->image = imagecreatetruecolor($this->width, $this->height);
$startX = floor($this->centerX - ($this->width / $this->tileSize) / 2);
$startY = floor($this->centerY - ($this->height / $this->tileSize) / 2);
@ -205,7 +204,7 @@ Class staticMapGis
for ($x = $startX; $x <= $endX; $x++) {
for ($y = $startY; $y <= $endY; $y++) {
$url = str_replace(array('{Z}', '{X}', '{Y}'), array($this->zoom, $x, $y), $this->tileSrcUrl[$this->maptype]['url']);
$url = str_replace(['{Z}', '{X}', '{Y}'], [$this->zoom, $x, $y], $this->tileSrcUrl[$this->maptype]['url']);
$tileData = $this->fetchTile($url);
if ($tileData) {
$tileImage = @imagecreatefromstring($tileData);
@ -222,8 +221,7 @@ Class staticMapGis
}
public function placeMarkers()
{
public function placeMarkers() {
// loop thru marker array
foreach ($this->markers as $marker) {
// set some local variables
@ -236,22 +234,23 @@ Class staticMapGis
$matches = false;
// marker perso ou par défaut ?
if ($markerUrl) {
include_spip("inc/distant");
include_spip('inc/distant');
$markerPath = copie_locale($markerUrl);
list($h,$w) = taille_image($markerPath);
$markerOffsetX = -$w/2;
$markerOffsetX = -$w / 2;
$markerOffsetY = -$h;
} else {
$markerPath = sinon(find_in_path('images/marker_defaut.png'),find_in_path('lib/leaflet/dist/images/marker-icon.png'));
$markerShadow = sinon(find_in_path('images/marker_defaut_shadow.png'),find_in_path('lib/leaflet/dist/images/marker-shadow.png'));
$markerPath = sinon(find_in_path('images/marker_defaut.png'), find_in_path('lib/leaflet/dist/images/marker-icon.png'));
$markerShadow = sinon(find_in_path('images/marker_defaut_shadow.png'), find_in_path('lib/leaflet/dist/images/marker-shadow.png'));
$markerOffsetX = -12;
$markerOffsetY = -41;
}
// create img + shadow resource
$markerImg = imagecreatefrompng($markerPath);
if ($markerShadow)
if ($markerShadow) {
$markerShadowImg = imagecreatefrompng($markerShadow);
}
// calc position
$destX = floor(($this->width / 2) - $this->tileSize * ($this->centerX - $this->lonToTile($markerLon, $this->zoom)));
@ -259,68 +258,77 @@ Class staticMapGis
// copy shadow on basemap
if ($markerShadow && $markerShadowImg) {
imagecopy($this->image, $markerShadowImg, $destX + intval($markerOffsetX), $destY + intval($markerOffsetY),
0, 0, imagesx($markerShadowImg), imagesy($markerShadowImg));
imagecopy(
$this->image,
$markerShadowImg,
$destX + intval($markerOffsetX),
$destY + intval($markerOffsetY),
0,
0,
imagesx($markerShadowImg),
imagesy($markerShadowImg)
);
}
// copy marker on basemap above shadow
imagecopy($this->image, $markerImg, $destX + intval($markerOffsetX), $destY + intval($markerOffsetY),
0, 0, imagesx($markerImg), imagesy($markerImg));
imagecopy(
$this->image,
$markerImg,
$destX + intval($markerOffsetX),
$destY + intval($markerOffsetY),
0,
0,
imagesx($markerImg),
imagesy($markerImg)
);
};
}
public function tileUrlToFilename($url)
{
return $this->tileCacheBaseDir . "/" . str_replace(array('http://'), '', $url);
public function tileUrlToFilename($url) {
return $this->tileCacheBaseDir . '/' . str_replace(['http://'], '', $url);
}
public function checkTileCache($url)
{
public function checkTileCache($url) {
$filename = $this->tileUrlToFilename($url);
if (file_exists($filename)) {
return file_get_contents($filename);
}
}
public function checkMapCache()
{
public function checkMapCache() {
$this->mapCacheID = md5($this->serializeParams());
$filename = $this->mapCacheIDToFilename();
if (file_exists($filename)) return true;
if (file_exists($filename)) { return true;
}
}
public function serializeParams()
{
return join("&", array($this->zoom, $this->lat, $this->lon, $this->width, $this->height, serialize($this->markers), $this->maptype));
public function serializeParams() {
return join('&', [$this->zoom, $this->lat, $this->lon, $this->width, $this->height, serialize($this->markers), $this->maptype]);
}
public function mapCacheIDToFilename()
{
public function mapCacheIDToFilename() {
if (!$this->mapCacheFile) {
$this->mapCacheFile = $this->mapCacheBaseDir . "/" . $this->maptype . "/" . $this->zoom . "/cache_" . substr($this->mapCacheID, 0, 2) . "/" . substr($this->mapCacheID, 2, 2) . "/" . substr($this->mapCacheID, 4);
$this->mapCacheFile = $this->mapCacheBaseDir . '/' . $this->maptype . '/' . $this->zoom . '/cache_' . substr($this->mapCacheID, 0, 2) . '/' . substr($this->mapCacheID, 2, 2) . '/' . substr($this->mapCacheID, 4);
}
return $this->mapCacheFile . "." . $this->mapCacheExtension;
return $this->mapCacheFile . '.' . $this->mapCacheExtension;
}
public function mkdir_recursive($pathname, $mode)
{
public function mkdir_recursive($pathname, $mode) {
is_dir(dirname($pathname)) || $this->mkdir_recursive(dirname($pathname), $mode);
return is_dir($pathname) || @mkdir($pathname, $mode);
}
public function writeTileToCache($url, $data)
{
public function writeTileToCache($url, $data) {
$filename = $this->tileUrlToFilename($url);
$this->mkdir_recursive(dirname($filename), 0777);
file_put_contents($filename, $data);
}
public function fetchTile($url)
{
if ($this->useTileCache && ($cached = $this->checkTileCache($url))) return $cached;
public function fetchTile($url) {
if ($this->useTileCache && ($cached = $this->checkTileCache($url))) { return $cached;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, _INC_DISTANT_USER_AGENT);
@ -331,30 +339,27 @@ Class staticMapGis
$this->writeTileToCache($url, $tile);
}
return $tile;
}
public function copyrightNotice()
{
public function copyrightNotice() {
$credits = $this->tileSrcUrl[$this->maptype]['credits'];
$largeur = $this->width * 2;
include_spip('inc/filtres_images_mini');
$logoImg = imagecreatefrompng(extraire_attribut(image_reduire_par(image_typo($credits,'taille=20','padding=8','align=right',"largeur=$largeur",'police=dustismo_bold.ttf'),2),'src'));
$logoImg = imagecreatefrompng(extraire_attribut(image_reduire_par(image_typo($credits, 'taille=20', 'padding=8', 'align=right', "largeur=$largeur", 'police=dustismo_bold.ttf'), 2), 'src'));
imagecopy($this->image, $logoImg, imagesx($this->image) - imagesx($logoImg), imagesy($this->image) - imagesy($logoImg), 0, 0, imagesx($logoImg), imagesy($logoImg));
}
public function makeMap()
{
if (count($this->markers)) $this->getBoundsZoom();
public function makeMap() {
if (count($this->markers)) { $this->getBoundsZoom();
}
$this->initCoords();
$this->createBaseMap();
if (count($this->markers)) $this->placeMarkers();
if (count($this->markers)) { $this->placeMarkers();
}
$this->copyrightNotice();
}
public function showMap()
{
public function showMap() {
if ($this->useMapCache) {
// use map cache, so check cache for map
if (!$this->checkMapCache()) {
@ -377,5 +382,4 @@ Class staticMapGis
return imagepng($this->image);
}
}
}

Loading…
Cancel
Save