Model Builder - How to remove selected data from a shapefile?

7136
13
01-09-2013 09:56 AM
RussellQuintero
New Contributor
I am attempting to automate a process I have historically done manually using model builder.

So far I have managed to successfully convert one shapefile to points, and then iterate through multiple rasters to extract the values of those rasters to points, creating multiple shapefiles.

This is where I run into problems.

Some of those newly created shapefiles contain invalid data, with values of -9999 in a field showing the raster had no data there.  I want to remove that data.

I have tried the Analysis toolbox "Select" tool with the SQL query set to "Field Name" <> -9999, but it returns an error 000229.

I have tried "Feature Class to Feature Class" (FC2FC) with the same SQL query, and it says the geometry is invalid.  When I try and open the file outside of Model Builder using FC2FC, it says it is unusable. If I use "Add Data" to add the shapefile to my current map, and then use FC2FC, it can suddenly use the file just fine.

So my questions here are two-fold:

1) What is the proper way in Model Builder to remove selected data from a shapefile?
2) How do I create a tool in Model Builder that will run without requiring that intermediate data be added to the map?
0 Kudos
13 Replies
T__WayneWhitley
Frequent Contributor
You need in-line substitution for your FC2FC output -- the output workspace can be constant, but the actually fc name must change, so make that %Name%_filtered as you said you tried, but I don't see it in the graphic.  (I think the .shp will automatically be applied)

Can you make sure that you tried that?

Also, FC2FC has an optional where_clause parameter you are not using - is this not where you want to eliminate 'empty' or null records?  I think this can be a 'static' query but I'd have to look closer at it.
0 Kudos
T__WayneWhitley
Frequent Contributor
This illustrates what I was thinking, using Make Feature Layer and Delete Features - this way you aren't creating essentially 2 copies of the output...the in-line variable substitution looks fine, although I didn't test it.  I would say this as well, to set:

1- the overwriteoutputs geoprocessing env to 'true' (gp options)
2- 'un-check' the intermediate output for your shp output before Make Feature layer (this is now your final output)
3- run a small sample if you can as a test...no need to run everything at once and have to wait, if it needs tweaking.

Notice the query in the 1st pic, may need to tweak that as well.  Let me know if that works... interesting.  If not, I'd like to 'dissect' it tomorrow when back at the office.

-Wayne
0 Kudos
T__WayneWhitley
Frequent Contributor
My apologies, first of all, for the number of posts I've made...but bear with me, I've developed a keen interest in this part of the topic dealing with iterators and in-line var substitution.

You said your problem was isolated in the copy of features without data from that same source that satisfies a query...see my last post.  The idea was to use FC2FC with a query to output to a in-line sub determined output.  An alternate idea was to modify the point shapefile output with Delete Features with a query entered at the tool, Make Feature Layer.

Please see the attached model graphic (and toolbox zip containing that model).  What I have done is 'similate' your Rasters iterator with a Feature Classes iterator (I do not have the Spatial Analyst extension, and didn't need to simulate that part of the model anyway).

Let me explain:
The Extract Values to Points in your model was substituted for Copy Features in my model to simulate feature class output (points in your case; polygons in mine).  The output in-line var sub name for yours is %Name%_raw.shp; for mine it is %Name%_CF (CF stands for Copy Features).

I used Make Feature Layer and Delete Features to operate on the individual CF outputs, essentially removing the features from each those satisfying the query, "PARCELNO" LIKE '2166%', which is a static query (the '%' in this case is a SQL wildcard character).

This ran without error, making each copy for each fc found in the gdb, appending text to the input name, then proceeding to execute the same SQL query on each CF output via Make Feature Layer....once there is a feature layer 'handle' on those records to delete, this is in turn fed into Delete Features (no error trapping provided).  The final outputs are the individual CF feature classes with the designated features removed - carried out by Make Feature Layer and Delete Features, 'moderated' or fed by the iterator.  So a 2nd 'hard copy' output was not necessary.

Hope that helps...ordinarily I would script this type thing in Python, but this is an interesting way to do it too.
If you're still having problems, I would examine the 'intermediate' output and the query to make sure those are valid.  Hope it isn't a bug somewhere....

Thanks,
Wayne
0 Kudos
T__WayneWhitley
Frequent Contributor
Forgot the toolbox, in case you want to examine it...see attached.
0 Kudos