Thursday, June 25, 2015

Geoprocessing with Python

Are we really half-way through the semester already? Indeed time is flying. But I can definitely say we are having way too much fun. Right? This was a very cool Module. Now we are learning how to use the power of Python (not Monty-though that would be cool too) to do some ArcGIS Geoprocessing. ArcGIS and Python are closely integrated. ArcPy modules, classes, and functions give acess to all the Geoprocessing tools in ArcGIS. It was fun using some of them for this Lab.

Our task was to write a script that performs three geoprocessing functions: Add XY coordinates to the hospitals.shp layer using the AddXY tool; Create a 1000 meter buffer around the hospital features using the Buffer tool; and Dissolve the hospital buffers into a separate, single feature using the Dissolve tool. Also, after each of the three tasks above were performed, we needed to print the messages from that tool, using the GetMessages() function. Here's the run down on how I accomplished this for this week's lab:


I began by reviewing my data in ArcCatalog.

1. I copied the data from the R-drive and extracted into my Module 6 folder.
2. I opened a blank map so I could access the Arcpy window and manipulate the data I was given.
3. I set the environment (workspace) to my Module6 Data folder and prepared to begin working on my code.
4. From doing the Exercise, I surmised that to create the script I would need something like the following code:
arcpy.Buffer_analysis("hospitals.shp","S:/GISProgramming/Module6/Results/hospitals_buffer.shp","1000 METERS","","","ALL")

5. I spent time adjusting the above and researching ArcGIS Help to ensure I had this part correct.
6. I set the Local Variables for in_data & in_features and ran this part of the code.
7. Finally, I was convinved that my code would do the actions of create a buffer, and dissolve the hospital buffers into a separate, single feature.
8. Next, I needed to use the AddXY tool to insert an X and Y column at the end of my attribute table for the hospital layer. I found the format for AddXY_management.
9. Since I had set the environment to my workspace ("S:/GISProgramming/Module6/Data"), I could Execute the AddXY tool with the following code: arcpy.AddXY_management(in_features)
10. The above steps gave me a script that would perform the three geoprocessing functions: Add XY coordinates to the hospitals.shp layer; Create a 1000 meter buffer around the hospital features using the Buffer tool; and Dissolve the hospital buffers into a separate, single feature.
11. However, the Lab called for a separate line of code for the Dissolve feature, so I had to look up the Dissolve tool to use this tool separately.
12. The Dissolve tool ArcGIS Help stated that: Dissolve (Data Management) Syntax=

Dissolve_management (in_features, out_feature_class, {dissolve_field}, {statistics_fields}, {multi_part}, {unsplit_lines})

13. This was a tremendous help to me. I used the syntax from above to create my code.

The below code dissolves the hospital buffers into a separate, single feature:

fc = arcpy.Dissolve_management("S:/GISProgramming/Module6/Results/hospitals_buffer.shp","S:/GISProgramming/Module6/Results/hospitals_dissolved.shp")#, "", "","ALL")

14. By this time I had all three Geoprocessing functions working correctly, however, I did not have the code for printing the messages.
15. The last part for me: Find the code example for printing messages.
16. This felt like the longest part of this lab for me. I did my research and eventually found the example code at: http://resources.arcgis.com/en/help/main/10.2/index.html#//03q30000006s000000

GetMessage (arcpy)
import arcpy
fc = arcpy.GetParameterAsText(0)
arcpy.GetCount_management(fc)
# Print the first and last message returned by the last
#  tool executed (GetCount)
message_count = arcpy.GetMessageCount()
print(arcpy.GetMessage(0))
print(arcpy.GetMessage(message_count - 1))

17. The above was to print the first and last message of the tool. I modified the above code and after a while, I had:
# Prints Geoprocessing message
fc = arcpy.GetParameterAsText(1)
message_count = arcpy.GetMessageCount()
print(arcpy.GetMessage(0))

I added the above code after each Geoprocessing function and ran it a few times to make sure it worked properly. When I was satisfied my code was working properly, I saved my script as Mod6_gilcastillo.py and exited PythonWin. Here's my results:



No comments:

Post a Comment