forked from flashlxy/PyMICAPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClipBorder.py
83 lines (75 loc) · 2.8 KB
/
ClipBorder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
#
# Author: Liu xianyao
# Email: [email protected]
# Update: 2017-04-06
# Copyright: ©江西省气象台 2017
# Version: 1.1.20170409
from __future__ import print_function
from datetime import datetime
from matplotlib.path import Path
import maskout
from Projection import Projection
class ClipBorder:
"""
边界裁切类
"""
def __init__(self, leaf):
start_pos = 0
filehead = Projection.leaf_to_string(leaf, "File")
filetype = Projection.leaf_to_string(leaf, "Type", "shp")
code = Projection.leaf_to_list(leaf, "Code", [360000])
drawswitch = str.upper(Projection.leaf_to_string(leaf, "Draw", "off"))
self.encoding = Projection.leaf_to_string(leaf, "Encoding", "utf-8")
self.path = self.getPath(
filehead=filehead,
code=code,
filetype=filetype,
start_pos=start_pos,
encoding=self.encoding,
)
self.draw = str.upper(Projection.leaf_to_string(leaf, "Draw", "off"))
self.using = Projection.leaf_to_bool(leaf, "Using", True)
self.linewidth = Projection.leaf_to_float(leaf, "LineWidth", 1)
self.linecolor = (
Projection.leaf_to_string(leaf, "LineColor", "k")
if drawswitch == "ON"
else "none"
)
@staticmethod
def getPath(filehead, code, filetype, start_pos, encoding=None):
"""
根据文件类型获取path对象
:param start_pos: 正文开始的位置,对txt文件有效
:param filehead: 文件名
:param code: 闭合区域行政区号-对shp文件有效
:param filetype: 文件类型
:return: path对象的一个实例
"""
if encoding is None:
encoding = "utf-8"
if filetype == "shp":
path = maskout.getPathFromShp(filehead, code, encoding=encoding)
else:
path = ClipBorder.readPath(filehead, start_pos)
return path
@staticmethod
def readPath(filename, start_pos=13):
"""
从类似第9类micaps数据中获取path
:param start_pos:
:param filename: 数据文件全名
:return: path对象的一个实例
"""
try:
file_object = open(filename)
all_the_text = file_object.read()
file_object.close()
poses = all_the_text.strip().split()
lon = [float(p) for p in poses[start_pos::2]]
lat = [float(p) for p in poses[start_pos + 1 :: 2]]
path = Path(zip(lon, lat))
return path
except Exception as err:
print("【{0}】{1}-{2}".format(filename, err, datetime.now()))
return None