UNPKG

3.63 kBMarkdownView Raw
1# Geohex
2
3[![npm](https://img.shields.io/npm/v/geohex?logo=npm&style=flat-square)](https://www.npmjs.com/package/geohex)
4[![Code style: Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?logo=prettier&style=flat-square)](https://prettier.io)
5[![License: MIT](https://img.shields.io/github/license/leon-win/geohex?style=flat-square)](http://opensource.org/licenses/MIT)
6
7Hexagonal geocoding system, library for converting geographic coordinates to hexagonal grid cells and vice versa.
8
9This is ECMAScript 2015 fork of [GeoHex library](http://geohex.net) which was originally made by [@sa2da](http://twitter.com/sa2da).
10
11## Installation
12
13```sh
14npm install geohex --save
15```
16
17Or grab from [jsDelivr CDN](https://www.jsdelivr.com/package/npm/geohex):
18
19```html
20<script src="https://cdn.jsdelivr.net/npm/geohex@0.0.6/lib/geohex.min.js"></script>
21```
22
23Or from [unpkg CDN](https://unpkg.com/geohex/):
24
25```html
26<script src="https://unpkg.com/geohex@0.0.6/lib/geohex.min.js"></script>
27```
28
29## Usage
30
31### ES6 Modules
32
33```js
34import Geohex from "geohex";
35// or import { getCellByCode } from 'geohex'
36```
37
38### CommonJS
39
40```js
41const Geohex = require("geohex");
42// or const { getCellByCode } = require('geohex')
43```
44
45### JS modules:
46
47```html
48<script type="module">
49 import Geohex from "geohex/src/index.js";
50 // or import { getCellByCode } from 'geohex/src/index.js'
51</script>
52```
53
54### Global variable
55
56```html
57<script src="geohex/lib/geohex.min.js"></script>
58```
59
60## Examples
61
62[Online demo of Geohex usage](https://leon-win.github.io/geohex-examples/)
63
64```javascript
65// Get Geohex cell instance by code
66const geohexCell = Geohex.getCellByCode('QH3360')
67
68// Get Geohex cell instance by geographic coordinates and zoom level
69const geohexCell = Geohex.getCellByLocation(59.943201, 30.324086, 4)
70
71// Get Geohex cell instance by cell coordinates and zoomLevel
72const geohexCell = Geohex.getCellByXY(326, 203, 4)
73
74// Get Geohex cell coordinates by geographic coordinates and zoom level
75Geohex.getXYByLocation(59.943201, 30.324086, 4):
76// { x: 326, y: 203 }
77
78// Get Geohex cell coordinates by code
79Geohex.getXYByCode('QH3360')
80// { x: 326, y: 203 }
81```
82
83### Geohex cell instance
84
85Geohex cell instance is hexagon grid cell with properties and methods:
86
87```javascript
88console.log(JSON.stringify(geohexCell, null, 2))
89// {
90// "lat": 59.97788999458348,
91// "lon": 30.37037037037038,
92// "x": 326,
93// "y": 203,
94// "code": "QH3360"
95// }
96
97// Cell zoom level
98geohexCell.getZoomLevel()
99// 4
100
101// Cell side length in degrees
102geohexCell.getHexSize():
103// 9162.098006401464
104
105// Geographic coordinates of hexagon corners
106geohexCell.getHexCoords():
107// [
108// { lat: 59.97788999458348, lon: 30.205761316872437 },
109// { lat: 60.0491386517641, lon: 30.288065843621407 },
110// { lat: 60.0491386517641, lon: 30.45267489711935 },
111// { lat: 59.97788999458348, lon: 30.53497942386832 },
112// { lat: 59.90648768479527, lon: 30.45267489711935 },
113// { lat: 59.90648768479527, lon: 30.288065843621407 }
114// ]
115```
116
117## Other implementations
118
119- [Original GeoHex library](http://www.geohex.org)
120- [GeoHex TypeScript implementation](https://github.com/uupaa/GeoHex)
121- [GeoHex Dart implementation](https://github.com/NiKoTron/geohex)
122- [GeoHex Swift implementation](https://github.com/nekowen/GeoHex3.swift)
123- [GeoHex Scala implementation](https://github.com/teralytics/geohex)
124- [GeoHex Java implementation](https://github.com/chsh/geohex4j)
125- [GeoHex C# implementation](https://github.com/mattak/GeoHex.cs)
126
127## License
128
129[MIT](http://opensource.org/licenses/MIT)
130
131© 2009 @sa2da (http://twitter.com/sa2da) http://www.geohex.org
132
133© 2020 Leonid Vinogradov