SoFunction
Updated on 2024-10-30

Graphical details of python2's tool for generating projection interfaces using wxpython

Functions of this projection interface tool:

Prepare the .prj projection file by taking the WGS84 latitude and longitude coordinates shp file in the input folder, projecting it to a flat file, with the results automatically named prj_*** and newly created in the same path as the input folder.

Next Objective:

Generating exe files using pyinstaller or other packaging libraries is currently stalled on issues related to python2 syntax, arcpy packaging errors.

References:

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

"How to package arcpy scripts with py2exe?".

Schematic diagram of GUI interface

The folder in which the projection files are located has the following structure:

                 

GUI code

# -*- coding: utf-8 -*-
# =============================================================================
# Input files - point and click, copy, drag and drop
# Select the folder to be projected, the folder where the projected files are located.
# =============================================================================
"""
Created on Thu Feb  4 16:12:00 2021
@author: zhutong
"""
 
import wx
from Def_Projection_common_E import createPrjFile,projection
# Create application objects
app = ()
 
 
# Customized window class MyFrame
class MyFrame():
    
    def __init__(self):
        
        super(MyFrame,self).__init__(None,title="Universal Latitude and Longitude to Plane Coordinates Tool",pos=(600,500),size=(600,300))#Python2 syntax
        
        panel = (parent=self)# Create panel objects
        
        self.statictext_shp = (parent=panel,label="The folder where the data to be projected is located.",pos=(60,30))# Create static text objects
        self.statictext_shp = (parent=panel,label="The folder where the projection file is located.",pos=(60,80))# Create static text objects
 
        self.shp_text = (parent=panel,value="",pos=(60,50),size=(350,25))# [Text Control 1]
        open_shp_button = (parent=panel, label='Open',pos=(430,50))#[Button Control 1]
 
 
        
        self.prj_text = (parent=panel,value="",pos=(60,100),size=(350,25))# [Text Control 2]
        open_prj_button = (parent=panel, label='Open',pos=(430,100))# [Button Control 2]
        
        projection_button = (parent=panel, label='Planar projection',pos=(150,150),size=(180,30))# [Button Control 3]
        
        (wx.EVT_BUTTON, self.onButton_opendir, open_shp_button)#Binding Event 1 - Open Folder
        
        (wx.EVT_BUTTON, self.onButton_opendir, open_prj_button)#Binding Event 2 - Open Folder
        
        (wx.EVT_BUTTON, self.onButton_projection, projection_button)#Binding Event 3 - Projection
 
        (wx.EVT_TEXT, , self.shp_text)#Binding Event 4 - Entering a path directly into a text box
 
        (wx.EVT_TEXT, , self.prj_text)#Binding Event 4 - Entering a path directly into a text box
 
    def onButton_opendir(self,control):# Handlers after a specific event (left-click) is generated on the event source (control)
        # Create open file dialog
        openDirDialog = (parent=self, message="Select a folder.", defaultPath="", style=wx.DD_DEFAULT_STYLE)
         
        ()
         = ()        
        print()
        
        ()
        ()# Display the path in text box 1
 
    def inputText(self,control):
         = ()
 
    def onButton_projection(self,event):
        inWorkspace = self.shp_text.GetValue()
        prjdir = self.prj_text.GetValue()
        prjWorkspace = createPrjFile(inWorkspace,add_str="prj_")# New root directory for projection results prjWorkspace
        projection(inWorkspace,prjdir,prjWorkspace)
    
if __name__ == "__main__":
    # # Create a window object
    frm = MyFrame()
    # # Display window
    ()       
    
    # Enter the main event loop
    ()

Functions correctly, but suggests a redundancy error

Plane projection code

# coding=utf-8
# ---------------------------------------------------------------------------
#
# Generate corresponding planar shp for latitude/longitude shp for all cities in the folder
# Note that all paths in the folder must be English paths, python2
#
# ---------------------------------------------------------------------------
#Note Xi'an and *
import arcpy
import os,re
import time
#os,arcpy file override write
 = True  #Enable overriding output of geoprocessing operations
 
inWorkspace = r'D:\PythonCode_E3DCM\01Data\04BackPoiProcess\02POI\POI_4'# Root directory to be projected [Confirm changes before running!
prjdir = r'D:\PythonCode_E3DCM\01Data\prjFile'#Path to the projection file
 
 
## Determine if it's a shp file
def isShapefile(file_name):
    if ".shp" in file_name and ".xml" not in file_name:
        flag = True
    else:
        flag = False
    return flag
 
