1
1
import _ from 'underscore' ;
2
+ import fastMax from 'fast-max' ;
2
3
import get from '../get' ;
4
+ import load from '../load' ;
3
5
import utils from '../utils' ;
4
6
import convertGeometry from '../convert-geometry' ;
5
7
import intersectPolygon from '../intersect-polygon' ;
6
8
7
- const getMax = ( values , noDataValue ) => {
8
- const numberOfValues = values . length ;
9
- if ( numberOfValues > 0 ) {
10
- let max = null ;
11
- for ( let i = 0 ; i < numberOfValues ; i ++ ) {
12
- const value = values [ i ] ;
13
- if ( value !== noDataValue ) {
14
-
15
- /* We first compare the current value to the stored maximum.
16
- If the new value is greater than the stored minimum, replace the
17
- stored minimum with the new value. When checking a greater than
18
- comparison aganist a null value, like in the first comparison,
19
- the statement resolves as true. */
20
-
21
- if ( value > max ) {
22
- max = value ;
23
- }
24
- }
25
- }
26
- return max ;
27
- } else {
28
- throw 'No values were provided' ;
29
- }
30
- } ;
31
9
32
10
const getMaxForRaster = ( georaster , geom ) => {
33
11
@@ -37,34 +15,72 @@ const getMaxForRaster = (georaster, geom) => {
37
15
38
16
if ( geom === null || geom === undefined ) {
39
17
40
- return georaster . values . map ( band => {
41
- return _ . max ( band . map ( row => getMax ( row , noDataValue ) ) ) ;
42
- } ) ;
18
+ if ( georaster . values ) {
19
+ return georaster . values . map ( band => {
20
+ let band_max ;
21
+ band . forEach ( row => {
22
+ let row_max = fastMax ( row , { no_data : noDataValue } ) ;
23
+ if ( row_max !== undefined && ( band_max === undefined || row_max > band_max ) ) band_max = row_max ;
24
+ } )
25
+ return band_max ;
26
+ } ) ;
27
+ } else {
28
+ const options = { height : georaster . height , left : 0 , right : georaster . width , bottom : georaster . height , top : 0 , width : georaster . width } ;
29
+ return georaster . getValues ( options ) . then ( values => {
30
+ return values . map ( band => {
31
+ let band_max ;
32
+ band . forEach ( row => {
33
+ let row_max = fastMax ( row , { no_data : noDataValue } ) ;
34
+ if ( row_max !== undefined && ( band_max === undefined || row_max > band_max ) ) band_max = row_max ;
35
+ } )
36
+ return band_max ;
37
+ } ) ;
38
+ } ) ;
39
+ }
43
40
44
41
} else if ( utils . isBbox ( geom ) ) {
45
42
geom = convertGeometry ( 'bbox' , geom ) ;
46
43
47
44
// grab array of values;
48
45
const flat = true ;
49
- const values = get ( georaster , geom , flat ) ;
50
-
51
- // get max value
52
- return values . map ( band => getMax ( band , noDataValue ) ) ;
46
+ if ( georaster . values ) {
47
+ const values = get ( georaster , geom , flat ) ;
48
+
49
+ // get max value
50
+ return values . map ( band => fastMax ( band , { no_data : noDataValue } ) ) ;
51
+ } else {
52
+ return get ( georaster , geom , flat ) . then ( values => (
53
+ values . map ( band => fastMax ( band , { no_data : noDataValue } )
54
+ ) ) ) ;
55
+ }
53
56
54
57
} else if ( utils . isPolygon ( geom ) ) {
55
58
geom = convertGeometry ( 'polygon' , geom ) ;
56
59
const values = [ ] ;
57
60
58
- intersectPolygon ( georaster , geom , ( value , bandIndex ) => {
59
- if ( ! values [ bandIndex ] ) {
60
- values [ bandIndex ] = value ;
61
- } else if ( value > values [ bandIndex ] ) {
62
- values [ bandIndex ] = value ;
63
- }
64
- } ) ;
65
-
66
- if ( values ) return values ;
67
- else throw 'No Values were found in the given geometry' ;
61
+ if ( georaster . values ) {
62
+ intersectPolygon ( georaster , geom , ( value , bandIndex ) => {
63
+ if ( ! values [ bandIndex ] ) {
64
+ values [ bandIndex ] = value ;
65
+ } else if ( value > values [ bandIndex ] ) {
66
+ values [ bandIndex ] = value ;
67
+ }
68
+ } ) ;
69
+
70
+ if ( values ) return values ;
71
+ else throw 'No Values were found in the given geometry' ;
72
+ } else {
73
+ return intersectPolygon ( georaster , geom , ( value , bandIndex ) => {
74
+ if ( ! values [ bandIndex ] ) {
75
+ values [ bandIndex ] = value ;
76
+ } else if ( value > values [ bandIndex ] ) {
77
+ values [ bandIndex ] = value ;
78
+ }
79
+ } ) . then ( ( ) => {
80
+ if ( values ) return values ;
81
+ else throw 'No Values were found in the given geometry' ;
82
+ } ) ;
83
+ }
68
84
69
85
} else {
70
86
throw 'Non-Bounding Box geometries are currently not supported.' ;
@@ -75,4 +91,12 @@ const getMaxForRaster = (georaster, geom) => {
75
91
}
76
92
} ;
77
93
78
- export default getMaxForRaster ;
94
+ const getMaxForRasterWrapper = ( georaster , geom ) => {
95
+ if ( typeof georaster === "string" ) {
96
+ return load ( georaster ) . then ( loaded => getMaxForRaster ( loaded , geom ) ) ;
97
+ } else {
98
+ return getMaxForRaster ( georaster , geom ) ;
99
+ }
100
+ }
101
+
102
+ export default getMaxForRasterWrapper ;
0 commit comments