# shape2geohash

> Turns any GeoJSON shape into a list of geohashes

Latest version **1.2.6** (published 2022-06-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install shape2geohash
pnpm add shape2geohash
yarn add shape2geohash
bun add shape2geohash
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.6 |
| Published | 2022-06-07 |
| First published | 2019-05-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 11 |
| Unpacked size | 540.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Marcel Reppenhagen |
| Maintainers | marcelreppi |
| Keywords | geohash, shape, polygon, line, polygon2geohash, line2geohash, geojson, point, point2geohash |

## Links

- npm: https://www.npmjs.com/package/shape2geohash
- Repository: https://github.com/marcelreppi/shape2geohash
- Homepage: https://github.com/marcelreppi/shape2geohash#readme
- Issues: https://github.com/marcelreppi/shape2geohash/issues
- npm.io page: https://npm.io/package/shape2geohash

## Dependencies (11)

- [ngeohash](https://npm.io/package/ngeohash.md) ^0.6.3
- [@turf/area](https://npm.io/package/@turf/area.md) ^6.5.0
- [@turf/bbox](https://npm.io/package/@turf/bbox.md) ^6.5.0
- [@turf/helpers](https://npm.io/package/@turf/helpers.md) ^6.5.0
- [@turf/envelope](https://npm.io/package/@turf/envelope.md) ^6.5.0
- [@turf/intersect](https://npm.io/package/@turf/intersect.md) ^6.5.0
- [@turf/line-split](https://npm.io/package/@turf/line-split.md) ^6.5.0
- [@turf/bbox-polygon](https://npm.io/package/@turf/bbox-polygon.md) ^6.5.0
- [@turf/boolean-within](https://npm.io/package/@turf/boolean-within.md) ^6.5.0
- [@turf/boolean-overlap](https://npm.io/package/@turf/boolean-overlap.md) ^6.5.0
- [@turf/boolean-point-in-polygon](https://npm.io/package/@turf/boolean-point-in-polygon.md) ^6.5.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.2.6 (latest) — 2022-06-07
- 1.2.5 — 2020-03-27
- 1.2.4 — 2019-12-29
- 1.2.3 — 2019-07-24
- 1.2.2 — 2019-07-24
- 1.2.1 — 2019-07-23
- 1.2.0 — 2019-07-23
- 1.1.1 — 2019-05-25
- 1.1.0 — 2019-05-23
- 1.0.3 — 2019-05-19
- 1.0.2 — 2019-05-19

## README

[![codecov](https://codecov.io/gh/marcelreppi/shape2geohash/branch/master/graph/badge.svg)](https://codecov.io/gh/marcelreppi/shape2geohash)
[![npm version](https://badge.fury.io/js/shape2geohash.svg)](https://badge.fury.io/js/shape2geohash)
[![Build Status](https://travis-ci.org/marcelreppi/shape2geohash.svg?branch=master)](https://travis-ci.org/marcelreppi/shape2geohash)

# shape2geohash

A small library that turns **any GeoJSON shape** into a list of geohashes.


## Installation

```
npm install shape2geohash
```

## Usage

```js
const shape2geohash = require("shape2geohash")

// Providing polygon as GeoJSON
const geohashes1 = await shape2geohash({
  type: "Polygon",
  coordinates: [
    [
      [13.0, 52.5], //[long, lat]
      [13.3, 52.5],
      [13.3, 52.2],
      [13.0, 52.2],
      [13.0, 52.5], // make sure the last coordinate is equal to the first one
    ]
  ]
})

// returns ["u336xp", etc.]


// Providing polygon as an array of coordinates
const geohashes2 = await shape2geohash([
  [
    [13.0, 52.5], //[long, lat]
    [13.3, 52.5],
    [13.3, 52.2],
    [13.0, 52.2],
    [13.0, 52.5], // make sure the last coordinate is equal to the first one
  ]
])

// returns ["u336xp", etc.]
```

### shape2geohash(geoJSON, options)

* `geoJSON` can be any of these things:
  * Any GeoJSON object of the following type:
    * `FeatureCollection`
    * `Feature`
    * `Polygon`
    * `MultiPolygon`
    * `LineString`
    * `MultiLineString`
    * `Point`
    * `MultiPoint`
  * A single polygon as a simple array of coordinates
  * An array of polygons

Returns a promise that resolves to an array of geohashes that intersect with the given shape(s)

### Options

```js
const defaultOptions = {
  precision: 6,
  hashMode: "intersect",
  minIntersect: 0,
  allowDuplicates: true,
  customWriter: null
}
```

* `precision`: Length of the returned geohashes. Also known as geohash level (Level 6 geohash = u336dc)
* `hashMode`: Determines what kind of hashes are being included. Available modes are:
  * `intersect`: Includes all geohashes that intersect with the shape.
  * `envelope`: Includes all geohashes that are inside the rectangular border of the shape.
  * `insideOnly`: Includes only the geohashes that are fully within the polygon.
  * `border`: Includes only the geohashes that intersect with the border of the polygon.
* `minIntersect`: Percentage value between `0` and `1`. Defines the minimum area of a geohash that needs to be covered by the polygon to be included in the geohash list. This is only relevant for the edge of the polygon when using the hashMode `intersect`.
* `allowDuplicates`: Determines if the output array may contain duplicate geohashes. These can occur when, for example, multiple polygons overlap.
* `customWriter`: Custom `Writable` Stream that can used for custom stream processing. See [Custom Stream Processing](#custom-stream-processing) section for more details.

## Custom Stream Processing

This package uses Node.js Streams to process the incoming shape row-wise from top to bottom. You can substitute the internally used `Writable` Stream with your custom `Writable` Stream. This may be useful if you want to process extremely large polygons. 

You need to implement the `write` method to receive the data. The data passed into the `write` method will be all geohashes that are in the current row from top to bottom. 

Since the incoming data is always an array you MUST enable `objectMode`!

```js
const Stream = require("stream")

const myGeohashes = []
const myCustomWriter = new Stream.Writable({
  objectMode: true, // THIS IS IMPORTANT
  write: (rowGeohashes, enc, callback) => {
    // rowGeohashes = ["u336xp", ...]
    // Do some processing with the incoming geohashes per row
    myGeohashes.push(...rowGeohashes)
    callback()
  },
})

shape2geohash(polygon, { 
  customWriter: myCustomWriter,
  // ...other options
})
```

## Testing

run `npm test`

Afterwards you can open the `index.html` in `test/visualization` to see a visualization of the test.

---
_Source: https://npm.io/package/shape2geohash · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
