Thursday, June 18, 2015

GIS5103 Programming - Geoprocessing in ArcGIS

Here we are in week five-- time is flying fast. Learning the ins-and-outs of Python has been a challenge, but it has also been fun. This week we learned how to:


- Create a toolbox

- Create a tool using ModelBuilder

- Set Model Parameters

- Export a script from ModelBuilder

- Update a model-derived script to work outside of ArcMap

- Create a script tool


The Model, Script and Tool were all created to Clip the Soils layer to the Basin layer, then Select from the Soils layer attribute table "Not Prime Farmland." And finally, Erase the soil selection from the basin polygon. Working with ModelBuilder was quite enjoyable though exporting my Model as a Script left me with more work than I had anticipated. ModelBuilder does a great job of visually presenting you an overall picture of the task or tasks you are attempting to accomplish. 

A few notes on my process for creating my SoilErase model
1.       Before creating the model, I checked that “Overwrite the outputs of geoprocessing operations” was selected.
2.       Next, I created a toolbox
o   ArcCatalog > Toolboxes > My Toolboxes > New > Toolbox
3.       To create the model, I selected from my newly created toolbox, New> Model and named it SoilErase
o   I added the Basin and Soils shapefiles by dragging and dropping directly to the blank model.
o   I selected the Clip tool from the toolbox and dragged-dropped to the model canvas as well.
o   I used the connector tool to make the Soils shapefile the input and the Basin shapefile the clip feature

4.       Once I had the beginning of the model I ran it a few times to ensure it was going to execute.         
o   I had difficulties getting the Erase portion to work properly and I actually created three models.
o   My third model executed correctly and places new layers on my map:
§  soils_Clip.shp
§  soils_Clip_Select.shp
§  soils_Clip_Select_Erase.shp

5.  The model executed correctly as far as I could tell. Working on the python script was another matter. I had to modify the script, as outlined in the Lab instructions, each time I re-created the model. I also tried using debugging to find if I had other errors.  


So, when you export the model to Python, the code may or may not be nearly complete, so this is the real detective aspect of "Modelbuilder-to-Python code" and this is why I had to re-create my model several times. I built my Model using the Clip, Select and Erase tools, multiple times to determine where my errors were. I reset the Environment, spelled-out the file pathways, and added a line of code to defeat the "Output Feature Class already exits" error. The line of code to resolve this error is:

                                                       arcpy.env.overwriteOutput = True  

By the time I was done I could run the script in ArcGIS successfully and see the layers added to my map. When I ran the script as a tool outside of ArcGIS I was not sure of the results. That is I could not verify the new layers were added to my map even though they existed in my file folder.  Here's what my final output looked like:


No comments:

Post a Comment