Project layer with custom projection

328
5
08-29-2012 07:29 AM
DeeptiPuri
New Contributor
My ultimate goal is to know UTM and UTM Northing and Easting. The data can be anywhere in North america. I calculated UTM using arcpy tool (arcpy.CalculateUTMZone_cartography) and I tried to truncate the value (e.g.'17') to Project_management as part of Output coordinate string but so far no luck. Please advise. I can run the code below in shell but whole MXD collapsed when I try to run in a model.

Thanks

The code is here:
    arcpy.CalculateUTMZone_cartography(outPointSHP, "UTM")
    
    pointUTM= arcpy.SearchCursor(outPointSHP)
    for row in pointUTM:
        UTM_var= str(row.getValue('UTM'))[30:32]
    del pointUTM
    
    arcpy.AddMessage(ok)
    out_coordinate_system = os.path.join(connectionPath+'/', r"projections/NAD1983/NAD 1983 UTM Zone "+UTM_var+"N.prj")

    arcpy.Project_management(outPointSHP, projPointSHP, out_coordinate_system)
Tags (2)
0 Kudos
5 Replies
MathewCoyle
Frequent Contributor
What version of ArcGIS are you running? And the problem you are having is the code works in IDLE but not in ArcMap as a tool or through the python window?

Also, post your code using [CODE ][ /CODE] tags
0 Kudos
DeeptiPuri
New Contributor
Hi Mathew

I tried both 10 and 10.1 and got the same results. Yes the code works fine in IDLE (I get projected layer) but if I run as a tool in MXD, MXD encounters problem and everything closes down.

Thanks
0 Kudos
MathewCoyle
Frequent Contributor
Can you post the full code you are executing that works in IDLE?
0 Kudos
DeeptiPuri
New Contributor
hi Mathew
It has to do with my cursor. The script was written by someone else where I am trying to add this. There is little done to capture exception and it is huge. I am not sure why my search cursor is failing here.  With ok= 17 it works fine:


arcpy.CreateFeatureclass_management(scratch, outPointFileName, "POINT", "", "DISABLED", "DISABLED", srGCS83)
outPointSHP = os.path.join(scratch, outPointFileName)

# Now all we need to do is insert the point into our new shapefile.

cur = arcpy.InsertCursor(outPointSHP)
row = cur.newRow()
# It's unclear to me why *both* of these lines are needed, but it fails without the two-steps...
# What I don't fully understand is why I need "Point" and "PointGeometry"...?

point = arcpy.Point(xCoord,yCoord)
pnt = arcpy.PointGeometry(point)
#Insert the point... 
row.shape = pnt
cur.insertRow(row)
del cur

##new code
try:
    arcpy.AddXY_management(outPointSHP)

    ###New Work - Steps to know UTM zone
    # Process: Add Field (7)
    arcpy.AddField_management(outPointSHP, "UTM", "TEXT", "", "", "1500", "", "NULLABLE", "NON_REQUIRED", "")

    # Process: Calculate UTM Zone
    arcpy.CalculateUTMZone_cartography(outPointSHP, "UTM")

    # Process: Add Field 
    arcpy.AddField_management(outPointSHP, "Lon_X", "DOUBLE", "12", "6", "", "", "NULLABLE", "NON_REQUIRED", "")

    # Process: Add Field 
    arcpy.AddField_management(outPointSHP, "Lat_Y", "DOUBLE", "12", "6", "", "", "NULLABLE", "NON_REQUIRED", "")

    # Process: Calculate Field 
    arcpy.CalculateField_management(outPointSHP, "Lon_X", "[POINT_X]", "VB", "")

    # Process: Calculate Field 
    arcpy.CalculateField_management(outPointSHP, "Lat_Y", "[POINT_Y]", "VB", "")

    #Get the UTM value to calculate X and Y 
    pointUTM= arcpy.SearchCursor(outPointSHP, "", "", "UTM;Lat_Y;Lon_X")
    for row in pointUTM:
       UTM_var= str(row.getValue('UTM'))[30:32]
       Lat_y = row.getValue('POINT_Y')
       Lon_X = row.getValue('POINT_X')
    del pointUTM
    ###To generate fields with UTM Northing and Easting 
    # Process: Project
    ok= '17'
    out_coordinate_system = os.path.join(connectionPath+'/', r"projections/NAD1983/NAD 1983 UTM Zone %sN.prj"%ok)
    arcpy.Project_management(outPointSHP, scratch+'/'+outPointPR, out_coordinate_system)
0 Kudos
MathewCoyle
Frequent Contributor
All you are doing is taking the last UTM value and projecting that. I'm not really clear on what this code is doing. It will only ever project one file, are you trying to loop through multiple?
0 Kudos