Skip to content

Commit

Permalink
chore: ee layer json in app
Browse files Browse the repository at this point in the history
  • Loading branch information
turban committed Oct 4, 2023
1 parent 6f5fb3d commit 2ca7552
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 98 deletions.
69 changes: 2 additions & 67 deletions src/actions/externalLayers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ import * as types from '../constants/actionTypes.js'
import { createExternalLayer } from '../util/external.js'
import { fetchExternalLayers } from '../util/requests.js'
import { addBasemaps } from './basemap.js'
import { EARTH_ENGINE_LAYER } from '../constants/layers.js'

export const EXTERNAL_LAYERS_NAMESPACE = 'EXTERNAL_LAYERS'

// const isBasemap = (layer) => layer.mapLayerPosition === 'BASEMAP'
const isBasemap = (layer) => layer.position === 'basemap'
const isBasemap = (layer) => layer.mapLayerPosition === 'BASEMAP'
const isOverlay = (layer) => !isBasemap(layer)

// Add external overlay
Expand All @@ -18,79 +14,18 @@ export const addExternalLayer = (layer) => ({
})

export const tSetExternalLayers = (engine) => async (dispatch) => {
engine
.query({
dataStore: {
resource: 'dataStore',
},
})
.then(({ dataStore }) => {
if (dataStore.includes(EXTERNAL_LAYERS_NAMESPACE)) {
engine
.query({
layerIds: {
resource: `dataStore/${EXTERNAL_LAYERS_NAMESPACE}`,
},
})
.then(({ layerIds }) => {
// TODO: Possible to load all layers at once?
Promise.all(
layerIds.map((layerId) =>
engine
.query({
layer: {
resource: `dataStore/${EXTERNAL_LAYERS_NAMESPACE}/${layerId}`,
},
})
.then(({ layer }) => layer)
)
).then((layers) => {
layers.sort((a, b) => a.name.localeCompare(b.name))

const basemaps = layers
.filter(isBasemap)
.map(createExternalLayer)

if (basemaps.length) {
dispatch(addBasemaps(basemaps))
}

// console.log('basemaps', basemaps)

layers.filter(isOverlay).forEach((layer) => {
const layerId = layer.id // TODO
delete layer.id // TODO

dispatch(
addExternalLayer({
...layer,
layer: EARTH_ENGINE_LAYER, // TODO
layerId, // TODO
})
)
})
})
})
}
})

try {
const externalLayers = await fetchExternalLayers(engine)
const externalBasemaps = externalLayers.externalLayers.externalMapLayers
.filter((layer) => layer.mapLayerPosition === 'BASEMAP')
.filter(isBasemap)
.map(createExternalLayer)

// console.log('externalBasemaps', externalBasemaps)

/*
dispatch(addBasemaps(externalBasemaps))

externalLayers.externalLayers.externalMapLayers
.filter(isOverlay)
.map(createExternalLayer)
.map((layer) => dispatch(addExternalLayer(layer)))
*/
} catch (e) {
log.error('Could not load external map layers')
return e
Expand Down
26 changes: 17 additions & 9 deletions src/components/edit/earthEngine/EarthEngineDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
setFilter,
setBufferRadius,
} from '../../../actions/layerEdit.js'
import { getEarthEngineLayer } from '../../../constants/earthEngine.js'
// import { getEarthEngineLayer } from '../../../constants/earthEngine.js'
import {
DEFAULT_ORG_UNIT_LEVEL,
EE_BUFFER,
Expand All @@ -35,7 +35,7 @@ const EarthEngineDialog = (props) => {
const [error, setError] = useState()

const {
layerId,
// layerId,
datasetId,
band,
rows,
Expand All @@ -59,11 +59,12 @@ const EarthEngineDialog = (props) => {
periodType,

Check failure on line 59 in src/components/edit/earthEngine/EarthEngineDialog.js

View workflow job for this annotation

GitHub Actions / lint

'periodType' is missing in props validation
periodReducer,

Check failure on line 60 in src/components/edit/earthEngine/EarthEngineDialog.js

View workflow job for this annotation

GitHub Actions / lint

'periodReducer' is missing in props validation
bands,

Check failure on line 61 in src/components/edit/earthEngine/EarthEngineDialog.js

View workflow job for this annotation

GitHub Actions / lint

'bands' is missing in props validation
filters = defaultFilters,
filters, // = defaultFilters,

Check failure on line 62 in src/components/edit/earthEngine/EarthEngineDialog.js

View workflow job for this annotation

GitHub Actions / lint

'filters' is missing in props validation
unit,
source,
sourceUrl,
aggregations,
defaultAggregations,
} = props // dataset

const period = getPeriodFromFilter(filter)
Expand Down Expand Up @@ -121,6 +122,7 @@ const EarthEngineDialog = (props) => {

const noBandSelected = Array.isArray(bands) && (!band || !band.length)

const hasAggregations = !!(aggregations || defaultAggregations)
const hasMultipleAggregations = !aggregations || aggregations.length > 1

const hasOrgUnitField = !!orgUnitField && orgUnitField !== NONE
Expand Down Expand Up @@ -182,6 +184,12 @@ const EarthEngineDialog = (props) => {
}
}, [hasOrgUnitField, areaRadius, setBufferRadius])

useEffect(() => {
if (!periodType && filters) {
setFilter(filters)
}
}, [periodType, filters, setFilter])

useEffect(() => {
if (validateLayer) {
const isValid = !noBandSelected && (!periodType || period)
Expand Down Expand Up @@ -258,7 +266,7 @@ const EarthEngineDialog = (props) => {
}
/>
)}
<AggregationSelect />
{hasAggregations && <AggregationSelect />}
{unit && (
<div className={styles.paragraph}>
{i18n.t('Unit')}: {unit}
Expand Down Expand Up @@ -306,7 +314,7 @@ const EarthEngineDialog = (props) => {

EarthEngineDialog.propTypes = {
datasetId: PropTypes.string.isRequired,
layerId: PropTypes.string.isRequired,
// layerId: PropTypes.string.isRequired,
setBufferRadius: PropTypes.func.isRequired,
setFilter: PropTypes.func.isRequired,
setOrgUnits: PropTypes.func.isRequired,
Expand All @@ -318,10 +326,10 @@ EarthEngineDialog.propTypes = {
legend: PropTypes.object,
orgUnitField: PropTypes.string,
orgUnits: PropTypes.object,
params: PropTypes.shape({
max: PropTypes.number.isRequired,
min: PropTypes.number.isRequired,
palette: PropTypes.array.isRequired,
style: PropTypes.shape({
max: PropTypes.number,
min: PropTypes.number,
palette: PropTypes.array,
}),
rows: PropTypes.array,
}
Expand Down
36 changes: 22 additions & 14 deletions src/components/edit/earthEngine/StyleTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,34 @@ import styles from '../styles/LayerDialog.module.css'
import LegendPreview from './LegendPreview.js'
import StyleSelect from './StyleSelect.js'

const StyleTab = ({ unit, style, hasOrgUnitField }) => (
<div className={styles.flexColumnFlow}>
<div className={styles.flexColumn}>
{style && <StyleSelect unit={unit} style={style} />}
<BufferRadius
defaultRadius={EE_BUFFER}
hasOrgUnitField={hasOrgUnitField}
/>
const StyleTab = ({ unit, style, hasOrgUnitField }) => {
const { min, max, palette } = style
const isClassStyle =
min !== undefined && max !== undefined && palette !== undefined

return (
<div className={styles.flexColumnFlow}>
<div className={styles.flexColumn}>
{isClassStyle && <StyleSelect unit={unit} style={style} />}
<BufferRadius
defaultRadius={EE_BUFFER}
hasOrgUnitField={hasOrgUnitField}
/>
</div>
{isClassStyle && <LegendPreview style={style} />}
</div>
{style && <LegendPreview style={style} />}
</div>
)
)
}

StyleTab.propTypes = {
hasOrgUnitField: PropTypes.bool.isRequired,
unit: PropTypes.string,
style: PropTypes.shape({
max: PropTypes.number.isRequired,
min: PropTypes.number.isRequired,
palette: PropTypes.array.isRequired,
max: PropTypes.number,
min: PropTypes.number,
palette: PropTypes.array,
color: PropTypes.string,
strokeWidth: PropTypes.number,
}),
}

Expand Down
1 change: 1 addition & 0 deletions src/components/map/layers/earthEngine/EarthEngineLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export default class EarthEngineLayer extends Layer {
config.getAuthToken = getAuthToken

try {
console.log('config', config)
this.layer = map.createLayer(config)
await map.addLayer(this.layer)
} catch (error) {
Expand Down
Loading

0 comments on commit 2ca7552

Please sign in to comment.