0

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']

asked Sep 26, 2021 at 19:38
5
  • 2
    You created the variable subset -- use it when formatting sql (though you should add a space after the IN operator, and should NOT use str() around subset since integers don't get quoted) Commented Sep 26, 2021 at 21:34
  • 3
    "{} IN ({})".fomat(arcpy.AddFieldDelimiters(in_FC, "FID"), subset) Commented Sep 26, 2021 at 22:41
  • Furthermore, 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). Commented Sep 27, 2021 at 2:45
  • @user2856. I have question . when I want to make feature class for i=2 gis show me MAKE_FC_10 but I want to create MAKE_FC_2 . How can I name them in order? Commented Sep 29, 2021 at 19:37
  • gis.stackexchange.com/questions/ask Commented Sep 29, 2021 at 22:20

1 Answer 1

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.

answered Sep 27, 2021 at 3:05
2
  • I have a question. For first solution that you recommended If we assume length (New_OID) = 10. So the arcpy create MAKE_FC_0 and MAKE_FC_1 and MAKE_FC_10 Respectively. so I want to create for i=2,MAKE_FC_2 but it shows me MAKE_FC_10. What is your suggestion?@SonofaBeach Commented 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). Commented Sep 29, 2021 at 21:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.