跳转到主要内容

npm geogrids 库的 Python 实现 - 用于处理全球离散地理网格 (GDGGs) 的工具

项目描述

Latest PyPI version

这是 Iván Sánchez Ortega 开发的 npm geogrids 库的 Python 实现 - 用于处理全球离散地理网格 (GDGGs) 的工具。

此模块包含一个 Location 对象,可用于生成哈希值或从哈希值生成位置,以及一个编码器模块,可以将代码转换为一个(希望)有用的文本字符串。

此代码使用原始库的默认编码器编写,并且可以轻松扩展以使用您选择的文本集。

用法

库有两个组件

表示位置

给定一些位置,例如经纬度 -35.6498, 150.2935,您可以轻松地创建一个简单的字符串或数值哈希值

>>> latitude = -35.6498
>>> longitude = 150.2935
>>> import geogrids
>>> geogrids.gdgg.latitude_longitude_to_readable_hash(latitude=latitude, longitude=longitude)
'702020210311'
>>> geogrids.gdgg.latitude_longitude_to_numeric_hash(latitude=latitude, longitude=longitude)
12108871

当然,您也可以反向操作

>>> geogrids.gdgg.numeric_hash_to_latitude_longitude(12108871)
(-35.65283203125, 150.2789682218808)
>>> geogrids.gdgg.readable_hash_to_latitude_longitude('702020210311')
(-35.65283203125, 150.2789682218808)

请注意,哈希值是位置的近似值,取决于精度级别 - 精度越高,准确性越好

>>> numeric_hash = geogrids.gdgg.latitude_longitude_to_numeric_hash(latitude=latitude, longitude=longitude, precision=55)
>>> geogrids.gdgg.numeric_hash_to_latitude_longitude(numeric_hash, precision=55)
(-35.64979965984821, 150.2934998246466)

实际上,这些哈希值定义了一个三角形区域内的位置,您可以从 numeric_hash_to_areareadable_hash_to_area 函数中检索它,这两个函数返回一个包含 Location 对象的集合(通常是三角形区域的三个顶点,但在极地附近为了简化,默认返回一个矩形)

>>> vertices = geogrids.gdgg.numeric_hash_to_area(numeric_hash)
>>> vertices
[<Location [702020210311]>, <Location [702020210311]>, <Location [702020210311]>]
>>> vertices[0].latitude, vertices[0].longitude
(-35.63964843750004, 150.24252223120465)

通常建议只使用散列值和经纬度,但位置对象实现了__geo_feature__接口,这意味着您可以使用它与其他支持此接口的库进行更复杂的几何操作,例如通过Shapely库。

>>> from shapely import geometry
>>> points = [geometry.shape(vertex) for vertex in vertices]
>>> line = geometry.LineString(points)
>>> line.length
0.11570586750499379
>>> polygon = geometry.Polygon(line)
>>> polygon.area
0.0015986572857657128

散列值的编码和解码

编码器允许您将散列值转换为易于记忆的字符串,然后再转换回来。开箱即用,它包含一些编码器

给定一个位置的数值散列(见上文),这些编码器很容易使用

>>> geogrids.encoders.cheeses.hash_to_string(numeric_hash, precision=25)
'Dubliner Requeijão Provolone Telemea'
>>> geogrids.encoders.cheeses.hash_to_string(numeric_hash, precision=55)
'Dubliner Requeijão Provolone Telemea Danablu Coulommiers Chevrotin'

或者给定可读的编码,简单地返回另一个方向

>>> numeric_hash, precision = geogrids.encoders.cheeses.string_to_hash('Dubliner Requeijão Provolone Telemea')
>>> numeric_hash, precision
(3870868551, 32)
>>> geogrids.gdgg.numeric_hash_to_latitude_longitude(numeric_hash, precision)
(-35.647064208984375, 150.2948563112389)

如果您不想使用内置的编码器,您可以轻松地生成自己的编码器

>>> wordlist = list('😀😎🤬😱😈👍🖖⚽🐶🐍🐡🦜🍀🌞🌚🔥')

注意单词列表的长度应该是2的幂 - 用于计算精度的级别四舍五入到最接近的2的幂 - 任何超过这个数字的单词都将被跳过。

>>> emoji_encoder = geogrids.encoders.Encoder(wordlist, separator='')
>>> emoji_encoder.hash_to_string(numeric_hash, precision)
'⚽😈😈🍀🐶🦜🖖🌚'

警告 与编码器相关的一个关键考虑因素:如果您创建了一个编码并与他人分享,单词列表必须完全相同!否则,在解码时,您将得到完全不同的结果!

>>> numeric_hash, precision = emoji_encoder.string_to_hash('⚽😈😈🍀🐶🦜🖖🌚')
>>> geogrids.gdgg.numeric_hash_to_latitude_longitude(numeric_hash, precision)
(-35.647064208984375, 150.2948563112389)

安装

pip install geogrids

需求

geogrids没有第三方库的需求

兼容性

Python 3.5+

许可

这是在Do What The Fuck You Want Public License下许可的,就像原始的JS实现一样。所以尽情享受吧!

作者

geogrids是用Python编写的,由Henry Walshaw编写,由Iván Sánchez Ortega从npm geogrids库翻译而来

ducks编码器由Adam Steer贡献

项目详情


下载文件

下载适用于您的平台文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分发

geogrids-1.1.0.tar.gz (22.7 kB 查看散列值)

上传时间 源代码

构建分发

geogrids-1.1.0-py3-none-any.whl (21.1 kB 查看散列值)

上传时间 Python 3

由以下支持