A data compression and Geo-hasing library for large sized Geo-polygons. It solves the problem when standard point-in-polygon query takes too much time and conversion of Geo-polygon into Geohashes for constant time point-in-polygon query, takes large space in memory. This library provides a memory effiecent soltution for constant time point-in-polygon query by converting the hashes to lowest possible order.
npm install geohash-compress
- Clone this repo
https://github.com/raghav1408/geohash-compress.git
- Install NPM packages
npm install
- Run tests
npm run test
const geoHashCompress = require('geohash-compress');
// Geofence is array of {long,lat} of the geofence.
(async()=>{
let geofence = [[
[75.4375024, 22.8725924],
[75.4401784, 22.9105034],
[75.4562348, 22.9185316],
[75.4620329, 22.9287898],
[75.4375024, 22.8725924],
]]
//construct a new polygon from the geofence
// maximum hash length in the output = 7(default = 7)
// minimum hash length(if possible) in the output = 1(default = 1)
const polygon = await new geoHashCompress(geofence,7,1);
// compress the polygon and returns a map with geohash as key eg: {tsj8p6n:true}
let hashes = polygon.compress();
// prints object {...,tsj8:true,tsj8p6n:true,tsj8p6o:true,tsj8p6p:true,...}
console.log(hashes)
//polygon.insideOrOutside(long,lat)
//return true/false if point{long,lat} is inside/outside polygon.
console.log(polygon.insideOrOutside(75.8814993,22.7418224))
// returns compressed geometry as Geojson.
const geojson = polygon.toGeoJson();
})()
Intial data size of polygon : 34.8 MB
Final data size of polygon : 1.2 MB
100 point-in-polygon query without compression: 3ms
100 point-in-polygon query with compression: 4ms
Distributed under the Apache 2.0 License. See LICENSE
for more information.