Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
clementprdhomme committed Dec 10, 2020
2 parents b10b308 + 3eb1ae9 commit 71a7c74
Show file tree
Hide file tree
Showing 39 changed files with 423 additions and 127 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ jobs:
ANALYSIS_API_URL: https://soilsrevealed.org/api/v1/analysis
GEE_KEY: ${{ secrets.GEE_KEY }}
GOOGLE_ANALYTICS_KEY: UA-179817360-1
TILES_URL: https://soils-revealed.s3.amazonaws.com/tiles
with:
host: ${{ secrets.SSH_HOST }}
key: ${{ secrets.SSH_KEY }}
port: ${{ secrets.SSH_PORT }}
username: ${{ secrets.SSH_USER }}
envs: NODE_PORT,MAPBOX_API_KEY,API_URL,ANALYSIS_API_URL,GEE_KEY,GOOGLE_ANALYTICS_KEY
envs: NODE_PORT,MAPBOX_API_KEY,API_URL,ANALYSIS_API_URL,GEE_KEY,GOOGLE_ANALYTICS_KEY,TILES_URL
script: |
echo '> Source nvm'
export NVM_DIR=~/.nvm
Expand All @@ -47,7 +48,7 @@ jobs:
echo '> Generate a deployment key'
DEPLOYMENT_KEY=`date +%s`
echo '> Create .env file'
echo -e "PORT=$NODE_PORT\nMAPBOX_API_KEY=$MAPBOX_API_KEY\nAPI_URL=$API_URL\nANALYSIS_API_URL=$ANALYSIS_API_URL\nDEPLOYMENT_KEY=$DEPLOYMENT_KEY\nGOOGLE_ANALYTICS_KEY=$GOOGLE_ANALYTICS_KEY" > .env
echo -e "PORT=$NODE_PORT\nMAPBOX_API_KEY=$MAPBOX_API_KEY\nAPI_URL=$API_URL\nANALYSIS_API_URL=$ANALYSIS_API_URL\nDEPLOYMENT_KEY=$DEPLOYMENT_KEY\nGOOGLE_ANALYTICS_KEY=$GOOGLE_ANALYTICS_KEY\nTILES_URL=$TILES_URL" > .env
echo '> Create gee.key.json file'
echo $GEE_KEY > gee.key.json
echo '> Build the app'
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ jobs:
ANALYSIS_API_URL: https://soilsrevealed.org/api/v1/analysis
GEE_KEY: ${{ secrets.GEE_KEY }}
GOOGLE_ANALYTICS_KEY:
TILES_URL: https://soils-revealed.s3.amazonaws.com/tiles
with:
host: ${{ secrets.SSH_HOST }}
key: ${{ secrets.SSH_KEY }}
port: ${{ secrets.SSH_PORT }}
username: ${{ secrets.SSH_USER }}
envs: NODE_PORT,MAPBOX_API_KEY,API_URL,ANALYSIS_API_URL,GEE_KEY,GOOGLE_ANALYTICS_KEY
envs: NODE_PORT,MAPBOX_API_KEY,API_URL,ANALYSIS_API_URL,GEE_KEY,GOOGLE_ANALYTICS_KEY,TILES_URL
script: |
echo '> Source nvm'
export NVM_DIR=~/.nvm
Expand All @@ -47,7 +48,7 @@ jobs:
echo '> Generate a deployment key'
DEPLOYMENT_KEY=`date +%s`
echo '> Create .env file'
echo -e "PORT=$NODE_PORT\nMAPBOX_API_KEY=$MAPBOX_API_KEY\nAPI_URL=$API_URL\nANALYSIS_API_URL=$ANALYSIS_API_URL\nDEPLOYMENT_KEY=$DEPLOYMENT_KEY\nGOOGLE_ANALYTICS_KEY=$GOOGLE_ANALYTICS_KEY" > .env
echo -e "PORT=$NODE_PORT\nMAPBOX_API_KEY=$MAPBOX_API_KEY\nAPI_URL=$API_URL\nANALYSIS_API_URL=$ANALYSIS_API_URL\nDEPLOYMENT_KEY=$DEPLOYMENT_KEY\nGOOGLE_ANALYTICS_KEY=$GOOGLE_ANALYTICS_KEY\nTILES_URL=$TILES_URL" > .env
echo '> Create gee.key.json file'
echo $GEE_KEY > gee.key.json
echo '> Build the app'
Expand Down
41 changes: 30 additions & 11 deletions api/tiles/land-cover.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const ee = require('@google/earthengine');
const axios = require('axios').default;

