Skip to content

Instantly share code, notes, and snippets.

@rabbleNaut
Last active August 29, 2015 14:01
Show Gist options
  • Save rabbleNaut/45b4cf7c053f041ed2fa to your computer and use it in GitHub Desktop.
Save rabbleNaut/45b4cf7c053f041ed2fa to your computer and use it in GitHub Desktop.
EMST Vegetation Clipper tool
import os, csv
from arcpy import *
# Checks to see which districts are intersected by the project area
def checkDistricts():
x = 0
for fc in fcList:
x += 1
flyr = 'flayer' + str(x)
MakeFeatureLayer_management(fc, flyr)
SelectLayerByLocation_management(flyr, 'intersect', pArea)
currentSegment = outputGDB + os.sep + 'segment_' + str(x)
matchcount = int(GetCount_management(flyr).getOutput(0))
if matchcount == 0:
AddMessage ('No features matched in: {0}'.format(fc[-11:]))
else:
AddWarning ('** Features found in: {0}'.format(fc[-11:]))
CopyFeatures_management(flyr, currentSegment)
def clipPlusPlus():
env.workspace = outputGDB
segList = ListFeatureClasses()
segmentList = []
[segmentList.append(fcs) for fcs in segList if fcs.startswith('segment')]
Merge_management(segmentList, mergedSegments)
AddMessage ("Merging segments...")
Clip_analysis(mergedSegments, pArea, outputFC)
AddMessage ("Clipping segment to project area...")
CalculateField_management(outputFC, 'acres', "!SHAPE.AREA@ACRES!", "PYTHON")
AddMessage ("Calculating acreage...")
#sr = r"Coordinate Systems\Geographic Coordinate Systems\North America\Nad 1983.prj"
#prjFile = os.path.join(arcpy.GetInstallInfo()["InstallDir"],"Coordinate Systems/Geographic Coordinate Systems/North America/NAD 1983.prj")
#sr = arcpy.SpatialReference(prjFile)
#Project_management(tempFC, outputFC, sr)
#AddMessage ("Projecting to NAD83...")
Statistics_analysis(outputFC, MOU_Table, "Acres SUM", "Common;TPWD_Ecosys;MOU_Habitat;EcoRegion;Feature_Type")
Statistics_analysis(outputFC, Summary_Table, "Acres SUM", "Common;Feature_Type")
return segList
def createReport():
TableToDBASE_conversion(MOU_Table,reportFolder)
def flayerZoom():
mxd = mapping.MapDocument("CURRENT")
df = mapping.ListDataFrames(mxd, "Layers")[0]
fcLayer = mapping.Layer(outputFC)
AddMessage("Adding layer to TOC...")
mapping.AddLayer(df, fcLayer, "TOP")
#gets extent
ext = fcLayer.getExtent()
df.extent = ext
AddMessage("Zooming to layer extent...")
# Checks to make sure outputName has no spaces, doesn't begin with number
def checkName(name):
if ' ' in name: raise Exception('No spaces allowed in output name!')
if name[0].isdigit(): raise Exception('Output name must not begin with a number!')
pArea = GetParameterAsText(0)
outputGDB = GetParameterAsText(1)
outputName = GetParameterAsText(2)
#tempFC = outputGDB + os.sep + 'tempVeg_Output'
outputFC = outputGDB + os.sep + outputName
MOU_Table = outputGDB + os.sep + 'MOU_Table' + '_' + outputName
Summary_Table = outputGDB + os.sep + 'Summary_Table' + '_' + outputName
mergedSegments = outputGDB + os.sep + 'MergedSegments'
reportFolder = os.path.split(outputGDB)[0] + os.sep + "Reports"
env.workspace = r"Database Connections\Comanche.sde\ENV_GIS.ENV_GIS.EMST_Districts"
fcList = ListFeatureClasses()
try:
checkName(outputName)
checkDistricts()
segList = clipPlusPlus()
createReport()
flayerZoom()
except:
AddError (GetMessages(2))
AddMessage('A dbf file has been created at the following location: {0}'.format(reportFolder))
# delete temporary files
[Delete_management(fcs) for fcs in segList if fcs.startswith('segment')]
#Delete_management(tempFC)
Delete_management(mergedSegments)
AddMessage ("Deleting temporary files...")
AddWarning ('''
Finished!
''')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment