Three contiguous counties will be merged together to create a single regional geography. The counties will have the same attribute information. This operation will be completed using a single Python script.
Comments have been placed in the script in Figure 4. Each line of the script will be explained that has functional code.
- The first two lines of the functional code are used to load arcpy and the environment.
- The location of the geodatabase is set using the workspace location. The ‘r’ allows the standard pathway convention to be used. The geodatabase is located in the author’s ‘gis data’ folder and called KY1.
- A list variable is created that contains the names of the three counties that are being used. One of the counties is in Indiana and the other two are in Kentucky. Note how descriptive the naming convention of the content and the variables are. The naming convention will allow the data to be easily located.
- The merge command is in the management toolbox. The first parameter is a list variable, since it is describing those counties which will be merged together. They could have been manually inputted into this function but that is not the recommended way. The second parameter is the name of the output file, which could have been a variable. All the data and the saving location is in the KY1 geodatabase, since the workspace is set.
- To confirm the operation go to ArcMap or ArcCatalog and make sure the new file has been created and that when displayed or previewed it looks correct. Do not have any files open while running the Python script. Close ArcMap or ArcCatalog before proceeding to the next step. If errors are generated, after fixing the issue, delete the created file, this will be necessary because no overwrite commands were used in the script.
This section of the merge example will explain how to complete redundant tasks more efficiently. The merge command demonstrated in Part I will be used, but an additional merge statement will be added so that multiple components can be combined together. Thus, both county boundaries and census tracts for the three counties will be merged together as well as creating variable lists. There are multiple ways that a script could be modified to increase functionality.
- The first three statements load the arcpy module, sets the environment and the pathway to the geodatabase.
- A string variable named Census is defined, notice the underscore is used as part of the variable, this is because spaces are not valid characters and cannot be used.
- A string list variable for the Census Tracts is defined; it uses a mathematical function to combine the county names and the value of the Census variable.
- Another string list variable is defined which is the names of the county boundary files.
- There are two merge statements, one to combine the county boundary files together and the other to combine the census tract boundary files together.
- Confirm the results of both of these merge commands by using ArcMap and displaying the information. Close ArcMap when the review is completed.