# EdgeListToFeatureClass.py # # Description: # Creates a polyline feature class from an edge list and a patch centroid FC # # Inputs: <Patch centroids FC> <EdgeList CSV file> # # Output: Edge feature class # # June 2012 # John.Fay@duke.edu import sys, os, arcpy import arcpy.sa as sa arcpy.env.overwriteOutput = 1 # Check out the ArcGIS Spatial Analyst extension license arcpy.CheckOutExtension("Spatial") # Inputs patchRaster = sys.argv[1] #r'C:WorkSpaceGBAT2012GHAT_V01CellBasedScratchSubnetPatches' edgeList = sys.argv[2] #r'C:WorkSpaceGBAT2012GHAT_V01CellBasedDataEdgeList.txt' # Output outFC = sys.argv[3] #r'C:WorkSpaceGBAT2012GHAT_V01CellBasedScratchTestEdges.shp' # Create zonal geometry table, including coordinates of each patch centroid outTable = os.path.join(arcpy.env.scratchWorkspace,'DeleteMe') zGeomTbl = sa.ZonalGeometryAsTable(patchRaster,"VALUE",outTable) # Make a dictionary of patch coordinates coordDict = {} recs = arcpy.SearchCursor(outTable) rec = recs.next() while rec: coordDict[rec.VALUE] = (rec.XCENTROID, rec.YCENTROID) rec = recs.next() del rec, recs # Create the output feature class & add fields sr = arcpy.Describe(patchRaster).SpatialReference arcpy.CreateFeatureclass_management(os.path.dirname(outFC), os.path.basename(outFC),"POLYLINE",'','','', sr) arcpy.AddField_management(outFC,"FromID","LONG",5) arcpy.AddField_management(outFC,"ToID","LONG",5) arcpy.AddField_management(outFC,"Cost","DOUBLE",10,2) # Create and add features to the feature class cur = arcpy.InsertCursor(outFC) arr = arcpy.Array() fileObj = open(edgeList,'r') headerLine = fileObj.readline() dataLine = fileObj.readline() while dataLine: dataList = dataLine[:-1].split(",") #iterate through from/to points for i in [0,1]: coords = coordDict[int(dataList[i])] pt = arcpy.Point(coords[0],coords[1]) arr.add(pt) polyline = arcpy.Polyline(arr) arr.removeAll() row = cur.newRow() row.FromID = int(dataList[0]) row.ToID = int(dataList[1]) row.Cost = float(dataList[2]) row.shape = polyline cur.insertRow(row) dataLine = fileObj.readline() del cur, row arcpy.Delete_management(outTable)