Skip to content

Commit

Permalink
style: 根据codacy修改风格
Browse files Browse the repository at this point in the history
  • Loading branch information
Clarmy committed Apr 8, 2022
1 parent 0873b5d commit 6448e95
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 27 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
alt="cnmaps"></a>
</h1>


<p align="center">

<a href="https://app.travis-ci.com/github/Clarmy/cnmaps">
Expand Down Expand Up @@ -40,7 +39,6 @@
cnmaps是一个可以让中国地图画起来更丝滑的地图类python扩展包
</h4>


## 安装
cnmaps依赖于`cartopy>=0.19.0`,因此在安装cnmaps之前请确保cartopy已安装,[cartopy的安装方法](https://scitools.org.uk/cartopy/docs/latest/installing.html)

Expand Down Expand Up @@ -153,6 +151,7 @@ plt.savefig('../static/images/logo-base.png', bbox_inches='tight')
## 引用

本项目适用的地图边界的数据源包括:

1. GaryBikini/ChinaAdminDivisonSHP: v2.0, 2021, DOI: 10.5281/zenodo.4167299

海拔高度地形数据来自ASTER数字高程模型,并对原始数据进行了稀释。
2 changes: 2 additions & 0 deletions cnmaps/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""cnmaps包入口"""

import os
import re
import warnings
Expand Down
32 changes: 27 additions & 5 deletions cnmaps/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,45 @@


class MapNotFoundError(Exception):
"""地图无法找到的错误"""
pass


class MapPolygon(sgeom.MultiPolygon):
"""地图多边形类
"""
地图多边形类
该是基于shapely.geometry.MultiPolygon的自定义类,
并实现了对于加号操作符的支持.
"""

def __init__(self, *args, **kwargs):
"""实例化MapPolygon"""
super().__init__(*args, **kwargs)

def __add__(self, other):
"""+ 支持."""
return self.union(other)

def __and__(self, other):
"""& 支持."""
return self.intersection(other)

def __sub__(self, other):
"""- 支持."""
return self.difference(other)

@staticmethod
def drop_inner_duplicate(map_polygon: sgeom.MultiPolygon):
"""
清理内部重复多边形
参数:
map_polygon (sgeom.MultiPolygon): 地图边界对象
返回值:
MapPolygon: 清理后的地图边界对象
"""
polygons = list(map_polygon)
couples = [couple for couple in product(polygons, repeat=2)]

Expand All @@ -47,20 +62,23 @@ def drop_inner_duplicate(map_polygon: sgeom.MultiPolygon):
return MapPolygon(polygons)

def union(self, other):
"""并集."""
union_result = super().union(other)
if isinstance(union_result, sgeom.Polygon):
return MapPolygon([union_result])
elif isinstance(union_result, sgeom.MultiPolygon):
return self.drop_inner_duplicate(MapPolygon(union_result))

def difference(self, other):
"""差集."""
difference_result = super().difference(other)
if isinstance(difference_result, sgeom.Polygon):
return MapPolygon([difference_result])
elif isinstance(difference_result, sgeom.MultiPolygon):
return self.drop_inner_duplicate(MapPolygon(difference_result))

def intersection(self, other):
"""交集."""
intersection_result = super().intersection(other)
if isinstance(intersection_result, sgeom.Polygon):
return MapPolygon([intersection_result])
Expand All @@ -70,7 +88,8 @@ def intersection(self, other):
return MapPolygon()

def get_extent(self, buffer=2):
"""获取范围坐标
"""
获取范围坐标
参数:
buffer (int, 可选): 外扩缓冲边缘, 单位为°, 该值越大, 所取的范围越大. 默认为 2.
Expand All @@ -83,7 +102,8 @@ def get_extent(self, buffer=2):


def read_mapjson(fp):
"""读取geojson地图边界文件
"""
读取geojson地图边界文件
参数:
fp (str, 可选): geojson文件名.
Expand Down Expand Up @@ -117,7 +137,8 @@ def get_adm_names(province: str = None,
level: str = '省',
country: str = '中华人民共和国',
source: str = '高德'):
"""获取行政名称
"""
获取行政名称
参数:
province (str, optional): 省/自治区/直辖市/行政特区中文名, 必须为全称
Expand Down Expand Up @@ -162,7 +183,8 @@ def get_adm_maps(province: str = None,
record: str = 'all',
only_polygon: bool = False,
*args, **kwargs):
"""获取行政地图的边界对象
"""
获取行政地图的边界对象
参数:
province (str, optional): 省/自治区/直辖市/行政特区中文名, 必须为全称
Expand Down
3 changes: 2 additions & 1 deletion cnmaps/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@


def load_dem(area_name=None):
"""加载海拔高度样例数据
"""
加载海拔高度样例数据
参数:
area_name (str, 可选): 区域名称, 若为None则取全国. 默认为 None.
Expand Down
10 changes: 5 additions & 5 deletions tests/test_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


def test_clip_pcolormesh():
"""测试剪切格点图"""
"""测试剪切格点图."""
lons, lats, data = load_dem()

for map_arg in map_args:
Expand Down Expand Up @@ -54,7 +54,7 @@ def test_clip_pcolormesh():


def test_clip_contour():
"""测试剪切等值线"""
"""测试剪切等值线."""
lons, lats, data = load_dem()

for map_arg in map_args:
Expand Down Expand Up @@ -83,7 +83,7 @@ def test_clip_contour():


def test_clip_contourf():
"""测试切割填色等值线"""
"""测试切割填色等值线."""
lons, lats, data = load_dem()

for map_arg in map_args:
Expand Down Expand Up @@ -112,7 +112,7 @@ def test_clip_contourf():


def test_clip_clabel():
"""测试切割等值线标签"""
"""测试切割等值线标签."""
lons, lats, data = load_dem()

map_polygon = get_adm_maps(
Expand Down Expand Up @@ -145,7 +145,7 @@ def test_clip_clabel():


def test_projection():
"""测试不同投影"""
"""测试不同投影."""
PROJECTIONS = [
ccrs.Orthographic(central_longitude=100),
ccrs.AlbersEqualArea(central_longitude=100),
Expand Down
29 changes: 15 additions & 14 deletions tests/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@


def test_not_found():
"""测试未找到预定义地图时是否抛出异常"""
"""测试未找到预定义地图时是否抛出异常."""
with pytest.raises(MapNotFoundError):
get_adm_maps(city='纽约市')


def test_get_map_by_fp():
"""测试是否可以按文件路径加载地图"""
"""测试是否可以按文件路径加载地图."""
pattern = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'../cnmaps/data/geojson.min/*/*/*/*.geojson')
fps = sorted(glob(pattern))
Expand All @@ -23,7 +23,7 @@ def test_get_map_by_fp():


def test_map_load():
"""测试各级地图数量是否完整,以及各种规则是否都能加载成功"""
"""测试各级地图数量是否完整,以及各种规则是否都能加载成功."""
assert len(get_adm_maps(level='国')) == 2
assert len(get_adm_maps(level='省')) == 34
assert len(get_adm_maps(level='市')) == 370
Expand Down Expand Up @@ -53,7 +53,7 @@ def test_map_load():


def test_map_operator():
"""测试地图之间的操作符是否正常"""
"""测试地图之间的操作符是否正常."""

# 加法操作符(并集)
assert isinstance(
Expand All @@ -73,7 +73,7 @@ def test_map_operator():


def test_province_orthogonality():
"""检验省边界地图的正交性"""
"""检验省边界地图的正交性."""
provinces_meta = get_adm_maps(level='省')
province_names = [pm['省/直辖市'] for pm in provinces_meta]

Expand All @@ -85,7 +85,7 @@ def test_province_orthogonality():


def test_city_orthogonality():
"""检验市边界地图的正交性"""
"""检验市边界地图的正交性."""
provinces_meta = get_adm_maps(level='省')
province_names = [pm['省/直辖市'] for pm in provinces_meta]

Expand Down Expand Up @@ -120,38 +120,38 @@ def test_city_orthogonality():


def test_province_union():
"""检验所有省两两相加是否会报错"""
"""检验所有省两两相加是否会报错."""
provinces_meta = get_adm_maps(level='省')
province_names = [pm['省/直辖市'] for pm in provinces_meta]

couples = sorted([couple for couple in combinations(province_names, r=2)])

for (one, another) in couples:
get_adm_maps(province=one)[0]['geometry'] + \
_ = get_adm_maps(province=one)[0]['geometry'] + \
get_adm_maps(province=another)[0]['geometry']


def test_province_difference():
"""检验所有省两两相减是否会报错"""
"""检验所有省两两相减是否会报错."""
provinces_meta = get_adm_maps(level='省')
province_names = [pm['省/直辖市'] for pm in provinces_meta]

couples = sorted([couple for couple in product(province_names, repeat=2)])

for (one, another) in couples:
get_adm_maps(province=one)[0]['geometry'] - \
_ = get_adm_maps(province=one)[0]['geometry'] - \
get_adm_maps(province=another)[0]['geometry']


def test_get_extent():
"""测试get_extent函数的返回结果是否符合预期"""
"""测试get_extent函数的返回结果是否符合预期."""
extent = (113.42394680348974, 119.51379082389037,
37.44400605531913, 43.060480499941455)
assert get_adm_maps(province='北京市')[0]['geometry'].get_extent() == extent


def test_only_polygon_and_record():
"""测试only_polygon参数和record参数功能"""
"""测试only_polygon参数和record参数功能."""
polygons = get_adm_maps(city='北京市', record='all',
level='区县', only_polygon=True)
assert isinstance(polygons, list)
Expand All @@ -168,6 +168,7 @@ def test_only_polygon_and_record():


def test_get_adm_names():
"""测试是否能正确获取地名."""
names = ['东城区',
'西城区',
'朝阳区',
Expand All @@ -194,7 +195,7 @@ def test_get_adm_names():


def test_regions():
"""测试区域组合"""
"""测试区域组合."""
AERAS = {'东北地区': 92.0,
'华北地区': 168.0,
'华东地区': 80.0,
Expand All @@ -212,7 +213,7 @@ def test_regions():


def test_inner_duplicate():
"""测试内部多边形重复问题"""
"""测试内部多边形重复问题."""
union_maps = [
get_adm_maps(province='黑龙江省', only_polygon=True, record='first') +
get_adm_maps(province='内蒙古自治区', only_polygon=True, record='first'),
Expand Down

0 comments on commit 6448e95

Please sign in to comment.