GetParameterAsText
The GetParameterAsText is the arcpy command that will be used and it will create a dialog box that will allow the user to input information. The utilization of this tool is a two-step process:
Variable = arcpy.GetParameterAsText(index)
- Creating the code for the script
- Setting the input parameters, done in the Add Script Wizard
Variable = arcpy.GetParameterAsText(index)
Problem
In earlier versions of ArcMap, most files were stored as individual shapefiles and used the Microsoft Windows Tree Structure to separate geographic regions; today it is strongly recommended that mapping data (feature class files) be stored within a geodatabase. This change in how information is stored has required organizations to move shapefiles into geodatabases. The script that will be composed will complete this task in an automated process. This discussion of geodatabases will not include relational databases but only file and personal geodatabases. It is assumed that the geodatabase has already been created prior to working on this lesson, if it has not been created, create an empty geodatabase. Make sure that none of the shapefiles to be placed in the geodatabase already exists in it; this will cause an error or will create a file with a different name. This occurs since there are no commands in the script provided for overwrite existing files.
The user will specify the input folder that will contain the shapefile(s). The user will also specify the geodatabase that the geospatial files will be stored in. As noted previously it is assumed that the geodatabase has already been created before running the script.
- For example, while creating the script, it is run and thus the part of the scripting putting the feature class file into geodatabase is executed, therefore the geodatabase is no longer empty and will contain a file, when the script is run again a conflict will arise. The user needs to use ArcCatalog and manually delete the file from the geodatabase. If the file is not removed, the script will create a file with a generic name or a number after the provided name, thus each time the script is tested another file will be created which will clutter the geodatabase with useless information.
- For this example make sure all the shapefiles to be moved to the geodatabase are placed in the same file folder, remember shapefiles are composed of multiple individual files and all must be placed appropriately in the same folder. The script that will be created will move all files from a single folder.
The user will specify the input folder that will contain the shapefile(s). The user will also specify the geodatabase that the geospatial files will be stored in. As noted previously it is assumed that the geodatabase has already been created before running the script.
Creating a Script
In Figure 2, the script is shown moving the shapefiles to the geodatabase; it was created in an IDE but not executed.
- Loading arcpy (came from the author’s starting template) is not required but will not damage the process.
- The OS is the operating system and is required to make this script function properly.
- The environment is loaded from arcpy. The environment will be used in context with the GetParameterAsText command.
- In the next line of code, the workspace will be set to the value inputted by the user in the first GetParameterAsText with an index of 0, this is the location of the shapefile folder.
- The next functional line again uses the GetParameterAsText with a 1 index, which will be the user supplied output location; this is the location of the geodatabase.
- The ListFeatureClasses will copy all the shapefiles that are saved in the specified workspace. No parameters are required with this command, so the variable shapefiles contains all the content from a specified file folder.
- A for loop is used to write each shapefile to the geodatabase. Each iteration of the loop will write one shapefile and the associated information to the geodatabase. The variable shape will contain the information to be written. The command Describe and CopyFeatures are used. The Describe command returns the properties of the file contained in shape. The CopyFeatures command has multiple parameters. The first parameter is the input feature, which is contained in the file, named shape, the second parameter is the output location and the last parameter is the name and properties of the file.
Adding the Script
Normally when a script is constructed, it is run in the IDE and edited until the script runs properly without errors and that is the end of the process. For this case running the script is possible within the IDE, but may not be functional since it requires the use of the toolbox wizard. If instead the script was the creation of a Python toolbox, which requires more programing, then it might fully execute. That type of script requires more Python scripting knowledge than is covered in this course. The next step in the construction process will be using the tool wizard to load the script properly, see Figure 3.
|
Editing and Correcting Errors
Once the script has been created in the wizard, it can be edited. To edit the script right click on it and select edit. This will open the script in a text editor, generally Notepad. The script can also be edited using an IDE, but not directly. If the learner right clicks on the script and selects properties, those properties implemented in the wizard can be edited. Once the editing has been completed, it is important to make sure that no files were created. If files were created, they must be manually deleted, since they can cause potential errors when the script is run again.
Once the editing is completed and any files created, the script can be run again. When the script tool is run, it should provide an input box, in which the user will locate the input folder; this is done through the use of the browse button. Next the storage location should be specified and again can be located using the browse button. When the script has completed the operation, the new shapefiles should be located in the geodatabase. There should be a one to one correspondence to the old older shapefiles. It is important that none of the shapefiles used in the process be opened in ArcMap when the script is run, this can create a lock on the shapefile and thus the operation will not function properly.
Once the editing is completed and any files created, the script can be run again. When the script tool is run, it should provide an input box, in which the user will locate the input folder; this is done through the use of the browse button. Next the storage location should be specified and again can be located using the browse button. When the script has completed the operation, the new shapefiles should be located in the geodatabase. There should be a one to one correspondence to the old older shapefiles. It is important that none of the shapefiles used in the process be opened in ArcMap when the script is run, this can create a lock on the shapefile and thus the operation will not function properly.