Skip to content

Transform a geoJSON MultiPolygon or Polygon into a list of geohashes that form it.

License

Notifications You must be signed in to change notification settings

jakeorr/geohash-poly

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Geohash-poly

npm install geohash-poly

Transform a GeoJSON (Multi)Polygon to a list of geohashes that cover it.

Currently only includes hashes whose centroid falls within the poly itself. This does not create 100% coverage, but I could consider adding this if there is a need.

Method used is pretty brute-force, but still relatively quick compared to alternative implementations if hash precision is not too granular. Creates an envelope around poly, and iterates over rows and columns, only including hashes that fall in the poly.

Streaming

Hashes can be streamed. Each _read will generate a row of hashes into buffer, as some form of throttling. This allows massive polygons with high precision hashes to avoid memory constraint issues. If your polys have the potential to hit memory issues, use this method.

If you specify rowMode as true, such as .stream(poly, precision, rowMode), each chunk in the stream will be an array using streams2 objectMode.

var through2 = require('through2');

var polygon = [[[-122.350051, 47.702893 ], [-122.344774, 47.702877 ], [-122.344777, 47.70324 ], [-122.341982, 47.703234 ], [-122.341959, 47.701421 ], [-122.339749, 47.701416 ], [-122.339704, 47.69776 ], [-122.341913, 47.697797 ], [-122.341905, 47.697071 ], [-122.344576, 47.697084 ], [-122.344609, 47.697807 ], [-122.349999, 47.697822 ], [-122.350051, 47.702893 ]]];

var stream = geohashpoly.stream(polygon, 7);

stream
  .on('end', function () {
    console.log("It's all over.");
  })
  .pipe(through2(function (chunk, enc, callback) {
    console.log(chunk.toString());
    callback();
  }));

Results in the hashes spit out line by line to the console.

Standard

If you just want your hashes out in an array, use this.

var polygon = [[[-122.350051, 47.702893 ], [-122.344774, 47.702877 ], [-122.344777, 47.70324 ], [-122.341982, 47.703234 ], [-122.341959, 47.701421 ], [-122.339749, 47.701416 ], [-122.339704, 47.69776 ], [-122.341913, 47.697797 ], [-122.341905, 47.697071 ], [-122.344576, 47.697084 ], [-122.344609, 47.697807 ], [-122.349999, 47.697822 ], [-122.350051, 47.702893 ]]];

geohashpoly(polygon, 7, function (err, hashes) {
	console.log(hashes);
});

Results in:

[ 'c22zrgg', 'c22zrgu', 'c22zrgv', 'c22zrgy', 'c22zrgz', 'c23p25b', 'c22zrge', 'c22zrgs', 'c22zrgt', 'c22zrgw', 'c22zrgx', 'c23p258', 'c23p259', 'c23p25d', 'c22zrg7', 'c22zrgk', 'c22zrgm', 'c22zrgq', 'c22zrgr', 'c23p252', 'c23p253', 'c23p256', 'c22zrg5', 'c22zrgh', 'c22zrgj', 'c22zrgn', 'c22zrgp', 'c23p250', 'c23p251', 'c23p254' ]

Completely unscientific benchmarks

These are just from running my machine, running the streaming example in rowMode.

v0.2.1

  • Modifications: only performs an intersection on the poly if there are > n points in the polygon.
  • Runtime: 84.20s

v0.2.0

  • Split the operating polygon to only utilize its current row, defined by bounding box E/W and geohash N/S.
  • Runtime: 83.01s

About

Transform a geoJSON MultiPolygon or Polygon into a list of geohashes that form it.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%