const getPregeneratedTile = require('./pregenerated-tile');

const RAMP = `
<RasterSymbolizer>
<ColorMap type="values" extended="false">
Expand Down Expand Up @@ -45,8 +47,13 @@ const RAMP = `
</RasterSymbolizer>
`;

module.exports = ({ params: { year, x, y, z } }, res) => {
try {
const sendImage = (res, data) => {
res.set('Content-Type', 'image/png');
return res.send(Buffer.from(data));
};

const getOnTheFlyTile = async (year, x, y, z) => {
return new Promise((resolve, reject) => {
const image = ee
.Image(
ee
Expand All @@ -55,18 +62,30 @@ module.exports = ({ params: { year, x, y, z } }, res) => {
.first()
)
.sldStyle(RAMP);

image.getMap({}, async ({ formatTileUrl }) => {
const url = formatTileUrl(x, y, z);
const serverPromise = axios.get(url, {
headers: { Accept: 'image/*' },
responseType: 'arraybuffer',
});
await serverPromise.then(serverResponse => {
res.set('Content-Type', 'image/png');
return res.send(Buffer.from(serverResponse.data));
});
axios
.get(url, {
headers: { Accept: 'image/*' },
responseType: 'arraybuffer',
})
.then(({ data }) => resolve(data))
.catch(reject);
});
});
};

module.exports = async ({ params: { year, x, y, z } }, res) => {
try {
const image = await getPregeneratedTile(['land-cover', year, z, x, y]);
sendImage(res, image);
} catch (e) {
res.status(404).end();
try {
const image = await getOnTheFlyTile(year, x, y, z);
sendImage(res, image);
} catch (e) {
res.status(404).end();
}
}
};
12 changes: 12 additions & 0 deletions api/tiles/pregenerated-tile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const axios = require('axios').default;

module.exports = async params => {
const url = encodeURI(`${process.env.TILES_URL}/${params.join('/')}.png`);

const { data } = await axios.get(url, {
headers: { Accept: 'image/*' },
responseType: 'arraybuffer',
});

return data;
};
54 changes: 43 additions & 11 deletions api/tiles/soc-experimental-change.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const ee = require('@google/earthengine');
const axios = require('axios').default;

const { LAYERS } = require('../../components/map/constants');
const getPregeneratedTile = require('./pregenerated-tile');

const STOCK_RAMP = `
<RasterSymbolizer>
<ColorMap extended="false" type="ramp">
Expand Down Expand Up @@ -33,8 +36,13 @@ const CONCENTRATION_RAMP = `
</RasterSymbolizer>
`;

