How to use values from arcpy.GetParameters in expression?

5844
3
Jump to solution
08-15-2015 09:08 AM
GabrielaMandziuk
New Contributor II

Hi, I have a big big problem

I am writing a script in PythonWin and want to use it as a Script in ArcGIS toolbox (I have created a script in ArcMap and it is linked with pythonWin script).

I really want to make some parameters to enable users of my tool to set their own settings. This is my code:

import arcpy

from arcpy import env

env = "C:/Users/Desktop/an1"

inputfc = arcpy.GetParameterAsText(0)

outputfc = arcpy.GetParameterAsText(1)

dateFrom = arcpy.GetParameterAsText(2)

dateTo = arcpy.GetParameterAsText(3)

xyTolerance = arcpy.GetParameterAsText(4)

select = inputfc + "_sel"

where_clause = ' "DATE" > date \'dateFrom\' AND "DATE" < date \'dateTo\' '

arcpy.Select_analysis(inputfc, select, where_clause)

As you see, I want to get the date from the value of parameter. Unfortunately, I cannot find a way to use it in my where_clause expression!

("DATE" is a field name). How can I access to these values? The problem is, as I suppose, in the expression itself. When I paste a normal date there (like: where_clause = ' "DATE" > date '2015-01-01' AND "DATE" < date '2015-02-01' '​) everything works.

Thanks!:)

0 Kudos
1 Solution

Accepted Solutions
GabrielaMandziuk
New Contributor II

Hi. I have finally solved my problem!!!

As I thought, the problem was in the expression. Now the code is:

import arcpy

from arcpy import env

env = "C:/Users/Desktop/an1"

inputfc = arcpy.GetParameterAsText(0)

outputfc = arcpy.GetParameterAsText(1)

dateFrom = arcpy.GetParameter(2)

dateTo = arcpy.GetParameter(3)

xyTolerance = arcpy.GetParameterAsText(4)

select = inputfc + "_sel"

where_clause = '"DATE" > date \'%s\' AND "DATE" < date \'%s\' ' % (dateFrom, dateTo)

arcpy.Select_analysis(inputfc, select, where_clause)

integrated = select + "_int"

arcpy.CopyFeatures_management(inputfc, integrated)

arcpy.Integrate_management(integrated, str(xyTolerance) + " Meters")

ce = arcpy.CollectEvents_stats(integrated, outputfc)

Hope it'' help someone in the future

View solution in original post

3 Replies
DanPatterson_Retired
MVP Emeritus

You need a date field...do you have a Date field? and do the dates conform to what you say works?  You can create a derived parameter if you had a field parameter, set to default, then the values derived from it.

http://desktop.arcgis.com/en/desktop/latest/analyze/creating-tools/setting-script-tool-parameters.ht...

http://desktop.arcgis.com/en/desktop/latest/tools/analysis-toolbox/select.htm

SQL syntax is covered here

http://desktop.arcgis.com/en/desktop/latest/map/working-with-layers/sql-reference-for-query-expressi...

GabrielaMandziuk
New Contributor II

My parameter has a data type "Date" and I do have a DATE field in my feature class attribute table. I will study your materials, thanks for the answear

0 Kudos
GabrielaMandziuk
New Contributor II

Hi. I have finally solved my problem!!!

As I thought, the problem was in the expression. Now the code is:

import arcpy

from arcpy import env

env = "C:/Users/Desktop/an1"

inputfc = arcpy.GetParameterAsText(0)

outputfc = arcpy.GetParameterAsText(1)

dateFrom = arcpy.GetParameter(2)

dateTo = arcpy.GetParameter(3)

xyTolerance = arcpy.GetParameterAsText(4)

select = inputfc + "_sel"

where_clause = '"DATE" > date \'%s\' AND "DATE" < date \'%s\' ' % (dateFrom, dateTo)

arcpy.Select_analysis(inputfc, select, where_clause)

integrated = select + "_int"

arcpy.CopyFeatures_management(inputfc, integrated)

arcpy.Integrate_management(integrated, str(xyTolerance) + " Meters")

ce = arcpy.CollectEvents_stats(integrated, outputfc)

Hope it'' help someone in the future