Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Last active April 28, 2019 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasG77/a93c1b3b98c688b3fa5c4fea804b3ea1 to your computer and use it in GitHub Desktop.
Save ThomasG77/a93c1b3b98c688b3fa5c4fea804b3ea1 to your computer and use it in GitHub Desktop.
Generate H3 hexagons
node_modules/
package-lock.json
*.geojson
*.gpkg

Generate some H3 polygons

You need Node installed on your computer

Clone the repo then cd into it

Then do:

npm install
npm run generate # Run command in package.json file "node --max-old-space-size=8192 index.js" where 8192 = RAM. You may change it depending of your configuration
const geojson2h3 = require('geojson2h3');
const fs = require('fs');
const polygonEurope = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-11.953125,
31.353636941500987
],
[
42.890625,
31.353636941500987
],
[
42.890625,
72.18180355624855
],
[
-11.953125,
72.18180355624855
],
[
-11.953125,
31.353636941500987
]
]
]
}
};
for (let level = 0; level < 6; level++) {
const hexagons = geojson2h3.featureToH3Set(polygonEurope, level);
const featuresCollection = geojson2h3.h3SetToFeatureCollection(hexagons);
fs.writeFileSync(`./europe-${level}.geojson`, JSON.stringify(featuresCollection));
}
const polygonFrance = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-5.185546875,
41.11246878918088
],
[
9.8876953125,
41.11246878918088
],
[
9.8876953125,
51.34433866059924
],
[
-5.185546875,
51.34433866059924
],
[
-5.185546875,
41.11246878918088
]
]
]
}
};
for (let level = 6; level < 9; level++) {
const hexagons = geojson2h3.featureToH3Set(polygonFrance, level);
const featuresCollection = geojson2h3.h3SetToFeatureCollection(hexagons);
fs.writeFileSync(`./france-${level}.geojson`, JSON.stringify(featuresCollection));
}
const polygonPaysDeLaLoire = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-2.867431640625,
46.210249600187225
],
[
0.9777832031250001,
46.210249600187225
],
[
0.9777832031250001,
48.60385760823255
],
[
-2.867431640625,
48.60385760823255
],
[
-2.867431640625,
46.210249600187225
]
]
]
}
};
for (let level = 9; level < 10; level++) {
const hexagons = geojson2h3.featureToH3Set(polygonPaysDeLaLoire, level);
const featuresCollection = geojson2h3.h3SetToFeatureCollection(hexagons);
fs.writeFileSync(`./pays-de-la-loire-${level}.geojson`, JSON.stringify(featuresCollection));
}
const polygonNantes = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-1.6555023193359375,
47.177112073280966
],
[
-1.4756011962890625,
47.177112073280966
],
[
-1.4756011962890625,
47.29692789291742
],
[
-1.6555023193359375,
47.29692789291742
],
[
-1.6555023193359375,
47.177112073280966
]
]
]
}
};
for (let level = 10; level < 13; level++) {
const hexagons = geojson2h3.featureToH3Set(polygonNantes, level);
const featuresCollection = geojson2h3.h3SetToFeatureCollection(hexagons);
fs.writeFileSync(`./nantes-${level}.geojson`, JSON.stringify(featuresCollection));
}
{
"name": "generate-h3",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"generate": "node --max-old-space-size=8192 index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"geojson2h3": "^1.0.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment