赞
踩
python arcgis出公示图带tkinter界面
from site import addsitedir
from sys import executable
from os import path
interpreter = executable
sitepkg = path.dirname(interpreter) + “\Lib\site-packages”
addsitedir(sitepkg)
import arcpy, time, os, sys
import arcpy.mapping as mapping
import traceback
import Tkinter as tk
from Tkinter import *
from ttk import *
import threading, base64
import tkMessageBox,tkFileDialog
import win32com.client
import update_gsb
defaultencoding = ‘utf-8’
if sys.getdefaultencoding() != defaultencoding:
reload(sys)
sys.setdefaultencoding(defaultencoding)
class dc_gst():
def isPointinPolygon(self, point, rangelist1): # [[0,0],[1,1],[0,1],[0,0]] [1,0.8]
# 判断是否在外包矩形内,如果不在,直接返回false
rangelist = []
for aa in rangelist1:
rangelist.append(list(aa))
lnglist = []
latlist = []
for i in range(len(rangelist) - 1):
lnglist.append(rangelist[i][0])
latlist.append(rangelist[i][1])
# print(lnglist, latlist)
maxlng = max(lnglist)
minlng = min(lnglist)
maxlat = max(latlist)
minlat = min(latlist)
# print(maxlng, minlng, maxlat, minlat)
if (point[0] > maxlng or point[0] < minlng or
point[1] > maxlat or point[1] < minlat):
return False
count = 0
point1 = rangelist[0]
for i in range(1, len(rangelist)):
point2 = rangelist[i]
# 点与多边形顶点重合
if (point[0] == point1[0] and point[1] == point1[1]) or (point[0] == point2[0] and point[1] == point2[1]):
return True
# 判断线段两端点是否在射线两侧 不在肯定不相交 射线(-∞,lat)(lng,lat)
if (point1[1] < point[1] and point2[1] >= point[1]) or (point1[1] >= point[1] and point2[1] < point[1]):
# 求线段与射线交点 再和lat比较
point12lng = point2[0] - (point2[1] - point[1]) * (point2[0] - point1[0]) / (point2[1] - point1[1])
# print(point12lng)
# 点在多边形边上
if (point12lng == point[0]):
#print(“点在多边形边上”)
return True
if (point12lng < point[0]):
count += 1
point1 = point2
# print(count)
if count % 2 == 0:
return False
else:
return True
def bt_text2(self, cxz): bt = arcpy.mapping.ListLayoutElements(self.mapdoc,"","gsbt")[0] gsrq = arcpy.mapping.ListLayoutElements(self.mapdoc,"","gsrq")[0] gsdd = arcpy.mapping.ListLayoutElements(self.mapdoc,"","gsdd")[0] zfgz = arcpy.mapping.ListLayoutElements(self.mapdoc,"","zfgz")[0] cwgz = arcpy.mapping.ListLayoutElements(self.mapdoc,"","cwgz")[0] ztrq = arcpy.mapping.ListLayoutElements(self.mapdoc,"","ztrq")[0] zl = self.zl self.cm = zl[zl.find("镇") + 1:zl.find("村")+ 1] zm = zl[zl.find("县") + 1:zl.find("镇")+ 1] bt.text = '%s%s%s民委员会“房地一体”农村宅基地和集体建设用地权籍调查信息公示图\n(首轮)'%(self.xqmc, zm, self.cm) gsrq.text = '【公示日期:%s】'%self.gsrq_x gsdd.text = '公示地点:%s%s%s民委员会'%(self.xqmc, zm, self.cm) zfgz.text = '%s%s人民政府(盖章)'%(self.xqmc, zm) cwgz.text = "%s%s%s民委员会(盖章)"%(self.xqmc, zm, self.cm) ztrq.text = '制图日期:%s'%self.ztrq_x def bt_text3(self, cxz): bt = arcpy.mapping.ListLayoutElements(self.mapdoc,"","gsbt")[0] gsrq = arcpy.mapping.ListLayoutElements(self.mapdoc,"","gsrq")[0] gsdd = arcpy.mapping.ListLayoutElements(self.mapdoc,"","gsdd")[0] zfgz = arcpy.mapping.ListLayoutElements(self.mapdoc,"","zfgz")[0] cwgz = arcpy.mapping.ListLayoutElements(self.mapdoc,"","cwgz")[0] ztrq = arcpy.mapping.ListLayoutElements(self.mapdoc,"","ztrq")[0] cxzgz = arcpy.mapping.ListLayoutElements(self.mapdoc,"","cxzgz")[0] zl = self.zl self.cm = zl[zl.find("镇") + 1:zl.find("村")+ 1] zm = zl[zl.find("县") + 1:zl.find("镇")+ 1] bt.text = '%s%s%s%s民小组“房地一体”农村宅基地和集体建设用地权籍调查信息公示图\n(首轮)'%(self.xqmc, zm, self.cm, cxz) gsrq.text = '【公示日期:%s】'%self.gsrq_x gsdd.text = '公示地点:%s%s%s民委员会\n%s民小组'%(self.xqmc, zm, self.cm, cxz) zfgz.text = '%s%s人民政府(盖章)'%(self.xqmc, zm) cwgz.text = "%s%s%s民委员会(盖章)"%(self.xqmc, zm, self.cm) ztrq.text = '制图日期:%s'%self.ztrq_x cxzgz.text = '%s%s%s民委员会%s民小组(盖章)' % (self.xqmc, zm, self.cm, cxz) def select_qlr(self): self.qlrmc = {} with arcpy.da.SearchCursor(self.qlr_path, ("BDCDYH", "QLRMC")) as cursor: for row in cursor: self.qlrmc.setdefault(row[0][:19], []).append(row[1]) del row, cursor def select_xl(self): wb = win32com.client.Dispatch('Excel.Application') book = wb.Workbooks.Open(self.path_gsrq_x)# sht = book.Sheets(1) row = sht.UsedRange.Rows.Count self.list_gsrq = {} self.list_gsz = {} for i in range(2, row + 1): self.list_gsrq[sht.Cells(i,2).Value] = sht.Cells(i,3).Value self.list_gsz[sht.Cells(i,2).Value] = sht.Cells(i,4).Value book.Save() book.Close() wb.Quit() print self.list_gsrq def main(self): sk_path = ent_sk.get()#r'C:\Users\ye\Desktop\aa\cc' bc_path = ent_bc.get()#r'D:\2-python\7-海丰\9、出图表\源代码' self.path_gsrq_x = ent_gsrq.get()#u'2021年1月1日至2020年1月31日' self.ztrq_x = ent_ztrq.get()#u'2021年1月1日' qxdm = ent_qxdm.get()#'441521' self.xqmc = ent_qxmc.get()#'海丰县' zjd_path = os.path.join(sk_path, "数据库成果", "空间数据", "%sZJD.shp"%qxdm) zd_path = os.path.join(sk_path, "数据库成果", "空间数据", "%sSHYQZD_DB.shp"%qxdm) self.qlr_path = os.path.join(sk_path, "数据库成果", "非空间数据", "%sSXB.mdb"%qxdm, "QLR") path_mxd_2 = os.path.join(os.path.split(sys.argv[0])[0], 'GST.mxd') path_mxd_3 = os.path.join(os.path.split(sys.argv[0])[0], 'GST_XZ.mxd') self.select_xl() list_djq = [] self.select_qlr() arcpy.AddField_management(zjd_path, "QLR", "TEXT", 9, "", "", "refcode", "NULLABLE", "NON_REQUIRED")#添加字段 arcpy.AddField_management(zd_path, "QLR", "TEXT", 9, "", "", "refcode", "NULLABLE", "NON_REQUIRED")#添加字段 list_cxz = {} with arcpy.da.UpdateCursor(zjd_path, ("ZDDM", "QLR", "ZL")) as cursor: for row in cursor: row[0] = row[0] djq = row[0][:12] list_djq.append(djq) try: row[1] = "、".join(self.qlrmc[row[0]]) except: row[1] = '/' row[2] = row[2] zl = row[2] cm = zl[zl.find("镇") + 1:zl.find("村小组")+ 1] print cm list_cxz.setdefault(djq, []).append(cm) cursor.updateRow(row) try: del row, cursor except: pass with arcpy.da.UpdateCursor(zd_path, ("ZDDM", "QLR", "ZL")) as cursor: for row in cursor: row[0] = row[0] try: row[1] = "、".join(self.qlrmc[row[0]]) except: row[1] = '/' row[2] = row[2] cursor.updateRow(row) try: del row, cursor except: pass list_djq = list(set(list_djq)) print list_djq for djq in list_djq: li_cxz = list(set(list_cxz[djq])) print li_cxz for cxz in li_cxz: if self.list_gsz[cxz] == 2: mapdoc = arcpy.mapping.MapDocument(path_mxd_2) self.mapdoc = mapdoc js = 2 else: mapdoc = arcpy.mapping.MapDocument(path_mxd_3) self.mapdoc = mapdoc js = 3 listdf = arcpy.mapping.ListDataFrames(mapdoc) layers = arcpy.mapping.ListLayers(mapdoc) arcpy.SelectLayerByAttribute_management(layers[0], "CLEAR_SELECTION") # 清楚所选要素 datasource = layers[0].dataSource mapdoc.findAndReplaceWorkspacePaths(os.path.dirname(os.path.dirname(os.path.dirname(datasource))), sk_path) # 替换图层路径 self.gsrq_x = self.list_gsrq[cxz] qry = '"ZL" LIKE ' + "'%%%s小组%%'"% cxz print qry layers[0].definitionQuery = qry #arcpy.SelectLayerByAttribute_management(layers[0], "NEW_SELECTION", qry)#选择每个村的数据 #判断输出几幅公示图 list_xy = {} list_x = [] list_y = [] with arcpy.da.SearchCursor(zjd_path, ("ZDDM", "SHAPE@XY", "ZL"), qry) as cursor: for row in cursor: xy = row[1] list_x.append(xy[0]) list_y.append(xy[1]) list_xy[xy]= row[0] self.zl = row[2] del row, cursor #替换地图文本内容 xz = cxz[cxz.find("村") + 1:] if js == 2: self.bt_text2(xz) else: self.bt_text3(xz) max_x = max(list_x) max_y = max(list_y) min_x = min(list_x) min_y = min(list_y) len_x = max_x - min_x len_y = max_y - min_y i = 1 for x in range(int(len_x / 400) + 1): for y in range(int(len_y / 400) + 1): print int(len_y / 400) + 1 list_tfk = [] list_tfk.append([x * 400, y * 400]) list_tfk.append([(x + 1) * 400, y * 400]) list_tfk.append([(x + 1) * 220, (y + 1) * 400]) list_tfk.append([x * 400, (y + 1) * 400]) list_tfk.append([x * 400, y * 400]) tem = 1 print list_tfk for point in list_xy: sfzmn = self.isPointinPolygon([point[0] - min_x, point[1] - min_y], list_tfk) if sfzmn: zddm = list_xy[point] print '"ZDDM" = ' + "'" + zddm + "'", layers[0] arcpy.SelectLayerByAttributemanagement(layers[0], "ADD_TO_SELECTION", '"ZDDM" = ' + "'" + zddm +"'") tem += 1 if tem > 1: listdf[0].zoomToSelectedFeatures()#缩放至图层 listdf[0].scale = 500 arcpy.SelectLayerByAttribute_management(layers[0], "CLEAR_SELECTION") # 清楚所选要素 path_pdf = os.path.join(bc_path, '%s小组-%s.pdf'%(cxz, i)) text_get('导出【%s】完成\n'%('%s小组-%s.pdf'%(cxz, i))) arcpy.mapping.ExportToPDF(mapdoc, path_pdf) i += 1 #mapdoc.save() print 1 del mapdoc
def dc_gsrq():
path_gsrq = ent_gsrq.get()
path_excel = os.path.join(path_gsrq, ‘1.xlsx’)
wb = win32com.client.Dispatch(‘Excel.Application’)
book = wb.Workbooks.Add()
sht = book.Sheets(1)
sht.Cells(1, 1).Value = u’地籍子区’
sht.Cells(1, 2).Value = u’小组名’
sht.Cells(1, 3).Value = u’公示日期’
sht.Cells(1, 4).Value = u’填写2或3,代表二级章三级章’
sk_path = ent_sk.get() qxdm = ent_qxdm.get() zjd_path = os.path.join(sk_path, "数据库成果", "空间数据", "%sZJD.shp" % qxdm) list_cxz = {} list_djq = [] with arcpy.da.SearchCursor(zjd_path, ("ZDDM", "ZL")) as cursor: for row in cursor: djq = row[0][:12] list_djq.append(djq) zl = row[1] cm = zl[zl.find("镇") + 1:zl.find("村小组") + 1] list_cxz.setdefault(djq, []).append(cm) list_djq = list(set(list_djq)) i = 2 for djq in list_djq: li_cxz = list(set(list_cxz[djq])) for cxz in li_cxz: sht.Cells(i, 1).Value = djq sht.Cells(i, 2).Value = cxz print djq, cxz i += 1 print path_excel book.SaveAs(path_excel.replace('/', '\\')) book.Close() wb.Quit() ent_gsrq.delete("0", "end") ent_gsrq.insert(0, path_excel)
def center_window(root, width, height):
“”“生成指定大小GUI的根窗口,位置为屏幕中心”“”
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
size = ‘%dx%d+%d+%d’ % (width, height, (screenwidth - width)/2, (screenheight - height)/2)
root.geometry(size)
def in_path():
“”“弹框选择,获取输入路径”“”
folder_path = tkFileDialog.askdirectory()
ent_sk.delete(“0”, “end”)
ent_sk.insert(0,folder_path)
def in_path1():
“”“弹框选择,获取输入路径”“”
folder_path = tkFileDialog.askdirectory()
ent_bc.delete(“0”, “end”)
ent_bc.insert(0,folder_path)
def in_path2():
“”“弹框选择,获取输入路径”“”
folder_path = tkFileDialog.askdirectory()
ent_gsrq.delete(“0”, “end”)
ent_gsrq.insert(0,folder_path)
def in_path3():
“”“弹框选择,获取输入路径”“”
folder_path = tkFileDialog.askopenfilename(filetypes=[(“word格式”,“doc”)])
gsb_sk.delete(“0”, “end”)
gsb_sk.insert(0,folder_path)
def un_ye():
t = threading.Thread(target=ye, args=())
t.start()
def text_get(text1):
text.insert(tk.END, text1)
root.update()
def ye_gsb():
try:
text1.insert(tk.END, ‘开始运行程序\n程序正在运行中,请稍等\n’)
root.update()
path = gsb_sk.get()
update_gsb.cad().sel_word(path)
text1.insert(tk.END, ‘更新公示表完成\n’)
root.update()
except:
text1.insert(tk.END, traceback.format_exc())
root.update()
def ye():
try:
text_get(“开始运行程序\n”)
dc_gst().main()
text_get(“导出完成\n”)
except:
text_get(traceback.format_exc())
def gst():
frm_dcgst.pack(expand = YES)
frm_gxgsb.forget()
def gsb():
frm_gxgsb.pack(expand = YES)
frm_dcgst.forget()
tmp= open(“tmp.ico”, “wb+”)
tmp.close()
def close():
“”“关闭”“”
root.destroy()
root = tk.Tk()
root.title(‘精测测绘成果信息输出工具’)
center_window(root, 740, 548)
root.iconbitmap(‘tmp.ico’)
os.remove(“tmp.ico”)
menubar = Menu(root)
#qlmenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label=“输出公示图”, command = gst)
immenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label=“更新检查公示表”, command = gsb)
root.config(menu = menubar)
frm_dcgst = Frame(root)
frm_gxgsb = Frame(root)
#输出公示图
frm0 = Frame(frm_dcgst)
frm01 = Frame(frm_dcgst)
frm1 = Frame(frm_dcgst)
frm2 = Frame(frm_dcgst)
frm4 = Frame(frm_dcgst)
frm5 = Frame(frm_dcgst)
lab = Label(frm0, font=(“宋体”, 10), text = “区县代码:”).pack(side=LEFT,expand=NO,fill=None)
ent_qxdm = tk.Entry(frm0, width = 20, font=(“微软雅黑”, 9))
ent_qxdm.pack(side=LEFT)
lab = Label(frm0, font=(“宋体”, 10), text = " 区县名称:“).pack(side=LEFT,expand=NO,fill=None)
ent_qxmc = tk.Entry(frm0, width = 20, font=(“微软雅黑”, 9))
ent_qxmc.pack(side=LEFT)
lab = Label(frm0, font=(“宋体”, 10), text = " 制图日期:”).pack(side=LEFT,expand=NO,fill=None)
ent_ztrq = tk.Entry(frm0, width = 20, font=(“微软雅黑”, 9))
ent_ztrq.pack(side=LEFT)
frm0.pack(pady = 10,padx = 0)
lab = Label(frm01, font=(“宋体”, 10), text = “公式日期:”).pack(side=LEFT,expand=NO,fill=None)
ent_gsrq = tk.Entry(frm01, width = 60, font=(“微软雅黑”, 9))
ent_gsrq.pack(side=LEFT)
but_gdrq1 = tk.Button(frm01, text = ‘···’, width = 6, command = in_path2)
but_gdrq1.pack(side = LEFT, padx=10)
but_gdrq2 = tk.Button(frm01, text = ‘导出日期表’, width = 10, command = dc_gsrq)
but_gdrq2.pack(side = LEFT, padx=10)
frm01.pack(pady = 10,padx = 0)
lab = Label(frm1, font=(“宋体”, 10), text = “选择成果数据:”).pack(side=LEFT,expand=NO,fill=None)
ent_sk = tk.Entry(frm1, width = 70, font=(“微软雅黑”, 9))
ent_sk.pack(side=LEFT)
but1 = tk.Button(frm1, text = ‘···’, width = 6, command = in_path)
but1.pack(side = LEFT, padx=10)
frm1.pack(pady = 8)
lab = Label(frm2, font=(“宋体”, 10), text = “选择保存路径:”).pack(side=LEFT,expand=NO,fill=None)
ent_bc = tk.Entry(frm2, width = 70, font = (‘微软雅黑’, 9))
ent_bc.pack(side=LEFT)
but2 = tk.Button(frm2, text = ‘···’, width = 6, command = in_path1)
but2.pack(side = LEFT, padx=10)
frm2.pack()
but3 = tk.Button(frm4, text = ‘开始转换’, width = 10, command = ye)
but3.pack(side = LEFT, padx=50)
but5 = tk.Button(frm4, text = ‘退出程序’, width = 10, command = close)
but5.pack(side = RIGHT, padx=50)
frm4.pack(pady = 15)
text = Text(frm5, font=(“微软雅黑”, 9), width=100, height=14)
text.pack(side = TOP, pady = 10)
frm5.pack(pady = 5)
ent_qxdm.delete(“0”, “end”)
ent_qxdm.insert(0,‘441521’)
ent_qxmc.delete(“0”, “end”)
ent_qxmc.insert(0,‘海丰县’)
ent_ztrq.delete(“0”, “end”)
ent_ztrq.insert(0,‘2020年12月31日’)
#输出公示表
frm_gsb0 = Frame(frm_gxgsb)
frm_gsb1 = Frame(frm_gxgsb)
frm_gsb2 = Frame(frm_gxgsb)
lab = Label(frm_gsb0, font=(“宋体”, 10), text = “选择公示表路径:”).pack(side=LEFT,expand=NO,fill=None)
gsb_sk = tk.Entry(frm_gsb0, width = 70, font=(“微软雅黑”, 9))
gsb_sk.pack(side=LEFT)
gsb_but1 = tk.Button(frm_gsb0, text = ‘···’, width = 6, command = in_path3)
gsb_but1.pack(side = LEFT, padx=10)
frm_gsb0.pack(pady = 8)
gsb_but3 = tk.Button(frm_gsb1, text = ‘开始转换’, width = 10, command = ye_gsb)
gsb_but3.pack(side = LEFT, padx=50)
gsb_but5 = tk.Button(frm_gsb1, text = ‘退出程序’, width = 10, command = close)
gsb_but5.pack(side = RIGHT, padx=50)
frm_gsb1.pack(pady = 15)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。