Hi!
I'm pretty new into Python and just do my first steps by modifying existing code to match my problem.
The aim is to change the X/Y coordinates of all features within a feature class / shapefile and save the transformed features in a new file. For this I've used a well working example code to switch X/Y coordinates of polygons and modified it:
I'm running the code as a script form a custom toolbox, so i can specify the inputs manually. The code works fine for polygons and polylines, only as soon as i choose a shapefile with point features I'm getting the message: 'Point' object has no attribute 'next'.
What am I doing wrong? Thanks in advance for any help!
I'm pretty new into Python and just do my first steps by modifying existing code to match my problem.
The aim is to change the X/Y coordinates of all features within a feature class / shapefile and save the transformed features in a new file. For this I've used a well working example code to switch X/Y coordinates of polygons and modified it:
Code:
# 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
What am I doing wrong? Thanks in advance for any help!