项目作者: leon-win

项目描述 :
Hexagonal geocoding system
高级语言: JavaScript
项目地址: git://github.com/leon-win/geohex.git
创建时间: 2020-06-07T22:22:28Z
项目社区:https://github.com/leon-win/geohex

开源协议:MIT License

下载


Geohex

npm
Code style: Prettier
License: MIT

Hexagonal geocoding system, library for converting geographic coordinates to hexagonal grid cells and vice versa.

This is ECMAScript 2015 fork of GeoHex library which was originally made by @sa2da.

Installation

  1. npm install geohex --save

Or grab from jsDelivr CDN:

  1. <script src="https://cdn.jsdelivr.net/npm/geohex@0.0.7/lib/geohex.min.js"></script>

Or from unpkg CDN:

  1. <script src="https://unpkg.com/geohex@0.0.7/lib/geohex.min.js"></script>

Usage

ES6 Modules

  1. import Geohex from "geohex";
  2. // or import { getCellByCode } from 'geohex'

CommonJS

  1. const Geohex = require("geohex");
  2. // or const { getCellByCode } = require('geohex')

JS modules:

  1. <script type="module">
  2. import Geohex from "geohex/src/index.js";
  3. // or import { getCellByCode } from 'geohex/src/index.js'
  4. </script>

Global variable

  1. <script src="geohex/lib/geohex.min.js"></script>

Examples

Online demo of Geohex usage

  1. // Get Geohex cell instance by code
  2. const geohexCell = Geohex.getCellByCode('QH3360')
  3. // Get Geohex cell instance by geographic coordinates and zoom level
  4. const geohexCell = Geohex.getCellByLocation(59.943201, 30.324086, 4)
  5. // Get Geohex cell instance by cell coordinates and zoomLevel
  6. const geohexCell = Geohex.getCellByXY(326, 203, 4)
  7. // Get Geohex cell coordinates by geographic coordinates and zoom level
  8. Geohex.getXYByLocation(59.943201, 30.324086, 4):
  9. // { x: 326, y: 203 }
  10. // Get Geohex cell coordinates by code
  11. Geohex.getXYByCode('QH3360')
  12. // { x: 326, y: 203 }

Geohex cell instance

Geohex cell instance is hexagon grid cell with properties and methods:

  1. console.log(JSON.stringify(geohexCell, null, 2))
  2. // {
  3. // "lat": 59.97788999458348,
  4. // "lon": 30.37037037037038,
  5. // "x": 326,
  6. // "y": 203,
  7. // "code": "QH3360"
  8. // }
  9. // Cell zoom level
  10. geohexCell.getZoomLevel()
  11. // 4
  12. // Cell side length in degrees
  13. geohexCell.getHexSize():
  14. // 9162.098006401464
  15. // Geographic coordinates of hexagon corners
  16. geohexCell.getHexCoords():
  17. // [
  18. // { lat: 59.97788999458348, lon: 30.205761316872437 },
  19. // { lat: 60.0491386517641, lon: 30.288065843621407 },
  20. // { lat: 60.0491386517641, lon: 30.45267489711935 },
  21. // { lat: 59.97788999458348, lon: 30.53497942386832 },
  22. // { lat: 59.90648768479527, lon: 30.45267489711935 },
  23. // { lat: 59.90648768479527, lon: 30.288065843621407 }
  24. // ]

Other implementations

License

MIT

© 2009 @sa2da (http://twitter.com/sa2da) http://www.geohex.org

© 2020 Leonid Vinogradov