Skip to content

Instantly share code, notes, and snippets.

@AlexArcPy
Last active February 5, 2022 06:13
Show Gist options
  • Save AlexArcPy/12b3a5bf8b47a3d03a11 to your computer and use it in GitHub Desktop.
Save AlexArcPy/12b3a5bf8b47a3d03a11 to your computer and use it in GitHub Desktop.
OOP approach for arcpy users
import arcpy
import os
########################################################################
class FeatureClass(object):
"""FeatureClass object"""
#----------------------------------------------------------------------
def __init__(self,path):
"""Constructor for FeatureClass"""
gdb_attributes_dict = {'path': arcpy.Describe(path).path,
'baseName': arcpy.Describe(path).baseName,
'featureType': arcpy.Describe(path).featureType,
'hasM': arcpy.Describe(path).hasM,
'hasZ': arcpy.Describe(path).hasZ,
'hasSpatialIndex': arcpy.Describe(path).hasSpatialIndex,
'shapeFieldName': arcpy.Describe(path).shapeFieldName,
'shapeType': arcpy.Describe(path).shapeType,
}
for k,v in gdb_attributes_dict.iteritems():
setattr(self, k, v)
setattr(self,'fcPath',os.path.join(self.path,self.baseName))
setattr(self,'fcFeatures',Features(path))
setattr(self,'fields',[field.name for field in arcpy.ListFields(path)])
#----------------------------------------------------------------------
def __str__(self):
return self.baseName
########################################################################
class Features:
"""Feature objects in FeatureClass object"""
#----------------------------------------------------------------------
def __init__(self,path):
"""Constructor for Features"""
self.path = path
self.features = [feature for feature in arcpy.da.SearchCursor(self.path,"*")]
self.fields = [field.name for field in arcpy.ListFields(path)]
self.attribute_table = [{self.fields[feat.index(v)]:v for v in feat} for feat in self.features]
class Geodatabase(object):
"""Geodatabase object"""
_path = None
#----------------------------------------------------------------------
def __init__(self,path,initialize=False):
self.path = path
if initialize:
self.__init()
#----------------------------------------------------------------------
def __init(self):
"""populates advanced gdb properties"""
path = self.path
arcpy.env.workspace = self.path
gdb_attributes_dict = {'name':arcpy.Describe(path).name,
'release': arcpy.Describe(path).release,
'workspaceType': arcpy.Describe(path).workspaceType,
'connectionProperties': arcpy.Describe(path).connectionProperties,
'connectionString': arcpy.Describe(path).connectionString,
'workspaceFactoryProgID': arcpy.Describe(path).workspaceFactoryProgID,
'featureClassesNames': arcpy.ListFeatureClasses(),
'featureClassesFullPaths': [os.path.join(self.path,fc)
for fc in arcpy.ListFeatureClasses()]}
for k,v in gdb_attributes_dict.iteritems():
setattr(self, k, v)
#----------------------------------------------------------------------
def __str__(self):
"""returns the object as a string"""
return str({'path':self.path,
'release':self.release})
#----------------------------------------------------------------------
def __repr__(self):
"""system representation of a class"""
return str({'name': self.name,
'properties': str(self.__dict__)})
#----------------------------------------------------------------------
@property
def path(self):
return self._path
#----------------------------------------------------------------------
@path.setter
def path(self,path_value):
if self._path is not None:
raise ValueError("You shouldn't modify the gdb path!")
if path_value[-4:] != ".gdb":
print "Not a valid gdb"
raise AttributeError("Geodatabase path must end with .gdb")
self._path = path_value
#----------------------------------------------------------------------
@property
def feature_classes_objects(self):
"""gets a list of feature class objects in the gdb"""
return [FeatureClass(path) for path in self.featureClassesFullPaths]
#----------------------------------------------------------------------
@property
def feature_class_features(self,fc_name):
"""gets a list of feature objects in the feature class"""
return [FeatureClass(path) for path in [os.path.join(gdb.path,fc_name)]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment