# Path: lib/channelunits.py
# def loadChannelUnitsFromSQLite(visitID, workbenchDB):
#     """
#     Loads a dictionary of channel unit information from the workbench SQLite database
#     :param visitID: VisitID
#     :param workbenchDB: Full path to the SQLite workbench database
#     :return: dictionary of channel unit info.
#     """
# 
#     dUnits = {}
# 
#     conn = sqlite3.connect(workbenchDB)
#     c = conn.cursor()
#     c.execute("select ChannelUnitNumber, Tier1, Tier2, SegmentNumber from CHaMP_ChannelUnits WHERE (VisitID = ?)", [visitID])
#     for unitRow in c.fetchall():
#         dUnits[unitRow[0]] = (unitRow[1], unitRow[2], unitRow[3])
# 
#     return dUnits
# 
# def loadChannelUnitsFromAPI(vid):
# 
#     apiUnits = APIGet('visits/{}/measurements/Channel Unit'.format(vid))
#     dUnits = {}
# 
#     for nodUnit in apiUnits['values']:
#         value = nodUnit['value']
#         nCUNumber = int(value['ChannelUnitNumber'])
#         tier1 = value['Tier1']
#         tier2 = value['Tier2']
#         segment = value['ChannelSegmentID']
# 
#         dUnits[nCUNumber] = (tier1, tier2, segment)
# 
#     log = Logger("Channel Units")
#     log.info("{0} channel units loaded from XML file".format(len(dUnits)))
# 
#     return dUnits


from vector import CHaMP_Polygon
from validation_classes import *
from lib.channelunits import loadChannelUnitsFromSQLite, loadChannelUnitsFromAPI
from shapely.geometry import MultiPolygon
from vector import polyinsidepoly
        self.wetted_extent = None

    def load_attributes_db(self, visitid, sqlitedb):
        self.dAuxUnits = loadChannelUnitsFromSQLite(visitid, sqlitedb)
        return

    def load_attributes(self, visitID):
        self.dAuxUnits = loadChannelUnitsFromAPI(visitID)
        return

    def validate(self):
        results = super(CHaMP_ChannelUnits, self).validate()
        validate_minarea = ValidationResult(self.__class__.__name__, "MinimumArea")
        validate_unitnumber_field = ValidationResult(self.__class__.__name__, "FieldUnitNumberExists")
        validate_unitnumber_field_notnull = ValidationResult(self.__class__.__name__, "FieldUnitNumberNotNull")
        validate_unitnumber_field_positive = ValidationResult(self.__class__.__name__, "FieldUnitNumberPositive")
        validate_unitnumber_field_values_unique = ValidationResult(self.__class__.__name__, "FieldUnitNumberUnique")
        # Attributes
        validate_aux_in_gis = ValidationResult(self.__class__.__name__, "UnitNumbersAuxInGIS")
        validate_gis_in_aux = ValidationResult(self.__class__.__name__, "UnitNumbersGISInAux")
        # Geoms
        validate_inwettedextent = ValidationResult(self.__class__.__name__, "ChannelUnitsWithinWettedExtent")
        validate_no_overlap = ValidationResult(self.__class__.__name__, "ChannelUnitsOverlap")

        if self.exists():
            if self.feat_min_area(self.minArea):
                validate_minarea.pass_validation()
            else:
                validate_minarea.error("One or more features in dataset is smaller than minimum allowed (" +
                                       str(self.minArea) + ")")

# Path: lib/channelunits.py
# def loadChannelUnitsFromSQLite(visitID, workbenchDB):
#     """
#     Loads a dictionary of channel unit information from the workbench SQLite database
#     :param visitID: VisitID
#     :param workbenchDB: Full path to the SQLite workbench database
#     :return: dictionary of channel unit info.
#     """
# 
#     dUnits = {}
# 
#     conn = sqlite3.connect(workbenchDB)
#     c = conn.cursor()
#     c.execute("select ChannelUnitNumber, Tier1, Tier2, SegmentNumber from CHaMP_ChannelUnits WHERE (VisitID = ?)", [visitID])
#     for unitRow in c.fetchall():
#         dUnits[unitRow[0]] = (unitRow[1], unitRow[2], unitRow[3])
# 
#     return dUnits
# 
# def loadChannelUnitsFromAPI(vid):
# 
#     apiUnits = APIGet('visits/{}/measurements/Channel Unit'.format(vid))
#     dUnits = {}
# 
#     for nodUnit in apiUnits['values']:
#         value = nodUnit['value']
#         nCUNumber = int(value['ChannelUnitNumber'])
#         tier1 = value['Tier1']
#         tier2 = value['Tier2']
#         segment = value['ChannelSegmentID']
# 
#         dUnits[nCUNumber] = (tier1, tier2, segment)
# 
#     log = Logger("Channel Units")
#     log.info("{0} channel units loaded from XML file".format(len(dUnits)))
# 
#     return dUnits


# Path: tools/validation/classes/channel_units.py
from vector import CHaMP_Polygon
from validation_classes import *
from lib.channelunits import loadChannelUnitsFromSQLite, loadChannelUnitsFromAPI
from shapely.geometry import MultiPolygon
from vector import polyinsidepoly

        self.wetted_extent = None

    def load_attributes_db(self, visitid, sqlitedb):
        self.dAuxUnits = loadChannelUnitsFromSQLite(visitid, sqlitedb)
        return

    def load_attributes(self, visitID):
        self.dAuxUnits = loadChannelUnitsFromAPI(visitID)
        return

    def validate(self):
        results = super(CHaMP_ChannelUnits, self).validate()
        validate_minarea = ValidationResult(self.__class__.__name__, "MinimumArea")
        validate_unitnumber_field = ValidationResult(self.__class__.__name__, "FieldUnitNumberExists")
        validate_unitnumber_field_notnull = ValidationResult(self.__class__.__name__, "FieldUnitNumberNotNull")
        validate_unitnumber_field_positive = ValidationResult(self.__class__.__name__, "FieldUnitNumberPositive")
        validate_unitnumber_field_values_unique = ValidationResult(self.__class__.__name__, "FieldUnitNumberUnique")
        # Attributes
        validate_aux_in_gis = ValidationResult(self.__class__.__name__, "UnitNumbersAuxInGIS")
        validate_gis_in_aux = ValidationResult(self.__class__.__name__, "UnitNumbersGISInAux")
        # Geoms
        validate_inwettedextent = ValidationResult(self.__class__.__name__, "ChannelUnitsWithinWettedExtent")
        validate_no_overlap = ValidationResult(self.__class__.__name__, "ChannelUnitsOverlap")

        if self.exists():
            if self.feat_min_area(self.minArea):
                validate_minarea.pass_validation()
            else:
                validate_minarea.error("One or more features in dataset is smaller than minimum allowed (" +
                                       str(self.minArea) + ")")

            if self.field_exists(self.fieldName_UnitNumber):