I have point Feature Class that includes 49 OID
. After that I have a Tuple that includes random set of OID
from point feature class for example new_OID = ('10,24,38,41', '17,26,34,47', '5,7,18,25', '7,12,26,46', '2,12,23,36', '7,18,21,43', '2,13,16,39', '5,8,32,43', '1,9,17,44', '8,13,30,46', '2,14,34,37')
.
How can I create new feature class for each new_OID
member?
For example, my first feature class create by (5,7,21,24)
and so on and so forth.
import arcpy
in_FC = "E:/gis payannameh/TABU search/data/90spring_new.shp"
out_FC = r'E:\gis payannameh\Pychram_tabu\vince.gdb'
new_OID = ('10,24,38,41', '17,26,34,47', '5,7,18,25', '7,12,26,46', '2,12,23,36', '7,18,21,43', '2,13,16,39', '5,8,32,43', '1,9,17,44', '8,13,30,46', '2,14,34,37')
for i in range(len(new_OID)):
templayer = "templayer_{}".format(i)
sql = """ {0} IN ({1})""".format("FID",new_OID[i])
arcpy.MakeFeatureLayer_management(in_FC,"templayer",sql)
arcpy.FeatureClassToFeatureClass_conversion(templayer,out_FC,"MAKE_FC_{}".format[i])
[u'0.shp', u'1.shp', u'10.shp', u'2.shp', u'3.shp', u'4.shp', u'5.shp', u'6.shp', u'7.shp', u'8.shp', u'9.shp']
1 Answer 1
Try this (untested):
import arcpy
in_FC = "E:/gis payannameh/TABU search/data/90spring_new.shp"
out_workspace = "E:/gis payannameh/Pychram_tabu/vince.gdb"
new_OID = ('2,24,28,42', '3,5,13,38', '2,17,20,47', '9,16,27,34')
for i in range(len(new_OID)):
templayer = "templayer_{}".format(i)
sql = "{} IN ({})".format("FID", new_OID[i])
arcpy.MakeFeatureLayer_management(in_FC, templayer, sql)
arcpy.FeatureClassToFeatureClass_conversion(templayer, out_workspace, "MAKE_FC_{}".format(i))
Here, I'm iterating through new_OID
with the i
variable. This can be used in both the sql
formulation as well as in the output field name in the FeatureClassToFeatureClass
.
I'm also using the variable name out_workspace
instead of out_FC
just because I think it makes more sense.
Additionally, I've changed the out_workspace
string value to use slash (/
) instead of back-slash (\
) and the r
prefix, just for the sake of consistency with the string on the previous line (could have changed the previous line instead, but either way, it's nice to have some consistency).
Alternatively, if you have specific names that you want to use for your new output feature classes (instead of, "MAKE_FC_0" to "MAKE_FC_3"), you could try something like this (untested):
import arcpy
in_FC = "E:/gis payannameh/TABU search/data/90spring_new.shp"
out_workspace = 'E:/gis payannameh/Pychram_tabu/vince.gdb'
out_FC_OIDs = {'thingy': '2,24,28,42', 'blah': '3,5,13,38', 'custard': '2,17,20,47', 'rhubarb': '9,16,27,34'}
for fc_name in out_FC_OIDs:
templayer = "templayer_{}".format(fc_name)
sql = "{} IN ({})".format("FID", out_FC_OIDs[fc_name])
arcpy.MakeFeatureLayer_management(in_FC, templayer, sql)
arcpy.FeatureClassToFeatureClass_conversion(templayer, out_workspace, fc_name)
Ie, using a dictionary with output feature class names as keys and corresponding OIDs as values.
-
I have a question. For first solution that you recommended If we assume length
(New_OID) = 10
. So the arcpy createMAKE_FC_0
andMAKE_FC_1
andMAKE_FC_10
Respectively. so I want to create fori=2
,MAKE_FC_2
but it shows meMAKE_FC_10
. What is your suggestion?@SonofaBeachMohammad hassan moayyedian– Mohammad hassan moayyedian2021年09月29日 16:27:58 +00:00Commented Sep 29, 2021 at 16:27 -
I don’t understand the question, sorry. It should create MAKE_FC_0 ... MAKE_FC_9 (including MAKE_FC_2).Son of a Beach– Son of a Beach2021年09月29日 21:01:00 +00:00Commented Sep 29, 2021 at 21:01
Explore related questions
See similar questions with these tags.
subset
-- use it when formattingsql
(though you should add a space after theIN
operator, and should NOT usestr()
aroundsubset
since integers don't get quoted)"{} IN ({})".fomat(arcpy.AddFieldDelimiters(in_FC, "FID"), subset)
out_FC
is a file geodatabase, not a feature class, so the variable name is a bit misleading. Plus you appear to be attempting to overwrite the same feature class at each iteration of the loop (ie, to "MAKE_FC" within that fGDB each time).i=2
gis show meMAKE_FC_10
but I want to createMAKE_FC_2
. How can I name them in order?