1.模板信息標(biāo)準(zhǔn)化"pagesize1":(1080,700), "pagesize2":(1080,1300), "pagesize3":(1180,900) } # ---------------------------------------------------------- # Author: LiaoChenchen # Created on: 2021/1/29 17:34 # ---------------------------------------------------------- from __future__ import absolute_import from __future__ import unicode_literals from __future__ import print_function from __future__ import division import arcpy import os from os.path import splitext as st from os import listdir as ld def size_creator(path): """ 根據(jù)輸入的模板文件地址生成標(biāo)準(zhǔn)格式的字典,。 :param path: {String} mxd file path :return: """ print("請(qǐng)確保模板文件夾中不存在無關(guān)文件,!") m_dict = {} counter = 1 for m_name in [st(x)[0] for x in ld(path) if ".mxd" or ".MXD" in x]: width, height = m_name.split("x") name = "pagesize{}".format(counter) m_dict[name] = (int(width), int(height)) counter+=1 return m_dict 2.最小邊界幾何
# ---------------------------------------------------------- # Author: LiaoChenchen # Created on: 2021/1/29 17:34 # ---------------------------------------------------------- from __future__ import absolute_import from __future__ import unicode_literals from __future__ import print_function from __future__ import division import arcpy import os from os.path import splitext as st from os import listdir as ld """_______global_values_______""" # 地址 mxd_template = "tempMXDS" # 模板文件位置 output_dir = "out" # 制圖輸出位置 gdb_path = "arcpy指南.gdb" # 數(shù)據(jù)庫(kù)地址 # 重要常量 FIELD = "CITY" # 檢索字段 MI = "MappingIndex" # 制圖索引文件名稱 SCALE = 200000 # 制圖的比例尺 """_______global_values_______""" arcpy.env.overwriteOutput = True arcpy.env.workspace = gdb_path def check_field(layer, field): """ 檢查一個(gè)要素文件中是否存在field字段 """ fields = arcpy.ListFields(layer) return field in [_.name for _ in fields] class PageSizeMatch(object): """ 適配頁(yè)面大小 """ def __init__(self, feature_name, field, mxd_template_d): """ :param feature_name: {String} # 制圖索引圖層的名字 :param field: {String} field = "CITY" # 檢索字段 :param mxd_template_d: {Dict} 模板匹配信息(字典) """ self.f = feature_name # 制圖索引圖層的名字 self.field = field self.m_d = mxd_template_d self.minimum_bounding() # 制作最小幾何邊界圖層 true_height = self.check_width_height() # 獲取真實(shí)的高度信息 # 將高度信息更新入制圖索引圖層(MappingIndex) self.update_width_height(true_height) self.update_page_size(SCALE) def minimum_bounding(self): """ 1.make MinimumBoundingGeometry feature layer 2.add PAGRSIZE field :return: """ if not check_field(MI,"MBG_Width"): #?注釋1? #?注釋2? mbe = arcpy.MinimumBoundingGeometry_management mbe(self.f, "m_out", "ENVELOPE", "LIST", self.field, True) print("Complete MinimumBoundingGeometry") arcpy.Delete_management(self.f,"FeatureClass")#?注釋3? arcpy.Rename_management("m_out",self.f,"FeatureClass") if not check_field(self.f,"PAGESIZE"): #?注釋4? # 沒有計(jì)算過最小幾何邊界 print('Add field PAGESIZE') arcpy.AddField_management( self.f, "PAGESIZE", "TEXT", field_length = 100) 3.更新MBG_Width 和 MBG_Length 字段# ---------------------------------------------------------- # Author: LiaoChenchen # Created on: 2021/1/29 17:34 # ---------------------------------------------------------- from __future__ import absolute_import from __future__ import unicode_literals from __future__ import print_function from __future__ import division import arcpy import os from os.path import splitext as st from os import listdir as ld class PageSizeMatch(object): """ ... 省略部分代碼 """ def check_width_height(self): #?注釋1? # 使用 “要素折點(diǎn)轉(zhuǎn)點(diǎn)要素” 工具,將最小邊界幾何轉(zhuǎn)變成點(diǎn),; # 每個(gè)最小邊界幾何都會(huì)生成5個(gè)點(diǎn):左下,,左上,右上,,右下,,左下 # 前兩個(gè)點(diǎn)的差值就是高度 # 返回值為高度的字典:{地級(jí)市名稱:高度} fea_v = "feature_vertices" # feature name short_f = arcpy.FeatureVerticesToPoints_management short_f(self.f, fea_v, "ALL") cursor = arcpy.da.SearchCursor(fea_v, [self.field, "SHAPE@Y"]) cursor2l = [(x[0],x[1]) for x in cursor] # 轉(zhuǎn)換成列表 del cursor #?注釋2? cursor2l_1, cursor2l_2 = cursor2l[::5], cursor2l[1::5] height_info = {} for i in xrange(len(cursor2l_1)):#?注釋2? height = cursor2l_2[i][1] - cursor2l_1[i][1] height_info[cursor2l_2[i][0]] = abs(height) return height_info #?注釋4? def update_width_height(self, height_infomation): """ 該方法將 "MBG_Width","MBG_Length"兩字段更新為寬和高 :param height_infomation: {Dict} 包含了制圖單位的高度信息 :return: """ _field = [self.field,"MBG_Width", "MBG_Length"] with arcpy.da.UpdateCursor(self.f, _field) as cursor: for row in cursor: name, width, height = row #?注釋5? if round(height_infomation[name], 2)==round(width, 2): row[1] = height row[2] = width cursor.updateRow(row) print("update width&height completly!") 4.更新PAGESIZE# ---------------------------------------------------------- # Author: LiaoChenchen # Created on: 2021/1/29 17:34 # ---------------------------------------------------------- from __future__ import absolute_import from __future__ import unicode_literals from __future__ import print_function from __future__ import division import arcpy import os from os.path import splitext as st from os import listdir as ld def select_template_size(size, template_size): """ :param size: 寬和高組成的列表 such as:[659.8490915000066, 822.3146429999917] :param template_size: 制圖模板大小 :return: 返回制圖模板大小的名稱(鍵),, 如果找不到適合的制圖模板就返回 -1 """ map_w, map_h = size[0], size[1] map_div = map_w / map_h # 符合該制圖單位的模板大小的字典 template_size_fit = { k:(v[0], v[1], v[0]*v[1], v[0]/v[1]) for k,v in template_size.items() if v[0]>map_w and v[1]>map_h } #?注釋6? d_len = len(template_size_fit) # 字典轉(zhuǎn)列表 d2l = zip(template_size_fit.keys(), template_size_fit.values()) # 按元組中第三個(gè)數(shù)大小排序(按面積大?。?/span> d2l_sorted = sorted(d2l, key=lambda x: x[1][2]) if d_len > 2: #?注釋7? two_remaind = d2l_sorted[:2] # (u'pagesize3', (1380, 850, 1173000, 1.6235294117647059)) res = min(two_remaind, key=lambda x: abs(x[1][3]-map_div)) return res[0] # u'pagesize3' elif d_len==2: res = d2l_sorted[0] return res[0] elif d_len==1: return d2l_sorted[0][0] else: # info="存在超出頁(yè)面大小的制圖單位" return -1 class PageSizeMatch(object): """ ... 省略部分代碼 """ def update_page_size(self,scale): """ 更新填充字段 "PAGESIZE" 的值 :param scale: {Int} 比例大小 :return: """ _field = ["MBG_Width", "MBG_Length", "PAGESIZE"] with arcpy.da.UpdateCursor(self.f, _field) as cursor: for row in cursor: row_p = [row[0], row[1]] new_row = [x/scale*1000 for x in row_p] # PAGESIZE1 or -1 pgs_name = select_template_size(new_row, self.m_d) if pgs_name != -1: p_size = self.m_d[pgs_name] # (1180, 900) # update PAGESIZE field values 1180x900 #?注釋8? row[2] = "{}x{}".format(p_size[0],p_size[1]) else: print("存在超出所有模板頁(yè)面大小的制圖單位") cursor.updateRow(row) print("update PAGESIZE completly!") ... v[0]>map_w and v[1]>map_h... 這段代碼做了初步的篩選,,篩選出滿足該制圖單位寬和高的 mxd模板。同時(shí)這里引入了 面積 和 寬高比例,,這兩個(gè)指標(biāo)用于進(jìn)一步的判斷和篩選,。總結(jié)
結(jié)束語(yǔ)
|
|