Calculate Field - Convert Meters to Feet

284
4
Jump to solution
a month ago
kapalczynski
Occasional Contributor III

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).

 

0 Kudos
1 Solution

Accepted Solutions
kapalczynski
Occasional Contributor III

got it... hunting and hunting finally looking at the network payload in the browser i saw the parameter was called calcExpression not expression or Update Expresstion in the image above... ... gotta love cryptic naming conventions ESRI puts out... parameter names are different for each method of updating... uggg

 

    url = urlCalculateFeatures
    whereclause='1=1'
    gis_payload = {
        'token': currenttoken,
        'f': 'json',
        'calcExpression':'[{"field": "LENGTH_FT", "sqlExpression": "Shape__Length*300.280839895"}]',
        'sqlFormat':'standard',
        'where': f'''{whereclause}'''
        }
    print(gis_payload)
    response = requests.request("POST", url=urlCalculateFeatures, data=gis_payload)

 

 

kapalczynski_0-1711567830858.png

 

View solution in original post

0 Kudos
4 Replies
kapalczynski
Occasional Contributor III

Never mind I got it with this

 

def MetersToFeet(length):
    return (3.280839895) * length

def updateLengthField():
    for layer in layers_to_Update.keys():
        fromSDE = layer
        updateLayer = layers_to_Update[layer]
        fieldUpdate = "LENGTH_FT"
        arcpy.env.workspace = "Database Connections\\GIS_3_DEV@gis_data.sde"

        arcpy.management.CalculateField("GIS_DATA.SDE_VDOT_MAINT_DITCHING", "LENGTH_FT", "MetersToFeet((float(!SHAPE.LEN!)))", "PYTHON3", "", "TEXT", "NO_ENFORCE_DOMAINS")
0 Kudos
kapalczynski
Occasional Contributor III

The above now works BUT I want to get this working against a REST service ... 

So something like the below but not DELETE rather CACULATE 

Any thoughts?

 

    url = urlDeleteFeatures
    whereclause='1=1'
    gis_payload = {
        'token': currenttoken,
        'f': 'json',
        'where': f'''{whereclause}'''
        }
    response = requests.request("POST", url=url, data=gis_payload)
    print("Done Deleting")

 

I am not sure how the expression would look 

kapalczynski_0-1711560010853.png

 

0 Kudos
kapalczynski
Occasional Contributor III

Got with this:

kapalczynski_0-1711565973413.png

BUT not sure how to get that into a python script.. 

Tried this... no error but nothing happening.. 

    url = urlCalculateFeatures
    whereclause='1=1'
    gis_payload = {
        'token': currenttoken,
        'f': 'json',
        'url':'https://xxxx.xxx.xx.gov/env/rest/services/DEV/UpdateLengthField/FeatureServer/0',
        'expression':'[{"field": "LENGTH_FT", "sqlExpression": "Shape__Length*30.280839895"}]',
        'where': f'''{whereclause}'''
        }
    response = requests.request("POST", url=url, data=gis_payload)
0 Kudos
kapalczynski
Occasional Contributor III

got it... hunting and hunting finally looking at the network payload in the browser i saw the parameter was called calcExpression not expression or Update Expresstion in the image above... ... gotta love cryptic naming conventions ESRI puts out... parameter names are different for each method of updating... uggg

 

    url = urlCalculateFeatures
    whereclause='1=1'
    gis_payload = {
        'token': currenttoken,
        'f': 'json',
        'calcExpression':'[{"field": "LENGTH_FT", "sqlExpression": "Shape__Length*300.280839895"}]',
        'sqlFormat':'standard',
        'where': f'''{whereclause}'''
        }
    print(gis_payload)
    response = requests.request("POST", url=urlCalculateFeatures, data=gis_payload)

 

 

kapalczynski_0-1711567830858.png

 

0 Kudos