Skip to content

Commit 7205ec0

Browse files
chfwchenjiandongx
authored andcommitted
Provide pyecharts with 138,398 cities of the world with a population of at least 1000 inhabitants (pyecharts#663)
* ✨ OPEN / CLOSE principle - open for extension but closed for modification. Allow the extension of city coordinates so that echarts-cities-pypkg can be installed, hence 138,398 city coordinations is added covering all countries in the world * 🐛 what if no country data is returned * 🔥 remove lazy object test * 🎨 last resort, have to introduce country as a parameter to geo.add() function. no way to search for the coordinates without the country name. * 📚 documentation update * 📚 fix typos and add missing docstrings * 🔬 more unit tests * 💚 make unit test pass * 🐛 without unicode_literals but with utf-8, all Chinese strings are utf-8 encoded, hence requires decoding. * 🎨 🔨 code refacotring and 📚 update documentation * 🎨 finalized version * 🔥 remove un-used function * 🔨 change function parameter name: map_country -> coordinate_country * 🔨 country -> region * 🔨 country -> region * 🚜 rename db file * 🔨 whitening with black * 📖 update dataset * 📖 update data set country
1 parent a4168d8 commit 7205ec0

17 files changed

+494
-119
lines changed

MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
include pyecharts/datasets/city_coordinates.json
2+
include pyecharts/datasets/countries_regions_db.json
23
include pyecharts/templates/*.html
34
include README.md
45
include changelog.md

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ $ python setup.py install
5656

5757
Please note: since version 0.3.2, **NO LONGER** pyecharts comes with any map files. Please read next section for more informations.
5858

59+
### Geo Data extensions (0.5.7+)
60+
61+
1. From geonames.org, [138,398 cities of the world with a population of at least 1000 inhabitants](https://github.com/echarts-maps/echarts-cities-js): [echarts-cities-pypkg](https://github.com/pyecharts/echarts-cities-pypkg)
62+
63+
In order to install them, you can try one or all of them below:
64+
65+
```shell
66+
$ pip install echarts-cities-pypkg
67+
```
68+
5969
### Map extensions
6070

6171
Here is a list of map extensions from pyecharts dev team:

docs/zh-cn/changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* [issue#651](https://github.com/pyecharts/pyecharts/issues/651) Scatter 图新增 `extra_name` 参数,额外的数据项的名称,可以为每个数据点指定一个名称。
77
* [issue#657](https://github.com/pyecharts/pyecharts/issues/657) 基本图形新增 `extra_html_text_label` 参数用于显示额外的文本标签,仅限于在单图形或者 Page 时使用。
88
* [issue#660](https://github.com/pyecharts/pyecharts/issues/660) 为 X/Y 坐标轴新增 `xaxis_line_color`, `xaxis_line_width`, `yaxis_line_color`, `yaxis_line_width` 四个参数,用于控制其坐标轴线线的颜色以及宽度。
9+
* 新增 echarts-cities-pypkg 为可选的地理数据扩展。引入来自 geonames.org 的 138,398 个城市坐标
910

1011

1112
* ### version 0.5.6 - 2018.7.28(current)

docs/zh-cn/charts.md

+3
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,7 @@ Geo.add() 方法签名
11611161
add(name, attr, value,
11621162
type="scatter",
11631163
maptype='china',
1164+
coordinate_region='中国',
11641165
symbol_size=12,
11651166
border_color="#111",
11661167
geo_normal_color="#323c48",
@@ -1178,6 +1179,8 @@ add(name, attr, value,
11781179
图例类型,有'scatter', 'effectScatter', 'heatmap'可选。默认为 'scatter'
11791180
* maptype -> str
11801181
地图类型。 从 v0.3.2+ 起,地图已经变为扩展包,支持全国省份,全国城市,全国区县,全球国家等地图,具体请参考 [地图自定义篇](zh-cn/customize_map)
1182+
* coordinate_region -> str
1183+
城市坐标所属国家。从 v0.5.7 引入,针对国际城市的地理位置的查找。
11811184
* symbol_size -> int
11821185
标记图形大小。默认为 12
11831186
* border_color -> str

docs/zh-cn/datasets.md

+62-4
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ pyecharts 内置了一些常用的城市地理坐标数据,这些数据保存
2727
}
2828
```
2929

30-
### 检索地理坐标
30+
### 检索中国地理坐标
3131

32-
`get_coordinate(name)` 返回城市名称的地理坐标,如果未定义将返回 None 。
32+
`get_coordinate(name, region="中国")` 返回城市名称的地理坐标,如果未定义将返回 None 。
3333

3434
```python
3535
from pyecharts.datasets.coordinates import get_coordinate
@@ -59,12 +59,12 @@ print(result) # {'北京':[116.46, 39.92], '北京市': [116.4, 39.9]}
5959
```python
6060
from pyecharts.datasets.coordinates import search_coordinates_by_keyword
6161
result = search_coordinates_by_keyword('福州', '杭州')
62-
print(result) # {'福州市': [119.3, 26.08], '杭州市': [120.15, 30.28] ...}
62+
print(result) # {'福州市': [119.3, 26.08], '杭州市': [120.15, 30.28] ...}
6363
```
6464

6565
### 按过滤函数搜索地理坐标
6666

67-
`search_coordinates_by_filter(func)` 根据过滤函数,返回一个匹配的字典对象。
67+
`search_coordinates_by_filter(func, region="中国")` 根据过滤函数,返回一个匹配的字典对象。
6868
用法(结果同上)
6969

7070
```python
@@ -113,6 +113,64 @@ geo.add(
113113
geo.render()
114114
```
115115

116+
## 国际城市地理坐标
117+
118+
自 v0.5.7 之后,[echarts-cities-pypkg](https://github.com/pyecharts/echarts-cities-pypkg) 给 pyecharts 补充了 [138,398 个城市地理坐标](https://github.com/echarts-maps/echarts-cities-js),覆盖了200 多个国家。你可以配合 echarts-countries-pypkg 画地理散点图。
119+
120+
### 安装方法
121+
122+
```
123+
pip install echarts-cities-pypkg
124+
```
125+
126+
### 使用方法
127+
128+
```python
129+
from pyecharts.datasets.coordinates import get_coordinate
130+
131+
coordinate = get_coordinate('Oxford', region="英国")
132+
print(coordinate) # [-1.25596, 51.75222]
133+
```
134+
135+
### 按关键字搜索地理坐标
136+
137+
`search_coordinates_by_region_and_keyword(*args)` 根据一个或多个关键字,返回一个匹配的字典对象。
138+
139+
用法 1:单个关键字模糊搜索
140+
141+
```python
142+
from pyecharts.datasets.coordinates import search_coordinates_by_region_and_keyword
143+
144+
result = search_coordinates_by_region_and_keyword("英国", 'London')
145+
print(result)
146+
#{
147+
# "Londonderry County Borough": [-7.30917, 54.99721],
148+
# "City of London": [-0.09184, 51.51279],
149+
# "London": [-0.12574, 51.50853],
150+
#}
151+
```
152+
153+
用法 2:多个关键字模糊搜索
154+
155+
```python
156+
from pyecharts.datasets.coordinates import search_coordinates_by_region_and_keyword
157+
result = search_coordinates_by_region_and_keyword('中国香港', 'Central', 'Hong Kong')
158+
print(result) # { "Hong Kong": [114.15769, 22.28552], "Central": [114.15846, 22.28299]}
159+
```
160+
161+
### 按过滤函数搜索地理坐标
162+
163+
```python
164+
from pyecharts.datasets.coordinates import search_coordinates_by_filter
165+
166+
result = search_coordinates_by_filter(
167+
func=lambda name: "Central" in name or "Hong Kong" in name,
168+
region="中国香港",
169+
170+
)
171+
print(result) # { "Hong Kong": [114.15769, 22.28552], "Central": [114.15846, 22.28299]}
172+
```
173+
116174
## 地图数据
117175

118176
以下是 pyecharts 开发组托管的地图扩展(map extension):

pyecharts/charts/geo.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# coding=utf-8
2+
from __future__ import unicode_literals
23

34
from pyecharts.chart import Chart
45
from pyecharts.datasets.coordinates import get_coordinate
@@ -29,7 +30,7 @@ def add_coordinate(self, name, longitude, latitude):
2930
"""
3031
self._coordinates.update({name: [longitude, latitude]})
3132

32-
def get_coordinate(self, name, raise_exception=False):
33+
def get_coordinate(self, name, region="中国", raise_exception=False):
3334
"""
3435
Return coordinate for the city name.
3536
@@ -40,7 +41,7 @@ def get_coordinate(self, name, raise_exception=False):
4041
if name in self._coordinates:
4142
return self._coordinates[name]
4243

43-
coordinate = get_coordinate(name)
44+
coordinate = get_coordinate(name, region=region)
4445
if coordinate is None and raise_exception:
4546
raise ValueError("No coordinate is specified for {}".format(name))
4647

@@ -56,6 +57,7 @@ def __add(
5657
value,
5758
type="scatter",
5859
maptype="china",
60+
coordinate_region="中国",
5961
symbol_size=12,
6062
border_color="#111",
6163
geo_normal_color="#323c48",
@@ -77,6 +79,8 @@ def __add(
7779
:param maptype:
7880
地图类型。 从 v0.3.2+ 起,地图已经变为扩展包,支持全国省份,全国城市,全国区县,
7981
全球国家等地图,具体请参考 [地图自定义篇](zh-cn/customize_map)
82+
:param coordinate_region:
83+
城市坐标所属国家。从 v0.5.7 引入,针对国际城市的地理位置的查找。
8084
:param symbol_size:
8185
标记图形大小。
8286
:param border_color:
@@ -105,7 +109,9 @@ def __add(
105109

106110
_data = []
107111
for _name, _value in zip(attr, value):
108-
_coordinate = self.get_coordinate(_name, raise_exception=True)
112+
_coordinate = self.get_coordinate(
113+
_name, coordinate_region, raise_exception=True
114+
)
109115
_data_value = [_coordinate[0], _coordinate[1], _value]
110116
_data.append({"name": _name, "value": _data_value})
111117
self._option.update(

pyecharts/charts/geolines.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# coding=utf-8
2+
from __future__ import unicode_literals
23

34
from pyecharts.charts.geo import Geo
45
from pyecharts.constants import SYMBOL
@@ -20,6 +21,7 @@ def add(
2021
name,
2122
data,
2223
maptype="china",
24+
coordinate_region="中国",
2325
symbol=None,
2426
symbol_size=12,
2527
border_color="#111",
@@ -46,6 +48,8 @@ def add(
4648
:param maptype:
4749
地图类型。 从 v0.3.2+ 起,地图已经变为扩展包,支持全国省份,全国城市,全国区县,
4850
全球国家等地图,具体请参考 [地图自定义篇](zh-cn/customize_map)
51+
:param coordinate_region:
52+
城市坐标所属国家。从 v0.5.7 引入,针对国际城市的地理位置的查找。
4953
:param symbol:
5054
线两端的标记类型,可以是一个数组分别指定两端,也可以是单个统一指定。
5155
:param symbol_size:
@@ -99,7 +103,7 @@ def add(
99103
_from_name, _to_name, _line_value = element
100104

101105
_from_coordinate = self.get_coordinate(
102-
_from_name, raise_exception=True
106+
_from_name, coordinate_region, raise_exception=True
103107
)
104108
_to_coordinate = self.get_coordinate(
105109
_to_name, raise_exception=True

pyecharts/constants.py

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
JUPYTER_PRESENTATIONS = [SVG, PNG, JPEG, DEFAULT_HTML, NTERACT]
1616
ENVIRONMENT_PLUGIN_TYPE = "pyecharts_environment"
1717
JS_EXTENSION_PLUGIN_TYPE = "pyecharts_js_extension"
18+
GEO_DATA_PLUGIN_TYPE = "pyecharts_geo_data_bank"
1819

1920
# themes
2021
LIGHT_THEME = "light"

0 commit comments

Comments
 (0)