跳转到主要内容

表示地理坐标的方法

项目描述

LatLon

许可/分叉信息

Copyright (c) 2014-2015 Gen Del Raye
Copyright (c) 2015 Ryan Vennell

This is a derivative, forked from the original work by:
Gen Del Raye <gdelraye@hawaii.edu> and located at:
https://pypi.python.org/pypi/LatLon

Licensed under the GPLv3: https://gnu.ac.cn/licenses/gpl-3.0.html

The purpose of this fork is to provide full Python3 (and Python2)
support in light of the fact that the original work has no public
repository which can be contributed to or traditionally forked.

特性

表示地理坐标(纬度和经度)的方法,包括以下功能:

  • 将来自几乎任何格式的纬/经字符串转换为 LatLon 对象(类似于datetime库的 stptime 方法)

  • 自动将十进制度、十进制分和度、分、秒信息存储在 LatLon 对象中

  • 将纬/经信息输出到格式化的字符串(类似于datetime库的 strftime 方法)

  • 将项目纬/经坐标投影到某些其他proj投影

  • 使用FAI或WGS84近似值计算纬/经对之间的距离和航向

  • 通过偏移初始坐标和距离和航向来创建一个新的 LatLon 对象

  • 从另一个 LatLon 对象减去一个 LatLon 对象将创建一个具有距离和航向属性的 GeoVector 对象(类似于datetime库的 timedelta 对象)

  • Latlon 对象和 GeoVector 对象相加或相减将创建一个新的 LatLon 对象,其坐标根据 GeoVector 对象的距离和航向进行调整

  • GeoVector 对象可以相加、相减、相乘或相除

安装

LatLon 仅在Python 2.7中进行了测试

通过pip安装

$ pip install LatLon23

通过pip安装

$ pip3 install LatLon23

需要以下非标准库

  • pyproj

使用说明

LatLon 的使用主要是通过类 LatLon,该类用于存储一对单独的 纬度经度 对象。可以使用 string2latlon 方法将字符串转换为 LatLon 对象,使用 string2geocoord 方法将字符串转换为 LatitudeLongitude 对象。或者,可以通过相减两个 LatLon 对象,或者加上或减去一个 Latlon 对象和一个 GeoVector 对象来构造一个 LatLon 对象。

纬度或经度构造

纬度和经度的构造分别通过类 LatitudeLongitude。您可以以十进制度数、度分或度分秒的任意组合传递纬度或经度坐标。或者,您可以使用 string2geocoord 函数传递一个包含单个纬度或经度的格式化字符串,或者使用 string2latlon 函数传递代表纬度和经度的字符串对。

字符串格式化

string2latlonstring2geocoord 都接受一个 formatter 字符串,该字符串类似于 datetimestrftime 函数中使用的 format 关键字。指示字符(例如 HD)放置在特定的分隔字符(%)之间,以指定坐标字符串的格式化方式。可能的值如下:

*H* is a hemisphere identifier (e.g. N, S, E or W)

*D* is a coordinate in decimal degrees notation (e.g. 5.833)

*d* is a coordinate in degrees notation (e.g. 5)

*M* is a coordinate in decimal minutes notation (e.g. 54.35)

*m* is a coordinate in minutes notation (e.g. 54)

*S* is a coordinate in seconds notation (e.g. 28.93)

Any other characters (e.g. ' ' or ', ') will be treated as a separator between the above components.

All components should be separated by the *%* character. For example, if the coord_str is '5, 52,
59.88_N', the format_str would be 'd%, %m%, %S%_%H'

重要

目前不适用的一种格式是,半球标识符和度或十进制度之间没有任何字符分隔。例如‘5 52 59.88 N’是有效的,而‘5 52 59.88N’则不是。

字符串输出

LatLon 以及 LatitudeLongitude 对象都包含一个 to_string() 方法,用于输出格式化的坐标。

投影

使用 LatLon.project 将地理坐标转换为所选投影。需要您传递一个 pyprojbasemap 投影。

距离和航向计算

LatLon 对象有一个 distance() 方法,该方法接受一个作为参数的第二个 LatLon 对象。distance() 将默认使用 WGS84 椭球体计算两个坐标之间的大圆距离。要使用更近似的 FAI 球体,请将 ellipse 设置为‘sphere’。可以使用类似的方式使用 heading_initial()heading_reverse() 方法计算初始航向和反向航向(以度为单位)。或者,通过从一个 LatLon 对象减去另一个 LatLon 对象,将返回一个具有航向和距离属性的 GeoVector 对象。

通过从另一个对象偏移创建新的 LatLon 对象

使用 LatLon 对象的 offset() 方法,该方法接受一个初始航向(以度为单位)和距离(以公里为单位),以返回一个新的 LatLon 对象,该对象位于偏移坐标处。您还可以通过添加或减去一个 LatLon 对象和一个 GeoVector 对象来执行相同的操作。

示例

从坐标创建 LatLon 对象

>> palmyra = LatLon(Latitude(5.8833), Longitude(-162.0833)) # Location of Palmyra Atoll in decimal degrees
>> palmyra = LatLon(5.8833, -162.0833) # Same thing but simpler!
>> palmyra = LatLon(Latitude(degree = 5, minute = 52, second = 59.88),
>>                  Longitude(degree = -162, minute = -4.998) # or more complicated!
>> print palmyra.to_string('d% %m% %S% %H') # Print coordinates to degree minute second
('5 52 59.88 N', '162 4 59.88 W')

从格式化字符串创建 Latlon 对象

>> palmyra = string2latlon('5 52 59.88 N', '162 4 59.88 W', 'd% %m% %S% %H')
>> print palmyra.to_string('d%_%M') # Print coordinates as degree minutes separated by underscore
('5_52.998', '-162_4.998')

执行一些计算

>> palmyra = LatLon(Latitude(5.8833), Longitude(-162.0833)) # Location of Palmyra Atoll
>> honolulu = LatLon(Latitude(21.3), Longitude(-157.8167)) # Location of Honolulu, HI
>> distance = palmyra.distance(honolulu) # WGS84 distance in km
>> print distance
1766.69130376
>> print palmyra.distance(honolulu, ellipse = 'sphere') # FAI distance in km
1774.77188181
>> initial_heading = palmyra.heading_initial(honolulu) # Heading from Palmyra to Honolulu on WGS84 ellipsoid
>> print initial_heading
14.6907922022
>> hnl = palmyra.offset(initial_heading, distance) # Reconstruct Honolulu based on offset from Palmyra
>> print hnl.to_string('D') # Coordinates of Honolulu
('21.3', '-157.8167')

使用 GeoVectors 操作 LatLon 对象

>> vector = (honolulu - palmyra) * 2 # A GeoVector with 2x the magnitude of a vector from palmyra to honolulu
>> print vector # Print heading and magnitude
14.6907922022 3533.38260751
print palmyra + (vector/2.0) # Recreate the coordinates of Honolulu by adding half of vector to palmyra
21.3, -157.8167

版本

变更日志

1.0.7(2015年3月29日)

  • 从原始工作分支:[链接](https://pypi.python.org/pypi/LatLon)

  • 增加了 Python3 支持,并对代码进行了一些重构

  • 更新了 Readme 以纠正问题并提供适当的归属

  • 添加 MANIFEST.in

项目详情


下载文件

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

源分布

LatLon23-1.0.7.tar.gz (25.1 kB 查看哈希值)

上传时间: 源代码

支持