Extracting from attribute tables by arcpy

3789
5
Jump to solution
04-27-2015 07:19 AM
deleted-user-3rTxRfVTcNm-
New Contributor III

I'm having some trouble connecting ideas on how to solve a problem. What I have is a shapefile from which I have to extract from one field, which contains 3 types of attributes (BS, RS, BRN). For each of those attributes, I've created a geodatabase and separate feature classes using another script.

I short, attributes have to go to their specific designation e.g. BS values go to feature BS, RS values etc.

My first guess is that I have to use Cursors to do it, but in this case I have to use both Search and Insert and how would I do that?

Would it make sense a start such as this?

import arcpy

shape = "D:/M1 Geomatique/Programmation II/Dossier/ZONE_INONDATION_SYNTHESE_67.shp"
gdb = "D:/M1 Geomatique/Programmation II/Dossier/inondation.gdb"
field = "CODE_DEGRE"
entite1 = "rs"
entite2 = "bs"
entite3 = "brn"

rows = arcpy.InsertCursor(gdb)
cursor = arcpy.SearchCursor(shape)

Perform a loop start with a SearchCursor followed by an InsertCursor could be a solution ? :

e.g.

with arcpy.da.SearchCursor(sourceFC,fieldnames) as sCur: 

     with arcpy.da.InsertCursor(targetFC,fieldnames) as iCur:

Above is just an example I've picked up on stackexchange

How would you advise me to tackle this problem?

0 Kudos
1 Solution

Accepted Solutions
JeffWard
Occasional Contributor III

Make Feature Layer tool reference -  Use the Where Clause parameter to restrict the features you want exported to the various layers.

Jeff Ward
Summit County, Utah

View solution in original post

5 Replies
IanMurray
Frequent Contributor

From what you said, I believe you are overthinking this too much.  You have a shapefile with a certain field in it with 3 different values.  You want to make a new feature class for each based on the field value, either "rs", "bs" or "brn".  Why not do a select by attribute for each one of those values, then make a feature layer then convert the feature layer to a feature class?

JeffWard
Occasional Contributor III

Or you could make a feature layer with a definition query  FIELD1 = 'BS' etc. and export those features to a separate feature class.

Jeff Ward
Summit County, Utah
0 Kudos
deleted-user-3rTxRfVTcNm-
New Contributor III

Hey Ian,

Ian Murray wrote:

From what you said, I believe you are overthinking this too much.  You have a shapefile with a certain field in it with 3 different values.  You want to make a new feature class for each based on the field value, either "rs", "bs" or "brn".  Why not do a select by attribute for each one of those values, then make a feature layer then convert the feature layer to a feature class?

Indeed, I was overthinking it. There goes my tendency of forgetting that ArcPy module already has plenty of functions and classes to relieve from making countless of procedures, loops, classes all by yourself.

Eventually, I solved the problem last evening by using MakeFeatureLayer tool, SelectLayerByAttribute and CopyFeatures, and it worked perfectly for all three of them. Everything got automatized in a single script, including the creation of a geodatabase to which these new features will go to.

Anyhow, thank you for taking your time.

JeffWard
Occasional Contributor III

Make Feature Layer tool reference -  Use the Where Clause parameter to restrict the features you want exported to the various layers.

Jeff Ward
Summit County, Utah
deleted-user-3rTxRfVTcNm-
New Contributor III

Actually, this is one of the tools which I had used to solve the problem.

Thank you for taking part in this.

0 Kudos