Skip to content

Commit

Permalink
feat: work with multiple locations, for now static
Browse files Browse the repository at this point in the history
  • Loading branch information
lubojr committed Jan 19, 2024
1 parent 0efba1b commit 589383c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
34 changes: 27 additions & 7 deletions app/src/config/ideas.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,11 @@ export const globalIndicators = [
minesweeperOptions: {
// Board dimensions in number of hex cells
size: 30,
// height: 40,
// width: 20,
geotiff: {
projection: 'EPSG:4326',
url: 'https://eox-gtif-public.s3.eu-central-1.amazonaws.com/ideas_data/Copernicus_DSM_30_N47_00_E014_00_DEM_COG.tif',
},
locations: [// 0.3 and 0.15
locations: [
[14.0, 47.0, 15.0, 48.0],
],
},
Expand Down Expand Up @@ -392,7 +390,11 @@ export const globalIndicators = [
indicatorName: 'Indicator 2: Wildlife',
subAoi: {
type: 'FeatureCollection',
features: [],
features: [{
type: 'Feature',
properties: {},
geometry: wkt.read('POLYGON ((-5.800781 41.409776, -5.800781 51.536086, 10.546875 51.536086, 10.546875 41.409776, -5.800781 41.409776))').toJson(),
}],
},
aoiID: 'World',
time: [],
Expand All @@ -405,11 +407,11 @@ export const globalIndicators = [
label: 'Wildlife biodiversity',
id: 'wildlife',
// dataInfo: 'WindPowerDensity',
min: 0.5,
min: 0.25,
max: 5,
step: 0.25,
header: true,
range: [0.5, 5],
range: [0.25, 5],
},
biodiversity_connectivity_quintile: {
display: true,
Expand Down Expand Up @@ -443,6 +445,24 @@ export const globalIndicators = [
},
},
},
minesweeperOptions: {
// Board dimensions in number of hex cells
size: 30,
geotiff: {
projection: 'EPSG:4326',
url: 'https://eox-ideas.s3.eu-central-1.amazonaws.com/ideas_data/AR2_wildlife_simplify_COG_b1.tif',
},
selectedLocationIndex: 0,
locations: [{
name: 'Dordogne Valley',
bbox: [-1.3289, 44.4393, 1.944, 45.6092],
isMineCondition: (val) => val >= 3,
}, {
name: 'Jura/Savoie',
bbox: [4.7013, 45.7953, 7.0053, 47.036],
isMineCondition: (val) => val >= 3,
}],
},
display: {
protocol: 'cog',
id: 'IND2_1',
Expand All @@ -452,7 +472,7 @@ export const globalIndicators = [
name: 'Indicator 2: Wildlife',
style: {
variables: {
wildlifeMin: 0.5,
wildlifeMin: 0.25,
wildlifeMax: 5,
biodiversity_connectivity_quintileMin: -1,
biodiversity_connectivity_quintileMax: 5,
Expand Down
31 changes: 21 additions & 10 deletions app/src/plugins/minesweeper/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ export default class HexSweeperGame {
constructor(options, difficulty) {
this.size = options.size;
this.locations = options.locations;
this.selectedLocationIndex = options.selectedLocationIndex;
// this.height = height;
this.difficulty = difficulty;
this.board = [];
this.image = null;
this.center = [];
this.gameSize = 2100;
this.gameSize = null;
this.fieldCount = 0;
this.mineCount = 0;
}
Expand Down Expand Up @@ -59,16 +60,21 @@ export default class HexSweeperGame {
async fromGeoTIFF(options) {
try {
const tiff = await fromUrl(options.geotiff.url);
const image = await tiff.getImage();
// get currently zoomed to location index
const locationIndex = 0;
const location = this.locations[this.selectedLocationIndex];

// Convert geographic coordinates to distances using EPSG:3857
const xmin = proj4(options.geotiff.projection, 'EPSG:3857', [this.locations[locationIndex][0], this.locations[locationIndex][1]]);
const xmax = proj4(options.geotiff.projection, 'EPSG:3857', [this.locations[locationIndex][2], this.locations[locationIndex][3]]);
const xmin = proj4(options.geotiff.projection, 'EPSG:3857', [location.bbox[0], location.bbox[1]]);
const xmax = proj4(options.geotiff.projection, 'EPSG:3857', [location.bbox[2], location.bbox[3]]);

const xDistance = xmax[0] - xmin[0];
const yDistance = xmax[1] - xmin[1];

// compute the width of a single hex (gameSize), divided by 2 but multiply by 1.2 to

Check failure on line 74 in app/src/plugins/minesweeper/board.js

View workflow job for this annotation

GitHub Actions / deploy

Trailing spaces not allowed
// account for the below mentioned hexagon wider than taller
this.gameSize = Math.round(xDistance / this.size / 1.667);

// Adjust board dimensions based on actual distances
this.width = this.size;
this.height = Math.round(
Expand All @@ -78,8 +84,8 @@ export default class HexSweeperGame {
* 1.2,
);
// Read the GeoTIFF data into a 1-dimensional array
let data = (await tiff.readRasters({
bbox: options.locations[locationIndex],
let data = (await image.readRasters({
bbox: location.bbox,
width: this.width,
height: this.height,
resampleMethod: 'bilinear',
Expand All @@ -98,8 +104,8 @@ export default class HexSweeperGame {
}

data = flippedData;

const centerInLatLon = [this.locations[locationIndex][0], this.locations[locationIndex][1]];
// not actually center but left bottom corner
const centerInLatLon = [location.bbox[0], location.bbox[1]];
this.center = proj4(options.geotiff.projection, 'EPSG:3857', centerInLatLon);

console.log(`GeoTIFF size is ${this.width}x${this.height}`);
Expand All @@ -109,9 +115,14 @@ export default class HexSweeperGame {
const row = [];
for (let x = 0; x < this.width; x++) {
const value = data[y * this.width + x];

let isMine;
if (location.isMineCondition) {
isMine = location.isMineCondition(value);
} else {
isMine = Math.round(Math.random());
}
row.push({
isMine: value > 1500,
isMine,
adjacentMines: 0,
isRevealed: false,
isFlagged: false,
Expand Down

0 comments on commit 589383c

Please sign in to comment.