本投影界面工具的功能:
準(zhǔn)備好.prj投影文件,將輸入文件夾內(nèi)的wgs84經(jīng)緯度坐標(biāo)shp文件,投影為平面文件,成果自動命名為prj_***并新建在輸入文件夾同一路徑下。
下一步目標(biāo):
利用pyinstaller或其他打包庫生成exe文件,目前停滯在python2語法、arcpy打包出錯相關(guān)問題上。
參考文獻(xiàn):
《Using Py2exe with Arcpy- It can be done easily!》
gui界面示意圖
投影文件所在文件夾結(jié)構(gòu)如下:
gui代碼
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
|
# -*- coding: utf-8 -*- # ============================================================================= # 輸入文件——點選、復(fù)制、拖拽 # 選擇待投影的文件夾、投影文件所在文件夾 # ============================================================================= """ created on thu feb 4 16:12:00 2021 @author: zhutong """ import wx from def_projection_common_e import createprjfile,projection #創(chuàng)建應(yīng)用程序?qū)ο?/code> app = wx.app() #自定義窗口類myframe class myframe(wx.frame): def __init__( self ): super (myframe, self ).__init__(none,title = "通用經(jīng)緯度轉(zhuǎn)平面坐標(biāo)工具" ,pos = ( 600 , 500 ),size = ( 600 , 300 )) #python2語法 panel = wx.panel(parent = self ) #創(chuàng)建面板對象 self .statictext_shp = wx.statictext(parent = panel,label = "待投影數(shù)據(jù)所在文件夾" ,pos = ( 60 , 30 )) #創(chuàng)建靜態(tài)文本對象 self .statictext_shp = wx.statictext(parent = panel,label = "投影文件所在文件夾" ,pos = ( 60 , 80 )) #創(chuàng)建靜態(tài)文本對象 self .shp_text = wx.textctrl(parent = panel,value = "",pos = ( 60 , 50 ),size = ( 350 , 25 )) #【文本控件1】 open_shp_button = wx.button(parent = panel, label = '打開' ,pos = ( 430 , 50 )) #【按鈕控件1】 self .prj_text = wx.textctrl(parent = panel,value = "",pos = ( 60 , 100 ),size = ( 350 , 25 )) #【文本控件2】 open_prj_button = wx.button(parent = panel, label = '打開' ,pos = ( 430 , 100 )) #【按鈕控件2】 projection_button = wx.button(parent = panel, label = '平面投影' ,pos = ( 150 , 150 ),size = ( 180 , 30 )) #【按鈕控件3】 self .bind(wx.evt_button, self .onbutton_opendir, open_shp_button) #綁定事件1——打開文件夾 self .bind(wx.evt_button, self .onbutton_opendir, open_prj_button) #綁定事件2——打開文件夾 self .bind(wx.evt_button, self .onbutton_projection, projection_button) #綁定事件3——投影 self .bind(wx.evt_text, self .inputtext, self .shp_text) #綁定事件4——直接在文本框輸入路徑 self .bind(wx.evt_text, self .inputtext, self .prj_text) #綁定事件4——直接在文本框輸入路徑 def onbutton_opendir( self ,control): #在事件源(控件)上產(chǎn)生特定事件(左鍵單擊)后的處理程序 # create open file dialog opendirdialog = wx.dirdialog(parent = self , message = "選擇一個文件夾" , defaultpath = "", style = wx.dd_default_style) opendirdialog.showmodal() self .path = opendirdialog.getpath() print ( self .path) opendirdialog.destroy() control.setvalue( self .path) #將路徑顯示在文本框1中 def inputtext( self ,control): self .path = control.getvalue() def onbutton_projection( self ,event): inworkspace = self .shp_text.getvalue() prjdir = self .prj_text.getvalue() prjworkspace = createprjfile(inworkspace,add_str = "prj_" ) #新建投影成果根目錄prjworkspace projection(inworkspace,prjdir,prjworkspace) if __name__ = = "__main__" : # #創(chuàng)建窗口對象 frm = myframe() # #顯示窗口 frm.show() #進(jìn)入主事件循環(huán) app.mainloop() |
功能正確,但提示有冗余報錯
平面投影代碼
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# coding=utf-8 # --------------------------------------------------------------------------- # # 為文件夾內(nèi)所有城市的經(jīng)緯度shp生成對應(yīng)的平面shp # 注意文件夾內(nèi)所有路徑須為英文路徑,python2 # # --------------------------------------------------------------------------- #注意西安和香港 import arcpy import os,re import time #os,arcpy文件覆蓋寫 arcpy.env.overwriteoutput = true #啟用覆蓋地理處理操作的輸出 inworkspace = r 'd:\pythoncode_e3dcm\01data\04backpoiprocess\02poi\poi_4' #待投影根目錄【運行前確認(rèn)修改!】 prjdir = r 'd:\pythoncode_e3dcm\01data\prjfile' #投影文件所在路徑 ##判斷是否為shp文件 def isshapefile(file_name): if ".shp" in file_name and ".xml" not in file_name: flag = true else : flag = false return flag ##建立對應(yīng)投影成果文件夾——絕對路徑中【葉子節(jié)點】文件夾前加"add_str" def createprjfile(file_dir,add_str): dir_name,base_name = os.path.split(file_dir) #如果路徑末有//,則輸出路徑和為空文件名 #print dir_name #print base_name prj_file_dir = os.path.join(dir_name,add_str + base_name) if os.path.exists(prj_file_dir) = = false: os.mkdir(prj_file_dir) print prj_file_dir + u " 文件夾新建成功!" return prj_file_dir #返回一個文件在投影文件列表中匹配的投影文件 def prjmatch(shp_dir,prjdir): #shp_dir最好為絕對路徑,1文件夾或2文件名匹配投影文件均可行 #打印作為參數(shù)輸入的shp路徑 print "\nshp_dir:\n" + shp_dir.lower() prjfile_ls = os.listdir(prjdir) #city_ls = [i.replace(suffix,"") for i in os.listdir(prjdir)] #檢查城市名是否有包含的情況,如香港xinggang包含西安xian for prjfile in prjfile_ls: suffix = ".prj" city = prjfile.replace(suffix,"") if city.lower() in shp_dir.lower(): print city #排除特殊城市西安xian【有錯誤!】 if ( "xian" in shp_dir.lower()) and ( "xianggang" not in shp_dir.lower()): print "xian branch prj" return os.path.join(prjdir, "xian.prj" ) elif "xianggang" in shp_dir.lower(): print "xianggang branch prj" return os.path.join(prjdir, "xianggang.prj" ) else : #忽略shp_dir中城市名大小寫 prjfile_dir = os.path.join(prjdir,prjfile) print "ordinary branch:\n" + prjfile_dir + "\n" return prjfile_dir #else: #print "prj match fail!" #如果列表中的元素是字符串,判斷任一元素不被包含在其他元素中 num_shp = 0 num_shp_ok = 0 num_shp_fail = 0 ##針對文件夾內(nèi)shp,建立對應(yīng)所在投影文件夾、并投影 #參數(shù):inworkspace待投影成果根目錄,ini_root(=inworkspace)新建投影文件夾替換字符用 def projection(inworkspace,prjdir,prjworkspace): #遞歸函數(shù)的參數(shù)只能是變量參數(shù) global num_shp global num_shp_ok global num_shp_fail file_names = os.listdir(inworkspace) for file_name in file_names: #文件或文件夾名,不是絕對路徑 file_dir = os.path.join(inworkspace,file_name) #待投影文件的絕對路徑 if os.path.isdir(file_dir): #判斷是否為文件夾 #建立對應(yīng)投影成果文件夾 prjsubfolder = file_dir.replace(inworkspace,prjworkspace) if os.path.exists(prjsubfolder) = = false: os.mkdir(prjsubfolder) #inworkspace = file_dir#將當(dāng)前文件夾當(dāng)作根目錄 projection(file_dir,prjdir,prjsubfolder) #遞歸 else : if isshapefile(file_name): print "file_name:" + file_name #投影成果shp的絕對路徑 prj_file_dir = file_dir.replace(inworkspace,prjworkspace) #print prj_file_dir #投影文件prj的絕對路徑 prjfile_dir = prjmatch(file_dir,prjdir) #print prjfile_dir #如果投影成果不存在(6個文件),再投影 try : arcpy.project_management(file_dir, prj_file_dir, prjfile_dir) #prj_file_dir投影成果shp文件的路徑,prjfile_dir投影文件的路徑 num_shp_ok + = 1 print file_dir + u "投影成功!" except : num_shp_fail + = 1 print file_dir + u "投影失敗!" else : pass #print "srange error in: "+file_dir print inworkspace + u " 文件夾投影完成!" #注意不是局部變量inworkspace print str (num_shp_ok) + u "shp文件投影成功!" print str (num_shp_ok) + u "shp文件投影失敗!" ## return paths if __name__ = = '__main__' : time_start = time.time() prjworkspace = createprjfile(inworkspace,add_str = "prj_" ) #新建投影成果根目錄prjworkspace projection(inworkspace,prjdir,prjworkspace) time_end = time.time() print u '投影耗時:{:.2f}min' . format ((time_end - time_start) / 60 ) |
附錄:在python2中調(diào)試wxpython,界面一閃而過的解決辦法:
到此這篇關(guān)于python2利用wxpython生成投影界面工具的圖文詳解的文章就介紹到這了,更多相關(guān)python投影界面工具內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/lzqg1990/article/details/113685905