npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

geohash-poly

v0.6.0

Published

Transform a GeoJSON Polygon or MultiPolygon to a list of geohashes that form it.

Downloads

2,256

Readme

Geohash-poly

npm install geohash-poly

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

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, including relevant hashes with respect to hashMode.

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({..., rowMode: true, ...}), 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({
	coords: polygon,
	precision: 7,
	rowMode: true
});

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({coords: polygon, precision: 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' ]

Also, integer geohashes are available.


geohashpoly({coords: polygon, precision: 34, integerMode: true}, function (err, hashes) {
    console.log(hashes);
});

Results in:

[ 5940702973,
  5940702975,
  5941052501,
  5940702972,
  5940702974,
  5941052500,
  5940702969,
  5940702971,
  5941052497,
  5940702968,
  5940702970,
  5941052496 ]

Options

  • coords: coordinate array for the geojson shape. required
  • precision: geohash precision (eg. "gfjf1" is a precision 5 geohash).
  • rowMode: allows for processing of geohashes by row.
  • hashMode: defines filtering of returned geohashes. See below.
  • integerMode: (true/false) Outputs integer versions of geohashes. Default precision for integer mode is 32 bits.

hashMode

The hashMode option can be used to specify which hashes to return. Defaults to 'inside'.

  • 'inside': return hashes whose center points fall inside the shape.
  • 'extent': return all hashes which make up the bounding box of the shape.
  • 'intersect': return all hashes that intersect with the shape. Use the 'threshold' option to specify a percentage of least coverage. See examples/streaming.js.

Command Line

You can also install as a command line util via

npm install geohash-poly -g

command line usage

Here are some examples of running it:

geohash-poly --csv=true --precision=9 --geocode=true  "[[[-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 ]]]"

Where additional command line switches are

  • --csv=true output csv. default output is ndjson
  • --geocode=true to add the lat and lon to the output

All of the standard options are supported as well.