I am trying to calculate a field from the SHAPE.LEN field and convert it from meters to feet....
I ran the calculate field in ArcPro and it worked fine.. I exported the Python and it gave me this
import arcpy
arcpy.management.CalculateField("GIS_DATA.SDE_DITCHING", "LENGTH_FT", "MetersToFeet((float(!SHAPE.LEN!)))", "PYTHON3", """import math
def MetersToFeet(length):
return (3.280839895) * length
""", "TEXT", "NO_ENFORCE_DOMAINS")
ANY THOUGHTS on why this is failing?
I now want to put this in a stand alone .py script that I will run with a bunch of other code... I did this and am getting this error... I tried all
import arcpy
layers_to_Update = {
"DitchingLayers": "Database Connections\\DEV@gis_data.sde\\GIS_DATA.SDE_DITCHING"
}
def updateLengthField():
for layer in layers_to_Update.keys():
fromSDE = layer
updateLayer = layers_to_Update[layer]
fieldUpdate = "LENGTH_FT"
arcpy.env.workspace = "Database Connections\\DEV@gis_data.sde"
arcpy.management.CalculateField(updateLayer, fieldUpdate, "MetersToFeet((float(!SHAPE.LEN!)))", "PYTHON3", """import math
def MetersToFeet(length):
return (3.280839895) * length
""", "TEXT", "NO_ENFORCE_DOMAINS")
if __name__ == '__main__':
updateLengthField()
ERROR:
arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000989: Python syntax error: File "<string>", line 2
def MetersToFeet(length):
^
IndentationError: unexpected indent
Failed to execute (CalculateField).
I then tried this and got this error
import arcpy
layers_to_Update = {
"DitchingLayers": "Database Connections\\DEV@gis_data.sde\\GIS_DATA.SDE_DITCHING"
}
def updateLengthField():
for layer in layers_to_Update.keys():
fromSDE = layer
updateLayer = layers_to_Update[layer]
print(updateLayer)
fieldUpdate = "LENGTH_FT"
print(fieldUpdate)
arcpy.env.workspace = "Database Connections\\DEV@gis_data.sde"
arcpy.management.CalculateField("GIS_DATA.SDE_DITCHING", "LENGTH_FT", "MetersToFeet((float(!SHAPE.LEN!)))", "PYTHON3", """import math def MetersToFeet(length):return (3.280839895) * length""", "TEXT", "NO_ENFORCE_DOMAINS")
if __name__ == '__main__':
updateLengthField()
ERROR:
arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000989: Python syntax error: File "<string>", line 1
import math def MetersToFeet(length):return (3.280839895) * length
^
SyntaxError: invalid syntax
Failed to execute (CalculateField).