5

In my last post I had an issue where I wanted to create multiple shapefiles based on only one attribute field (years). How to do it was answered in that post. I want to make that tool more dynamic so that the user can select any field and any value and make a shapefile for it. Any suggestions on how to use the tool dynamic so that fields and the operator(<,<=,=,>,>=) can also be defined by the user itself.


I tried the following:

import arcpy
fc = arcpy.GetParameterAsText(0)
yrs = arcpy.GetParameterAsText(1)
yrs = yrs.split(';')
subtract = ''
for yr in yrs:
     where = '"arcpy.GetParameterAsText(3)" arcpy.GetParameterAsText(2) ' + str(yr) + subtract
     subtract = ' AND "arcpy.GetParameterAsText(3)" > ' + str(yr)
     filename = str(arcpy.GetParameterAsText(3)) + '.shp'
     arcpy.FeatureClassToFeatureClass_conversion(fc, "arcpy.GetParameterAsText(4)", filename, where)

del yr

I get the following error message: : ERROR 999999: Error executing function. An invalid SQL statement was used. An invalid SQL statement was used. Failed to execute (FeatureClassToFeatureClass).

Also attaching the screenshot for properties while adding the script.

enter image description here

enter image description here

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
SNT
  • 3,379
  • 11
  • 36
  • 49
  • 1
    There are too many problems here and I don't understand what the intended logic is. Please explain what you are trying to accomplish in exact terms (adding comments to your code may help). Also brush up on either of the string formatting links I put at the end of my answer. You cannot expect to use an expression in quotes and have it evaluate to anything but a string literal... you need to use string formatting. – blah238 Dec 12 '12 at 09:42
  • Also in your Operator value list it looks like you have put a space between the characters of the less than and greater than operators. There needs to not be a space there. – blah238 Dec 12 '12 at 09:45
  • You can also help yourself by learning to debug. Use a decent Python IDE such as PyScripter or Eclipse with PyDev. – blah238 Dec 12 '12 at 09:49
  • Another tip, assign the result of each of the GetParameterAsText() calls to a variable near the top of your script rather than calling it in-line in the main part. – blah238 Dec 12 '12 at 09:51
  • For anyone curious about doing this with OGR, as shown by Matt Wilkie, all it takes is one simple ogr2ogr instruction to create a new shapefile based on an existing shapefile (i.e. same fields):

    ogr2ogr NewFile.shp Template.shp -where "FID < 0"

    – elrobis Dec 12 '12 at 15:49
  • What I want to achieve is create new shapefile based on the attributes of an existing shapefile. And it should be generic tool too. Eg: If I say shapefiles for Year <= 1990;1995 Then it should give me 2 shapefiles 1990.shp and 1995.shp. Similarly if I say for Consumption=High it should give me only shapefiles which has that particular value in that attribute field. – SNT Dec 13 '12 at 00:21
  • Year <= 1990;1995 doesn't make much sense. Perhaps you could add the IN operator and build a WHERE clause like YEAR IN(1990, 1995), which basically expands to YEAR = 1990 OR YEAR = 1995, up to how ever many elements within the IN statement. I have an example of how to do that here: http://gis.stackexchange.com/questions/21760/extract-by-attribute-using-modelbuilder-with-user-input/21777#21777 – blah238 Dec 13 '12 at 04:38
  • Have you seen the Sql Expression parameter? It is a control that allows the User to define a Where Clause from an existing feature class or table. You can use this expression as the where clause in FC_to_FC. – klewis Mar 15 '19 at 22:45

1 Answers1

7

The built-in Feature Class to Feature Class tool does everything you want.

If you want more control than that, you can create a script tool and customize its validation behavior if necessary (for example, to list all the unique values of a chosen field).

To simply provide a drop-down of available fields you don't need to customize the behavior, just set up an input field parameter that is "Obtained from" the input feature class or feature layer parameter.

Obtained from parameter setup
(source: arcgis.com)

If you want to limit what the user can enter to a single SQL expression where the operator is chosen from a list, you could create a String parameter and set up its Filter property with the desired values. Then, in the script you would create the SQL where clause with Python, using string formatting (new or old styles) or concatenation to build the statement from the variable elements (field name, operator and value).

Glorfindel
  • 1,096
  • 2
  • 9
  • 14
blah238
  • 35,793
  • 7
  • 94
  • 195