AnsweredAssumed Answered

Not Able to Copy Selection From Shapefile to File Geodatabase

Question asked by behClevest on Oct 16, 2018

I am using following to copy selected feature from a Shapefile to a Dataset in a File Geodatabase

 

As you can see I tried to create layer from shapefile by doing

arcpy.MakeFeatureLayer_management(inFeature, 'lyr')

and then select the attribute like 

 

arcpy.SelectLayerByAttribute_management('lyr', "NEW_SELECTION", '"NAME_1" = \''+slectedCounty+'\' AND "NAME_2" = \''+county+'\'')

and eventually I tried to copy selected to the created dataset in the generated File Geodatabase

arcpy.CopyFeatures_management("lyr", dataset_path+'\\'+county.replace(" ", "_") )

 

I am not getting any error message but the created dataset in the Geodatabase is empty. I testes the out message of 

arcpy.AddMessage('"NAME_1" = \''+slectedCounty+'\'  AND "NAME_2" = \''+county+'\'')

in ArcMap Select by Attribute window and it is selecting the layer which mean the query is correct but I am not able to load the selection into the dataset.

    def execute(self, parameters, messages):
        """The source code of the tool."""
        inFeature = parameters[0].valueAsText
        slectedCounty = parameters[1].valueAsText
        counties = []
        ws = parameters[2].valueAsText
        arcpy.CreateFileGDB_management(ws, slectedCounty)
        dataset_path = ws+'\\'+ slectedCounty +'.gdb'
        sr = arcpy.SpatialReference(4326)
        arcpy.AddMessage(dataset_path)
        arcpy.MakeFeatureLayer_management(inFeature, 'lyr')
        def unique_values(table , field):
            arcpy.AddMessage('Proccess Started ...')
            with arcpy.da.SearchCursor(table, [field], '"NAME_1" = \''+slectedCounty+'\'') as cursor:
                 return sorted({row[0] for row in cursor})
        uniques = unique_values(inFeature, 'NAME_2')
        for unique in uniques:
            counties.append(unique)
        for county in counties:
            arcpy.AddMessage(county)
            arcpy.CreateFeatureDataset_management(dataset_path, county.replace(" ", "_"), sr)
            arcpy.AddMessage('The ' + county + ' County Added to the Geodatabase')
            arcpy.AddMessage('Adding Layer to GDB Started')
            arcpy.AddMessage('"NAME_1" = \''+slectedCounty+'\'  AND "NAME_2" = \''+county+'\'')
            arcpy.SelectLayerByAttribute_management('lyr', "NEW_SELECTION", '"NAME_1" = \''+slectedCounty+'\'  AND "NAME_2" = \''+county+'\'')
            arcpy.CopyFeatures_management("lyr", dataset_path+'\\'+county.replace(" ", "_") )
        return

 

Can you please let me know what I am doing wrong or missing here?

Outcomes