import arcpy
import sys
import os
# Script to create LGA Map with WP Infrastructure
# Define variables
arcpy.env.workspace = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde"
arcpy.env.overwriteOutput = True
#LGA = arcpy.GetParameterAsText(0) # Input Required Local Government Area
LGA ="COOROW, SHIRE OF"
#Define Layers to Intersect
#1. Transmission Layers
T330OH = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_330kV_OH_Carrier"
T330UG = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_330kV_UG_Cable"
T220OV = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_220kV_OH_Carrier"
T132OH = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_132kV_OH_Carrier"
T132UG = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_132kV_UG_Cable"
T66OV = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_66kV_OH_Carrier"
T66UG = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_66kV_UG_Cable"
T33OH = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_33kV_OH_Carrier"
T33UG = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_33kV_UG_Cable"
TransStruc = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_Structure"
Substation = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Substation"
#Group Transmission Values:
Transmission = (T330OH, T330UG, T220OV, T132OH, T132UG, T66OV, T66UG, T33OH, T33UG, TransStruc, Substation)
#2. Distrubution Layers
POLES = os.path.join("ARCFM.Structure", "ARCFM.Pole")
HVOH = os.path.join("ARCFM.Distribution_HV", "ARCFM.Conductor_HV")
HVUG = os.path.join("ARCFM.Distribution_HV", "ARCFM.Cable_HV")
LVOH = os.path.join("ARCFM.Distribution_LV", "ARCFM.Conductor_LV")
LVUG = os.path.join("ARCFM.Distribution_LV", "ARCFM.Cable_LV")
#Group Distribution Values
Distribution = (POLES, HVOH, HVUG, LVOH, LVUG)
#Select Required LGA
arcpy.MakeFeatureLayer_management("GISEXT.Local_Government_Authority", "LGAlyr")
fieldName = "NAME"
Query = "\""+fieldName+"\"='"+LGA+"'"
arcpy.SelectLayerByAttribute_management ("LGAlyr", "NEW_SELECTION", Query)
###arcpy.SelectLayerByAttribute_management ("LGAlyr", "NEW_SELECTION", """ "NAME" = 'ALBANY, CITY OF' """) - working code
for Struc in Transmission:
outputPath = r"S:\SupportServices\LGAShapefiles"
basefileName = arcpy.Describe(Struc).baseName + LGA
fileFormat= '.shp'
finalDest = os.path.join(outputPath,basefileName+fileFormat)
arcpy.Clip_analysis(Struc, "LGAlyr",finalDest)
#arcpy.Clip_analysis(Struc,"LGAlyr", finalDest)
print "Transmission Shapefile Created"
##for Struc in Distribution:
## outputPath = r"S:\SupportServices\LGAShapefiles"
## basefileName = arcpy.Describe(Struc).baseName + LGA
## fileFormat= '.shp'
## finalDest = os.path.join(outputPath,basefileName+fileFormat)
## arcpy.Clip_analysis(Struc, "LGAlyr",finalDest)
#Find empty shapefiles and delete them
arcpy.env.workspace = r"S:\SupportServices\LGAShapefiles"
shapefiles = arcpy.ListFeatureClasses()
for shapefile in shapefiles:
layerName = arcpy.Describe(shapefile).baseName
arcpy.AddMessage(layerName)
arcpy.MakeFeatureLayer_management (shapefile, layerName)
arcpy.AddMessage(arcpy.GetCount_management(layerName).getOutput(0))
if int(arcpy.GetCount_management(layerName).getOutput(0)) == 0:
arcpy.Delete_management(shapefile)