Skip to content

Instantly share code, notes, and snippets.

@Yengas
Last active April 18, 2022 13:33
Show Gist options
  • Save Yengas/49c07708ff33b3f4e2158f3f9d6908b3 to your computer and use it in GitHub Desktop.
Save Yengas/49c07708ff33b3f4e2158f3f9d6908b3 to your computer and use it in GitHub Desktop.
uniformly distributed points inside a geojson feature / polygon

Description

This script generates points inside a given geojson feature / polygon using turf.

You can use geojson for visualization or creating your own geojson by hand.

Input

image

Output

image

import { readFileSync } from 'fs'
import { URL } from 'url'
import bbox from '@turf/bbox'
import pointGrid from '@turf/point-grid'
const FEATURE_FILE_PATH = new URL('./4-my-polygon.geojson', import.meta.url).pathname
const POINT_DISTANCE_IN_KM = 5;
const feature = JSON.parse(readFileSync(FEATURE_FILE_PATH, 'utf-8'))
const boundingBox = bbox(feature)
const points = pointGrid(boundingBox, POINT_DISTANCE_IN_KM, { unit: 'kilometers', mask: feature })
console.log(JSON.stringify(points))
{
"name": "geopoints",
"version": "1.0.0",
"type": "module",
"description": "calculate the geopoints inside the given polygon",
"main": "2-script.js",
"author": "Yengas",
"license": "ISC",
"dependencies": {
"@turf/bbox": "^6.5.0",
"@turf/point-grid": "^6.5.0"
}
}
Display the source blob
Display the rendered blob
Raw
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
29.11102294921875,
41.24167453726628
],
[
28.649597167968746,
41.35104125623227
],
[
28.3447265625,
41.03793062246529
],
[
28.6029052734375,
40.950862628132775
],
[
28.95172119140625,
40.950862628132775
],
[
29.10003662109375,
40.93322773476865
],
[
29.131622314453125,
40.88963898943264
],
[
29.3994140625,
40.72124187397379
],
[
29.56832885742187,
40.73893324113601
],
[
29.708404541015625,
41.16211393939692
],
[
29.11102294921875,
41.24167453726628
]
]
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment