Select to view content in your preferred language

Model tool to Script not running

5896
43
02-09-2020 09:49 AM
AyokunleAdebisi
Emerging Contributor

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
AyokunleAdebisi1
Emerging Contributor

Thanks correction taken...

0 Kudos
DanPatterson_Retired
MVP Emeritus

Run the Select_analysis manually to make sure it works.  Get the code snippet from the run and compare it to what you have

0 Kudos
AyokunleAdebisi1
Emerging Contributor

Yes I did ...it is taking forever...maybe I should uninstall and reinstall arcmap, because it is show module error like what is above.

0 Kudos
DanPatterson_Retired
MVP Emeritus

If the tool can't even run, then there may be something fundamentally wrong with your data

0 Kudos
AyokunleAdebisi1
Emerging Contributor

hmmm ..ok...the data is like 70,000 records with polygon duplicated that I couldn't avoid..are there no way around a problem like this or what is causing this:

0 Kudos
AyokunleAdebisi1
Emerging Contributor

Ok..The select tools SQL already gave me incorrect sql statements already, i guess that is where to start from..what is  wwrong here please:

0 Kudos
DanPatterson_Retired
MVP Emeritus

If you typed any of that, get rid of it and only select the fields and operators from the dialog to build the query.  And don't try to do it all at once, verify your query as you go, building it incrementally

AyokunleAdebisi1
Emerging Contributor

I did that with tools it worked...but going forward those inline substitution are parameters that i want users to select from.....how do i go through this.

0 Kudos
DanPatterson_Retired
MVP Emeritus

A quick tour of creating tools with Python—Geoprocessing and Python | Documentation 

Start with custom toolboxes, You associate your parameters with lines in the tool dialog.  Models are useful to establish workflows and to begin to learn how a model and code are comparable.  When you get a feeling for the differences, you will be able to transition models to code much easier.  That is why I said, get a standalone tool working, spit it out as a script, compare it to what a model spit out and you have your translator.  

0 Kudos
AyokunleAdebisi1
Emerging Contributor

My tool is only running good for a particular selection.., but I want it to run dynamically, i mean users should select from list of values which has been done in the model using inline substitution variables but the inline substitution variables seems to not be working in the python script, how to I make it work is the problem now so as to allow users select and filter. Thanks.

Also in the standalone select tools.....this sql statement is fine: Sitename = '%Sitename%' AND Category = '%Category%' AND Specie = '%Specie%'   AND  Datum >= 1966

but the moment i introduce :  Sitename = '%Sitename%' AND Category = '%Category%' AND Specie = '%Specie%'   AND  Datum >= %StartYear%, it becomes invalid sql statement... i.e ...it does not want inline variable for number format..how can i solve this to make it valid

0 Kudos