Structure area definition query in python

1435
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
PavanYadav
Esri Contributor

Can you see if the same works from the Python Window?

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

I get the same error using the Python Window.  If I look at the definition query in the layer properties, it looks OK (SHAPE.AREA >= 10000).

If, I then edit the definition query (through layer properties, not Python) by selecting the drop-downs for the fields and relationship, then add 10000, it works fine.  Likewise, this works in the Python Window.  I may just have to hard code some preset area values using if statements for now.

stru.definitionQuery = 'SHAPE.AREA >= 10000'
PavanYadav
Esri Contributor

Thanks for checking. The reason I asked because recently when I was using a definitionQuery from a python tool, ArcGIS Pro returned an error (or, did not do anything; don't recall exactly) but I used the same definitionQuery from the python window, it worked as expected. I did not do any additional tests at that time, but I felt it’s a problem in the software. I will do some more testing and let you know what I find out.

0 Kudos
RichardFairhurst
MVP Honored Contributor

You have quoted the string wrong.  Just use one pair of double quotes, not 3 pairs.

Also, you may want to try using code to get the area field name instead of hard coding a name that only works for a specific database.  something like the following should work

sqft = arcpy.GetParameterAsText(2)

stru = m.listLayers('Structures')[0]
fc = stru.dataSource
desc = arcpy.Describe(fc)
areafieldname = desc.areaFieldName
stru.definitionQuery = "{} >= {}".format(arcpy.AddFieldDelimiters(stru, areafieldname), sqft)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
deleted-user-25j2k-XonNEg
New Contributor III

Thanks.

I'll give it a shot, though Esri documentation shows three sets of double quotes, and that way works fine with setting the station's first-due area.

Charlie

0 Kudos
DanPatterson_Retired
MVP Emeritus
"{} >= {}".format('a', 10)
'a >= 10'

"""{} >= {}""".format('a', 10)
'a >= 10'

"""""""""{} >= {}""""""""".format('a', 10)
'a >= 10'

Left and right just need to match/balance in python

RichardFairhurst
MVP Honored Contributor

Good to know, but in several languages the use of multiple quotes together makes the quotation mark a literal character within a string and 3 pairs of quotation marks together would return a quoted string within a string equivalent to:

'"a >= 10"'‍‍‍

So given that your examples show that only the first pair of quotation marks is required in Python to get the desired output, I consider the use of more than one pair of quotations both unnecessary and potentially confusing to those familiar with other languages (as it was to me).

0 Kudos
DanPatterson_Retired
MVP Emeritus
"""""""""'{} >= {}'""""""""".format('a', 10)
"'a >= 10'"‍‍

'''''''''"{} >= {}"'''''''''.format('a', 10)
'"a >= 10"'

which why a knowledge of balance and quote type is important

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

I thought that using the Describe function that Richard suggested would work, but it isn't displaying any structures.  I'll go back to the hard-coded statements for now; there are only three choices at this point  I'll further check it as a stand-alone script and see if that makes any difference and add more flow traces.

Thanks to everyone.

Charlie

0 Kudos