module.exports = ({ params: { type, depth, year1, year2, x, y, z } }, res) => {
try {
const sendImage = (res, data) => {
res.set('Content-Type', 'image/png');
return res.send(Buffer.from(data));
};

const getOnTheFlyTile = async (type, depth, year1, year2, x, y, z) => {
return new Promise((resolve, reject) => {
let image;

if (type === 'stock') {
Expand Down Expand Up @@ -68,16 +76,40 @@ module.exports = ({ params: { type, depth, year1, year2, x, y, z } }, res) => {

image.getMap({}, async ({ formatTileUrl }) => {
const url = formatTileUrl(x, y, z);
const serverPromise = axios.get(url, {
headers: { Accept: 'image/*' },
responseType: 'arraybuffer',
});
await serverPromise.then(serverResponse => {
res.set('Content-Type', 'image/png');
return res.send(Buffer.from(serverResponse.data));
});
axios
.get(url, {
headers: { Accept: 'image/*' },
responseType: 'arraybuffer',
})
.then(({ data }) => resolve(data))
.catch(reject);
});
});
};

module.exports = async ({ params: { type, depth, year1, year2, x, y, z } }, res) => {
try {
const depthValue = LAYERS['soc-experimental'].paramsConfig.settings.type.options
.find(option => option.value === type)
.settings.depth.options[depth].label.replace(/\scm/, '');

const image = await getPregeneratedTile([
'soc-experimental-change',
type,
depthValue,
`${year2}-${year1}`,
z,
x,
y,
]);

sendImage(res, image);
} catch (e) {
res.status(404).end();
try {
const image = await getOnTheFlyTile(type, depth, year1, year2, x, y, z);
sendImage(res, image);
} catch (e) {
res.status(404).end();
}
}
};
60 changes: 46 additions & 14 deletions api/tiles/soc-experimental-timeseries.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const ee = require('@google/earthengine');
const axios = require('axios').default;

const { LAYERS } = require('../../components/map/constants');
const getPregeneratedTile = require('./pregenerated-tile');

const STOCK_RAMP = `
<RasterSymbolizer>
<ColorMap extended="false" type="ramp">
Expand Down Expand Up @@ -29,8 +32,16 @@ const CONCENTRATION_RAMP = `
</RasterSymbolizer>
`;

module.exports = ({ params: { type, depth, year, x, y, z } }, res) => {
try {
const sendImage = (res, z, data) => {
res.set('Content-Type', 'image/png');
if (+z <= 5) {
res.set('Cache-Control', 'public,max-age=604800');
}
return res.send(Buffer.from(data));
};

const getOnTheFlyTile = async (type, depth, year, x, y, z) => {
return new Promise((resolve, reject) => {
let image;

if (type === 'stock') {
Expand Down Expand Up @@ -59,19 +70,40 @@ module.exports = ({ params: { type, depth, year, x, y, z } }, res) => {

image.getMap({}, async ({ formatTileUrl }) => {
const url = formatTileUrl(x, y, z);
const serverPromise = axios.get(url, {
headers: { Accept: 'image/*' },
responseType: 'arraybuffer',
});
await serverPromise.then(serverResponse => {
res.set('Content-Type', 'image/png');
if (z < 5) {
res.set('Cache-Control', 'public,max-age=604800');
}
return res.send(Buffer.from(serverResponse.data));
});
axios
.get(url, {
headers: { Accept: 'image/*' },
responseType: 'arraybuffer',
})
.then(({ data }) => resolve(data))
.catch(reject);
});
});
};

module.exports = async ({ params: { type, depth, year, x, y, z } }, res) => {
try {
const depthValue = LAYERS['soc-experimental'].paramsConfig.settings.type.options
.find(option => option.value === type)
.settings.depth.options[depth].label.replace(/\scm/, '');

const image = await getPregeneratedTile([
'soc-experimental-timeseries',
type,
depthValue,
year,
z,
x,
y,
]);

sendImage(res, z, image);
} catch (e) {
res.status(404).end();
try {
const image = await getOnTheFlyTile(type, depth, year, x, y, z);
sendImage(res, z, image);
} catch (e) {
res.status(404).end();
}
}
};
55 changes: 44 additions & 11 deletions api/tiles/soc-stock-future-change.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const ee = require('@google/earthengine');
const axios = require('axios').default;

const { LAYERS } = require('../../components/map/constants');
const getPregeneratedTile = require('./pregenerated-tile');

const SCENARIOS = {
'00': 'crop_MGI',
'01': 'crop_I',
Expand Down Expand Up @@ -45,8 +48,13 @@ const DEGRADATION_RAMP = `
</RasterSymbolizer>
`;

module.exports = ({ params: { scenario, year, x, y, z } }, res) => {
try {
const sendImage = (res, data) => {
res.set('Content-Type', 'image/png');
return res.send(Buffer.from(data));
};

const getOnTheFlyTile = async (scenario, year, x, y, z) => {
return new Promise((resolve, reject) => {
let diff = ee.Image(
ee
.ImageCollection(`projects/soils-revealed/Future/scenario_${SCENARIOS[scenario]}_dSOC`)
Expand All @@ -69,16 +77,41 @@ module.exports = ({ params: { scenario, year, x, y, z } }, res) => {

image.getMap({}, async ({ formatTileUrl }) => {
const url = formatTileUrl(x, y, z);
const serverPromise = axios.get(url, {
headers: { Accept: 'image/*' },
responseType: 'arraybuffer',
});
await serverPromise.then(serverResponse => {
res.set('Content-Type', 'image/png');
return res.send(Buffer.from(serverResponse.data));
});
axios
.get(url, {
headers: { Accept: 'image/*' },
responseType: 'arraybuffer',
})
.then(({ data }) => resolve(data))
.catch(reject);
});
});
};

module.exports = async ({ params: { scenario, year, x, y, z } }, res) => {
try {
const depth = LAYERS['soc-stock'].paramsConfig.settings.type.options
.find(option => option.value === 'future')
.settings.depth.options[0].label.replace(/\scm/, '');

const image = await getPregeneratedTile([
'soc-stock-future-change',
SCENARIOS[scenario],
'stock',
depth,
`${year}-2018`,
z,
x,
y,
]);

sendImage(res, image);
} catch (e) {
res.status(404).end();
try {
const image = await getOnTheFlyTile(scenario, year, x, y, z);
sendImage(res, image);
} catch (e) {
res.status(404).end();
}
}
};
Loading

0 comments on commit 71a7c74

Please sign in to comment.