您的位置:首页技术文章
文章详情页

python2利用wxpython生成投影界面工具的图文详解

【字号: 日期:2022-06-21 17:30:32浏览:2作者:猪猪

本投影界面工具的功能:

准备好.prj投影文件,将输入文件夹内的WGS84经纬度坐标shp文件,投影为平面文件,成果自动命名为prj_***并新建在输入文件夹同一路径下。

下一步目标:

利用pyinstaller或其他打包库生成exe文件,目前停滞在python2语法、arcpy打包出错相关问题上。

参考文献:

《Using Py2exe with Arcpy- It can be done easily!》

《如何使用py2exe打包arcpy脚本?》

GUI界面示意图

python2利用wxpython生成投影界面工具的图文详解

投影文件所在文件夹结构如下:

python2利用wxpython生成投影界面工具的图文详解 python2利用wxpython生成投影界面工具的图文详解

GUI代码

# -*- coding: utf-8 -*-# =============================================================================# 输入文件——点选、复制、拖拽# 选择待投影的文件夹、投影文件所在文件夹# ============================================================================='''Created on Thu Feb 4 16:12:00 2021@author: zhutong''' import wxfrom Def_Projection_common_E import createPrjFile,projection#创建应用程序对象app = wx.App() #自定义窗口类MyFrameclass MyFrame(wx.Frame):def __init__(self):super(MyFrame,self).__init__(None,title='通用经纬度转平面坐标工具',pos=(600,500),size=(600,300))#Python2语法panel = wx.Panel(parent=self)#创建面板对象self.statictext_shp = wx.StaticText(parent=panel,label='待投影数据所在文件夹',pos=(60,30))#创建静态文本对象self.statictext_shp = wx.StaticText(parent=panel,label='投影文件所在文件夹',pos=(60,80))#创建静态文本对象 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):#在事件源(控件)上产生特定事件(左键单击)后的处理程序# Create open file dialogopenDirDialog = 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_')#新建投影成果根目录prjWorkspaceprojection(inWorkspace,prjdir,prjWorkspace) if __name__ == '__main__': # #创建窗口对象 frm = MyFrame() # #显示窗口 frm.Show() #进入主事件循环 app.MainLoop()

功能正确,但提示有冗余报错

python2利用wxpython生成投影界面工具的图文详解

平面投影代码

# coding=utf-8# ---------------------------------------------------------------------------## 为文件夹内所有城市的经纬度shp生成对应的平面shp# 注意文件夹内所有路径须为英文路径,python2## ---------------------------------------------------------------------------#注意西安和香港import arcpyimport os,reimport time#os,arcpy文件覆盖写arcpy.env.overwriteOutput = True #启用覆盖地理处理操作的输出 inWorkspace = r’D:PythonCode_E3DCM01Data04BackPoiProcess02POIPOI_4’#待投影根目录【运行前确认修改!】prjdir = r’D:PythonCode_E3DCM01DataprjFile’#投影文件所在路径 ##判断是否为shp文件def isShapefile(file_name): if '.shp' in file_name and '.xml' not in file_name:flag = True else:flag = False return flag ##建立对应投影成果文件夹——绝对路径中【叶子节点】文件夹前加'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文件名匹配投影文件均可行 #打印作为参数输入的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 = 0num_shp_ok = 0num_shp_fail = 0##针对文件夹内shp,建立对应所在投影文件夹、并投影#参数:inWorkspace待投影成果根目录,ini_root(=inWorkspace)新建投影文件夹替换字符用def projection(inWorkspace,prjdir,prjWorkspace):#递归函数的参数只能是变量参数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):#判断是否为文件夹 #建立对应投影成果文件夹 prjSubfolder= file_dir.replace(inWorkspace,prjWorkspace) if os.path.exists(prjSubfolder) == False:os.mkdir(prjSubfolder) #inWorkspace = file_dir#将当前文件夹当作根目录 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 pathsif __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中调试wxpython,界面一闪而过的解决办法:

python2利用wxpython生成投影界面工具的图文详解

python2利用wxpython生成投影界面工具的图文详解

到此这篇关于python2利用wxpython生成投影界面工具的图文详解的文章就介绍到这了,更多相关python投影界面工具内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!

标签: Python 编程
相关文章: