-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
136 lines (118 loc) · 4.86 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<html>
<head>
<script src="https://unpkg.com/dropzone-complete"></script>
<script src="https://unpkg.com/read-pixels"></script>
<script src="https://unpkg.com/to-canvas"></script>
<script src="https://unpkg.com/[email protected]/dist-browser/geotiff.js"></script>
<script src="https://unpkg.com/reproject-geojson"></script>
<script src="./dist/dufour-peyton-intersection.min.js"></script>
<script>window.state = {};</script>
<style>
#canvas-wrapper canvas {
height: 400px;
}
</style>
</head>
<body>
<h1>Dufour-Peyton Intersection Demo</h1>
<h2>Upload a GeoTIFF</h2>
<input id="raster" name="raster" type="file">
<div id="canvas-wrapper">
<canvas style="background: lightgray; max-height: 400px"></canvas>
</div>
<script>
document.getElementById("raster").addEventListener('input', async function(event) {
state.raster = {};
const file = event.target.files[0];
const fname = file.name.toLowerCase();
if (fname.endsWith('jpg') || fname.endsWith("png")) {
console.log({file});
const pixels = await readPixels({ data: file });
state.raster.data = { data: pixels.pixels, height: pixels.height, width: pixels.width };
} else if (fname.endsWith("tif") || fname.endsWith("tiff")) {
const tif = await GeoTIFF.fromBlob(file);
console.log({tif});
const im = await tif.getImage();
console.log({im});
state.raster.data = await im.readRGB({ enableAlpha: true, interleave: true });
state.raster.height = await im.getHeight();
state.raster.width = await im.getWidth();
state.raster.bbox = await im.getBoundingBox();
const [resolutionX, resolutionY] = im.getResolution();
state.raster.pixel_width = Math.abs(resolutionX);
state.raster.pixel_height = Math.abs(resolutionY);
const geokeys = im.getGeoKeys();
state.raster.crs = geokeys.ProjectedCSTypeGeoKey || geokeys.GeographicTypeGeoKey;
document.getElementById("raster-bbox").value = state.raster.bbox.toString();
document.getElementById("raster-crs").value = state.raster.crs.toString();
}
state.raster.canvas = await toCanvas(state.raster.data);
const wrapper = document.getElementById("canvas-wrapper");
wrapper.innerHTML = "";
wrapper.appendChild(state.raster.canvas);
});
</script>
<br/>
<br/>
<h2>Raster Bounding Box</h2>
<input id="raster-bbox" name="raster-bbox" type="text" placeholder="xmin,ymin,xmax,ymax" style="width: 400px;" >
<br/>
<br/>
<h2>Raster Projection</h2>
<input id="raster-crs" name="raster-crs" type="text" placeholder="4326" style="width: 400px;">
<script>
document.getElementById("raster-crs").addEventListener("change", function (event) {
state.raster.crs = event.target.value;
});
</script>
<br/>
<br/>
<h2>Clipping GeoJSON</h2>
<dropzone-complete id="vector" height=400 width="100%"></dropzone-complete>
<script>
document.getElementById("vector").addEventListener("change", async function (event) {
if (event.detail && event.detail.file) {
state.vector = JSON.parse(await event.detail.file.text());
}
});
</script>
<br/>
<br/>
<button id="go" style="cursor: pointer; font-size: 24px; padding: 15px 30px;">Clip</button>
<script>
document.getElementById("go").addEventListener("click", async evt => {
console.log("clicked");
const geometry = reprojectGeoJSON(state.vector, { to: state.raster.crs });
const [xmin, ymin, xmax, ymax] = state.raster.bbox;
const in_data = state.raster.data;
const out_data = new Uint8ClampedArray(4 * state.raster.height * state.raster.width);
console.log({out_data});
dufour_peyton_intersection.calculate({
raster_bbox: state.raster.bbox,
raster_height: state.raster.height,
raster_width: state.raster.width,
pixel_height: state.raster.pixel_height,
pixel_width: state.raster.pixel_width,
geometry,
per_pixel: ({ row, column }) => {
// clip
const i = 4 * (row * state.raster.width + column);
// r, g, b, a
for (let ii = i; ii < i + 4; ii++) {
out_data[ii] = in_data[ii];
}
}
});
const out_canvas = await toCanvas({ data: out_data, height: state.raster.height, width: state.raster.width });
out_canvas.style.width = "100%";
document.getElementById("result").innerHTML = "";
document.getElementById("result").appendChild(out_canvas);
});
</script>
<hr>
<h2>Result</h2>
<div id="result">
<canvas style="background: lightgray; max-width: 100%"></canvas>
</div>
</body>
</html>