It's sloppy, and I probably have redundant code in it, but I finally got it to work.Step 1. Check the group layer "BMR" for group layers that start with "interchange". If they do (and they will), check the sublayers and export them to individual shapefiles.Step 2. Add fields for the coal seam and the unique ID that goes along with the attribute table. I tried the new Alter Field, but it has to be in a GDB for it to work. Since they're shapefiles at this point I AddField and add the file name (which is the coal seam name) and add a field for the unique index. Also the twist is that field is named differently per shapefile, so I have it look for the unique identifier (*_ID) and use CalculateField to enter that information.Step 3. Combine all of those shapefiles into a single shapefile.Step 4. Create a buffer around the permit boundary (which was created prior) and combine those two into a shapefile.Step 5. Create a GDB for the two shapefiles that were created.Step 6. MakeFeatureLayer for the two shapefiles, do a select layer by location, copy those features into the GDB that was created, and finally use ExportXYV_stats to export to a text file that can be read by Access in relation to a table for referencing.There's 2 pieces that are bashed out, I have to relink some things when I bring it over since they were lyr files. This should give you an idea on what's what, sorta. Like I said, it's ugly, but I know what the variables are, and it works flawlessly.import arcpy
import string
import os
import fnmatch
from arcpy import env
# Set the workspace
print "Fixing annotation links, please wait..."
ws = r'c:\projects\test\shapefiles\testshp\\'
arcpy.env.workspace = ws
mxdPath = r'C:\Projects\test\test-03112014.mxd'
md = arcpy.mapping.MapDocument(mxdPath)
df = arcpy.mapping.ListDataFrames(md)[0]
targetGroupLayer = arcpy.mapping.ListLayers(md, "BMR", df)[0]
for layer in targetGroupLayer:
if layer.isGroupLayer:
if layer.name.startswith("annotation"):
anno, quadname = layer.name.split("_")
if layer.name.endswith(".wpa"):
s = layer.name
sanno = s[s.find("-")+1:s.find(".")]
bmrpath = r'C:\projects\test\mine_maps\bmr_data\\'
oldpath = bmrpath + quadname + "\maps_" + quadname
newpath = bmrpath + quadname + "\data\\" + sanno
#layer.findAndReplaceWorkspacePath(oldpath, newpath, True)
#print "Updating annotation from " + sanno + " to " + newpath
print "making interchanges into shapefiles, please wait..."
for layer in targetGroupLayer:
if layer.isGroupLayer:
if layer.name.startswith("interchange"):
interchange, quadname = layer.name.split("_")
for subLayer in layer:
coalseam = subLayer.name.split(" ", 1)[0]
fn = ws + quadname + "_" + coalseam + '.shp'
arcpy.Merge_management(subLayer,fn)
print "Creating " + fn
md.save()
del md
print "fixing shapefiles for proper format, please wait..."
fcs = arcpy.ListFeatureClasses()
for shp in fcs:
seam = shp[shp.find("_")+1:shp.find(".")]
arcpy.AddField_management(shp, "seam_wID", "text")
arcpy.AddField_management(shp, "Cards_wID", "text")
arcpy.CalculateField_management(shp, "seam_wID", "'%s'" % (seam), "PYTHON",)
print(seam), "processed"
tables = arcpy.ListFields(shp,"*_ID")
for table in tables:
oldid = table.name
arcpy.CalculateField_management(shp, "Cards_wID", "!"+oldid+"!", "PYTHON",)
print "coal seams added and cards info fixed"
print "creating massive shapefile for interchanges, please wait..."
ws = r'c:\projects\test\shapefiles'
arcpy.env.workspace = r'c:\projects\test\shapefiles\testshp'
fcs = arcpy.ListFeatureClasses()
sfn = r'c:\projects\test\shapefiles\testshp\interchange.shp'
arcpy.Merge_management(fcs,sfn)
arcpy.env.workspace = ws
print "creating shapefile of buffer, please wait..."
arcpy.Buffer_analysis("TomsRun.shp", "TomsRunBuffer1000.shp", "1000 Feet", "Outside_Only", "ROUND", "NONE", "")
print "combining permit buffer and boundary please wait..."
ps = "TomsRun.shp"
bs = "TomsRunBuffer1000.shp"
os = os.path.join(ws,"\\testshp\Supershape.shp")
arcpy.Merge_management([ps,bs],os)
testws = r'c:\projects\test\shapefiles\testshp'
arcpy.env.workspace = testws
fGDBpath = testws + "\\"
gdbname = "shpfGDB.gdb"
arcpy.CreateFileGDB_management(fGDBpath, gdbname)
shpfGDB = r'C:\projects\test\shapefiles\testshp\shpfGDB.gdb'
arcpy.MakeFeatureLayer_management("Supershape.shp", "permit_lyr")
arcpy.MakeFeatureLayer_management("interchange.shp", "interchange_lyr")
arcpy.SelectLayerByLocation_management("interchange_lyr", "INTERSECT", "permit_lyr", "", "NEW_SELECTION")
matchcount = int(arcpy.GetCount_management('interchange_lyr').getOutput(0))
if matchcount == 0:
print('no features matched spatial and attribute criteria')
else:
touchingpermit = shpfGDB + "\\touchingpermit"
arcpy.CopyFeatures_management('interchange_lyr', touchingpermit)
print('{0} interchanges that matched criteria written to {0}' .format(matchcount, touchingpermit))
featureclass = touchingpermit
filename = "C:/Projects/test/tables/interchanges.txt"
fields = [f.name for f in arcpy.ListFields(featureclass, "*_wID")]
arcpy.ExportXYv_stats(featureclass, fields, "SPACE", filename, "ADD_FIELD_NAMES")
print "Exported to a txt file"