# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# Make Feature Layer.py
# Select Layer By Attribute.py
# Delete Rows.py
# "for" loop and "if, else"
# Can run all layer in one geodatabase.
# Regonize railway and road layer.
# Created on: 2021-05-20 15:34:47.00000
#   (generated by ArcGIS/ModelBuilder)
# Description:
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy
import os

# Set path.
arcpy.env.workspace = "D:\\ArcMap test\\DeleteRow.gdb\\"
arcpy.env.overwriteOutput = True

# Select railway & road table by SQL.
railway_select = "fclass in ('abandoned', 'miniature')"
road_select = "fclass in ('unclassified', 'residential')"

# Get all contents in GDB.
datasets1 = arcpy.ListDatasets(feature_type='feature')
datasets2 = [''] + datasets1 if datasets1 is not None else []

for ds in datasets2:
    for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
        transform = os.path.join(arcpy.env.workspace, ds, fc)

        railway = "_railway"

        if railway in transform:
            # Make a railway temporary feature layer, select layer, and delete select rows.
            arcpy.MakeFeatureLayer_management(transform, "transform_layer")
            arcpy.SelectLayerByAttribute_management("transform_layer", "NEW_SELECTION", railway_select)
            arcpy.DeleteRows_management("transform_layer")
            print transform + " has Deleted."

        else:
            # Make a road temporary feature layer, select layer, and delete select rows.
            arcpy.MakeFeatureLayer_management(transform, "transform_layer")
            arcpy.SelectLayerByAttribute_management("transform_layer", "NEW_SELECTION", road_select)
            arcpy.DeleteRows_management("transform_layer")
            print transform + " has Deleted."

print "Finished."
