@anthropomorphic/leaflet-boxed-wms 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,69 @@
1
+ import L from 'leaflet';
2
+
3
+ L.BoxedWMS = L.TileLayer.WMS.extend({
4
+ initialize: function(url, options) {
5
+ this._fixedBounds = options.bounds || null;
6
+ L.TileLayer.WMS.prototype.initialize.call(this, url, options);
7
+ },
8
+
9
+ createTile: function(coords, done) {
10
+ const tile = L.TileLayer.WMS.prototype.createTile.call(this, coords, done);
11
+
12
+ if (this._fixedBounds && this._map) {
13
+ const tileBounds = this._tileCoordsToBounds(coords);
14
+
15
+ if (this._fixedBounds.intersects(tileBounds)) {
16
+ const iWest = Math.max(tileBounds.getWest(), this._fixedBounds.getWest());
17
+ const iSouth = Math.max(tileBounds.getSouth(), this._fixedBounds.getSouth());
18
+ const iEast = Math.min(tileBounds.getEast(), this._fixedBounds.getEast());
19
+ const iNorth = Math.min(tileBounds.getNorth(), this._fixedBounds.getNorth());
20
+
21
+ const isClipped = (iWest > tileBounds.getWest() || iSouth > tileBounds.getSouth() ||
22
+ iEast < tileBounds.getEast() || iNorth < tileBounds.getNorth());
23
+
24
+ if (isClipped) {
25
+ const crs = this._map.options.crs;
26
+ const zoom = coords.z;
27
+
28
+ const tNW = crs.latLngToPoint(L.latLng(tileBounds.getNorth(), tileBounds.getWest()), zoom);
29
+ const tSE = crs.latLngToPoint(L.latLng(tileBounds.getSouth(), tileBounds.getEast()), zoom);
30
+ const iNW = crs.latLngToPoint(L.latLng(iNorth, iWest), zoom);
31
+ const iSE = crs.latLngToPoint(L.latLng(iSouth, iEast), zoom);
32
+
33
+ const tileSize = 256;
34
+
35
+ const leftPct = ((iNW.x - tNW.x) / tileSize) * 100;
36
+ const topPct = ((iNW.y - tNW.y) / tileSize) * 100;
37
+ const rightPct = ((tSE.x - iSE.x) / tileSize) * 100;
38
+ const bottomPct = ((tSE.y - iSE.y) / tileSize) * 100;
39
+
40
+ tile.style.clipPath = `inset(${topPct}% ${rightPct}% ${bottomPct}% ${leftPct}%)`;
41
+ }
42
+ }
43
+ }
44
+ return tile;
45
+ },
46
+
47
+ getTileUrl: function(coords) {
48
+ if (!this._fixedBounds) {
49
+ return L.TileLayer.WMS.prototype.getTileUrl.call(this, coords);
50
+ }
51
+ var tileBounds = this._tileCoordsToBounds(coords);
52
+ if (!this._fixedBounds.intersects(tileBounds)) {
53
+ return 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
54
+ }
55
+ return L.TileLayer.WMS.prototype.getTileUrl.call(this, coords);
56
+ }
57
+ });
58
+
59
+ const boxedWms = function(url, options) {
60
+ return new L.BoxedWMS(url, options);
61
+ };
62
+
63
+
64
+ L.boxedWms = boxedWms;
65
+
66
+ export default L.BoxedWMS;
67
+
68
+
69
+ export { boxedWms };
package/L.BoxedWMS.js ADDED
@@ -0,0 +1,86 @@
1
+ 'use strict';
2
+
3
+ (function (factory) {
4
+
5
+ if (typeof define === 'function' && define.amd) {
6
+
7
+ define(['leaflet'], factory);
8
+
9
+ } else if (typeof module === 'object' && module.exports) {
10
+
11
+ module.exports = factory(require('leaflet'));
12
+
13
+ } else {
14
+
15
+ factory(window.L);
16
+
17
+ }
18
+
19
+ }(function (L) {
20
+
21
+ L.BoxedWMS = L.TileLayer.WMS.extend({
22
+ initialize: function(url, options) {
23
+ this._fixedBounds = options.bounds || null;
24
+ L.TileLayer.WMS.prototype.initialize.call(this, url, options);
25
+ },
26
+
27
+ createTile: function(coords, done) {
28
+ const tile = L.TileLayer.WMS.prototype.createTile.call(this, coords, done);
29
+
30
+ if (this._fixedBounds && this._map) {
31
+ const tileBounds = this._tileCoordsToBounds(coords);
32
+
33
+ if (this._fixedBounds.intersects(tileBounds)) {
34
+ const iWest = Math.max(tileBounds.getWest(), this._fixedBounds.getWest());
35
+ const iSouth = Math.max(tileBounds.getSouth(), this._fixedBounds.getSouth());
36
+ const iEast = Math.min(tileBounds.getEast(), this._fixedBounds.getEast());
37
+ const iNorth = Math.min(tileBounds.getNorth(), this._fixedBounds.getNorth());
38
+
39
+ const isClipped = (iWest > tileBounds.getWest() || iSouth > tileBounds.getSouth() ||
40
+ iEast < tileBounds.getEast() || iNorth < tileBounds.getNorth());
41
+
42
+ if (isClipped) {
43
+ const crs = this._map.options.crs;
44
+ const zoom = coords.z;
45
+
46
+ const tNW = crs.latLngToPoint(L.latLng(tileBounds.getNorth(), tileBounds.getWest()), zoom);
47
+ const tSE = crs.latLngToPoint(L.latLng(tileBounds.getSouth(), tileBounds.getEast()), zoom);
48
+
49
+ const iNW = crs.latLngToPoint(L.latLng(iNorth, iWest), zoom);
50
+ const iSE = crs.latLngToPoint(L.latLng(iSouth, iEast), zoom);
51
+
52
+ const tileSize = 256;
53
+
54
+ const leftPct = ((iNW.x - tNW.x) / tileSize) * 100;
55
+ const topPct = ((iNW.y - tNW.y) / tileSize) * 100;
56
+ const rightPct = ((tSE.x - iSE.x) / tileSize) * 100;
57
+ const bottomPct = ((tSE.y - iSE.y) / tileSize) * 100;
58
+
59
+ tile.style.clipPath = `inset(${topPct}% ${rightPct}% ${bottomPct}% ${leftPct}%)`;
60
+ }
61
+ }
62
+ }
63
+ return tile;
64
+ },
65
+
66
+ getTileUrl: function(coords) {
67
+ if (!this._fixedBounds) {
68
+ return L.TileLayer.WMS.prototype.getTileUrl.call(this, coords);
69
+ }
70
+
71
+ var tileBounds = this._tileCoordsToBounds(coords);
72
+
73
+ if (!this._fixedBounds.intersects(tileBounds)) {
74
+ return 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
75
+ }
76
+
77
+ return L.TileLayer.WMS.prototype.getTileUrl.call(this, coords);
78
+ }
79
+ });
80
+
81
+ L.boxedWms = function(url, options) {
82
+ return new L.BoxedWMS(url, options);
83
+ };
84
+
85
+ return L.BoxedWMS;
86
+ }));
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # LeafletBoxedWms
@@ -0,0 +1,106 @@
1
+ <!doctype html>
2
+ <html lang="it">
3
+
4
+ <head>
5
+ <meta charset="UTF-8" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
+ <title>Boxed WMS + TimeDimension Demo</title>
8
+
9
+ <!-- Leaflet CSS -->
10
+ <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
11
+
12
+ <!-- Leaflet.TimeDimension CSS -->
13
+ <link rel="stylesheet"
14
+ href="https://unpkg.com/leaflet-timedimension@1.1.1/dist/leaflet.timedimension.control.min.css" />
15
+
16
+
17
+ <style>
18
+ body {
19
+ margin: 0;
20
+ padding: 0;
21
+ }
22
+
23
+ html,
24
+ body,
25
+ #map {
26
+ height: 100%;
27
+ width: 100%;
28
+ }
29
+ </style>
30
+ </head>
31
+
32
+ <body>
33
+ <div id="map"></div>
34
+
35
+ <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
36
+ <script src=" https://cdn.jsdelivr.net/npm/leaflet-velocity@2.1.4/dist/leaflet-velocity.min.js "></script>
37
+ <link href=" https://cdn.jsdelivr.net/npm/leaflet-velocity@2.1.4/dist/leaflet-velocity.min.css " rel="stylesheet">
38
+
39
+ <script src="https://unpkg.com/iso8601-js-period@0.2.1/iso8601.js"></script>
40
+ <script src="https://unpkg.com/leaflet-timedimension@1.1.1/dist/leaflet.timedimension.min.js"></script>
41
+ <script src="../L.BoxedWMS.js"></script>
42
+
43
+ <script>
44
+
45
+
46
+ const fixedBBox = L.latLngBounds([
47
+ [38, 10],
48
+ [44, 16],
49
+ ]);
50
+
51
+ var map = L.map("map", {
52
+ timeDimension: true,
53
+ timeDimensionControl: true,
54
+ }).setView([40.5, 13.0], 6);
55
+
56
+ L.tileLayer(
57
+ "https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png",
58
+ {
59
+ attribution: "©OpenStreetMap, ©CartoDB",
60
+ },
61
+ ).addTo(map);
62
+
63
+ L.rectangle(fixedBBox, {
64
+ color: "#ff7800",
65
+ weight: 3,
66
+ fill: false,
67
+ dashArray: "10, 10",
68
+ }).addTo(map);
69
+
70
+ var nasaWmsUrl = "https://aspim.s4oceandata.eu/ncWMS/wms";
71
+
72
+ var boxedWmsLayer = L.boxedWms(nasaWmsUrl, {
73
+ layers: "cmems_mod_med_phy-sal_anfc_4_2km_P1D-m_so/so",
74
+ format: "image/png",
75
+ transparent: true,
76
+ bounds: fixedBBox,
77
+ });
78
+
79
+ var tdLayer = L.timeDimension.layer.wms(boxedWmsLayer, {
80
+ updateTimeDimension: true,
81
+ requestTimeFromCapabilities: true,
82
+ });
83
+
84
+
85
+ tdLayer.addTo(map);
86
+
87
+ fetch("./pippo.json")
88
+ .then(response => response.json())
89
+ .then(data => {
90
+
91
+ var velocityLayer = L.velocityLayer({
92
+ displayValues: true,
93
+ displayOptions: {
94
+ displayPosition: 'bottomleft',
95
+ displayEmptyString: 'No wind data'
96
+ },
97
+ data: data,
98
+ maxVelocity: 20
99
+ });
100
+
101
+ velocityLayer.addTo(map)
102
+ });
103
+ </script>
104
+ </body>
105
+
106
+ </html>
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@anthropomorphic/leaflet-boxed-wms",
3
+ "version": "1.0.0",
4
+ "description": "A Leaflet WMS layer that restricts tile loading to a fixed bounding box.",
5
+
6
+ "main": "L.BoxedWMS.js",
7
+ "module": "L.BoxedWMS.esm.js",
8
+
9
+ "exports": {
10
+ ".": {
11
+ "import": "./L.BoxedWMS.esm.js",
12
+ "require": "./L.BoxedWMS.js",
13
+ "default": "./L.BoxedWMS.js"
14
+ }
15
+ },
16
+
17
+ "peerDependencies": {
18
+ "leaflet": "^1.9.4"
19
+ }
20
+ }