Skip to content

Commit 44d4bee

Browse files
committed
Create a fiboa-0.3 hcat-enabled crop map
1 parent ec5c145 commit 44d4bee

8 files changed

Lines changed: 113 additions & 19 deletions

File tree

code/bg/bg_arable.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
original_name
2+
Arable Land
3+
Greenhouses
4+
Mixed Land Use
5+
Rice fields

map/crop/CropLegendControl.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export class CropLegendControl extends Control {
5959
const area = feature.properties_["area"] || toGeometry(feature).getArea();
6060
const crop = feature.properties_[this.attribute];
6161
const name = mapping[crop]?.name;
62+
if (!name) return;
6263
if (!byName[name]) byName[name] = mapping[crop];
6364
count++;
6465
totalArea += area;
@@ -124,7 +125,7 @@ export class CropLegendControl extends Control {
124125
}
125126
let levels = "";
126127
if (this.mapping.length) {
127-
levels = `<span style="font-weight: normal">Levels: </span>`;
128+
levels = `<span style="font-weight: normal">Level: </span>`;
128129
for (let i = 0; i < this.mapping.length; i++) {
129130
levels += `<button class="legend-level${i===this.level?" active":""}">${i}</button>`;
130131
}

map/crop/crop.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import collections from '../sources'
1+
import collections from './sources'
22
import {FiboaMap} from "../map";
33
import {hcat} from "./codes";
44
import VectorTile from "ol/layer/VectorTile";
55
import {PMTilesVectorSource} from "ol-pmtiles";
6-
const cropExtension = "https://fiboa.github.io/hcat-extension/v0.1.0/schema.yaml";
6+
const hcatExtension = "https://fiboa.org/hcat-extension/v0.3.0/schema.yaml"
77
import {CropLegendControl} from "./CropLegendControl";
88

9-
const CROP_ATTRIBUTE = "ec:hcat_code";
9+
const CROP_ATTRIBUTE = "hcat:code";
1010
const fieldStyle = {
1111
"stroke-color": 'rgb(88, 88, 88, 88)',
1212
"stroke-width": 0.5,
@@ -37,6 +37,6 @@ class CropMap extends FiboaMap {
3737
}
3838
}
3939

40-
const cropCollections = collections.filter(c => c.fiboa_extensions?.includes(cropExtension));
40+
const cropCollections = collections.filter(c => c.extensions?.includes(hcatExtension));
4141
const m = new CropMap();
4242
m.addCollections(cropCollections);

map/crop/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8" />
55
<link rel="icon" type="image/x-icon" href="https://openlayers.org/favicon.ico" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<title>Map of Fiboa HCAT datasets</title>
7+
<title>Map of Fiboa-0.3 HCAT datasets</title>
88
</head>
99
<body>
1010
<h1>

map/package-lock.json

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

map/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"vite": "^6.2.4"
1212
},
1313
"dependencies": {
14+
"fast-xml-parser": "^5.3.2",
1415
"lodash.debounce": "^4.0.8",
1516
"ol": "^10.0.0",
1617
"ol-ext": "^4.0.22",

map/prepare.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const fs = require('fs');
2-
const crypto = require('crypto');
32

43
const data = fs.readFileSync('../stac/catalog.json', 'utf8');
54
const json = JSON.parse(data);
@@ -43,8 +42,7 @@ Promise.allSettled(promises)
4342
if (parquet && parquet['table:row_count'] > 0) {
4443
data.count = parquet['table:row_count'];
4544
}
46-
}
47-
else if (c.count) {
45+
} else if (c.count) {
4846
data.count = c.count;
4947
}
5048

@@ -53,13 +51,3 @@ Promise.allSettled(promises)
5351
fs.writeFileSync('sources.js', `export default ${JSON.stringify(results, null, 2)}`);
5452
})
5553
.catch(console.error);
56-
57-
function strToColor(str) {
58-
return '#' + crypto.createHash('md5').update(str).digest('hex').substring(0, 6);
59-
}
60-
const hcat = fs.readFileSync('../code/hcat/HCAT3.csv', 'utf8');
61-
const hcatMapping = hcat.split('\n').slice(1).map((line) => {
62-
const [name, code] = line.split(',');
63-
return { name, code, color: strToColor(code || "") };
64-
});
65-
fs.writeFileSync('crop/codes.js', `export const hcat = ${JSON.stringify(hcatMapping, null, 2)}`);

map/prepare_crop.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const fs = require('fs');
2+
const crypto = require('crypto');
3+
const { XMLParser } = require("fast-xml-parser")
4+
5+
const base = "https://data.source.coop/fiboa/";
6+
const promise = fetch(`${base}?list-type=2&prefix=data`)
7+
.then(response => response.text())
8+
.then(text => new XMLParser().parse(text))
9+
.then(data => data.ListBucketResult.Contents
10+
.filter(item => item.Key.includes("collection.json"))
11+
.map(async (item) => {
12+
const url = new URL(item.Key, base);
13+
try {
14+
console.log("Fetching " + url);
15+
const response = await fetch(url);
16+
const collection = await response.json();
17+
collection.source = url.href;
18+
return collection;
19+
} catch (error) {
20+
console.error(`Failed to load ${url}: ${error}`);
21+
}
22+
}));
23+
24+
promise.then(
25+
promises => Promise.allSettled(promises)
26+
.then(results => {
27+
results = results
28+
.filter(p => p.status === 'fulfilled' && p.value)
29+
.map(p => {
30+
const c = p.value;
31+
const data = {
32+
id: c.id,
33+
title: (c.title || "").replace('Field boundaries for ', ''),
34+
attribution: c.attribution,
35+
bbox: c.extent.spatial.bbox,
36+
source: c.source,
37+
extensions: c.vecorel_extensions?.[c.id],
38+
};
39+
40+
const pmtiles = c.links.find(l => l.rel === 'pmtiles');
41+
if (pmtiles) {
42+
data.pmtiles = pmtiles.href;
43+
}
44+
45+
if (c.assets) {
46+
const parquet = Object.values(c.assets).find(a => a.type === 'application/vnd.apache.parquet');
47+
if (parquet && parquet['table:row_count'] > 0) {
48+
data.count = parquet['table:row_count'];
49+
}
50+
} else if (c.count) {
51+
data.count = c.count;
52+
}
53+
54+
return data;
55+
});
56+
fs.writeFileSync('crop/sources.js', `export default ${JSON.stringify(results, null, 2)}`);
57+
})
58+
.catch(console.error));
59+
60+
function strToColor(str) {
61+
return '#' + crypto.createHash('md5').update(str).digest('hex').substring(0, 6);
62+
}
63+
const hcat = fs.readFileSync('../code/hcat/HCAT3.csv', 'utf8');
64+
const hcatMapping = hcat.split('\n').slice(1).map((line) => {
65+
const [name, code] = line.split(',');
66+
return { name, code, color: strToColor(code || "") };
67+
});
68+
fs.writeFileSync('crop/codes.js', `export const hcat = ${JSON.stringify(hcatMapping, null, 2)}`);

0 commit comments

Comments
 (0)