# Title: shape2text # Developer: Joshua Been # File: poly2text2poly.py # Modified: January 14, 2006 # Import COM Dispatch Module import win32com.client, sys, os, fileinput, string from win32com.client import Dispatch # Create the Geoprocessor Object gp = win32com.client.Dispatch("esriGeoprocessing.GPDispatch.1") # Check-out Extensions # Declare Parameters fcInput = sys.argv[1] fields = sys.argv[2] tableOutput = "c:/Paleomap/table.csv" paleoTableOut = "c:/Paleomap/paleoInput.csv" descriptionOut = "c:/Paleomap/description.paleo" StandardFields = [] StandardFieldIndex = [] def shapeType(file): desc = gp.describe(fcInput) return desc.ShapeType def parseKeyFields(fields): StandardFields = fields.split(",") return StandardFields def initialCleanUp(): # Delete previous table if gp.exists(tableOutput): gp.delete(tableOutput) if gp.exists(paleoTableOut): gp.delete(paleoTableOut) def poly2table(fcInput,tableOutput): # Initialize pid, the autonumber field pid = 0 # Populate list with field names # This is necessary to refer to field names by index fields = gp.ListFields(fcInput) field = fields.next() fieldList = ["X","Y"] # Declare pntList pntList = [] # The StandardFieldIndex will hold the index values of the standard fields as they reside in the originating feature class. # The 'x' serve as a placeholder. for item in StandardFields: StandardFieldIndex.append("x") # Cycle through each field in the original feature class fieldNum = 0 while field: # Populate fieldList with all field names from original feature class fieldList.append(str(field.Name)) n = 0 for item in StandardFields: if str(field.Name) == item: # Record index in StandardFieldIndex StandardFieldIndex[n] = str(fieldNum+2) n = n + 1 field = fields.next() fieldNum = fieldNum + 1 # Create the large text file # Add the title row from the list (fieldList) f=open(tableOutput,'w') for item in fieldList: if item != "X": f.write(",") f.write(item) # Add pid field f.write(",pid,topology,inputfc\n") # Create paleoTableOut, whhich is limited to the standard fields p=open(paleoTableOut,'w') # First two fields will hold Lat/Long p.write("pid,longitude,latitude") # Populate remainder of field names in paleoTableOut # This loops through the fieldList to ensure the fields are in the same order in the small table. n = 0 for flItem in fieldList: for sfItem in StandardFields: if sfItem != "x": if str(flItem) == str(sfItem): p.write("," + str(sfItem)) # Add pid field p.write("\n") # Obtain XY data for each vertices rows = gp.SearchCursor(fcInput) row = rows.Next() while row: feat = row.shape a = 0 # For each part while a < feat.PartCount: polyArray = feat.GetPart(a) polyArray.Reset() b = 1 pnt = polyArray.Next() # Populate table with values while pnt: # Add XY data to pntList which will hold all attributes, one row (point) at a atime pntList.append(str(pnt.x)) pntList.append(str(pnt.y)) # Write Lat/Long directly to paleoTableOut # This is inconsistent with what is being done with the larger table (tableOutput), and this should be resolved eventually. p.write(str(pid) + "," + str(pnt.x) + "," + str(pnt.y)) # Add all other fields to pntList # Begin at 2 to skip X,Y fieldRange = range (2,len(fieldList)) for x in fieldRange: pntList.append(str(row.GetValue(fieldList[x]))) # Write all attributes (including X/Y) to tableOutput for item in pntList: f.write(item + ",") f.write(str(pid) + "," + topology + "," + fcInput + "\n") # Add attributes to paleoTableOut SelectedFields = [] num = 0 for item in pntList: # Send each field to the filterFields function # filterFields will return a "!!" if the field is not a standard field, and a "1" if it is. i = str(filterFields(num,StandardFieldIndex)) if i != "!!": SelectedFields.append(str(item)) num = num + 1 # Clear pntList pntList[:] = [] # Write remainder of fields (Lat/Long already written) to paleoTableOut for item in SelectedFields: p.write("," + item) p.write("\n") pid = pid + 1 pnt = polyArray.Next() if not pnt: pnt = polyArray.Next() if pnt: b = b + 1 a = a + 1 # Write NEW to signify a new feature, part, or inner ring f.write("NEW,\n") row = rows.Next() # Close both tables f.close() p.close() # Write description file d=open(descriptionOut,'w') d.write(fcInput + "," + tableOutput + "," + paleoTableOut) d.close() def filterFields(n,list): z = "!!" for item in list: if str(item) != "x": if str(item) == str(n): z = "1" return z try: topology = shapeType(fcInput) initialCleanUp() if str(topology) == "Polygon": StandardFields = parseKeyFields(fields) poly2table(fcInput,tableOutput) elif str(topology) == "Polyline": StandardFields = parseKeyFields(fields) poly2table(fcInput,tableOutput) elif str(topology) == "Point": StandardFields = parseKeyFields(fields) gp.addmessage("Script Not Designed for Point Topology...yet...") else: gp.addmessage("Sorry...Unknown Topology...Try Again!") ## point2table(fcInput,tableOutput) except: # Print Error Messages to Screen print gp.GetMessages()