1. 程式人生 > >python地理資料處理庫geopy

python地理資料處理庫geopy

python地理位置處理

python地理編碼地址以及用來處理經緯度的庫

GeoDjango – 世界級地理圖形 web 框架。
GeoIP – MaxMind GeoIP Legacy 資料庫的Python API。
geojson – GeoJSON 的 Python 繫結及工具。
geopy – Python 地址編碼工具箱。
pygeoip – 純 Python GeoIP API。
django-countries – 一個 Django應用程式,提供用於表格的國家選擇功能,國旗圖示靜態檔案以及模型中的國家欄位。

其它:

shapely,是geos的python封裝,而geos是jts的c++移植版本。

python地理資料處理相關程式碼

Python 地址編碼工具箱geopy

geopy is a Python 2 and 3 client for several popular geocoding web services.
geopy makes it easy for Python developers to locate the coordinates of addresses, cities, countries, and landmarks across the globe using third-party geocoders and other data sources.
geopy includes geocoder classes for the OpenStreetMap Nominatim, ESRI ArcGIS, Google Geocoding API (V3), Baidu Maps, Bing Maps API, Mapzen Search, Yandex, IGN France, GeoNames, NaviData, OpenMapQuest, What3Words, OpenCage, SmartyStreets, geocoder.us, and GeocodeFarm geocoder services. The various geocoder classes are located in geopy.geocoders.

安裝

pip install geopy

基本使用

Note: 函式引數都是緯度latitude在前,經度longitude在後

查詢地名轉換為具體地址和經緯度Geocoding

To geolocate a query to an address and coordinates:
>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> location = geolocator.geocode("175 5th Avenue NYC")
>>> print(location.address)
Flatiron Building, 175, 5th Avenue, Flatiron, New York, NYC, New York, ...
>>> print((location.latitude, location.longitude))
(40.7410861, -73.9896297241625)
>>> print(location.raw)
{'place_id': '9167009604', 'type': 'attraction', ...}
To find the address corresponding to a set of coordinates:

>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> location = geolocator.reverse("52.509669, 13.376294")
>>> print(location.address)
Potsdamer Platz, Mitte, Berlin, 10117, Deutschland, European Union
>>> print((location.latitude, location.longitude))
(52.5094982, 13.3765983)
>>> print(location.raw)
{'place_id': '654513', 'osm_type': 'node', ...}

計算兩地距離Measuring Distance

通過經緯度計算兩地距離的兩種方法。Geopy can calculate geodesic distance between two points using the Vincenty distance or great-circle distance formulas, with a default of Vincenty available as the class geopy.distance.distance, and the computed distance available as attributes (e.g., miles, meters,kilometers,etc.).

輸入資料型別可以是tuple, list, array...,元素型別可以是float, str...

Vincenty distance

>>> from geopy.distance import vincenty
>>> newport_ri = (41.49008, -71.312796)
>>> cleveland_oh = (41.499498, -81.695391)
>>> print(vincenty(newport_ri, cleveland_oh).miles)
538.3904451566326

Vincenty distance (vincenty) uses a more accurate ellipsoidal modelof the earth. This is the default distance formula, and is thus aliased asdistance.distance. There are multiple popular ellipsoidal models, andwhich one will be the most accurate depends on where your points are locatedon the earth. The default is the WGS-84 ellipsoid, which is the most globallyaccurate. geopy includes a few othermodels in the distance.ELLIPSOIDS dictionary:

              model             major (km)   minor (km)     flattening
ELLIPSOIDS = {'WGS-84':        (6378.137,    6356.7523142,  1 / 298.257223563),
              'GRS-80':        (6378.137,    6356.7523141,  1 / 298.257222101),
              'Airy (1830)':   (6377.563396, 6356.256909,   1 / 299.3249646),
              'Intl 1924':     (6378.388,    6356.911946,   1 / 297.0),
              'Clarke (1880)': (6378.249145, 6356.51486955, 1 / 293.465),
              'GRS-67':        (6378.1600,   6356.774719,   1 / 298.25),
              }

You can change the ellipsoid model used by the Vincenty formula like so:

>>> distance.vincenty(ne, cl, ellipsoid='GRS-80').miles

The above model name will automatically be retrieved from theELLIPSOIDS dictionary. Alternatively, you can specify the model valuesdirectly:

>>> distance.vincenty(ne, cl, ellipsoid=(6377., 6356., 1 / 297.)).miles

great-circle distance

>>> from geopy.distance import great_circle
>>> newport_ri = (41.49008, -71.312796)
>>> cleveland_oh = (41.499498, -81.695391)
>>> print(great_circle(newport_ri, cleveland_oh).miles)
537.1485284062816

兩種經緯度距離度量方法的區別

Vincenty's formulae are two related iterative methods used in geodesy to calculate the distance between two points on the surface of a spheroid, developed by Thaddeus Vincenty (1975a) They are based on the assumption that the figure of the Earth is an oblate spheroid(扁球形體), and hence are more accurate than methods such as great-circle distance which assume a spherical(球狀的) Earth.

計算出錯

TypeError: __new__() takes from 1 to 4 positional arguments but 5 were given

可能是某個座標的維度不對,如[[ ]]二維就不對,應該是[]或者()。

ValueError: Vincenty formula failed to converge!

vincenty演算法可以不收斂,可能是因為中間有一些三角函式的計算是通過迭代計算出來的,超過最大迭代次數就當作不收斂了。the basic iterative technique for the inverse method is just Vincenty (regular + his antipodal method); so it may fail to converge in some cases.

Examples where the solution of the inverse problem fails to converge:

lat1 lon1  lat2 lon2
     0.7   0   -0.3 179.7
     2.2   0   -1.8 179.6
     2.3   0   -2.0 179.6
def c():
from geopy import distance
    xs = [[0.7, 0], [2.2, 0], [2.3, 0], [12, 45]]
    ys = [[-0.3, 179.7], [-1.8, 179.6], [-2.0, 179.6], [25, 65]]
    for x, y in zip(xs, ys):
try:
d = distance.vincenty(x, y)
            print(d)
        except:
print('error')

設定很大的迭代次數也不會收斂:

d = distance.vincenty(x, y, iterations = 1000000)

解決:
It may be preferable to use  :class:`.great_circle`, which is marginally less accurate, but always produces a result.

ref: