Model tool to Script not running

4225
43
02-09-2020 09:49 AM
AyokunleAdebisi
New Contributor III

I built a model tool which is running perfectly. Because I need to add some more functionalities like filtering, which I can only do with the script, I export the model to script and set the filter which is working fine also. However, when I run the script, it failed and send me a message which i will attach. I guess maybe it is from my tools which is make query table or the SQL statements in the tool which consist inline substitution or something else might be wrong. I will post the script error message, the script and the model. Please kindly help ASAP.

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# single.py
# Created on: 2020-02-08 22:31:43.00000
# (generated by ArcGIS/ModelBuilder)
# Usage: single <Sitename> <Category> <Specie> <Start_Month> <End_Month> <Start_Year> <End_Year> <QueryTable> <QueryTable_Statistics>
# Description:
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy
arcpy.env.workspace = "C:\waterfowl\waterfowl\waterfowl.gdb"
arcpy.env.overwriteOutput = True


# Script arguments
Sitename = arcpy.GetParameterAsText(0)
if Sitename == '#' or not Sitename:
Sitename = "Abgrabung Malsch/Durmersheim" # provide a default value if unspecified

Category = arcpy.GetParameterAsText(1)
if Category == '#' or not Category:
Category = "Taucher" # provide a default value if unspecified

Specie = arcpy.GetParameterAsText(2)
if Specie == '#' or not Specie:
Specie = "Haubentaucher" # provide a default value if unspecified

Start_Month = arcpy.GetParameterAsText(3)
if Start_Month == '#' or not Start_Month:
Start_Month = "1" # provide a default value if unspecified

End_Month = arcpy.GetParameterAsText(4)
if End_Month == '#' or not End_Month:
End_Month = "3" # provide a default value if unspecified

Start_Year = arcpy.GetParameterAsText(5)
if Start_Year == '#' or not Start_Year:
Start_Year = "1966" # provide a default value if unspecified

End_Year = arcpy.GetParameterAsText(6)
if End_Year == '#' or not End_Year:
End_Year = "2007" # provide a default value if unspecified

QueryTable = arcpy.GetParameterAsText(7)
if QueryTable == '#' or not QueryTable:
QueryTable = "QueryTable" # provide a default value if unspecified

QueryTable_Statistics = arcpy.GetParameterAsText(8)
if QueryTable_Statistics == '#' or not QueryTable_Statistics:
QueryTable_Statistics = "C:\\waterfowl\\Scratch\\scratch.gdb\\QueryTable_Statistics1" # provide a default value if unspecified

# Local variables:
locationbirdinfo = "locationbirdinfo"
False = "true"
False__2_ = "true"

# Process: Make Query Table
arcpy.MakeQueryTable_management

("locationbirdinfo", QueryTable, "USE_KEY_FIELDS", "", "", "locationbirdinfo.locationinfo_wvz_zaehlgebiete_oag_karlsruhe_Sitename = '%Sitename%' And locationbirdinfo.birdinformation_KAT_Bezeichnung = '%Category%' And locationbirdinfo.birdinformation_ART_Bezeichnung = '%Specie%' And locationbirdinfo.birdinformation_Month_ >= %Start Month% And locationbirdinfo.birdinformation_Month_ <= %End Month% And locationbirdinfo.birdinformation_DAT_Datum >= %Start Year% And locationbirdinfo.birdinformation_DAT_Datum <= %End Year%")

# Process: Summary Statistics
arcpy.Statistics_analysis(QueryTable, QueryTable_Statistics, "locationinfo_wvz_zaehlgebiete_oag_karlsruhe_Sitename MAX;birdinformation_KAT_Bezeichnung MAX;birdinformation_ART_Bezeichnung MAX;birdinformation_Month_ MIN;birdinformation_Month_ MAX;birdinformation_DAT_Datum MIN;birdinformation_DAT_Datum MAX;birdinformation_DAT_Anzahl SUM", "")

0 Kudos
43 Replies
DanPatterson_Retired
MVP Emeritus

could you format your code so that it can be examined with proper indentation..

/blogs/dan_patterson/2016/08/14/script-formatting 

0 Kudos
AyokunleAdebisi
New Contributor III

sorry, I was quick to copy and send the code, but David Pike made the indentation right..

0 Kudos
DavidPike
MVP Frequent Contributor
# Import arcpy module
import arcpy

arcpy.env.workspace = r"C:\waterfowl\waterfowl\waterfowl.gdb"
arcpy.env.overwriteOutput = True

# Script arguments
Sitename = arcpy.GetParameterAsText(0)

if len(Sitename) == 0 :
    Sitename = "Abgrabung Malsch/Durmersheim" # provide a default value if unspecified

Category = arcpy.GetParameterAsText(1)
if len(Category) == 0 :
    Category = "Taucher" # provide a default value if unspecified

Specie = arcpy.GetParameterAsText(2)
if len(Specie) == 0 :
    Specie = "Haubentaucher" # provide a default value if unspecified

Start_Month = arcpy.GetParameterAsText(3)
if len(Start_Month) == 0 :
    Start_Month = "1" # provide a default value if unspecified

End_Month = arcpy.GetParameterAsText(4)
if len(End_Month) == 0 :
    End_Month = "3" # provide a default value if unspecified

Start_Year = arcpy.GetParameterAsText(5)
if len(Start_Year) == 0 :
    Start_Year = "1966" # provide a default value if unspecified

End_Year = arcpy.GetParameterAsText(6)
if len(End_Year) == 0 :
    End_Year = "2007" # provide a default value if unspecified

QueryTable = arcpy.GetParameterAsText(7)
if len(QueryTable) == 0 :
    QueryTable = "QueryTable" # provide a default value if unspecified

QueryTable_Statistics = arcpy.GetParameterAsText(8)
if len(QueryTable_Statistics) == 0 :
    QueryTable_Statistics = r"C:\\waterfowl\\Scratch\\scratch.gdb\\QueryTable_Statistics1" # provide a default value if unspecified


# Local variables:
locationbirdinfo = "locationbirdinfo"
#False = "true"
#False__2_ = "true"

# Process: Make Query Table
arcpy.MakeQueryTable_management
("locationbirdinfo", QueryTable, "USE_KEY_FIELDS", "", "", "locationbirdinfo.locationinfo_wvz_zaehlgebiete_oag_karlsruhe_Sitename = " + 
Sitename + " and locationbirdinfo.birdinformation_KAT_Bezeichnung = " + 
"'" + Category + "'" + " and locationbirdinfo.birdinformation_ART_Bezeichnung = " + 
"'" + Specie + "'" + " and locationbirdinfo.birdinformation_Month_ >= " + 
Start Month + " and locationbirdinfo.birdinformation_Month_ <= " + 
End Month + " and locationbirdinfo.birdinformation_DAT_Datum >= " + 
Start Year + " and locationbirdinfo.birdinformation_DAT_Datum <= " + 
End Year)

# Process: Summary Statistics
arcpy.Statistics_analysis(QueryTable, QueryTable_Statistics,
[[ "locationinfo_wvz_zaehlgebiete_oag_karlsruhe_Sitename", "MAX"],["birdinformation_KAT_Bezeichnung", "MAX"], 
["birdinformation_ART_Bezeichnung", "MAX"],
["birdinformation_Month_", "MIN"], 
["birdinformation_Month_" , "MAX"], 
["birdinformation_DAT_Datum", "MIN"], 
["birdinformation_DAT_Datum", "MAX"],
["birdinformation_DAT_Anzahl", "SUM"]], "")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
AyokunleAdebisi
New Contributor III

Thanks for the quick response. It worked but syntax error in line 65 0f my code which i don't see what is wrong with it.

0 Kudos
DavidPike
MVP Frequent Contributor

Ah ok just add underscores as per your parameters

0 Kudos
AyokunleAdebisi
New Contributor III

Thanks....done that but it got worse, my throwing more errors:

0 Kudos
AyokunleAdebisi1
New Contributor III

 My script is working fine now without error but does not display on the table of contents on the map document. what could be the reasons for such.

0 Kudos
DanPatterson_Retired
MVP Emeritus

I don't know/care if it works, but...  \ needed at the ends

Did modelbuilder really spit that out??? or was it edited??  If it was the former, then there is an issue that should be tracked down

Sitename = 'a'
Category = 'b'
Specie = 'c'
Start_Month = 'd'
End_Month = 'e'
End_Year = 'f'
Start_Year = 'g'

q = "locationbirdinfo.locationinfo_wvz_zaehlgebiete_oag_karlsruhe_Sitename = " + \
Sitename + " and locationbirdinfo.birdinformation_KAT_Bezeichnung = " + \
"'" + Category + "'" + " and locationbirdinfo.birdinformation_ART_Bezeichnung = " +  \
"'" + Specie + "'" + " and locationbirdinfo.birdinformation_Month_ >= " + \
Start_Month + " and locationbirdinfo.birdinformation_Month_ <= " + \
End_Month + " and locationbirdinfo.birdinformation_DAT_Datum >= " + \
Start_Year + " and locationbirdinfo.birdinformation_DAT_Datum <= " + \
End_Year‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

# useless result
q
"locationbirdinfo.locationinfo_wvz_zaehlgebiete_oag_karlsruhe_Sitename = a and locationbirdinfo.birdinformation_KAT_Bezeichnung = 'b' and locationbirdinfo.birdinformation_ART_Bezeichnung = 'c' and locationbirdinfo.birdinformation_Month_ >= d and locationbirdinfo.birdinformation_Month_ <= e and locationbirdinfo.birdinformation_DAT_Datum >= g and locationbirdinfo.birdinformation_DAT_Datum <= f"
AyokunleAdebisi
New Contributor III

Am sorry that does not work. Should I trace the problem of syntax back to my model tool and change the name of the parameters like put underscore in Start Month in my model tools parameter or what other thing could be wrong.

0 Kudos