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.
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.
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" | |
} | |
} |
{ | |
"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 | |
] | |
] | |
] | |
} | |
} |