-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgeohashToGeoJson.js
More file actions
33 lines (31 loc) · 880 Bytes
/
Copy pathgeohashToGeoJson.js
File metadata and controls
33 lines (31 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
let nodeGeohash = require('ngeohash')
let turf = require('@turf/turf')
const toGeoJson = (map) => {
let hashes = Object.keys(map);
let hashes_bbox = [];
hashes.forEach((hash) => {
let [minLat,minLong,maxLat,maxLong] = nodeGeohash.decode_bbox(hash)
hashes_bbox.push([
[minLong,minLat],
[maxLong,minLat,],
[maxLong,maxLat,],
[minLong,maxLat],
[minLong,minLat]
])
})
return turf.getGeom(turf.polygon(hashes_bbox))
}
const fromGeoJson = (geoJson) => {
if(geoJson.type != 'Polygon'){
throw Error('Geojson type must be a polygon!')
}
if(geoJson.coordinates.length == 0){
throw Error('Not a valid Polygon!')
}
console.log("geometry",geoJson.coordinates);
return (geoJson.coordinates)[0];
}
module.exports = {
toGeoJson,
fromGeoJson
}