1

I have polygon, line and point shapefiles in a folder and I am trying merge shapefiles based the shape like polygon, line and points.

I have searched and I got the code but it merges only if input files are same type.

My requirement is to merge all files based on the geometry. It means it merge all polygons and after that merge line files and point files.

How can I modify the code below?

import arcpy
import os

arcpy.env.workspace = r"C:\Shapefiles"
fcs_in = arcpy.ListFeatureClasses()
fn_source_field = 'SOURCE_SHP'

for fc in fcs_in:
    print 'fc:', fc

    name = os.path.splitext(fc)[0]
    arcpy.AddField_management(fc, fn_source_field, 'TEXT')

    with arcpy.da.UpdateCursor(fc, fn_source_field) as cur:
        for row in cur:
            row[0] = name
            cur.updateRow(row)

fc_output = r"C:\Shapefiles\Merge_poly.shp"
arcpy.Merge_management(fcs_in, fc_output)
  • 2
    Your wording is unclear as to whether your goal is to merge point, polyline, and polygon shapefiles (which isn't possible). It also isn't possible to merge shapefiles with differing attributes. – Vince Apr 16 '17 at 16:56
1

Try adding a loop to select all Point/Line/Polygon feature classes first. With the arcpy.ListFeatureClasses() tool you can specify the feature type that is returned.

import arcpy
import os

arcpy.env.workspace = r"C:\Shapefiles"
shpTypes = ['Point', 'Line', 'Polygon']

for shpType in shpTypes:
    # List only shapefile types that match shpType variable
    fcs_in = arcpy.ListFeatureClasses(feature_type=shpType) 

    fc_output = r"Merge_{}.shp".format(shpType)
    if fcs_in:
        arcpy.Merge_management(fcs_in, fc_output)
  • Hi, i tried to run above code but got the error ExecuteError: Failed to execute. Parameters are not valid. ERROR 000735: Input Datasets: Value is required Failed to execute (Merge). – Arb Apr 16 '17 at 16:40
  • @Arb I have updated the code – Midavalo Apr 16 '17 at 16:43
  • 1
    Also you might need to use FieldMapping if field's names differs from layer to layer. – Serge Norin Apr 17 '17 at 8:17
  • @Midavalo, sorry for the late reply. Above code works for me. Great. – Arb Apr 20 '17 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.