Select to view content in your preferred language

How to split the data for drawing number wise all features from mdb or gdb file?

2439
13
Jump to solution
02-02-2019 08:00 AM
santhoshp
Occasional Contributor

Hi Dear Friends,

How to split the data drawing number wise for all features from mdb or gdb file.

I have 8 types of drawing numbers available in data I want to split each drawing number separate data mdb format or gdb format file.

Thanks

Santhosh

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

Since you are using 10.1, I experimented with SelectLayerByAttributes.  I found it easiest to code to work with one geodatabase (the one in your zip file).  I first ran a script to get a list of the unique attributes used in the DRAWING_REFERENCE fields of all the features in the gdb.  Since the drawing reference is rather long, I paired it with a shorter substring.  This is what the gdb looks like in catalog:

split attributes

Each feature is appended with the short substring; in this case, GF is highlighted.  Here's the code:

import arcpy
import os

gdb = r'C:\Path\to\COBW_Firealarm.gdb'
arcpy.env.workspace = gdb # needed for ListDatasets

fields = ['DRAWING_REFERENCE']

draw_ref = [ # unique values in DRAWING_REFERENCE fields, name append value
    [ '111-111-AP','AP' ],
    [ 'C0500-GS-0117-04-EL-FA-A0-01-001-001', '01' ],
    [ 'C0500-GS-0117-04-EL-FA-A0-02-001-001', '02' ],
    [ 'C0500-GS-0117-01-EL-FA-A0-03-001-001', '03' ],
    [ 'C0500-GS-0117-04-EL-FA-A0-04-001-001', '04' ],
    [ 'C0500-GS-0117-04-EL-FA-A0-05-001-001', '05' ],
    [ 'C0500-GS-0117-04-EL-FA-A0-06-001-001', '06' ],
    [ 'C0500-GS-0117-01-EL-FA-A0-B1-001-001', 'B1' ],
    [ 'C0500-GS-0117-04-EL-FA-A0-B2-001-001', 'B2' ],
    [ 'C0500-GS-0117-04-EL-FA-A0-GF-001-001', 'GF' ]
    ] # name append keeps feature name from becoming too long


for fds in arcpy.ListDatasets('','Feature'):

    print "\nProcessing {}".format(fds)

    for fc in arcpy.ListFeatureClasses('','',fds):
        print "\t{}".format(fc) # use fc for name of target geodatabase
        in_fc = os.path.join(gdb,fds,fc)
        if arcpy.Exists('temp_layer'):
            arcpy.Delete_management('temp_layer')
        arcpy.MakeFeatureLayer_management(in_fc,'temp_layer')
        ref_num = 0
        
        for dr in draw_ref: # loop through drawing reference list
            # dr[0] = DRAWING_REFERENCE field value
            # dr[1] = a substring of DRAWING_REFERENCE used in naming new feature class
            arcpy.SelectLayerByAttribute_management('temp_layer',
                                                    'NEW_SELECTION',
                                                    where_clause = "DRAWING_REFERENCE = '{}'".format(dr[0]))
            count = int(arcpy.GetCount_management('temp_layer').getOutput(0))

            if count > 0:
                ref_num += 1
                new_fc = os.path.join(fds, "{}_{}".format(fc,dr[1]))
                print "\t\t{}  -- count: {}".format(new_fc,count)
                # write the selected features to a new featureclass
                arcpy.CopyFeatures_management('temp_layer', new_fc)

        print "\t\t{} total new features".format(ref_num)

arcpy.Delete_management('temp_layer')
print "\nDone."‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

For the next steps, you can use a script to loop through the draw_ref list, making a copy the first gdb, deleting the features that don't match the current value in the draw_ref list, and repeat until each drawing reference has its own gdb.

Hope this helps.

View solution in original post

13 Replies
DanPatterson_Retired
MVP Emeritus
santhoshp
Occasional Contributor

Dear Dan Patterson,

