1

I need to input PofC in the PNT_TYP field (created in the script below) in my feature class (assigned to the variable verts) the odd OID values will have PofC input in PNT_TYP and the even with PofT. I am not sure how to do this. I have tried a number of things but this code does not seem to be working. If I have to run it in ArcMap then this will not work as I am trying to make it part of a larger script? if that is the case how do I do this so that I can run it from a script? I am not getting an error message, it just prints out script completed at the end like the script says to but the PNT_TYP field is blank in the feature class (as this script converts it to a lyr file) and I do not see any lyr file. I have never used one so I do not know if it would show in the catalogue or not. I have updated this to include the whole script but it is just the end where I need that one column to be updated with the two values based on the OID value that I am not sure if it is working as I see nothing to indicate it is.

# imports
import arcpy, os
arcpy.env.overwriteOutput = True
gdb = raw_input("please input the path of the GDB you are working with. >> ")
arcpy.env.workspace = gdb
in_fc = raw_input("Please input the fc that requires utm coordinates. >> ").upper().strip()
out_fc = raw_input("Please enter the name of the new start and end point feature class. eg) RoadNo_amdNo_pnts >> ").upper().strip()

###################################################################################################

arcpy.FeatureVerticesToPoints_management(in_fc, out_fc, "BOTH_ENDS") #creating point fc from line fc
# coord = arcpy.AddField_management ("verts", "PNT_TYP","TEXT", "","",25)
# code using modulus possibly to determine PofC and PofT.

#################################################################################

arcpy.AddXY_management(out_fc) #creating points for each vertex

#################################################################################

verts = raw_input("What do you want to call your final feature class? Eg. R18564_amd5_UTM >> ")
y_field = 'POINT_Y' #column of y values
x_field = 'POINT_X' #column of x values
input_format = 'SHAPE'
output_format = 'UTM'

arcpy.ConvertCoordinateNotation_management(out_fc, verts, x_field, y_field, input_format, output_format)#takes out_fc (from vertices tool) and writes out to verts

coord = arcpy.AddField_management (verts, "Coords","TEXT", "","",25)
expression = '!UTM!.split(" ")[1] + ", " + !UTM!.split(" ")[2]'
arcpy.CalculateField_management(verts, "Coords", expression, "PYTHON_9.3")

in_fld = arcpy.AddField_management (verts, "PNT_TYP", "TEXT", "","",25)

###################################################################

in_feature = os.path.join(gdb, verts)

feat_lyr = raw_input("What would you like to call the feature layer? >> "

arcpy.MakeFeatureLayer_management (verts, feat_lyr)
arcpy.SelectLayerByAttribute_management (feat_lyr, "NEW_SELECTION", 'MOD("OID", 2) = 0')
arcpy.CalculateField_management(feat_lyr, "PNT_TYP", '!PNT_TYP! = "PofT"', "PYTHON_9.3")

arcpy.SelectLayerByAttribute_management (feat_lyr, "NEW_SELECTION", 'MOD("OID", 2) > 0')
arcpy.CalculateField_management(feat_lyr, "PNT_TYP", '!PNT_TYP! = "PofC"', "PYTHON_9.3")

print ("script complete.")
7
  • Do you get any errors or what do you mean by not working?
    – BERA
    Apr 19, 2021 at 16:08
  • No I did not get any errors at all. the script runs and then prints script complete but nothing is populated in the PNT_TYP column.
    – BCGeoGirl
    Apr 19, 2021 at 16:11
  • You could solve this using only da.UpdateCursor. Or do you need the layer for something else? What is PofT, a field?
    – BERA
    Apr 19, 2021 at 16:13
  • The PofT is the point of terminus and the PofC is the Point of Commencement. They indicate where my road lines start and end and are used in maps I create which are attached to permit approvals. I need them to be in a field so that I can label them PofC or PofT on my map. Otherwise I have to manually create the labels and there can be many of these points sometimes.
    – BCGeoGirl
    Apr 19, 2021 at 16:19
  • 1
    Ok, thanks I will look at that option. I am just figuring out all this Python and ArcPy stuff as I go here as that is how I learn best. I will post an answer when I figure this out.
    – BCGeoGirl
    Apr 19, 2021 at 16:30

2 Answers 2

2

Try using da.UpdateCursor:

import arcpy

gdb = r'C:\data.gdb'
verts = 'pointfc'

in_feature = os.path.join(gdb, verts)

with arcpy.da.UpdateCursor(in_feature, ["OID@", "PNT_TYP"]) as cursor:
    for row in cursor:
        if row[0] % 2 == 0:
            row[1] = 'PofT'
        else:
            row[1] = 'PofC'
        cursor.updateRow(row)

And if you also want to populate two fields with the x, y coordinates of your points, change "X" and "Y" to the names of your fields:

with arcpy.da.UpdateCursor(in_feature, ["OID@", "PNT_TYP", "X", "Y", "SHAPE@XY"]) as cursor:
    for row in cursor:
        if row[0] % 2 == 0:
            row[1] = 'PofT'
        else:
            row[1] = 'PofC'
        row[2], row[3] = row[4]
        cursor.updateRow(row)
0

Thanks everyone for your help. This is what my final script looks like

import arcpy, os
arcpy.env.overwriteOutput = True
gdb = raw_input("please input the path of the GDB you are working with. >> ")
arcpy.env.workspace = gdb
in_fc = raw_input("Please input the fc name that requires utm coordinates. >> ").upper().strip()
xy_fc = "{}_XY".format(in_fc)

##########################################################################################

arcpy.FeatureVerticesToPoints_management(in_fc, xy_fc, "BOTH_ENDS") #creating vertex points fc 
arcpy.AddXY_management(xy_fc) #adding xy coords for each vertex

##########################################################################################

utm_fc = "{}_UTM".format(in_fc)
y_field = 'POINT_Y' #column of y values
x_field = 'POINT_X' #column of x values
input_format = 'SHAPE'
output_format = 'UTM'

arcpy.ConvertCoordinateNotation_management(xy_fc, utm_fc, x_field, y_field, input_format, output_format)#takes out_fc (from vertices tool) and writes out to utm_fc
in_fld = arcpy.AddField_management (utm_fc, "PNT_TYP", "TEXT", "","",25)

in_feature = os.path.join(gdb, utm_fc)

with arcpy.da.UpdateCursor(in_feature, ["OID", "PNT_TYP"]) as cursor:
    for row in cursor:
        if row[0] %2 == 0:
            row[1] = 'PofT'
        else:
            row[1] = 'PofC'
        cursor.updateRow(row)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.