# Import system modules import arcpy import sys import os import traceback import string def Transform_XY(infc, outfc, outfcSR): # Transforms the X,Y coordinates of a feature class # Check type of input feature class infcType = arcpy.Describe(infc).shapeType if not arcpy.Exists(infc): AddMsgAndPrint("Input feature class not found! Script stops here.") return if not arcpy.Exists(outfc): fpath, fname = os.path.split(outfc) arcpy.CreateFeatureclass_management(fpath, fname, infcType, infc, "DISABLED", "DISABLED", outfcSR) AddMsgAndPrint("New shapefile of type " + infcType + " created...") else: AddMsgAndPrint("Output feature class already exists, please specify new location! Script stops here.") return # Define cursors for input & output readRows = arcpy.SearchCursor(infc) readRow = readRows.next() insRows = arcpy.InsertCursor(outfc) insRow = insRows.next() AddMsgAndPrint("Cursors Created...") # Update progress info in dialog box arcpy.SetProgressor("default") result = arcpy.GetCount_management(infc) count = int(result.getOutput(0)) fcount = 0 AddMsgAndPrint("Progressor Created...") # Create temporary geometry objects inPoly_Array = arcpy.Array() inPt = arcpy.Point() outPt = arcpy.Point() fieldList = arcpy.ListFields(infc) for field in fieldList: AddMsgAndPrint(field.name) # Transformation Parameters for affine transformation (example Helsinki-VVY -> ETRS-GK-25) A = 6654650.14636 B = 0.99998725362 C = 0.00120230340 D = 25447166.49457 # Loop through all features in the source feature class shapefieldname = arcpy.gp.Describe(infc).ShapeFieldName while readRow: fcount += 1 partnum = 0 inType = readRow.getValue(shapefieldname) if infcType == "Polygon" or "Polyline": partcount = inType.partCount # Update progressor message progressMessage = "Processing source feature: " + str(fcount) + " of " + str(count) arcpy.SetProgressorLabel(progressMessage) # Coordinate transformation if infcType == "Polygon" or "Polyline": while partnum < partcount: part = inType.getPart(partnum) inPt = part.next() while inPt: outPt.Y = A + (B * inPt.Y) - (C * inPt.X) outPt.X = D + (C * inPt.Y) + (B * inPt.X) inPoly_Array.append(outPt) inPt = part.next() outPoly = insRows.newRow() outPoly.Shape = inPoly_Array for field in fieldList: if field.name != "FID" and field.name != "Shape": fieldValue = readRow.getValue(field.name) AddMsgAndPrint(field.name) AddMsgAndPrint(fieldValue) outPoly.setValue(field.name, fieldValue) insRows.insertRow(outPoly) partnum += 1 inPoly_Array.removeAll() else: inPt = inType.getPart() outPt.Y = A + (B * inPt.Y) - (C * inPt.X) outPt.X = D + (C * inPt.Y) + (B * inPt.X) outFeature = insRows.newRow() outFeature.Shape = outPt insRows.insertRow(outFeature) readRow = readRows.next() return def AddMsgAndPrint(message): arcpy.AddMessage(message) print message return 0 #+-------------------------------------------------------------------------------------+ # Main Process Loop #+-------------------------------------------------------------------------------------+ # Create the Geoprocessor object and set the overwrite setting arcpy.OverWriteOutput = True try: # # Input Parameters # # infc - input feature class # outfc - output feature class # outfcSR - output feature class' spatial reference infc = arcpy.GetParameterAsText(0) outfc = arcpy.GetParameterAsText(1) outfcSR = arcpy.GetParameterAsText(2) Transform_XY(infc, outfc, outfcSR) AddMsgAndPrint("Transformation completed...") # Done except arcpy.ExecuteError: # Get the geoprocessing error messages msgs = arcpy.GetMessage(0) msgs += arcpy.GetMessages(2) # Return geoprocessing error messages for use with a script tool arcpy.AddError(msgs) # Print geoprocessing error messages for use in Python/PythonWin print msgs except: # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate error message pymsg = tbinfo + "\n" + str(sys.exc_type)+ ": " + str(sys.exc_value) # Return python error messages for use with a script tool arcpy.AddError(pymsg) # Print Python error messages for use in Python/PythonWin print pymsg
Solved! Go to Solution.
>>> # testing your suspected faulty 'if' statement... >>> infcType = 'Point' >>> if infcType == "Polygon" or "Polyline": print 'yes' yes >>> # the statement, print 'yes', executes -- so it is faulty allowing your point object to pass... >>> # here it the corrected statement: >>> if infcType in ["Polygon", "Polyline"]: print 'yes' >>> # okay, that works -- the 'else' statement of your code should now run... >>> # although I think you will have another error with 'getPart' -- test and see.
>>> # testing your suspected faulty 'if' statement... >>> infcType = 'Point' >>> if infcType == "Polygon" or "Polyline": print 'yes' yes >>> # the statement, print 'yes', executes -- so it is faulty allowing your point object to pass... >>> # here it the corrected statement: >>> if infcType in ["Polygon", "Polyline"]: print 'yes' >>> # okay, that works -- the 'else' statement of your code should now run... >>> # although I think you will have another error with 'getPart' -- test and see.