Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

share|improve this question
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 9: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 9: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 9: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 9: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

1 Answer 1

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

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).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.