Thursday, June 25, 2015

GIS Programming - Module 6 - Geoprocessing with Python

Python Results for All Three Tasks
 We used ArcGIS tools again this week to construct a script, but instead of using the ModelBuilder to put it together, we went directly to PythonWin and entered lines of code with ArcGIS tool functions.

There are very useful Help pages for each tool, that can be found via the ArcMap toolbox, or the Python window in ArcMap.  Correct syntax and parameters, as well as examples, are shown for every tool.








The stand-alone Python script of this week's assignment had three tasks to accomplish on a point shapefile representing locations of hospitals:

1) Add XY coordinates;
2) Create 1000 meter buffers around each hospital point;
3) Dissolve the multiple buffers into a single large buffer.

The figure above shows the results generated in the Interactive Window of PythonWin.  To obtain the results like this, three additional lines of code must be added to the script (one after each function or tool code), using the GetMessages function. This is a good way to keep track of what you've done, and document that the various parts of the script have run successfully.  The parameters that were used are also shown.


Results of Task 1
This figure shows the result of the first task in ArcMap, to add XY coordinates to each record in the hospitals.shp file. A few of the new fields are seen here in the attribute table. Each row is a record representing a hospital; other previously-existing data fields (not shown) are to the left in this table.








Results of Task 2
This shows the resulting output from the Buffer task (2) in the script.  A buffer zone with radius of 1000 meters was added around each small point representing a hospital.       The new polygon shapefile that was generated is called hosp_buffer.shp.  This shapefile has multiple features, one for each hospital point.














Results of Task 3
This figure shows the results in the ArcMap display of the last task executed by the script, to dissolve the multiple buffers into a single feature.  This new output polygon shapefile is called hosp_dis_buff.shp.

This feature represents all areas that lie within 1000 meters of a hospital.









As can be seen from its attribute table below, this shapefile has only a single feature, comprising all 1000 meter buffers for the hospitals.

Single Feature of Dissolve












Below is the detailed Process Summary for writing this script.


1.      I opened a new, blank Python script in PythonWin and saved it, then added the commentary at the top regarding name, date, etc.
2.      Before anything else can work, you must enter this line:  import arcpy, to have access to the functions of ArcGIS site package.  Also enter this line of code: from arcpy import env.  This imports the environment module, so that the environment (including file path) can be set.
3.      Two other lines to enter at the beginning of any script will 1) set the environment (specify the file path) and 2) make it possible for Python to overwrite previously generated results.  These commands in my script were:  
env.workspace = "S:/GISProgramming/Module6/Data"  
env.overwriteOutput = True
4.      The three tasks that this script had to complete were to 1) Add XY coordinates to the hospitals.shp layer 2) Make a 1000 meter buffer around all the hospital point features and 3) Dissolve the buffers into a single feature. 
5.      After the execution of each tool, the script will print the messages from that tool.
6.      To find out how to make the script for each tool, I opened each one in ArcMap, went to Tool Help, and looked at the script syntax and code samples.  Each parameter is also explained there.
7.      There are two parts to the code samples: one is the most basic form, and the other is an entire stand-alone script in PythonWin.  Both are useful.  The most basic form lists the parameters in order, by name.  The more involved script shows how the lines of code should be written, with respect to file paths and layer names.  This part also gives a few ideas of how to improve the script, such as by making extra copies of layers that will be changed. 
8.      Before executing the AddXY function, I did a Copy function on the hospitals.shp layer, to preserve an original version of that layer in the Data folder. This layer is called original_hospitals.shp.      The hospital.shp file itself was then altered by the AddXY tool/function. 
9.      Copy function:  arcpy.Copy_management(in_data, orig_data)
10.   But first (before steps 8-9), I assigned variables to the hospitals.shp (in_data) and the original_hospitals.shp (orig_data).  This was necessarily done before the Copy function was executed. 
11.   I then ran the AddXY function on in_data (the variable assigned to hospitals.shp) which added two new columns to the attribute table, for X and Y coordinates.
12.   The code is:    arcpy.AddXY_management(in_data)
13.   After this ran, I added the line of code:
print (arcpy.GetMessages() + "\n")
14.   This causes the results messages to be printed out for the tool that just ran and 
+ "\n" after the main part of the code causes a blank line to be returned after the messages, to enhance readability. 
15.   I added this line of code: print +"\n" in other parts of the script: this does the same thing. That serves to separate the parts and make the results easier to read. 
16.   After a couple of blank lines were returned, I added the code: print “Task 1 complete.”
17.   Before the next task results, I had the code return two more blank lines (as in step 13).
18.   Task number 2 was to create a 1000 meter buffer around each hospitals.shp point feature.
19.   First, I assigned two more variables:  buffer is for the new output shapefile that will be created in the Results folder.  Its line of code is:
buffer = "S:/GISProgramming/Module6/Results/hosp_buffer"
20.   hosp_buffer is the output file. 
21.   The path could also have read:
buffer = "../Results/hosp_buffer" instead of the entire path.  This is the syntax for a different folder at the same level (ex. Results) within the same higher-up folder as the folder specified in the environments (Data).  In other words, to go from Module6/Data to Module6/Results.
22.   The distance for the buffer is assigned the other variable: distance = "1000 meters"
23.   The next line is the Buffer function code: 
24.   arcpy.Buffer_analysis(in_data, buffer, distance)
25.   Again, the messages for the tool execution are printed using the GetMessages function, blank lines are added, and the line “Task 2 completed.” is returned.
26.   The last task is to dissolve the multiple hospital buffers into a single feature. 
27.   The code is:
28.   arcpy.Dissolve_management(buffer, dissolve)
29.   The default parameter for multi-part is taken, so no further optional parameters are listed.  This causes all the multiple hospital buffer features to be dissolved into one single feature for the hosp_dis_buffer.shp feature. 
30.   Again, the messages are printed for this tool’s execution, using the GetMessages function. 
31.   print “Task 3 complete.” and 2 blank lines are returned.
32.   To signify that the entire script has run, we have one last line:
33.   print “All processes complete.”

34.   A couple more print “\n” lines are added at the bottom, to separate multiple runs in the Interactive Window.  

No comments:

Post a Comment