Structure area definition query in python

1444
14
03-26-2019 02:19 PM
deleted-user-25j2k-XonNEg
New Contributor III

I'm writing a Python script tool that will generate a map and am stumped on one item.  The user enters the fire station number, the minimum area (in square feet) of structures to be displayed, and up to seven optional layers.  The tool will then create a PDF of the map.

Everything else works fine but I keep getting an error on the structure size (SHAPE.AREA). The code for this particular part is:

sqft = arcpy.GetParameterAsText(2)

stru = m.listLayers('Structures')[0]
stru.definitionQuery = """{} >= {}""".format(arcpy.AddFieldDelimiters(stru, 'SHAPE.AREA'), sqft)

The tool parameter for sqft is set to Double.  The script completes without crashing, however the structure layer does not display and I get an error if I try to open the attribute table: "Underlying DBMS error [ORA-00904: "SHAPE.AREA": invalid identifier].  Manually setting the definition query to SHAPE.AREA >= 5000 works fine, but I can't get it to go in Python.  I'm using ArcGIS Pro 2.3.1; I have not tried building the map and tool with ArcMap.

Thanks in Advance,

Charlie

0 Kudos
14 Replies
RichardFairhurst
MVP Honored Contributor

After your code sets the definition query you should print it to verify that it is the string you expected:

print(stru.definitionQuery)
deleted-user-25j2k-XonNEg
New Contributor III

I do (though with arcpy.AddMessage).

I made a slight change as I don't like long variable names.

# Set structure area size filter.
stru = m.listLayers('Structures')[0]
fc = stru.dataSource
desc = arcpy.Describe(fc)
area = desc.areaFieldName
stru.definitionQuery = "{} >= {}".format(arcpy.AddFieldDelimiters(stru, area), sqft)
arcpy.AddMessage('Structure Filter: {}'.format(stru.definitionQuery)) ## Flow trace

Output of the script tool

I notice that the field's case changes from all caps to mixed (SHAPE.AREA vs Shape.AREA) but the results are the same.  I may also try ArcMap and Python 2.x to see what happens.  I have a couple more tweaks to do like getting the scale bars properly set, so this is not a major problem.  Hard coding will work for the time being.

0 Kudos
RhettZufelt
MVP Frequent Contributor

Not sure where your data resides, but I have noticed with 10.2.1 arcmap and SDE data on SQL Server, the desc.areaFieldName reports u'Shape.area' even though in Catalog/Map it says it is 'Shape.STArea()'.

If I try to use the value returned from Describe, it does not select anything.  If I use the Shape.STArea(), everything works fine.

R_

0 Kudos
deleted-user-25j2k-XonNEg
New Contributor III

I found one issue - I'm using GetParametersAsText, so a string is being passed along.  I will try GetParametes to get the integer.  I will post if I figure it out.

0 Kudos
DanPatterson_Retired
MVP Emeritus

you  could just  int(arcpy.GetParameterAsText(2))

0 Kudos