Select to view content in your preferred language

Make new shapfile based on attributes of an existing shapefile

1497
3
12-09-2012 07:24 PM
SenthilnathanThyagarajan
Emerging Contributor
Hi All,

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.
Tags (2)
0 Kudos
3 Replies
T__WayneWhitley
Honored Contributor
No one else has answered, so I'll chime in --- first of all, with the general conditions in which you want to script this, it seems your problem is more suited for a model, because you can quickly construct it out of ready-made system tools.

A simple model with Make Feature Layer and Copy Features with 3 input parameters (input fc/layer, expression, and output fc) will allow you to have the flexibility to open the Query Builder to compose and validate whatever query on the input layer you want.  Additionally, if you right-click on the model and select 'Batch...', then you can run the whole process multiple times, essentially looping on each user-defined SQL query.

See the attached pics... should load in this order:
1- The model 'guts' containing 2 tools, MFL and Copy Features...
2- The single execution example, singleExecSetup (the default opening of the model, as you would run it once).  The Query Builder window is also shown, opens from your tool interface SQL button as shown to the right of the Expression text box.  Output fc result is already shown in map (black labels are Subdivision labels, same field queried by).
3- The multiple or 'batch' execution example, multiExecSetup (the batch opening of the model, looping on multiple input params).  The labels:  shown in black, Subdivision; shown in blue, Parcel IDs
4- The batch result --- shows how flexible the Query Builder is - notice the result of the 3rd query (from the 3rd line entered in 'batch mode'), which creates output for an ID range.  All of these queries can be validated before the actual execution is launched, an extremely important quality I'd say.

Hope that helps.
-Wayne
0 Kudos
SenthilnathanThyagarajan
Emerging Contributor
Thanks again Wayne. Adding to what you suggested in the last post, I tried to modify it and here how the code looks like:


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:
<class 'arcgisscripting.ExecuteError'>: 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.

[ATTACH=CONFIG]19844[/ATTACH]

[ATTACH=CONFIG]19845[/ATTACH]
0 Kudos
T__WayneWhitley
Honored Contributor
Sorry, that's a 'no go'...1st of all, that is not how the script looping mechanism works, due to the fact the very 1st parameter (a multi-value param) in the existing script was used to set up variables for your sql queries.

On a more elementary level, you cannot quote the GetParameterAsText function because then python simply thinks it is a string.

Lastly, it is a very bad idea to set up fetching parameters within a loop....normally, you fetch all user specified params in the beginning and set up your variables accordingly, then proceed to pass them in any looping mechanism you need.

It may be at this point just a little beyond your Python expertise to understand some of the subtleties of the code, and this is in part why I suggested the approach in the last post.  Also, you would be very hard-pressed to imitate the Query Builder behavior (including the pre-run validation).

If you must introduce some scripting as part of your class project, may I suggest you incorporate the model provided with the means perhaps to launch it by script - maybe call the model in a script scheduled task?  It is a very useful technique and the scripting is relatively simple.  What's more, the model component, Make Feature Layer, can be opened using ModelBuilder and setup with user input beforehand and saved - then the script can take over the launch later and perform the potentially intensive processing at a more convenient time (afterhours).

Hope that's the guidance you need.
0 Kudos