Skip to content

Commit

Permalink
Basic WMS-Support for wp-plugin-leaflet-map (bozdoz#233)
Browse files Browse the repository at this point in the history
* added basic support for wms

Signed-off-by: Janne Jakob Fleischer <[email protected]>

* fixed README

Signed-off-by: Janne Jakob Fleischer <[email protected]>

* resolving comments.

Signed-off-by: Janne Jakob Fleischer <[email protected]>

* modified wordpress plugin repository readme

Signed-off-by: Janne Jakob Fleischer <[email protected]>

* removes commented code

* adds test; sanitizes wms attributes

* update typo in readme

* added osm-attributions for mundials-wms

* match attribution of osm to mundialis defaults

Signed-off-by: Janne Jakob Fleischer <[email protected]>

* fix attribution

---------

Signed-off-by: Janne Jakob Fleischer <[email protected]>
Co-authored-by: bozdoz <[email protected]>
  • Loading branch information
jannefleischer and bozdoz authored Jan 9, 2024
1 parent dc95cd4 commit 017c1fa
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Add a map generated with [LeafletJS](http://leafletjs.com/): an open-source Java
- [[leaflet-map] Options:](#leaflet-map-options)
- [[leaflet-image]](#leaflet-image)
- [[leaflet-image] Options:](#leaflet-image-options)
- [[leaflet-wms]](#leaflet-wms)
- [[leaflet-wms] Options:](#leaflet-wms-options)
- [[leaflet-marker]](#leaflet-marker)
- [[leaflet-marker] Options:](#leaflet-marker-options)
- [[leaflet-line]](#leaflet-line)
Expand Down Expand Up @@ -175,6 +177,37 @@ Then in the console, check the coordinates when you move the marker (should only

---

### [leaflet-wms]

Much the same as leaflet-map above, but for wms services and uses `src` for the service url,
`layer` for the layer name and `crs` for the coordination system (only supported by
leaflet: EPSG:3857 and EPSG:4326).
```
[leaflet-wms source="https://your/wms/service?" layer="yourLayer" crs="EPSG:3857" zoom=1]
```

#### [leaflet-wms] Options:

| Option | Default |
| ----------------- | ----------------------------------------- |
| `src` | https://ows.mundialis.de/services/service? |
| `layer ` | TOPO-OSM-WMS |
| `crs ` | EPSG:3857 |
| `zoom` | 12 |
| `height` | 250 |
| `width` | 100% |
| `fitbounds` | 0 (false) |
| `zoomcontrol` | 0 (false) |
| `scrollwheel` | 0 (false) |
| `doubleclickzoom` | 0 (false) |
| `min_zoom` | 0 |
| `max_zoom` | 20 |
| `attribution` | ©Leaflet ©OpenStreetMap |
| `lat` | 0 |
| `lng` | 0 |

---

### [leaflet-marker]

![Markers with HTML within a popup](https://imgur.com/ap38lwe.jpg)
Expand Down
4 changes: 4 additions & 0 deletions class.leaflet-map.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class Leaflet_Map
'file' => 'class.gpx-shortcode.php',
'class' => 'Leaflet_Gpx_Shortcode'
),
'leaflet-wms' => array(
'file' => 'class.wms-shortcode.php',
'class' => 'Leaflet_Wms_Shortcode'
),
'leaflet-line' => array(
'file' => 'class.line-shortcode.php',
'class' => 'Leaflet_Line_Shortcode'
Expand Down
4 changes: 4 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ Yes, just give it a source URL: `[leaflet-geojson src="https://example.com/path/

Sure!? Use the same attributes as leaflet-geojson (above), but use the `[leaflet-kml]` or `[leaflet-gpx]` shortcode.

= Can I add wms? =

Sure. Use the same attributes as leaflet-map, but use the `[leaflet-wms]` shortcode; attributes include: `src`, `layer`, and `crs`.

= Can I add a message to a marker? =

Yes: `[leaflet-marker visible]Hello there![/leaflet-marker]`, where visible designates if it is visible on page load. Otherwise it is only visible when clicked.
Expand Down
92 changes: 92 additions & 0 deletions shortcodes/class.wms-shortcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Wms Shortcode
*
* Displays map with [leaflet-wms src="path/to/wms"]
*
* JavaScript equivalent : L.TileLayer.wms('path/to/wms?', {layer: 'layername', crs: L.CRS.EPSG3857});
*
* @category Shortcode
* @author Janne Jakob Fleischer <[email protected]>
*/

// Exit if accessed directly
if (!defined('ABSPATH')) {
exit();
}

require_once LEAFLET_MAP__PLUGIN_DIR . 'shortcodes/class.map-shortcode.php';

/**
* Leaflet Image Shortcode Class
*/
class Leaflet_Wms_Shortcode extends Leaflet_Map_Shortcode
{
/**
* Get HTML for shortcode
*
* @param string $atts or Array
* @param string $content produced by adding atts to JavaScript
*
* @return string HTML script
*/
protected function getHTML($atts = '', $content = null)
{
extract($this->getAtts($atts));

if (!empty($address)) {
include_once LEAFLET_MAP__PLUGIN_DIR . 'class.geocoder.php';
$location = new Leaflet_Geocoder($address);
$lat = $location->lat;
$lng = $location->lng;
}

$lat_set = isset($lat) || isset($y);
$lng_set = isset($lng) || isset($x);

$lat = empty($lat) ? (empty($y) ? '0' : $y) : $lat;
$lng = empty($lng) ? (empty($x) ? '0' : $x) : $lng;

// validate lat/lng
$lat = $this->LM->filter_float($lat);
$lng = $this->LM->filter_float($lng);

/* only required field for image map (src/source) */
$src = empty($src) ? '' : $src;
$source = empty($source)
? 'https://ows.mundialis.de/services/service?'
: $source;
$source = filter_var(empty($src) ? $source : $src, FILTER_SANITIZE_URL);

$layer = empty($layer) ? 'TOPO-OSM-WMS' : $layer;

$crs = str_replace(':', '', empty($crs) ? 'EPSG:3857' : $crs);

if ($source == 'https://ows.mundialis.de/services/service?' && $layer == 'TOPO-OSM-WMS') {
$attribution = empty($attribution) ? '© OpenStreetMap Contributors' : $attribution;
}
$attribution = empty($attribution) ? '' : $attribution;

ob_start();
?>/*<script>*/
var srcUrl = atob('<?php echo base64_encode( $source ); ?>');
var options = L.Util.extend({}, {
attributionControl: false
}, <?php echo $map_options; ?>);
var zoom = <?php echo $zoom; ?>;
var map = window.WPLeafletMapPlugin.createMap(options).setView(L.latLng(<?php echo $lat; ?>, <?php echo $lng; ?>), zoom);
var wmslayer = L.tileLayer.wms(
srcUrl,
{
layers: '<?php echo esc_js($layer); ?>',
crs: L.CRS['<?php echo esc_js($crs); ?>']
}
).addTo( map );

<?php
$script = ob_get_clean();

return $this->getDiv($height, $width) .
$this->wrap_script($script, 'WPLeafletWmsShortcode');
}
}
3 changes: 3 additions & 0 deletions templates/shortcode-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@
'[leaflet-map fitbounds]',
'[leaflet-video-overlay]',
),
__("WMS Map", 'leaflet-map') => array(
'[leaflet-wms zoomControl zoom=5 lat=38.9252 lng=35.33203 attribution="For Those of Us in Turkey"]',
),
);

foreach ($examples as $title => $collection) {
Expand Down

0 comments on commit 017c1fa

Please sign in to comment.