@@ -2,6 +2,7 @@ import { createSelector, createStructuredSelector } from 'reselect';
2
2
import isEmpty from 'lodash/isEmpty' ;
3
3
import sortBy from 'lodash/sortBy' ;
4
4
import moment from 'moment' ;
5
+ import { checkLocationInsideBbox } from 'utils/geoms' ;
5
6
6
7
import { initialState as mapInitialState } from 'components/maps/map/reducers' ;
7
8
import { initialState } from './recent-imagery-reducers' ;
@@ -22,14 +23,19 @@ const getMapUrlState = state =>
22
23
state . location && state . location . query && state . location . query . map ;
23
24
24
25
export const getMapSettings = createSelector ( [ getMapUrlState ] , urlState => ( {
25
- ...mapInitialState . settings ,
26
+ ...( mapInitialState && {
27
+ ...mapInitialState . settings
28
+ } ) ,
26
29
...urlState
27
30
} ) ) ;
28
31
29
32
export const getRecentImagerySettings = createSelector (
30
33
[ getRecentUrlState ] ,
31
34
urlState => ( {
32
35
...initialState . settings ,
36
+ ...( initialState && {
37
+ ...initialState . settings
38
+ } ) ,
33
39
...urlState
34
40
} )
35
41
) ;
@@ -62,43 +68,38 @@ export const getActive = createSelector(
62
68
}
63
69
) ;
64
70
65
- export const getFilteredData = createSelector (
71
+ export const getFilteredTiles = createSelector (
66
72
[ getData , getRecentImagerySettings ] ,
67
73
( data , settings ) => {
68
74
if ( isEmpty ( data ) ) return null ;
69
75
const { clouds } = settings ;
70
76
71
- return data
72
- . filter ( item => Math . round ( item . cloud_score ) <= clouds )
73
- . map ( item => item ) ;
77
+ return data . filter ( item => item . cloud_score <= clouds ) . map ( t => ( {
78
+ id : t . source ,
79
+ url : t . tile_url ,
80
+ thumbnail : t . thumbnail_url ,
81
+ cloudScore : t . cloud_score ,
82
+ dateTime : t . date_time ,
83
+ instrument : t . instrument ,
84
+ bbox : t . bbox
85
+ } ) ) ;
74
86
}
75
87
) ;
76
88
77
- export const getTiles = createSelector ( [ getFilteredData ] , data => {
89
+ export const getTiles = createSelector ( [ getFilteredTiles ] , data => {
78
90
if ( ! data || isEmpty ( data ) ) return [ ] ;
79
-
80
- return sortBy (
81
- data . map ( item => ( {
82
- id : item . source ,
83
- url : item . tile_url ,
84
- thumbnail : item . thumbnail_url ,
85
- cloudScore : item . cloud_score ,
86
- dateTime : item . date_time ,
87
- instrument : item . instrument ,
88
- bbox : item . bbox
89
- } ) ) ,
90
- d => new Date ( d . dateTime )
91
- ) . reverse ( ) ;
91
+ return sortBy ( data , d => new Date ( d . dateTime ) ) . reverse ( ) ;
92
92
} ) ;
93
93
94
94
export const getActiveTile = createSelector (
95
- [ getTiles , getRecentImagerySettings ] ,
95
+ [ getFilteredTiles , getRecentImagerySettings ] ,
96
96
( tiles , settings ) => {
97
97
if ( isEmpty ( tiles ) ) return null ;
98
98
const { selected, selectedIndex } = settings ;
99
99
const selectedTileById = tiles . find ( t => t . id === selected ) ;
100
100
if ( selectedTileById ) return selectedTileById ;
101
101
const selectedTileByIndex = selectedIndex && tiles [ selectedIndex ] ;
102
+
102
103
return selectedTileByIndex || tiles [ 0 ] ;
103
104
}
104
105
) ;
@@ -126,6 +127,14 @@ export const getTileBounds = createSelector([getActiveTile], activeTile => {
126
127
return activeTile . bbox . geometry . coordinates ;
127
128
} ) ;
128
129
130
+ export const getPositionInsideTile = createSelector (
131
+ [ getTileBounds , getPosition ] ,
132
+ ( bounds , position ) =>
133
+ ( bounds
134
+ ? checkLocationInsideBbox ( [ position . lat , position . lng ] , bounds )
135
+ : true )
136
+ ) ;
137
+
129
138
export const getSources = createSelector (
130
139
[ getData , getDataStatus ] ,
131
140
( data , dataStatus ) => {
@@ -174,7 +183,7 @@ export const getRecentImageryProps = createStructuredSelector({
174
183
dataStatus : getDataStatus ,
175
184
tiles : getTiles ,
176
185
activeTile : getActiveTile ,
177
- bounds : getTileBounds ,
186
+ positionInsideTile : getPositionInsideTile ,
178
187
location : getLocation ,
179
188
// url props
180
189
datasets : getActiveDatasetsFromState ,
0 commit comments