## Create a folder of corresponding projection results - add "add_str" in front of the [leaf node] folder in the absolute path.
def createPrjFile(file_dir,add_str):
    dir_name,base_name = (file_dir)# Output path and empty filename if // is at the end of the path.
    #print dir_name
    #print base_name
    prj_file_dir = (dir_name,add_str + base_name)
    if (prj_file_dir) == False:
        (prj_file_dir)
    print prj_file_dir + u" Folder created successfully!"
    return prj_file_dir
 
# Returns a projected file that matches a file in the projected file list
def prjMatch(shp_dir,prjdir):#shp_dir is preferred to be an absolute path, 1 folder or 2 filenames matching the projected file are both possible
    # Print the path to the shp entered as a parameter
    print "\nshp_dir:\n" + shp_dir.lower()
    prjfile_ls = (prjdir)
    #city_ls = [(suffix,"") for i in (prjdir)]
    
    #Check if the city name contains cases, such as * xinggang contains Xian xian
    for prjfile in prjfile_ls:
 
        suffix = ".prj"
        city = (suffix,"")
        
        if () in shp_dir.lower():
            print city          
            
            #Excluding the special city of Xi'an xian [There's an error!]
            if ("xian" in shp_dir.lower()) and ("xianggang" not in shp_dir.lower()):
                print "xian branch prj"
                return (prjdir,"")
            
            elif "xianggang" in shp_dir.lower():
                print "xianggang branch prj"
                return (prjdir,"")
 
            else:# Ignore case of city names in shp_dir
                prjfile_dir = (prjdir,prjfile)
                print "Ordinary branch:\n" + prjfile_dir + "\n"
                return prjfile_dir
 
        #else:
            #print "prj match fail!"
        
# If the elements in the list are strings, determine if any element is not included in the other elements.
 
num_shp = 0
num_shp_ok = 0
num_shp_fail = 0
## Create a projection folder for the shp in the folder and project it.
#Parameters: inWorkspace to be projected to the root directory, ini_root(=inWorkspace) to create a new projection folder to replace the characters.
def projection(inWorkspace,prjdir,prjWorkspace):# Recursive function arguments can only be variable arguments
    
    global num_shp
    global num_shp_ok
    global num_shp_fail
 
    file_names = (inWorkspace)
 
    for file_name in file_names:#File or folder names, not absolute paths
        file_dir = (inWorkspace,file_name)#Absolute path to the file to be projected
 
        if (file_dir):# Determine if it is a folder
 
            # Create a folder of corresponding projection results
            prjSubfolder= file_dir.replace(inWorkspace,prjWorkspace)
            if (prjSubfolder) == False:
                (prjSubfolder)
 
            #inWorkspace = file_dir# treat the current folder as the root directory
 
            projection(file_dir,prjdir,prjSubfolder)#recursive
 
        else:
            if isShapefile(file_name):
                print "file_name:"+file_name
                # Absolute path to the projection result shp
                prj_file_dir = file_dir.replace(inWorkspace,prjWorkspace)
                #print prj_file_dir
                
                # Absolute path to the projection file prj
                prjfile_dir = prjMatch(file_dir,prjdir)
                #print prjfile_dir
                #If the projection results do not exist (6 files), project again
                try:
                    arcpy.Project_management(file_dir, prj_file_dir, prjfile_dir)
                    #prj_file_dir path to projection result shp file,prjfile_dir path to projection file
                    num_shp_ok += 1
                    print file_dir + u"Projection successful!"
                except:
                    num_shp_fail += 1
                    print file_dir + u"Projection failed!"
 
            else:
                pass
                #print "Srange ERROR in: "+file_dir
 
    print inWorkspace + u" Folder projection complete!"# Note that it is not the local variable inWorkspace
    print str(num_shp_ok) + u"shp file projection successful!"
    print str(num_shp_ok) + u"shp file projection failed!"
 
##    return paths
if __name__ == '__main__':
    time_start=()
    prjWorkspace = createPrjFile(inWorkspace,add_str="prj_")# New root directory for projection results prjWorkspace
    projection(inWorkspace,prjdir,prjWorkspace)
    time_end=()
    print u'time-consuming projection:{:.2f}min'.format((time_end-time_start)/60)

Appendix:Solution for debugging wxpython in python2 with a flashing interface:

To this point this article on python2 using wxpython to generate a projection interface tool graphic details of the article is introduced to this, more related python projection interface tool content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!