一个用于学术联系字符串的快速地理工具包
PinPoint的Python项目详细描述
精确定位
Pinpoint是一个快速的地理工具包,用于学术联系字符串。 它提供以下基本功能:
- 查找位置(有关地图上的城市和国家的信息)
- 计算加权从属字符串列表的外观位置和协作距离
安装
使用pip安装和更新
pip install pinpoint
用法
frompinpointimportLocatorloc=Locator()
第一次初始化Locator
时,需要创建查找表和数据库。
对于这四个文件,从GeoNamesdump(~150mb)下载并优化:
- cities1000.zip
- admin1CodesASCII.txt
- countryinfo.txt
- alternatenames.zip
以后可以重建数据库:
frompinpointimportLocatorloc=Locator(refresh=True)
如果缓存的文件小于一周,则不会从GeoNames再次下载数据,以避免服务器上出现不必要的负载。 根据操作系统的不同,数据库和缓存文件存储在相应的文件夹中。 如果有必要,你可以用手把它们倒空。
frompinpointimportLocatorprint(Locator.resources_dir)print(Locator.resources_cache_dir)
查找位置
test_string="Department of Chemical and Biomolecular Engineering, Rice University, Houston, TX, United States"country,region,city=loc.find(test_string)
此函数为每个国家、地区和城市返回dict()
或None
。
根据GeoNames中的数据返回以下信息:
- 县
'a2'
ISO 3166-1α-2国家代码'a3'
ISO 3166-1α-3国家代码'n3'
ISO 3166-1数字国家代码'name'
'short_name_list'
短名称变体'name_list'
不同语言的名称'capital'
'continent'
'area'
平方公里'population'
'geonameid'
唯一id由GeoNames
- 地区(目前仅用于美国和加拿大)
'name'
'short_name_list'
短名称变体'name_list'
不同语言的名称'region_code'
'a2'
ISO 3166-1α-2国家代码'geonameid'
唯一id由GeoNames
- 城市
'name'
'asciiname'
'name_list'
不同语言的名称'latitude'
'longitude'
'a2'
ISO 3166-1α-2国家代码'admin1_code'
'elevation'
和'dem'
与海拔(米)有关'timezone'
'geonameid'
唯一id由GeoNames
计算视在位置和合作距离
根据加权的隶属关系列表,可以计算出科学文档的明显位置。
frompinpointimportLocatorloc=Locator()weighted_affiliations={"Dresden Center for Computational Material Science, Technische Universität Dresden, Dresden, Germany":2,"Department of Chemical and Biomolecular Engineering, Rice University, Houston, TX, United States":1,"Nanoscience and Nanotechnology Center, Institute of Scientific and Industrial Research (ISIR), Osaka University, 8-1 Mihogaoka, Ibaraki, Osaka, Japan":0.5,"Centro/Departamento de Física da Universidade do Minho, Campus de Gualtar, 4710-057 Braga, Portugal":0.5,}cooperation_distance,apparent_location=loc.calculate_str(weighted_affiliations)
合作距离以公里为单位返回。 如果坐标已知,则可以直接进行计算,而无需初始化资源。
Locator.calculate_coordinates(weighted_coordinates)
redis子系统
pinpoint的底层架构不太适合在生成许多进程或线程的系统中使用。 为了在这种情况下使用它,应用程序数据可以从搜索逻辑中分离出来。
然后,查找表和位置数据库存储在redis数据库(>;4.0)中。 在installation之后,需要另外两个python包:
pip install redis
pip install hiredis
使用redis子系统与pinpoint交互的方式不会改变。
初始化Locator
时,server
的值需要设置为True
。
frompinpointimportLocatorloc=Locator(server=True)
如果需要redis服务器的不同设置,server
可以设置为包含这些设置的字典。
允许的键与redis pydocumentation中列出的键相同。
frompinpointimportLocatorloc=Locator(server={'host':'localhost','port':6379,'db':0})
与默认实现相比,这种方法明显较慢。 如果需要并行运行多个pinpoint实例,就应该使用它。
示例
各种例子可以在source distribution的extra文件夹中找到。