Sunday, 13 January 2019

arcgis desktop - Batch merging field mapping


I made the following script to merge shape files of different format types (poly,line, points):


def main():    
import arcpy
from arcpy import env
import globalpath
import os

env.workspace = globalpath.toolDataPath
dataPath=globalpath.toolDataPath
arcpy.env.overwriteOutput = True


#try:
DataTypeL=[("Polygon","mergedTOTPoly.shp"),("Polyline","mergedTOTLines.shp"),("Point","MergedTOTPoint.shp")]

for dType in DataTypeL:
(Typei,Title)=dType

matches = []
counter=0

for dirpath, dirnames, filenames in arcpy.da.Walk(dataPath+"\\UtilityDataFromZorgel",datatype="FeatureClass",type=Typei):
for filename in filenames:
match = ( os.path.join(dirpath, filename))
arcpy.AddMessage(m)
matches.append (match)
counter = counter + 1
arcpy.Merge_management(matches, dataPath+"\\utilities"+Title)

arcpy.AddMessage(str(counter)+" "+str(Typei)+" layers were merged into "+ str(filename))
#except:
arcpy.AddWarning("Merging Utility Layers Failed")
if __name__ == "__main__":
main()

I get the following error:


Traceback (most recent call last):
File "C:\merger.py", line 30, in
main()

File "C:\merger.py", line 25, in main
arcpy.Merge_management(matches, dataPath+"\\utilities"+Title)
File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\management.py", line 3762, in Merge
raise e
ExecuteError: ERROR 001156: Failed on input OID 0, could not write value 'Kanata' to output field TILE_ID
Failed to execute (Merge).

When I merge these files manually from arcmap it works, and fields are mapped as follows:


"STRUCT_TYP \"STRUCT_TYP\" true true false 18 Text 0 0 ,First,#,Allstream_hdwr_L,STRUCT_TYP,-1,-1,allstream_L,STRUCT_TYP,-1,-1,Allstream_lines_L,STRUCT_TYP,-1,-1,Allstream_text_L,STRUCT_TYP,-1,-1,Atria_lines_L,STRUCT_TYP,-1,-1,Atria_text_L,STRUCT_TYP,-1,-1,bell_L,STRUCT_TYP,-1,-1)


where STRUCT_TYP is a shared field.



please let me know how can I get around this error, and if it is indeed mapping the fields how can I do it in batched merging, or should I merge each file individually?



Answer



I got it to run on a directory full of parcels, PLSS, and annexations. The directory had a mix of shapefiles and file geodatabases. I defined the datapath differently, but otherwise changed nothing.


It failed on the 13th polygon feature class. The resulting shapefile had 62 fields and 44525 records. My failure was a type mismatch, so it was different than yours.


Try running on a subset of your input feature classes and add some print commands to tell you which feature class failed. You may have a field name or file name that is invalid in arcpy, but works when the feature class is inserted as a layer in ArcMap.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...