If else statement not working in Python

5658
4
Jump to solution
02-05-2013 09:56 AM
MollyWatson
New Contributor III
Hello,

I have a Python script to generate an automated mapbook. I want to be able to adjust the data frame scale based on the size of the parcel. My if/else statement is not working. I think it's because it's not recognizing the acreage field in the Parcels table and/or it says it cannot combine a string with an integer. Here is my sample where I create the map document, select the parcel based on user parameters, and zoom to selected feature. The parcels layer is stored in an SDE database.

# Process: Create Mapbook Document, Data Frame, and Layer Objects
mxd = mapping.MapDocument(r"V:\gislu\_BasemapMXD\10.1 MXDS\PreApp_Location.mxd")
df = mapping.ListDataFrames(mxd, "PreApp Location")[0]
Layer = mapping.ListLayers(mxd, "Parcels", df)[0]

#Process: Select Layer by Attributes
whereClause = "\"ADDRNO\" = " + NUMBER + " AND \"ADDRSTREET\" = '" + STREET + "'"
arcpy.AddMessage("SELECTING: " + whereClause)
arcpy.SelectLayerByAttribute_management (Layer, "NEW_SELECTION", whereClause)
arcpy.AddMessage(arcpy.GetCount_management(Layer).getOutput(0))

#Process: Update the mapbook display in ArcMap
df.zoomToSelectedFeatures()
for Layer in mapping.ListLayers(mxd, "Parcels", df):
if [acreage] < str(153):
df.scale = 5000
else:
df.scale = 9000
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
legend = mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT", "Legend")[0]
legend.autoAdd = True
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor
  
# Process: Create Mapbook Document, Data Frame, and Layer Objects mxd = mapping.MapDocument(r"V:\gislu\_BasemapMXD\10.1 MXDS\PreApp_Location.mxd") df = mapping.ListDataFrames(mxd, "PreApp Location")[0] Layer = mapping.ListLayers(mxd, "Parcels", df)[0]  #Process: Select Layer by Attributes whereClause = "\"ADDRNO\" = " + NUMBER + " AND \"ADDRSTREET\" = '" + STREET + "'" arcpy.AddMessage("SELECTING: " + whereClause) arcpy.SelectLayerByAttribute_management (Layer, "NEW_SELECTION", whereClause) arcpy.AddMessage(arcpy.GetCount_management(Layer).getOutput(0))       #Process: Update the mapbook display in ArcMap df.zoomToSelectedFeatures() for Layer in mapping.ListLayers(mxd, "Parcels", df):     if [acreage] < str(153):         df.scale = 5000     else:         df.scale = 9000 arcpy.RefreshActiveView() arcpy.RefreshTOC() legend = mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT", "Legend")[0] legend.autoAdd = True


Please note the [post=166129]use of the [noparse]
[/noparse] block[/post] to format your code in the forum.

Mark is correct that you can't get the value this way. Do you want the total acreage of the selected features? You could determine that using the Summary Statistics tool. If you only have one parcel selected, you could get the acreage value out of the table using a search cursor on your layer.

What if your parcel is long and skinny? Seems to me a safer approach would be to examine the df.scale after you zoom to selected and decide based on that which standard scale to use:

if df.scale <= 5000:   df.scale = 5000 elif df.scale <= 9000:   df.scale = 9000

View solution in original post

0 Kudos
4 Replies
markdenil
Occasional Contributor III
There seem to be a number of issues here.
For example, I don't think you can get to a feature attribute by querying a layer object.
Nor can presenting a field name to the layer get you anywhere: layer objects don't have fields
They do have names and sources and max scales and whatnot.
you need to access the (lone) feature you expect to find in that layer to test the field's value.

As well, are the values in [acreage] really strings?
You present it with a string (str(153), which is really "153")
but expect it to resolve a less-than test...
This likely causes your "string with an integer" error.

int([acreage]) < "153" is more likely to work, if [acreage] really is a string....

(edit -> oops, that is right, int([acreage]) < 153 is what I meant.... )
0 Kudos
Zeke
by
Regular Contributor III
Also, please enclose your code in code tags.
I'd try
 int([acreage]) < 153
, without the quotes around 153.
0 Kudos
curtvprice
MVP Esteemed Contributor
  
# Process: Create Mapbook Document, Data Frame, and Layer Objects mxd = mapping.MapDocument(r"V:\gislu\_BasemapMXD\10.1 MXDS\PreApp_Location.mxd") df = mapping.ListDataFrames(mxd, "PreApp Location")[0] Layer = mapping.ListLayers(mxd, "Parcels", df)[0]  #Process: Select Layer by Attributes whereClause = "\"ADDRNO\" = " + NUMBER + " AND \"ADDRSTREET\" = '" + STREET + "'" arcpy.AddMessage("SELECTING: " + whereClause) arcpy.SelectLayerByAttribute_management (Layer, "NEW_SELECTION", whereClause) arcpy.AddMessage(arcpy.GetCount_management(Layer).getOutput(0))       #Process: Update the mapbook display in ArcMap df.zoomToSelectedFeatures() for Layer in mapping.ListLayers(mxd, "Parcels", df):     if [acreage] < str(153):         df.scale = 5000     else:         df.scale = 9000 arcpy.RefreshActiveView() arcpy.RefreshTOC() legend = mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT", "Legend")[0] legend.autoAdd = True


Please note the [post=166129]use of the [noparse]
[/noparse] block[/post] to format your code in the forum.

Mark is correct that you can't get the value this way. Do you want the total acreage of the selected features? You could determine that using the Summary Statistics tool. If you only have one parcel selected, you could get the acreage value out of the table using a search cursor on your layer.

What if your parcel is long and skinny? Seems to me a safer approach would be to examine the df.scale after you zoom to selected and decide based on that which standard scale to use:

if df.scale <= 5000:   df.scale = 5000 elif df.scale <= 9000:   df.scale = 9000
0 Kudos
MollyWatson
New Contributor III
Thank you everyone! The idea about using if/elif statement for the data frame scale worked and was the easiest route to go! I hadn't thought about it that way so thanks for the good idea.
0 Kudos