-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4707 from wri/hotfix/ttc-analysis-FLAG-943
[FLAG-943] Tropical Tree Cover analysis malfunctioning
- Loading branch information
Showing
6 changed files
with
226 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { getWHEREQuery } from '../get-where-query'; | ||
|
||
describe('getWHEREQuery', () => { | ||
it('should return an string with each parameter separated by AND', () => { | ||
const params = { | ||
type: 'country', | ||
adm0: 'BRA', | ||
locationType: 'country', | ||
extentYear: 2000, | ||
thresh: 30, | ||
threshold: 30, | ||
forestType: 'plantations', | ||
dataset: 'annual', | ||
}; | ||
const query = getWHEREQuery(params); | ||
const expected = | ||
"WHERE iso = 'BRA' AND umd_tree_cover_density_2000__threshold = 30 AND gfw_planted_forests__type IS NOT NULL "; | ||
|
||
expect(query).toEqual(expected); | ||
}); | ||
|
||
// Tree Cover Density has a default threshold | ||
it('should not return threshold for Tree Cover Density', () => { | ||
const params = { | ||
type: 'country', | ||
adm0: 'PER', | ||
locationType: 'country', | ||
extentYear: 2020, | ||
thresh: '40', | ||
threshold: 40, // passing threshold as parameter from tropical tree cover layer | ||
dataset: 'treeCoverDensity', | ||
}; | ||
|
||
const query = getWHEREQuery(params); | ||
const expected = "WHERE iso = 'PER' "; | ||
const notExpected = 'AND umd_tree_cover_density_2000__threshold = 40 '; | ||
|
||
expect(query).toEqual(expected); | ||
expect(query).not.toEqual(notExpected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import ALLOWED_PARAMS from 'utils/get-where-query-allowed-params'; | ||
import { translateParameterKey } from 'utils/get-where-query-translation'; | ||
|
||
import forestTypes from 'data/forest-types'; | ||
import landCategories from 'data/land-categories'; | ||
|
||
const isNumber = (value) => !!(typeof value === 'number' || !isNaN(value)); | ||
|
||
// build {where} statement for query | ||
export const getWHEREQuery = (params = {}) => { | ||
const { type, dataset } = params || {}; | ||
|
||
const allFilterOptions = forestTypes.concat(landCategories); | ||
const allowedParams = ALLOWED_PARAMS[params.dataset || 'annual']; | ||
const isTreeCoverDensity = dataset === 'treeCoverDensity'; | ||
const comparisonString = ' = '; | ||
|
||
let paramString = 'WHERE '; | ||
let paramKeys = Object.keys(params).filter((parameterName) => { | ||
return ( | ||
(params[parameterName] || parameterName === 'threshold') && | ||
allowedParams.includes(parameterName) | ||
); | ||
}); | ||
|
||
if (!paramKeys?.length) { | ||
return ''; | ||
} | ||
|
||
/* | ||
* Removing threshold from Tree Cover Density request | ||
* Tree Cover Density has a default threshold >=10 | ||
*/ | ||
if (isTreeCoverDensity && paramKeys.includes('threshold')) { | ||
paramKeys = paramKeys.filter((item) => item !== 'threshold'); | ||
} | ||
|
||
paramKeys.forEach((parameter, index) => { | ||
const isLastParameter = paramKeys.length - 1 === index; | ||
const hasFilterOption = ['forestType', 'landCategory'].includes(parameter); | ||
const value = hasFilterOption ? 1 : params[parameter]; | ||
const filterOption = allFilterOptions.find( | ||
(pname) => pname.value === params[parameter] | ||
); | ||
|
||
const tableKey = | ||
filterOption && | ||
(filterOption.tableKey || filterOption.tableKeys[dataset || 'annual']); | ||
let isNumericValue = isNumber(value); | ||
|
||
const paramKey = translateParameterKey(parameter, params); | ||
|
||
if (parameter === 'adm0' && type === 'wdpa') { | ||
isNumericValue = false; | ||
} | ||
|
||
if (dataset === 'net_change') { | ||
isNumericValue = false; | ||
} | ||
|
||
const hasPrefixIs__ = hasFilterOption && tableKey.includes('is__'); | ||
let WHERE = ''; | ||
|
||
if (hasFilterOption) { | ||
if (hasPrefixIs__) { | ||
WHERE = `${WHERE}${tableKey} = 'true'`; | ||
} | ||
|
||
if (!hasPrefixIs__) { | ||
WHERE = `${WHERE}${tableKey} IS NOT NULL`; | ||
} | ||
|
||
if ( | ||
filterOption && | ||
!hasPrefixIs__ && | ||
filterOption.default && | ||
filterOption.categories | ||
) { | ||
WHERE = `${WHERE} AND ${tableKey} ${filterOption.comparison || '='} ${ | ||
filterOption?.dataType === 'keyword' | ||
? `'${filterOption?.default}'` | ||
: `${filterOption?.default}` | ||
}`; | ||
} | ||
} | ||
|
||
if (!hasFilterOption) { | ||
WHERE = `${WHERE}${paramKey}${comparisonString}${ | ||
isNumericValue ? value : `'${value}'` | ||
}`; | ||
} | ||
|
||
if (isLastParameter) { | ||
WHERE = `${WHERE} `; | ||
} else { | ||
WHERE = `${WHERE} AND `; | ||
} | ||
|
||
paramString = paramString.concat(WHERE); | ||
}); | ||
|
||
return paramString; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const ALLOWED_PARAMS = { | ||
annual: ['adm0', 'adm1', 'adm2', 'threshold', 'forestType', 'landCategory'], | ||
treeCoverDensity: [ | ||
'adm0', | ||
'adm1', | ||
'adm2', | ||
'threshold', | ||
'forestType', | ||
'landCategory', | ||
], | ||
integrated_alerts: [ | ||
'adm0', | ||
'adm1', | ||
'adm2', | ||
'forestType', | ||
'landCategory', | ||
'is__confirmed_alert', | ||
], | ||
glad: [ | ||
'adm0', | ||
'adm1', | ||
'adm2', | ||
'forestType', | ||
'landCategory', | ||
'is__confirmed_alert', | ||
], | ||
viirs: ['adm0', 'adm1', 'adm2', 'forestType', 'landCategory', 'confidence'], | ||
modis: ['adm0', 'adm1', 'adm2', 'forestType', 'landCategory', 'confidence'], | ||
modis_burned_area: [ | ||
'adm0', | ||
'adm1', | ||
'adm2', | ||
'threshold', | ||
'forestType', | ||
'landCategory', | ||
'confidence', | ||
], | ||
net_change: [ | ||
'adm0', | ||
'adm1', | ||
'adm2', | ||
'threshold', | ||
'forestType', | ||
'landCategory', | ||
'confidence', | ||
], | ||
tropicalTreeCover: ['adm0', 'adm1', 'adm2', 'threshold', 'forestType'], | ||
}; | ||
|
||
export default ALLOWED_PARAMS; |
Oops, something went wrong.