|
| 1 | + |
| 2 | +# Georeferenced Image with Mask |
| 3 | + |
| 4 | +Display a georeferenced image with a polygon mask to show only a specific area. |
| 5 | + |
| 6 | +<iframe src="/maplibre-gl-layers/demos/image-mask.html" width="100%" style="border:none; height:400px"></iframe> |
| 7 | + |
| 8 | +```html |
| 9 | +<!doctype html> |
| 10 | +<html lang="en"> |
| 11 | + <head> |
| 12 | + <title>Georeferenced Image with Mask</title> |
| 13 | + <meta property="og:description" content="Display a georeferenced image with a polygon mask to show only a specific area." /> |
| 14 | + <meta charset="utf-8" /> |
| 15 | + <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 16 | + <link rel="stylesheet" href="https://unpkg.com/maplibre-gl/dist/maplibre-gl.css" /> |
| 17 | + <script src="https://unpkg.com/maplibre-gl/dist/maplibre-gl.js"></script> |
| 18 | + <script src="https://unpkg.com/proj4"></script> |
| 19 | + <script src="https://unpkg.com/@naivemap/maplibre-gl-image-layer"></script> |
| 20 | + < script src= "https://unpkg.com/[email protected]"></ script> |
| 21 | + <link rel="stylesheet" href="./style.css" /> |
| 22 | + </head> |
| 23 | + |
| 24 | + <body> |
| 25 | + <div id="map"></div> |
| 26 | + <script> |
| 27 | + // const pane = new Tweakpane.Pane() |
| 28 | + const map = new maplibregl.Map({ |
| 29 | + container: 'map', |
| 30 | + style: 'https://www.naivemap.com/demotiles/style.json', |
| 31 | + bounds: [ |
| 32 | + [105.289838, 32.204171], |
| 33 | + [110.195632, 28.164713] |
| 34 | + ], |
| 35 | + fitBoundsOptions: { |
| 36 | + padding: { top: 10, bottom: 10, left: 10, right: 10 } |
| 37 | + } |
| 38 | + }) |
| 39 | + map.on('load', () => { |
| 40 | + const layer4326 = new ImageLayer('image-layer', { |
| 41 | + url: './images/Terrain_CQ.png', |
| 42 | + projection: 'EPSG:4326', |
| 43 | + coordinates: [ |
| 44 | + [105.289838, 32.204171], // top-left |
| 45 | + [110.195632, 32.204171], // top-right |
| 46 | + [110.195632, 28.164713], // bottom-right |
| 47 | + [105.289838, 28.164713] // bottom-left |
| 48 | + ], |
| 49 | + mask: { |
| 50 | + data: { |
| 51 | + coordinates: [ |
| 52 | + [ |
| 53 | + [106.44608656573695, 29.874792116904175], |
| 54 | + [106.44608656573695, 29.35260054138334], |
| 55 | + [108.0312946475434, 29.35260054138334], |
| 56 | + [108.0312946475434, 29.874792116904175], |
| 57 | + [106.44608656573695, 29.874792116904175] |
| 58 | + ] |
| 59 | + ], |
| 60 | + type: 'Polygon' |
| 61 | + } |
| 62 | + } |
| 63 | + }) |
| 64 | +
|
| 65 | + map.addLayer(layer4326) |
| 66 | + }) |
| 67 | + </script> |
| 68 | + </body> |
| 69 | +</html> |
| 70 | + |
| 71 | +``` |
0 commit comments