def get_z_on_dem(self, filterlist=None):
     feats = self.features
     if feats and self.dem:
         r = Raster(self.dem)
         if filterlist:
             feats = [feat for feat in feats if feat['fields'][self.fieldName_Description] in filterlist]
         feats = [feat for feat in feats for poly in self.demDataExtent if poly.contains(feat['geometry'])]
         list_z = [r.getPixelVal([feat['geometry'].x, feat['geometry'].y]) for feat in feats]
         return list_z
     else:
         return []
    def calc(self, sThalwegshp, sDepthRaster, sWaterSurfaceRaster, fDist,
             visitMetrics):

        if not path.isfile(sThalwegshp):
            raise MissingException("Thalweg shapefile missing")
        if not path.isfile(sDepthRaster):
            raise MissingException("Depth raster missing")
        if not path.isfile(sWaterSurfaceRaster):
            raise MissingException("Surface raster missing")

        wettedMainstemLength = visitMetrics['Wetted']['Centerline'][
            'MainstemLength']

        if wettedMainstemLength is None:
            raise MissingException(
                "No wetted mainstem length found in visit metrics")

        sfile = Shapefile(sThalwegshp).featuresToShapely()

        if len(sfile) < 1:
            raise DataException("Thalweg shapefile has no features")

        thalweg = sfile[0]['geometry']
        depthRaster = Raster(sDepthRaster)
        waterSurfaceRaster = Raster(sWaterSurfaceRaster)
        samplepts = ThalwegMetrics.interpolateRasterAlongLine(thalweg, fDist)
        results = ThalwegMetrics.lookupRasterValues(samplepts,
                                                    depthRaster)['values']

        # Get the elevation at the first (downstream) point on the Thalweg
        dsElev = waterSurfaceRaster.getPixelVal(thalweg.coords[0])
        usElev = waterSurfaceRaster.getPixelVal(thalweg.coords[-1])

        if (np.isnan(dsElev)):
            raise DataException(
                'nodata detected in the raster for downstream point on the thalweg'
            )
        elif np.isnan(usElev):
            raise DataException(
                'nodata detected in the raster for upstream point on the thalweg'
            )

        waterSurfaceGradientRatio = (usElev - dsElev) / thalweg.length
        waterSurfaceGradientPC = waterSurfaceGradientRatio * 100.0

        # Thalweg straight length and sinuosity
        firstPoint = Point(thalweg.coords[0])
        lastPoint = Point(thalweg.coords[-1])
        straightLength = firstPoint.distance(lastPoint)
        sinuosity = thalweg.length / straightLength

        self.metrics = {
            'Min': np.nanmin(results),
            'Max': np.nanmax(results),
            'Mean': np.mean(results),
            'StDev': np.std(results),
            'Count': np.count_nonzero(results),
            'Length': thalweg.length,
            'WSGradientRatio': waterSurfaceGradientRatio,
            'WSGradientPC': waterSurfaceGradientPC,
            'Sinuosity': sinuosity,
            'CV': 0.0,
            'ThalwegToCenterlineRatio': thalweg.length / wettedMainstemLength
            #, 'Values': results.data
        }
        if self.metrics['StDev'] != 0 and self.metrics['Mean'] != 0:
            self.metrics['CV'] = self.metrics['StDev'] / self.metrics['Mean']
    def calc(self, shpCUPath, shpThalweg, rasDepth, visitMetrics, dUnits,
             unitDefs):

        if not os.path.isfile(shpCUPath):
            raise MissingException("Channel units file not found")
        if not os.path.isfile(shpThalweg):
            raise MissingException("Thalweg shape file not found")
        if not os.path.isfile(rasDepth):
            raise MissingException("Depth raster file not found")

        siteLength = visitMetrics['Wetted']['Centerline']['MainstemLength']

        if siteLength is None:
            raise DataException("No valid site length found in visit metrics")

        # Give us a fresh template with 0's in the value positions
        self.metrics = self._templateMaker(0, unitDefs)
        dResultsChannelSummary = self.metrics['ResultsChannelSummary']
        dResultsTier1 = self.metrics['ResultsTier1']
        dResultsTier2 = self.metrics['ResultsTier2']
        resultsCU = self.metrics['resultsCU']

        #Load the Thalweg feature
        thalweg = Shapefile(shpThalweg).featuresToShapely()
        thalwegLine = thalweg[0]['geometry']

        # Load the depth raster
        depthRaster = Raster(rasDepth)

        # Load the channel unit polygons and calculate the total area
        # The channel units should be clipped to the wetted extent and so this
        # can be used as the site area wetted
        shpCU = Shapefile(shpCUPath)
        arrCU = depthRaster.rasterMaskLayer(shpCUPath, "UnitNumber")

        feats = shpCU.featuresToShapely()
        for aFeat in feats:
            dResultsChannelSummary['Main']['Area'] += aFeat['geometry'].area

        # Loop over each channel unit and calculate topometrics
        for aFeat in feats:
            nCUNumber = int(aFeat['fields']['UnitNumber'])

            if nCUNumber not in dUnits:
                self.log.error(
                    "Channel Unit: '{0}' not present in the aux data.".format(
                        nCUNumber))
                # Keep it general for the exception so we can aggregate them
                raise DataException(
                    "The Channel Unit ShapeFile contains a unit number that is not present in the aux data."
                )

            tier1Name = dUnits[nCUNumber][0]
            tier2Name = dUnits[nCUNumber][1]
            nSegment = dUnits[nCUNumber][2]
            #print "Channel Unit Number {0}, Segment {1}, Tier 1 - {2}, Tier 2 - {3}".format(nCUNumber, nSegment, tier1Name, tier2Name)

            unitMetrics = {}
            resultsCU.append(unitMetrics)
            unitMetrics['ChannelUnitNumber'] = nCUNumber
            unitMetrics['Area'] = aFeat['geometry'].area
            unitMetrics['Tier1'] = tier1Name
            unitMetrics['Tier2'] = tier2Name
            unitMetrics['Length'] = None
            unitMetrics['ResidualDepth'] = None
            unitMetrics['DepthAtThalwegExit'] = None
            unitMetrics['ThalwegIntersect'] = 0

            # Get the depth raster for this unit as variable so we can check
            # whether it is entirely masked below.
            depArr = depthRaster.array[arrCU == nCUNumber]
            if depArr.count() == 0:
                unitMetrics['MaxDepth'] = 0
                unitMetrics['Volume'] = 0
            else:
                unitMetrics['MaxDepth'] = np.max(depArr)
                unitMetrics['Volume'] = np.sum(depthRaster.array[
                    arrCU == nCUNumber]) * (depthRaster.cellWidth**2)

            if nSegment != 1:
                dSideChannelSummary = dResultsChannelSummary[
                    'SideChannelSummary']
                dMain = dResultsChannelSummary['Main']
                # Side channel summary captures both small and large side channels
                dSideChannelSummary['Area'] += aFeat['geometry'].area
                dSideChannelSummary['Count'] += 1
                dSideChannelSummary['Percent'] = 100 * dSideChannelSummary[
                    'Area'] / dMain['Area']
                dSideChannelSummary['Volume'] += unitMetrics['Volume']

                if 'side' in tier1Name.lower():
                    dSmallSideChannel = dResultsChannelSummary[
                        'SmallSideChannel']
                    dSmallSideChannel['Area'] += aFeat['geometry'].area
                    dSmallSideChannel['Count'] += 1
                    dSmallSideChannel['Percent'] = 100 * dSmallSideChannel[
                        'Area'] / dMain['Area']
                    dSmallSideChannel['Volume'] += unitMetrics['Volume']
                else:
                    dLargeSideChannel = dResultsChannelSummary[
                        'LargeSideChannel']
                    dLargeSideChannel['Area'] += aFeat['geometry'].area
                    dLargeSideChannel['Count'] += 1
                    dLargeSideChannel['Percent'] = 100 * dLargeSideChannel[
                        'Area'] / dMain['Area']
                    dLargeSideChannel['Volume'] += unitMetrics['Volume']

            if tier1Name is None:
                raise DataException("tier1Name cannot be 'None'")

            if 'side' in tier1Name.lower():
                dResultsChannelSummary['ChannelUnitBreakdown'][
                    'SmallSideChannel'] += 1
            else:
                dResultsChannelSummary['ChannelUnitBreakdown']['Other'] += 1

            if (thalwegLine.intersects(aFeat['geometry'])):
                cuThalwegLine = thalwegLine.intersection(aFeat['geometry'])

                exitPoint = None
                if cuThalwegLine.type == 'LineString':
                    exitPoint = cuThalwegLine.coords[0]
                else:
                    exitPoint = cuThalwegLine[0].coords[0]

                # Retrieve a list of points along the Thalweg in the channel unit
                thalwegPoints = ChannelUnitMetrics.interpolatePointsAlongLine(
                    cuThalwegLine, 0.13)
                thalwegDepths = ChannelUnitMetrics.lookupRasterValuesAtPoints(
                    thalwegPoints, depthRaster)
                unitMetrics['MaxDepth'] = np.nanmax(thalwegDepths['values'])
                unitMetrics['DepthAtThalwegExit'] = depthRaster.getPixelVal(
                    exitPoint)
                unitMetrics['ResidualDepth'] = unitMetrics[
                    'MaxDepth'] - unitMetrics['DepthAtThalwegExit']
                unitMetrics['Length'] = cuThalwegLine.length
                unitMetrics['ThalwegIntersect'] = 1

            # Tier 1 and tier 2 topometrics. Note that metric dictionary keys are used for XML tags & require cleaning
            tier1NameClean = getCleanTierName(tier1Name)
            self._calcTierLevelMetrics(dResultsTier1[tier1NameClean],
                                       tier1Name, unitMetrics, siteLength,
                                       dResultsChannelSummary['Main']['Area'])

            tier2NameClean = getCleanTierName(tier2Name)
            self._calcTierLevelMetrics(dResultsTier2[tier2NameClean],
                                       tier2Name, unitMetrics, siteLength,
                                       dResultsChannelSummary['Main']['Area'])

        # Calculate the average of the channel unit max depths for each tier 1 and tier 2 type
        for tierKey, tierMetrics in {
                'Tier1': dResultsTier1,
                'Tier2': dResultsTier2
        }.iteritems():
            for tierName, metricDict in tierMetrics.iteritems():
                maxDepthList = [
                    aResult['MaxDepth'] for aResult in resultsCU
                    if getCleanTierName(aResult[tierKey]) == tierName
                ]
                if len(maxDepthList) > 0:
                    metricDict['AvgMaxDepth'] = np.average(maxDepthList)

        # Convert the sum of residual depth and depth at thalweg exist
        # to average residual depth for each tier 1 and tier 2 type
        for tierMetricDict in [dResultsTier1, dResultsTier2]:
            for tierName, tierMetrics in tierMetricDict.iteritems():
                # channel unit types that don't occur should retain the value None for Residual Depth and Depth at Thalweg exit
                if tierMetrics['Count'] > 0 and tierMetrics[
                        'ThalwegIntersectCount'] > 0:
                    for metricName in ['ResidualDepth', 'DepthAtThalwegExit']:
                        if tierMetrics[metricName] is not None and tierMetrics[
                                metricName] != 0:
                            tierMetrics[metricName] = tierMetrics[
                                metricName] / tierMetrics[
                                    'ThalwegIntersectCount']
                        else:
                            tierMetrics[metricName] = 0
    def validate(self):
        results = super(CHaMP_Thalweg, self).validate()

        validate_maxfeaturecount = ValidationResult(self.__class__.__name__, "MaxFeatureCount")
        validate_wsedemExtent = ValidationResult(self.__class__.__name__, "WithinWSEDEMExtent")
        validate_in_end_dist = ValidationResult(self.__class__.__name__, "InPointNearEnd")
        validate_out_start_dist = ValidationResult(self.__class__.__name__, "OutPointNearStart")
        validate_out_higher_inflow = ValidationResult(self.__class__.__name__, "StartPointLowerEndPoint")
        validate_thalwegstartstopraster = ValidationResult(self.__class__.__name__, "ThalwegStartStopOnDEM")

        if self.exists():
            if len(self.features) > self.maxFeatureCount:
                validate_maxfeaturecount.error("Number of features (" + str(len(self.features)) +
                                               ") exceeds the maximum number allowed ("
                                               + str(self.maxFeatureCount) + ")")
            else:
                validate_maxfeaturecount.pass_validation()
            if self.wsedemExtent:
                if self.start_stop_on_raster(raster_extent=self.wsedemExtent):
                    validate_wsedemExtent.pass_validation()
                else:
                    validate_wsedemExtent.error("Thalweg not entirely contained within WSEDEM.")
            if self.topo_in_point:
                inbuffer = self.topo_in_point.buffer(15)
                if inbuffer.contains(self.thalweg_end_pt()):
                    validate_in_end_dist.pass_validation()
                else:
                    validate_in_end_dist.warning("End point is greater than 15m from Topo 'in' point")
            if self.topo_out_point:
                if self.topo_out_point.buffer(15).contains(self.thalweg_start_pnt()):
                    validate_out_start_dist.pass_validation()
                else:
                    validate_out_start_dist.warning("Start point is greater than 15m from Topo 'out' point")
            if self.dem:
                if self.demDataExtent and self.features:
                    if self.start_stop_on_raster():
                        validate_thalwegstartstopraster.pass_validation()
                    else:
                        validate_thalwegstartstopraster.error("One or more line features does not start or stop on the DEM")
                r = Raster(self.dem)

                tStart = self.thalweg_start_pnt()
                tEnd = self.thalweg_end_pt()

                if tStart is None or tEnd is None:
                    validate_out_higher_inflow.error("Could not determine thalweg start and finish")
                else:
                    z_start = r.getPixelVal([tStart.x, tStart.y])
                    z_end = r.getPixelVal([tEnd.x, tEnd.y])
                    if z_start > z_end + 0.1:
                        validate_out_higher_inflow.error("Thalweg Start (outflow) more than 10cm higher than end (inflow)")
                    else:
                        validate_out_higher_inflow.pass_validation()

        results.append(validate_maxfeaturecount.get_dict())
        results.append(validate_wsedemExtent.get_dict())
        results.append(validate_in_end_dist.get_dict())
        results.append(validate_out_start_dist.get_dict())
        results.append(validate_out_higher_inflow.get_dict())
        results.append(validate_thalwegstartstopraster.get_dict())

        return results