Friday, 21 June 2013

Densify: The main script

Here is the main script that calls DensifyHelper.py

The toolbox etc can be found at

http://carleton-u.maps.arcgis.com/home/item.html?id=9d69bff9c3b64fbda5578c141ceed183




'''
Densify.py

Author
  Dan Patterson
  Dept of Geography and Environmental Studies
  Carleton University, Ottawa, Canada
 
Dan_Patterson@carleton.ca
Date      March 2008
Modified  June 2013

Purpose
  densifies points forming line segments.  currently requires point input for testing

Properties (right-click on the tool and specify the following)
General
  Name   Densify
  Label  Densify
  Desc   Densifies polyline or polygon files using a step increment to
         add vertices between segments

Source script Densify.py
Requires:  DensifyHelper.py
Parameter list
                                Parameter Properties
           Display Name         Data type        Type      Direction  MultiValue
  argv[1]  Input layer          Feature Layer    Required  Input      No
  argv[2]  step size            Double           Required  Input      No
  argv[3]  Output poly* file    Feature class    Optional  Output     No
  argv[4]  Poly* type           String           Optional  Input      No
           Domain: Polyline, Polygon  Default: Polyline
  argv[5]  Optional point file  Feature class    Optional  Output     No
'''
#--------------------------------------------------------------------
#Main portion
import os, sys, math
import arcpy
import DensifyHelper      #load the helper script after arcpy is created

arcpy.OverWriteOutput = 1
desc = arcpy.Describe
inFC = sys.argv[1]
stepBy = float(sys.argv[2])
outPoly = sys.argv[3]
outType = sys.argv[4]
outPoint = sys.argv[5]
if stepBy <= 0.0:
  arcpy.AddMessage("\n" + "A positive non-zero step value is required." + "\n")
  sys.exit()
#
if outPoly != "#":
  outPoly = outPoly.replace("
\\","/")
  fullName = os.path.split(outPoly)
  outFolder = fullName[0].replace("\\", "/")
shapeClass = desc(inFC).ShapeType
#
if shapeClass.lower().find("poly") < 0:
  arcpy.AddMessage("\n" + "Requires a polyline/polygon file...bailing" + "\n")
  sys.exit()
else:
  arcpy.AddMessage("\n" + "Processing " + inFC + " " + shapeClass)
#
OIDField = desc(inFC).OIDFieldName
fc = desc(inFC).CatalogPath.replace("
\\","/")
SR = arcpy.CreateSpatialReference_management("#",fc,"#","#","#","#")
#
rows = arcpy.SearchCursor(inFC)
outData = []
outDataArray = []
pnts = []
for row in rows:                    #cycle through the shapes
  OIDval = row.getValue(OIDField)
  aShape = row.shape
  shapeList = [OIDval, aShape]
  theReturned = DensifyHelper.polyToPnts(shapeList) #see helper script
  shapePnts = []
  for i in range(0, len(theReturned)):
    if theReturned[i] == None:   #account for interior rings and parts
      shapePnts.append(None)
    else:
      XY = [theReturned[i][1], theReturned[i][2]]
      shapePnts.append(XY)
  densPnts = DensifyHelper.densifyPoly(shapePnts, stepBy)
  outDataArray.append([densPnts, OIDval])

fieldsToAdd = []
if outPoly != "#":              #create the output, join the data, clean-up
  tempFile = outPoly.replace(".shp","temp.shp")
  DensifyHelper.polyFromPoints (tempFile, outType, SR, outDataArray, fieldsToAdd, arcpy)
  join_tbl = inFC.replace("*.shp", ".dbf")
  arcpy.MakeFeatureLayer_management(tempFile,"temp")
  arcpy.env.qualifiedFieldNames = False
  arcpy.AddJoin_management("temp",OIDField, join_tbl, OIDField)
  arcpy.CopyFeatures_management("temp",outPoly)
  arcpy.AddMessage("\n" + "The file: " + str(outPoly) + " has been created.\n")
  arcpy.Delete_management(tempFile)
if outPoint != "#":
  DensifyHelper.createPointFile(outPoint, "point", SR, outDataArray, fieldsToAdd, arcpy)

#clean up
delList = [row, rows, outData, outDataArray,sys.argv[1],
           sys.argv[2], sys.argv[3], sys.argv[4]]
for i in delList:
  i = None
del arcpy

No comments:

Post a Comment