yes It is working as a shape file but i nedd direct feature class it's possible or not if it's possible plz help me.

Thanks

Santhosh

0 Kudos
DanPatterson_Retired
MVP Emeritus

make the destination output a geodatabase, or easier still, use Copy Features to bring the shapefile into a gdb, then do the split

RandyBurton
MVP Alum

When you say 'drawing number', are you referring to the DRAWING_REFERENCE field in your data?  If so, I'm counting 10 different values.

111-111-AP
C0500-GS-0117-01-EL-FA-A0-03-001-001
C0500-GS-0117-01-EL-FA-A0-B1-001-001
C0500-GS-0117-04-EL-FA-A0-01-001-001
C0500-GS-0117-04-EL-FA-A0-02-001-001
C0500-GS-0117-04-EL-FA-A0-04-001-001
C0500-GS-0117-04-EL-FA-A0-05-001-001
C0500-GS-0117-04-EL-FA-A0-06-001-001
C0500-GS-0117-04-EL-FA-A0-B2-001-001
C0500-GS-0117-04-EL-FA-A0-GF-001-001
‍‍‍‍‍‍‍‍‍‍

If this is not what you mean, could you explain 'drawing number'.

0 Kudos
santhoshp
Occasional Contributor

Dear Randy Burton,

In this gdb have DRAWING_REFERENCE field in that different drawing numbers available in gdb.

for example:

C0500-GS-0117-01-EL-FA-A0-03-001-001 this is the drawing number.

Thanks

Santhosh

0 Kudos
RandyBurton
MVP Alum

And your version of ArcGIS?  10.5 or 10.6 or Pro ?  Looks like Split By Attributes requires one of these versions.

0 Kudos
RandyBurton
MVP Alum

The following code will split your features based on DRAWING_REFERENCE and place them in a geodatabase named with the feature class.  From here you should be able to copy them into the desired structure with additional scripting.  It does appear that domains and subtypes are preserved.  I could not get SplitAttributes to create the features inside a dataset, but it might output to shape files.

import arcpy
import os

gdb = r'C:\path\to\folder\COBW_Firealarm\COBW_Firealarm.gdb'
arcpy.env.workspace = gdb
out_folder =  r'C:\path\to\folder\COBW_Firealarm' # geodatabases go here

fields = ['DRAWING_REFERENCE']

for fds in arcpy.ListDatasets('','Feature'):
    print "Processing {}".format(fds)
    sr = arcpy.Describe(fds).SpatialReference
    for fc in arcpy.ListFeatureClasses('','',fds):
        print "\t{}".format(fc) # use fc for name of target geodatabase
        arcpy.CreateFileGDB_management(out_folder, '{}.gdb'.format(fc))
        in_fc = os.path.join(gdb,fds,fc)
        target = os.path.join(out_folder,'{}.gdb'.format(fc))
        # print in_fc
        # print target
        # save split in file geodatabase with name of original feature class
        arcpy.SplitByAttributes_analysis(in_fc, target, fields)

Here's what the output looked like:

split output

Hope this helps.

santhoshp
Occasional Contributor

Dear Randy Burton,

I am getting error can you give me the solution for this error. I am using ArcGIS 10.1

Attached below image for your reference.

Error Below Code

Processing HVAC
 HVAC_Fan
Traceback (most recent call last):
  File "C:\path\to\folder\COBW_Firealarm\split drawing.py", line 15, in <module>
    arcpy.CreateFileGDB_management(out_folder, '{}.gdb'.format(fc))
  File "C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 14458, in CreateFileGDB
    raise e
ExecuteError: ERROR 000258: Output C:\path\to\folder\COBW_Firealarm\HVAC_Fan.gdb already exists
Failed to execute (CreateFileGDB).

Thanks

Santhosh

0 Kudos
DanPatterson_Retired
MVP Emeritus

check the folder in the error message... the geodatabase already exists and you can't overwrite it.