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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment