Python recipes: Difference between revisions

From Wildsong
Jump to navigationJump to search
Brian Wilson (talk | contribs)
mNo edit summary
 
Brian Wilson (talk | contribs)
mNo edit summary
Line 1: Line 1:
The script so far:
== Tips and tricks ==


<pre>
=== Simple file reader ===
# ---------------------------------------------------------------------------
# StructId.py
# Created on: Tue Feb 14 2006 05:06:58 PM
#  (generated by ArcGIS/ModelBuilder)
# ---------------------------------------------------------------------------


# Import system modules
with open(filename) as fp:
import sys, string, os, win32com.client
  for line in fp.readlines():
    line = line.rstrip("\r\n") # remove trailing newline
    line = line.lstrip(" \t")  # remove leading whitespace
    if not len(line): continue # skip blank lines
    # do stuff here


# Create the Geoprocessor object
=== Writing a log ===
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")


#
=== Date stamps ===
gp.workspace = "H:/BrianWilson/Workspace/Collections/Collections.mdb/Collections"
out_workspace = "C:/Temp/NewShapefiles"


# Load required toolboxes...
import time
#gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")


# Process: Calculate Field...
# the expressions here can only be very simple.
# You can use variables in [square brackets], you can use constants, and you can use
# simple operators like + - / * and (for strings) &
# manhole_shp = "C:\\TEMP\\NewShapefiles\\manhole.shp"
#gp.CalculateField_management(manhole_shp, "STRUCT_ID", "[STRUCT_TYP]&\"-\"& [MH_LABEL]")


# Export all the feature classes from our geodatabase to individual shapefiles
=== Running commands ===
fcs=gp.ListFeatureClasses()
fcs.reset()
fc=fcs.Next()
while fc:
    output_shapefile = out_workspace + '/' + fc
    gp.AddMessage("Working on " + fc + "...")
    gp.FeatureClassToShapefile_conversion(fc, out_workspace)
    fc = fcs.next()


# Rename the new shapefiles to their correct GISMO names
  import subprocess
 
  try:
# Calculate the missing field values
    retcode = subprocess.call("mycmd" + " myarg", shell=True)
 
    if retcode < 0:
# Delete the fields that don't belong in GISMO output
        print >>sys.stderr, "Child was terminated by signal", -retcode
</pre>
    else:
        print >>sys.stderr, "Child returned", retcode
  except OSError, e:
    print >>sys.stderr, "Execution failed:", e

Revision as of 19:44, 2 December 2015

Tips and tricks

Simple file reader

with open(filename) as fp: 
  for line in fp.readlines():
    line = line.rstrip("\r\n") # remove trailing newline
    line = line.lstrip(" \t")  # remove leading whitespace
    if not len(line): continue # skip blank lines
    # do stuff here

Writing a log

Date stamps

import time


Running commands

 import subprocess
 try:
   retcode = subprocess.call("mycmd" + " myarg", shell=True)
   if retcode < 0:
       print >>sys.stderr, "Child was terminated by signal", -retcode
   else:
       print >>sys.stderr, "Child returned", retcode
 except OSError, e:
   print >>sys.stderr, "Execution failed:", e