|
| 1 | +import type { |
| 2 | + TileMatrix, |
| 3 | + TileMatrixSet, |
| 4 | +} from "@developmentseed/deck.gl-raster"; |
| 5 | +import type { GeoTIFF, GeoTIFFImage } from "geotiff"; |
| 6 | +import { |
| 7 | + extractGeotransform, |
| 8 | + getGeoTIFFProjection, |
| 9 | +} from "./geotiff-reprojection"; |
| 10 | +import proj4, { ProjectionDefinition } from "proj4"; |
| 11 | +import Ellipsoid from "./ellipsoids.js"; |
| 12 | +import type { PROJJSONDefinition } from "proj4/dist/lib/core"; |
| 13 | + |
| 14 | +// 0.28 mm per pixel |
| 15 | +// https://docs.ogc.org/is/17-083r4/17-083r4.html#toc15 |
| 16 | +const SCREEN_PIXEL_SIZE = 0.00028; |
| 17 | + |
| 18 | +/** |
| 19 | + * |
| 20 | + * Ported from Vincent's work here: |
| 21 | + * https://github.com/developmentseed/morecantile/pull/187/changes#diff-402eedddfa30af554d03750c8a82a09962b44b044976c321b774b484b98e8f48R665 |
| 22 | + * |
| 23 | + * @return {TileMatrixSet}[return description] |
| 24 | + */ |
| 25 | +export async function parseCOGTileMatrixSet( |
| 26 | + tiff: GeoTIFF, |
| 27 | +): Promise<TileMatrixSet> { |
| 28 | + const fullResImage = await tiff.getImage(); |
| 29 | + |
| 30 | + if (!fullResImage.isTiled) { |
| 31 | + throw new Error("COG TileMatrixSet requires a tiled GeoTIFF"); |
| 32 | + } |
| 33 | + |
| 34 | + const imageCount = await tiff.getImageCount(); |
| 35 | + const bbox = fullResImage.getBoundingBox(); |
| 36 | + const fullImageWidth = fullResImage.getWidth(); |
| 37 | + const fullImageHeight = fullResImage.getHeight(); |
| 38 | + |
| 39 | + const crs = await getGeoTIFFProjection(fullResImage); |
| 40 | + |
| 41 | + if (crs === null) { |
| 42 | + throw new Error( |
| 43 | + "Could not determine coordinate reference system from GeoTIFF geo keys", |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + const parsedCrs = parseCrs(crs); |
| 48 | + |
| 49 | + const projectToWgs84 = proj4(crs, "EPSG:4326").forward; |
| 50 | + const projectTo3857 = proj4(crs, "EPSG:3857").forward; |
| 51 | + |
| 52 | + const boundingBox: TileMatrixSet["boundingBox"] = { |
| 53 | + lowerLeft: [bbox[0]!, bbox[1]!], |
| 54 | + upperRight: [bbox[2]!, bbox[3]!], |
| 55 | + }; |
| 56 | + |
| 57 | + const transform = extractGeotransform(fullResImage); |
| 58 | + |
| 59 | + if (transform[1] !== 0 || transform[3] !== 0) { |
| 60 | + // TileMatrixSet assumes orthogonal axes |
| 61 | + throw new Error( |
| 62 | + "COG TileMatrixSet with rotation/skewed geotransform is not supported", |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + const cellSize = Math.abs(transform[0]); |
| 67 | + |
| 68 | + const tileWidth = fullResImage.getTileWidth(); |
| 69 | + const tileHeight = fullResImage.getTileHeight(); |
| 70 | + |
| 71 | + const tileMatrices: TileMatrix[] = [ |
| 72 | + { |
| 73 | + // Set as highest resolution / finest level |
| 74 | + id: String(imageCount - 1), |
| 75 | + scaleDenominator: |
| 76 | + (cellSize * metersPerUnit(parsedCrs)) / SCREEN_PIXEL_SIZE, |
| 77 | + cellSize, |
| 78 | + pointOfOrigin: [transform[2], transform[5]], |
| 79 | + tileWidth: fullResImage.getTileWidth(), |
| 80 | + tileHeight: fullResImage.getTileHeight(), |
| 81 | + matrixWidth: Math.ceil(fullImageWidth / tileWidth), |
| 82 | + matrixHeight: Math.ceil(fullImageHeight / tileHeight), |
| 83 | + geotransform: transform, |
| 84 | + }, |
| 85 | + ]; |
| 86 | + |
| 87 | + // Starting from 1 to skip full res image |
| 88 | + for (let imageIdx = 1; imageIdx < imageCount; imageIdx++) { |
| 89 | + const image = await tiff.getImage(imageIdx); |
| 90 | + |
| 91 | + if (!image.isTiled) { |
| 92 | + throw new Error("COG TileMatrixSet requires a tiled GeoTIFF"); |
| 93 | + } |
| 94 | + |
| 95 | + const tileMatrix = createOverviewTileMatrix({ |
| 96 | + id: String(imageCount - 1 - imageIdx), |
| 97 | + image, |
| 98 | + fullWidth: fullImageWidth, |
| 99 | + fullHeight: fullImageHeight, |
| 100 | + baseTransform: transform, |
| 101 | + parsedCrs, |
| 102 | + }); |
| 103 | + tileMatrices.push(tileMatrix); |
| 104 | + } |
| 105 | + |
| 106 | + // Reverse to have coarsest level first |
| 107 | + tileMatrices.reverse(); |
| 108 | + |
| 109 | + return { |
| 110 | + crs, |
| 111 | + boundingBox, |
| 112 | + tileMatrices, |
| 113 | + projectToWgs84, |
| 114 | + projectTo3857, |
| 115 | + }; |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Coefficient to convert the coordinate reference system (CRS) |
| 120 | + * units into meters (metersPerUnit). |
| 121 | + * |
| 122 | + * From note g in http://docs.opengeospatial.org/is/17-083r2/17-083r2.html#table_2: |
| 123 | + * |
| 124 | + * > If the CRS uses meters as units of measure for the horizontal dimensions, |
| 125 | + * > then metersPerUnit=1; if it has degrees, then metersPerUnit=2pa/360 |
| 126 | + * > (a is the Earth maximum radius of the ellipsoid). |
| 127 | + */ |
| 128 | +// https://github.com/developmentseed/morecantile/blob/7c95a11c491303700d6e33e9c1607f2719584dec/morecantile/utils.py#L67-L90 |
| 129 | +function metersPerUnit(parsedCrs: ProjectionDefinition): number { |
| 130 | + switch (parsedCrs.units) { |
| 131 | + case "metre": |
| 132 | + case "meter": |
| 133 | + case "meters": |
| 134 | + return 1; |
| 135 | + case "foot": |
| 136 | + return 0.3048; |
| 137 | + case "US survey foot": |
| 138 | + return 1200 / 3937; |
| 139 | + } |
| 140 | + |
| 141 | + if (parsedCrs.units === "degree") { |
| 142 | + // 2 * π * ellipsoid semi-major-axis / 360 |
| 143 | + const { a } = Ellipsoid[parsedCrs.ellps as keyof typeof Ellipsoid]; |
| 144 | + return (2 * Math.PI * a) / 360; |
| 145 | + } |
| 146 | + |
| 147 | + throw new Error(`Unsupported CRS units: ${parsedCrs.units}`); |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * Create tile matrix for COG overview |
| 152 | + */ |
| 153 | +function createOverviewTileMatrix({ |
| 154 | + id, |
| 155 | + image, |
| 156 | + fullWidth, |
| 157 | + baseTransform, |
| 158 | + parsedCrs, |
| 159 | +}: { |
| 160 | + id: string; |
| 161 | + image: GeoTIFFImage; |
| 162 | + fullWidth: number; |
| 163 | + fullHeight: number; |
| 164 | + baseTransform: [number, number, number, number, number, number]; |
| 165 | + parsedCrs: ProjectionDefinition; |
| 166 | +}): TileMatrix { |
| 167 | + const width = image.getWidth(); |
| 168 | + const height = image.getHeight(); |
| 169 | + |
| 170 | + // For now, just use scaleX |
| 171 | + // https://github.com/developmentseed/morecantile/pull/187/changes#r2621314673 |
| 172 | + const scaleX = fullWidth / width; |
| 173 | + |
| 174 | + // const scaleY = fullHeight / height; |
| 175 | + // if (Math.abs(scaleX - scaleY) > 1e-3) { |
| 176 | + // throw new Error("Non-uniform overview scaling detected (X/Y differ)"); |
| 177 | + // } |
| 178 | + |
| 179 | + const scale = scaleX; |
| 180 | + |
| 181 | + const geotransform: [number, number, number, number, number, number] = [ |
| 182 | + baseTransform[0] * scale, |
| 183 | + baseTransform[1] * scale, |
| 184 | + baseTransform[2], // x origin stays the same |
| 185 | + baseTransform[3] * scale, |
| 186 | + baseTransform[4] * scale, |
| 187 | + baseTransform[5], // y origin stays the same |
| 188 | + ]; |
| 189 | + const cellSize = Math.abs(geotransform[0]); |
| 190 | + |
| 191 | + const tileWidth = image.getTileWidth(); |
| 192 | + const tileHeight = image.getTileHeight(); |
| 193 | + |
| 194 | + return { |
| 195 | + id, |
| 196 | + scaleDenominator: (cellSize * metersPerUnit(parsedCrs)) / SCREEN_PIXEL_SIZE, |
| 197 | + cellSize, |
| 198 | + pointOfOrigin: [geotransform[2], geotransform[5]], |
| 199 | + tileWidth, |
| 200 | + tileHeight, |
| 201 | + matrixWidth: Math.ceil(width / tileWidth), |
| 202 | + matrixHeight: Math.ceil(height / tileHeight), |
| 203 | + geotransform, |
| 204 | + }; |
| 205 | +} |
| 206 | + |
| 207 | +function parseCrs(crs: PROJJSONDefinition): ProjectionDefinition { |
| 208 | + // If you pass proj4.defs a projjson, it doesn't parse it; it just returns the |
| 209 | + // input. |
| 210 | + // |
| 211 | + // Instead, you need to assign it to an alias and then retrieve it. |
| 212 | + |
| 213 | + const key = "__deck.gl-cog-internal__"; |
| 214 | + proj4.defs(key, crs); |
| 215 | + return proj4.defs(key); |
| 216 | +} |
0 commit comments