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 separate feature classes.
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" #field name from which to search data
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: 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?
-
Could you use an if-else statement after the SearchCursor? Depending on what entite1 is based on the SearchCursor, InsertCursor will point toward a different feature class.juturna– juturna2015年04月27日 14:27:27 +00:00Commented Apr 27, 2015 at 14:27
-
This is not python solution, but a solution may be the Split tool. Use the same features for input and split.Barbarossa– Barbarossa2015年04月27日 14:28:40 +00:00Commented Apr 27, 2015 at 14:28
1 Answer 1
Cursors are great, but you don't need them for this operation. Check out the Make Feature Layer tool, specifically the where_clause argument. You can make a feature layer that only contains the "rs" features, and then use Copy Features to place it in the new location.
Alternatively, you could make a feature layer without using the where_clause (which would include all of the features), and then make three separate Select Layer By Attribute operations to get the subsets you want. Once a selection is made, only those records will be copied with the Copy Features tool.
Finally, the Select tool will make a selection and output the features in a single step. This would be the way to do it with the least code. However, Feature Layers are a good thing to get used to using so I don't think you'd be wasting your time at all by pursuing the other methods.
-
That's an interesting proposition, I'll look into it and come back with a comment. Indeed, perhaps I should extend my vision towards other tools as well.Geosphere– Geosphere2015年04月27日 15:35:14 +00:00Commented Apr 27, 2015 at 15:35
-
Is it entirely possible with cursors as well, and that would be a good way to go if you wanted to alter the cell values in any way, but I think you'll find these methods a little faster.mr.adam– mr.adam2015年04月27日 16:02:21 +00:00Commented Apr 27, 2015 at 16:02
-
Thanks a lot. Indeed, they are quicker to make, easier to use, the code looks nice and clean, nothing really that could break if you added some additional functionality. I haven't tried the last one yet but will make sure that I do.Geosphere– Geosphere2015年04月27日 20:05:31 +00:00Commented Apr 27, 2015 at 20:05