@@ -8,12 +8,46 @@ import { CogifyCreationOptions } from './stac.js';
8
8
9
9
const isPowerOfTwo = ( x : number ) : boolean => ( x & ( x - 1 ) ) === 0 ;
10
10
11
- export function gdalBuildVrt ( targetVrt : URL , source : URL [ ] ) : GdalCommand {
11
+ interface TargetOptions {
12
+ targetSrs ?: string ;
13
+ extent ?: number [ ] ;
14
+ targetResolution ?: number ;
15
+ }
16
+
17
+ function getTargetOptions ( opt : CogifyCreationOptions ) : TargetOptions {
18
+ const targetOpts : TargetOptions = { } ;
19
+
20
+ if ( opt . tileMatrix ) {
21
+ const tileMatrix = TileMatrixSets . find ( opt . tileMatrix ) ;
22
+ if ( tileMatrix == null ) throw new Error ( 'Unable to find tileMatrix: ' + opt . tileMatrix ) ;
23
+ targetOpts . targetSrs = tileMatrix . projection . toEpsgString ( ) ;
24
+
25
+ if ( opt . tile ) {
26
+ const bounds = tileMatrix . tileToSourceBounds ( opt . tile ) ;
27
+ targetOpts . extent = [
28
+ Math . min ( bounds . x , bounds . right ) ,
29
+ Math . min ( bounds . y , bounds . bottom ) ,
30
+ Math . max ( bounds . x , bounds . right ) ,
31
+ Math . max ( bounds . y , bounds . bottom ) ,
32
+ ] ;
33
+ }
34
+
35
+ if ( opt . zoomLevel ) {
36
+ targetOpts . targetResolution = tileMatrix . pixelScale ( opt . zoomLevel ) ;
37
+ }
38
+ }
39
+ return targetOpts ;
40
+ }
41
+
42
+ export function gdalBuildVrt ( targetVrt : URL , source : URL [ ] , opt ?: CogifyCreationOptions ) : GdalCommand {
12
43
if ( source . length === 0 ) throw new Error ( 'No source files given for :' + targetVrt . href ) ;
13
44
return {
14
45
output : targetVrt ,
15
46
command : 'gdalbuildvrt' ,
16
- args : [ urlToString ( targetVrt ) , ...source . map ( urlToString ) ] ,
47
+ args : [ opt && opt . addalpha ? [ '-addalpha' ] : undefined , urlToString ( targetVrt ) , ...source . map ( urlToString ) ]
48
+ . filter ( ( f ) => f != null )
49
+ . flat ( )
50
+ . map ( String ) ,
17
51
} ;
18
52
}
19
53
@@ -26,6 +60,7 @@ export function gdalBuildVrtWarp(
26
60
) : GdalCommand {
27
61
const tileMatrix = TileMatrixSets . find ( opt . tileMatrix ) ;
28
62
if ( tileMatrix == null ) throw new Error ( 'Unable to find tileMatrix: ' + opt . tileMatrix ) ;
63
+ if ( opt . zoomLevel == null ) throw new Error ( 'Unable to find zoomLevel' ) ;
29
64
const targetResolution = tileMatrix . pixelScale ( opt . zoomLevel ) ;
30
65
31
66
return {
@@ -53,18 +88,8 @@ export function gdalBuildVrtWarp(
53
88
54
89
export function gdalBuildCog ( targetTiff : URL , sourceVrt : URL , opt : CogifyCreationOptions ) : GdalCommand {
55
90
const cfg = { ...Presets [ opt . preset ] , ...opt } ;
56
- const tileMatrix = TileMatrixSets . find ( cfg . tileMatrix ) ;
57
- if ( tileMatrix == null ) throw new Error ( 'Unable to find tileMatrix: ' + cfg . tileMatrix ) ;
58
91
59
- const bounds = tileMatrix . tileToSourceBounds ( cfg . tile ) ;
60
- const tileExtent = [
61
- Math . min ( bounds . x , bounds . right ) ,
62
- Math . min ( bounds . y , bounds . bottom ) ,
63
- Math . max ( bounds . x , bounds . right ) ,
64
- Math . max ( bounds . y , bounds . bottom ) ,
65
- ] ;
66
-
67
- const targetResolution = tileMatrix . pixelScale ( cfg . zoomLevel ) ;
92
+ const targetOpts = getTargetOptions ( cfg ) ;
68
93
69
94
return {
70
95
command : 'gdal_translate' ,
@@ -73,25 +98,27 @@ export function gdalBuildCog(targetTiff: URL, sourceVrt: URL, opt: CogifyCreatio
73
98
[ '-of' , 'COG' ] ,
74
99
[ '-co' , 'NUM_THREADS=ALL_CPUS' ] , // Use all CPUS
75
100
[ '--config' , 'GDAL_NUM_THREADS' , 'all_cpus' ] , // Also required to NUM_THREADS till gdal 3.7.x
76
- [ '-co' , 'BIGTIFF=IF_NEEDED' ] , // BigTiff is somewhat slower and most (All?) of the COGS should be well below 4GB
101
+ cfg . srcwin ? [ '-srcwin' , cfg . srcwin [ 0 ] , cfg . srcwin [ 1 ] , cfg . srcwin [ 2 ] , cfg . srcwin [ 3 ] ] : undefined ,
102
+ cfg . bigTIFF ? [ '-co' , `BIGTIFF=${ cfg . bigTIFF } ` ] : [ '-co' , 'BIGTIFF=IF_NEEDED' ] , // BigTiff is somewhat slower and most (All?) of the COGS should be well below 4GB
77
103
[ '-co' , 'ADD_ALPHA=YES' ] ,
104
+ [ '-co' , `BLOCKSIZE=${ cfg . blockSize } ` ] ,
78
105
/**
79
106
* GDAL will recompress existing overviews if they exist which will compound
80
107
* any lossly compression on the overview, so compute new overviews instead
81
108
*/
82
109
[ '-co' , 'OVERVIEWS=IGNORE_EXISTING' ] ,
83
- [ '-co' , `BLOCKSIZE =${ cfg . blockSize } ` ] ,
84
- // ['-co', 'RESAMPLING=cubic'] ,
85
- [ '-co' , `WARP_RESAMPLING=${ cfg . warpResampling } ` ] ,
86
- [ '-co' , `OVERVIEW_RESAMPLING=${ cfg . overviewResampling } ` ] ,
110
+ cfg . overviewCompress ? [ '-co' , `OVERVIEW_COMPRESS =${ cfg . overviewCompress } ` ] : undefined ,
111
+ cfg . overviewQuality ? [ '-co' , `OVERVIEW_QUALITY= ${ cfg . overviewQuality } ` ] : undefined ,
112
+ cfg . warpResampling ? [ '-co' , `WARP_RESAMPLING=${ cfg . warpResampling } ` ] : undefined ,
113
+ cfg . overviewResampling ? [ '-co' , `OVERVIEW_RESAMPLING=${ cfg . overviewResampling } ` ] : undefined ,
87
114
[ '-co' , `COMPRESS=${ cfg . compression } ` ] ,
88
115
cfg . quality ? [ '-co' , `QUALITY=${ cfg . quality } ` ] : undefined ,
89
116
cfg . maxZError ? [ '-co' , `MAX_Z_ERROR=${ cfg . maxZError } ` ] : undefined ,
90
117
cfg . maxZErrorOverview ? [ '-co' , `MAX_Z_ERROR_OVERVIEW=${ cfg . maxZErrorOverview } ` ] : undefined ,
91
118
[ '-co' , 'SPARSE_OK=YES' ] ,
92
- [ '-co' , `TARGET_SRS=${ tileMatrix . projection . toEpsgString ( ) } ` ] ,
93
- [ '-co' , `EXTENT=${ tileExtent . join ( ',' ) } ,` ] ,
94
- [ '-tr' , targetResolution , targetResolution ] ,
119
+ targetOpts . targetSrs ? [ '-co' , `TARGET_SRS=${ targetOpts . targetSrs } ` ] : undefined ,
120
+ targetOpts . extent ? [ '-co' , `EXTENT=${ targetOpts . extent . join ( ',' ) } ,` ] : undefined ,
121
+ targetOpts . targetResolution ? [ '-tr' , targetOpts . targetResolution , targetOpts . targetResolution ] : undefined ,
95
122
urlToString ( sourceVrt ) ,
96
123
urlToString ( targetTiff ) ,
97
124
]
@@ -117,8 +144,8 @@ export function gdalCreate(targetTiff: URL, color: Rgba, opt: CogifyCreationOpti
117
144
const tileMatrix = TileMatrixSets . find ( cfg . tileMatrix ) ;
118
145
if ( tileMatrix == null ) throw new Error ( 'Unable to find tileMatrix: ' + cfg . tileMatrix ) ;
119
146
120
- const bounds = tileMatrix . tileToSourceBounds ( cfg . tile ) ;
121
- const pixelScale = tileMatrix . pixelScale ( cfg . zoomLevel ) ;
147
+ const bounds = tileMatrix . tileToSourceBounds ( cfg . tile ?? { x : 0 , y : 0 , z : 0 } ) ;
148
+ const pixelScale = tileMatrix . pixelScale ( cfg . zoomLevel ?? 0 ) ;
122
149
const size = Math . round ( bounds . width / pixelScale ) ;
123
150
124
151
// if the value of 'size' is not a power of 2
0 commit comments