0

I have a script where I select point features within a mile of major roads. If the feature falls within a mile of a major road then the field within_mile_road is updated with yes. If not, it remains null. I want to update the within_mile_road from the feature layers that I create back to the original feature class. How do I update the values that were written to the feature layer back to the feature class, here is my code below which does everything except update the original feature class.

import arcpy
arcpy.env.workspace = 'workspace'
    roads = 'roadsfc'
    fields = ['within_mile_road']
    for fc in arcpy.ListFeatureClasses():
        arcpy.MakeFeatureLayer_management(fc, fc+"_lyr")
        arcpy.SelectLayerByLocation_management(fc+"_lyr", "WITHIN_A_DISTANCE", roads, search_distance="1 Miles")
        with arcpy.da.UpdateCursor(fc+"_lyr", fields) as cursor:
             for rowFL in cursor:
                rowFL[0] == 'yes'
                cursor.updateRow(rowFL)
  • 2
    Be extremely careful with the difference between comparison and assignment. Usually, the problem is the other direction. Your indentation is also invalid. – Vince Aug 24 '18 at 0:01
  • A feature class is the data itself and a feature layer is a representation/view of this feature class. Once you update the layer, the source feature class gets updated as well. Am I missing a point? – fatih_dur Aug 24 '18 at 14:53
1

This is Another way which does not require layers and selections. Instead geometries are compared using the SHAPE@-token and the method distanceTo. For large datasets using Select by Location is likely much faster.

import arcpy

linefc = r"C:\data.gdb\lines"
pointfc = r"C:\data.gdb\points"
updatefield = 'Within'
maxdistance = 1600

linegeometries = [i[0] for i in arcpy.da.SearchCursor(linefc,'SHAPE@')]

with arcpy.da.UpdateCursor(pointfc,['SHAPE@',updatefield]) as cursor:
    for row in cursor:
        if any(row[0].distanceTo(line) <= maxdistance for line in linegeometries):
            row[1]='Yes'
        else:
            row[1]='No'
        cursor.updateRow(row)
  • this is nice, what's the default unit of measure? – Geoffrey West Aug 27 '18 at 15:58
  • I'm pretty sure it is whatever unit of the coordinate system, for example meters or degrees – BERA Aug 27 '18 at 17:06
0

Solution:

import arcpy

arcpy.env.workspace = r'workspace'
roads = r'roads'
fields = ['within_mile_road']
for fc in arcpy.ListFeatureClasses():
    arcpy.MakeFeatureLayer_management(fc, fc+"_lyr", where_clause = '''DATE_ > DATEADD(d,-1,CURRENT_TIMESTAMP ) AND DATE_<= CURRENT_TIMESTAMP''' )
    arcpy.SelectLayerByLocation_management(fc+"_lyr", "WITHIN_A_DISTANCE", roads, search_distance="1 Miles")
    with arcpy.da.UpdateCursor(fc+"_lyr", fields) as cursor:
         for rowFL in cursor:
            rowFL[0] ='YES'
            cursor.updateRow(rowFL)
    with arcpy.da.SearchCursor(fc+"_lyr",fields) as search_cur:
          for search_row in search_cur:
            with arcpy.da.UpdateCursor(fc,fields) as upd_cur:
               for upd_row in upd_cur:
                   if upd_row[0] == search_cur[0]:
                      upd_row[0] = search_row[0]
                      upd_cur.updateRow(upd_row)
    with arcpy.da.UpdateCursor(fc, fields, where_clause = '''DATE_ > DATEADD(d,-1,CURRENT_TIMESTAMP ) AND DATE_<= CURRENT_TIMESTAMP''') as Ncursor:
         for rowNC in Ncursor:
            if rowNC[0] !='YES':
                rowNC[0] = 'NO'
                Ncursor.updateRow(rowNC)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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