Skip to main content
edited code
Source Link
dru87
  • 1.5k
  • 9
  • 23

So first of all you're using the old arcpy.UpdateCursor / arcpy.SearchCursor. The new ones are a lot faster and less prone to cause errors. But be aware that the syntax changed (see ArcGIS online help for details).

I assume that your "line" feature class only contains one line and does not need to be iterated. If this is correct, the following code should work:

import arcpy # A new attribute is first created to populate the distances with arcpy.AddField_management('buildings', 'distanceField', 'FLOAT') lCursor = arcpy.da.SearchCursor('line', ['SHAPE@']) lineGeometry = lCursor[0] with arcpy.da.UpdateCursor('buildings', ['SHAPE@', 'distanceField']) as bCursor: for building in bCursor: buildingGeometry = building[0] distance = buildingGeometry.distanceTo(lineGeometry) * 1.2 building[1] = distancedistance[0] bCursor.updateRow(building) del lineCursor 

Note the following:

  • the use of the with arcpy.da.UpdateCursor() statement makes the closing of the cursor unnecessary.

  • it is sufficient to use the line cursor to read the geometry once only, there is no need to put it inside the UpdateCursor.

  • as Delonix R. mentioned in his comment, you should use a FLOAT field instead of a LONG field, as LONG can only store integer numbers.

  • by adding the newly created 'distanceField' field to your cursor you can directly modify it, without the use of the .setValue() function.

  • in your original code there is no need for the '#', you may use empty ''.

So first of all you're using the old arcpy.UpdateCursor / arcpy.SearchCursor. The new ones are a lot faster and less prone to cause errors. But be aware that the syntax changed (see ArcGIS online help for details).

I assume that your "line" feature class only contains one line and does not need to be iterated. If this is correct, the following code should work:

import arcpy # A new attribute is first created to populate the distances with arcpy.AddField_management('buildings', 'distanceField', 'FLOAT') lCursor = arcpy.da.SearchCursor('line', ['SHAPE@']) lineGeometry = lCursor[0] with arcpy.da.UpdateCursor('buildings', ['SHAPE@', 'distanceField']) as bCursor: for building in bCursor: buildingGeometry = building[0] distance = buildingGeometry.distanceTo(lineGeometry) * 1.2 building[1] = distance bCursor.updateRow(building) del lineCursor 

Note the following:

  • the use of the with arcpy.da.UpdateCursor() statement makes the closing of the cursor unnecessary.

  • it is sufficient to use the line cursor to read the geometry once only, there is no need to put it inside the UpdateCursor.

  • as Delonix R. mentioned in his comment, you should use a FLOAT field instead of a LONG field, as LONG can only store integer numbers.

  • by adding the newly created 'distanceField' field to your cursor you can directly modify it, without the use of the .setValue() function.

  • in your original code there is no need for the '#', you may use empty ''.

So first of all you're using the old arcpy.UpdateCursor / arcpy.SearchCursor. The new ones are a lot faster and less prone to cause errors. But be aware that the syntax changed (see ArcGIS online help for details).

I assume that your "line" feature class only contains one line and does not need to be iterated. If this is correct, the following code should work:

import arcpy # A new attribute is first created to populate the distances with arcpy.AddField_management('buildings', 'distanceField', 'FLOAT') lCursor = arcpy.da.SearchCursor('line', ['SHAPE@']) lineGeometry = lCursor[0] with arcpy.da.UpdateCursor('buildings', ['SHAPE@', 'distanceField']) as bCursor: for building in bCursor: buildingGeometry = building[0] distance = buildingGeometry.distanceTo(lineGeometry) * 1.2 building[1] = distance[0] bCursor.updateRow(building) del lineCursor 

Note the following:

  • the use of the with arcpy.da.UpdateCursor() statement makes the closing of the cursor unnecessary.

  • it is sufficient to use the line cursor to read the geometry once only, there is no need to put it inside the UpdateCursor.

  • as Delonix R. mentioned in his comment, you should use a FLOAT field instead of a LONG field, as LONG can only store integer numbers.

  • by adding the newly created 'distanceField' field to your cursor you can directly modify it, without the use of the .setValue() function.

  • in your original code there is no need for the '#', you may use empty ''.

Source Link
dru87
  • 1.5k
  • 9
  • 23

So first of all you're using the old arcpy.UpdateCursor / arcpy.SearchCursor. The new ones are a lot faster and less prone to cause errors. But be aware that the syntax changed (see ArcGIS online help for details).

I assume that your "line" feature class only contains one line and does not need to be iterated. If this is correct, the following code should work:

import arcpy # A new attribute is first created to populate the distances with arcpy.AddField_management('buildings', 'distanceField', 'FLOAT') lCursor = arcpy.da.SearchCursor('line', ['SHAPE@']) lineGeometry = lCursor[0] with arcpy.da.UpdateCursor('buildings', ['SHAPE@', 'distanceField']) as bCursor: for building in bCursor: buildingGeometry = building[0] distance = buildingGeometry.distanceTo(lineGeometry) * 1.2 building[1] = distance bCursor.updateRow(building) del lineCursor 

Note the following:

  • the use of the with arcpy.da.UpdateCursor() statement makes the closing of the cursor unnecessary.

  • it is sufficient to use the line cursor to read the geometry once only, there is no need to put it inside the UpdateCursor.

  • as Delonix R. mentioned in his comment, you should use a FLOAT field instead of a LONG field, as LONG can only store integer numbers.

  • by adding the newly created 'distanceField' field to your cursor you can directly modify it, without the use of the .setValue() function.

  • in your original code there is no need for the '#', you may use empty ''.