Python Coding Examples for Drive Time Analysis

2,025 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,025
On SlideShare
0
From Embeds
0
Number of Embeds
19
Actions
Shares
0
Downloads
19
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Python Coding Examples for Drive Time Analysis

  1. 1. Python & Drive Time Analyses Python 2.6 ArcCatalog 9.3.1 ArcMap 9.3.1 Project technologies Programming language: Software products: Janice Poehlman Division of Forestry, Dept. Natural Resources [email_address]
  2. 2. Use python scripts to summarize the total number of people who are within drive times of a location. 30 Minute 60 Minute 90 Minute 120 Minute Drive Time Service Area Network Analyst }
  3. 3. <ul><li>Data </li></ul><ul><li>WI, MN, MI, IL, IA </li></ul><ul><li>Census 2000 </li></ul><ul><li>Block Groups </li></ul><ul><li>TIGER 2000 Road </li></ul><ul><li>PLSS Township </li></ul>
  4. 15. <ul><li>#--------------------------------------------------------------------- </li></ul><ul><li>#Author: JP </li></ul><ul><li># </li></ul><ul><li>#Last Update: December, 2010 </li></ul><ul><li># </li></ul><ul><li>#Purpose: </li></ul><ul><li>#Use cursors to select individual records (rows) in feature class. The </li></ul><ul><li>#individual record is saved to an individual feature class and used to </li></ul><ul><li>#clip census block groups. A town-range field is added and calculated </li></ul><ul><li>#for each census block group feature class. The block group individual </li></ul><ul><li>#files are merged into a single feature class, which can be summarized </li></ul><ul><li>#by DTR for total population for each drive time service area. </li></ul><ul><li>#----------------------------------------------------------------------- </li></ul>
  5. 16. What is a cursor and how does it work.
  6. 17. What is a cursor and how does it work.
  7. 18. What is a cursor and how does it work.
  8. 19. What is a cursor and how does it work.
  9. 20. What is a cursor and how does it work.
  10. 21. <ul><li>The cursor selects an individual record </li></ul><ul><li>in a feature class and uses the python </li></ul><ul><li> tool to create a new feature class with </li></ul><ul><li>one feature. (data export selected set): </li></ul><ul><li>gp.SearchCursor </li></ul>
  11. 22. What the python script does inside the cursor with one record: <ul><li>2. Creates a new feature class from the record </li></ul><ul><li>gp.Select_Analysis </li></ul><ul><li>3. Clips block groups by the new feature class </li></ul><ul><li>gp.Clip_Analysis </li></ul><ul><li>4. Calculates a total population for the service area </li></ul><ul><li>gp.Calculate_Field 40417 </li></ul>
  12. 23. <ul><li>#import modules </li></ul><ul><li>import arcgisscripting, sys, os, time </li></ul><ul><li>#create the geoproccessor object and set file overwrite </li></ul><ul><li>gp = arcgisscripting.create() </li></ul><ul><li>gp.overwriteoutput = 1 </li></ul><ul><li>#set workspace variables and add toolboxes </li></ul><ul><li>gp.workspace = &quot;D:drivetimeRoadsTigerResultsDT120Min_processing4.gdb” </li></ul><ul><li>gp.addtoolbox(&quot;C:/Program Files/ArcGIS/ArcToolBox/Toolboxes/Data Management Tools.tbx&quot;) </li></ul><ul><li>gp.addtoolbox(&quot;C:/Program Files/ArcGIS/ArcToolBox/Toolboxes/Analysis Tools.tbx&quot;) </li></ul><ul><li>servicearea = &quot;DriveTime120Minutes_SEW_NN” </li></ul><ul><li>log = open('D:/drivetime/logprocessing.log', 'a’) </li></ul><ul><li>log.write('nnFile is ' + servicearea) </li></ul><ul><li>timestamp = time.strftime('%I:%M:%S %p', time.localtime()) </li></ul><ul><li>log.write('nStarting time is ' + timestamp) </li></ul>
  13. 24. What the python script does inside the cursor with one record: <ul><li>2. Creates a new feature class from the record </li></ul><ul><li>gp.Select_Analysis </li></ul><ul><li>3. Clips block groups by the new feature class </li></ul><ul><li>gp.Clip_Analysis </li></ul><ul><li>4. Calculates a total population for the service area </li></ul><ul><li>gp.Calculate_Field 40417 </li></ul>
  14. 25. <ul><li>try: </li></ul><ul><li>rows = gp.SearchCursor(servicearea) </li></ul><ul><li>row = rows.Next() </li></ul><ul><li>while row <> None: </li></ul><ul><li>ShapeName = str(row.DTR) </li></ul><ul><li>gp.Select_analysis(servicearea, &quot;xxsel&quot; +ShapeName, '&quot;DTR&quot;=' +ShapeName) </li></ul><ul><li>gp.clip_analysis(&quot;pop2000&quot;, &quot;xxsel&quot;+ShapeName, &quot;xxclipSEWNN&quot;+ShapeName, &quot;&quot;) gp.calculatefield_management(&quot;xxclipSEWNN&quot;+ShapeName, &quot;DTR&quot;, row.DTR) </li></ul><ul><li>gp.delete_management (&quot;xxsel&quot; +ShapeName) </li></ul><ul><li>row = rows.next() </li></ul><ul><li>del row, rows </li></ul><ul><li>except: </li></ul><ul><li>if not gp.getmessages() == &quot;”: </li></ul><ul><li>gp.addmessage(gp.getmessages(2)) </li></ul><ul><li>if 'row' in dir(): </li></ul><ul><li>del row </li></ul><ul><li>if 'rows' in dir(): </li></ul><ul><li>del rows </li></ul>
  15. 26. After the cursor is used for geoprocessing every record, the individual feature classes containing block groups for each service area are merged together using a value table. gp.merge_management vTab = gp.createobject(&quot;ValueTable”) fcList = gp.listfeatureclasses(&quot;xxclipSEWNN*”) fc = fcList.Next() while fc: fcpath = gp.workspace + &quot;&quot; + fc vTab.Addrow(fcpath) fc = fcList.Next() gp.merge_management(vTab, &quot;AMergedDriveTime120MinutesSEWNN&quot;, &quot;”) print &quot;Completed merged file for features of %s&quot; %(servicearea) timestamp = time.strftime('%I:%M:%S %p', time.localtime()) log.write('nFinishing time is ' + timestamp) print&quot;Done.” del vTab del fc del fcpath del servicearea del timestamp log.close()
  16. 29. A supply and demand analysis of location to people. <ul><li>Using hunting or fishing licensing information to determine if quality or quantity of lands support types of recreation. </li></ul><ul><li>Using the density of people to determine where to build facilities or infrastructure. </li></ul>Voila !
  17. 30. Thank you for your attention.

×