# Luke Kaim 10/26/12 # The goal of this code is to close all line features such that the first coordinate and the last coordinate of a feature match. # Import arcpy module import arcpy, sys, os, re import arcpy from arcpy import env env.overwriteOutput = True env.workspace = "C:\\Users\\Luke Kaim\\Documents\\University of Maine\\Fall_2012\\Volunteer Geographic Information\\Data\\test" # Get the output feature class infile = r"C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\Data\test\selectedlines.gdb\line" outfile = (r"C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\Data\test\selectedlines.gdb\testline", 'w') # Identify the geometry field desc = arcpy.Describe(infile) shapefieldname = desc.ShapeFieldName OIDFieldName = desc.OIDFieldName try: # Create the output feature class # #arcpy.CreateFeatureclass(path,name,"POLYLINE", infile) arcpy.CreateFeatureclass_management(os.path.dirname(outfile), os.path.basename(outfile), "Polyline", infile) rows = arcpy.SearchCursor(infile) cur = arcpy.InsertCursor(outfile) # Create an array and point object needed to create features lineArray = arcpy.Array() pnt = arcpy.Point() # Iterate through the rows in the cursor for row in rows: # Create the geometry object feat = row.getValue(shapefieldname) # Print the current multipoint's ID print "Feature %i:" % row.getValue(desc.OIDFieldName) partnum = 0 # Step through each part of the feature for part in feat: # Print the part number print "Part %i:" % partnum # Step through each vertex in the feature for pnt in feat.getPart(partnum): if pnt: Print x,y coordinates of current point print pnt.X, pnt.Y if partnum==1: firstpnt.X=pnt.X firstpnt.Y=pnt.Y lineArray.add(pnt) feat = cur.newRow() # Set the geometry of the new feature to the array of points # lineArray.add(firstpnt) feat.shape = lineArray # Insert the feature cur.insertRow(feat) lineArray.removeAll() partnum += 1 del cur, rows except Exception as e: print e.message
Solved! Go to Solution.
# Import arcpy module import arcpy, sys, os, traceback from arcpy import env env.overwriteOutput = True env.workspace = r"C:\tmp\selectedlines.gdb" # Get the output feature class infile = r"C:\tmp\selectedlines.gdb\line" outfile = r"C:\tmp\selectedlines.gdb\testline" # Identify the geometry field desc = arcpy.Describe(infile) shapefieldname = desc.ShapeFieldName OIDFieldName = desc.OIDFieldName # Create the output feature class #arcpy.CreateFeatureclass(path,name,"POLYLINE", infile) arcpy.CreateFeatureclass_management(os.path.dirname(outfile), os.path.basename(outfile), "Polyline", infile, "SAME_AS_TEMPLATE","SAME_AS_TEMPLATE",infile) rows = arcpy.SearchCursor(infile) cur = arcpy.InsertCursor(outfile) # Iterate through the rows in the cursor for row in rows: # Create the geometry object feat = row.getValue(shapefieldname) # Print the current multipoint's ID print "Feature %i:" % row.getValue(desc.OIDFieldName) partnum = 0 # Step through each part of the feature for part in feat: # Print the part number #print "Part %i:" % partnum # Create an array of points from part pointsArray = feat.getPart(partnum) # Get the first point from Array firstPoint = pointsArray.getObject(0) # Add first point to the end of array to close a line pointsArray.add(firstPoint) feat = cur.newRow() feat.shape = pointsArray # Insert the feature cur.insertRow(feat) pointsArray.removeAll() del cur, rows