Class ModelsEngine


  • public class ModelsEngine
    extends Object
    A class containing several methods used by the modules.

    The methods are not static for usage in multithreading environment.

    Author:
    Andrea Antonello (www.hydrologis.com), Silvia Franceschi (www.hydrologis.com), Erica Ghesla, Daniele Andreis
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static org.opengis.referencing.datum.PixelInCell DEFAULTPIXELANCHOR  
      static int[][] DIR
      Deprecated.
      this should not be used.
    • Constructor Summary

      Constructors 
      Constructor Description
      ModelsEngine()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static double approximate2Multiple​(double valueToApproximate, double divisor)
      Approximate a value to a multiple of a divisor value.
      static double[] calcInverseSunVector​(double[] sunVector)
      Calculating the inverse of the sun vector.
      static double[] calcNormalSunVector​(double[] sunVector)
      Calculating the normal to the sun vector.
      static WritableRaster calculateFactor​(int h, int w, double[] sunVector, double[] inverseSunVector, double[] normalSunVector, WritableRaster demWR, double dx)
      Evaluate the shadow map calling the shadow method.
      static double calculateNthMoment​(double[] values, int validValues, double mean, double momentOrder, IHMProgressMonitor pm)
      Calculates the nth moment of a set of values.
      static WritableRaster extractSubbasins​(javax.media.jai.iterator.WritableRandomIter flowIter, int flowNovalue, javax.media.jai.iterator.RandomIter netIter, javax.media.jai.iterator.WritableRandomIter netNumberIter, int rows, int cols, IHMProgressMonitor pm)
      Extract the subbasins of a raster map.
      static double gamma​(double x)
      The Gamma function.
      static int getFlowDirection​(int i, int j)
      Returns the flow direction value for a given point as indexes (i, j) of the dir matrix.
      static boolean go_downstream​(int[] colRow, double flowdirection)
      Moves one pixel downstream.
      static void go_upstream_a​(int[] p, javax.media.jai.iterator.RandomIter flowRandomIter, javax.media.jai.iterator.RandomIter tcaRandomIter, javax.media.jai.iterator.RandomIter lRandomIter, int[] param)
      Moves one pixel upstream.
      static void goUpStreamOnNetFixed​(int[] colRow, javax.media.jai.iterator.RandomIter flowIterator, javax.media.jai.iterator.RandomIter netnumIterator, int[] param)
      Moves one pixel upstream following the supplied network.
      static double henderson​(double[][] data, int tp)
      Interpolates the width function in a given tp.
      static boolean isSourcePixel​(javax.media.jai.iterator.RandomIter flowIter, int col, int row)
      Verifies if the point is a source pixel in the supplied flow raster.
      static void markHillSlopeWithLinkValue​(javax.media.jai.iterator.RandomIter flowIter, int flowNovalue, javax.media.jai.iterator.RandomIter attributeIter, javax.media.jai.iterator.WritableRandomIter markedIter, int cols, int rows, IHMProgressMonitor pm)
      Marks a map on the hillslope with the values on the channel of an attribute map.
      static double meanDoublematrixColumn​(double[][] matrix, int column)
      Return the mean of a column of a matrix.
      static List<org.locationtech.jts.geom.MultiLineString> net2ShapeGeometries​(javax.media.jai.iterator.WritableRandomIter flowIter, javax.media.jai.iterator.RandomIter netNumIter, int[] nstream, org.geotools.coverage.grid.GridGeometry2D gridGeometry, IHMProgressMonitor pm)
      Create the line geometries of channel network
      static org.geotools.data.simple.SimpleFeatureCollection net2ShapeOnly​(RenderedImage flowImage, WritableRaster netNumImage, org.geotools.coverage.grid.GridGeometry2D gridGeometry, List<Integer> nstream, IHMProgressMonitor pm)
      It create the shape-file of channel network
      static WritableRaster netNumbering​(org.geotools.coverage.grid.GridCoverage2D flowGC, org.geotools.coverage.grid.GridCoverage2D netGC, org.geotools.coverage.grid.GridCoverage2D tcaGC, List<org.locationtech.jts.geom.Geometry> points, List<NetLink> netLinksList, IHMProgressMonitor pm)
      Calculate the map of netnumbering.
      static void outletdistance​(javax.media.jai.iterator.RandomIter flowIter, javax.media.jai.iterator.WritableRandomIter distanceToOutIter, RegionMap region, IHMProgressMonitor pm)
      Calculates the distance of every pixel of the basin from the outlet (in map units), calculated along the drainage directions
      static double scalarProduct​(double[] a, double[] b)
      Compute the dot product.
      static boolean sourcesNet​(javax.media.jai.iterator.RandomIter flowIterator, int[] colRow, int num, javax.media.jai.iterator.RandomIter netNum)
      Controls if the considered point is a source in the network map.
      static double split2realvectors​(double[] U, double[] T, SplitVectors theSplit, int binNum, int num_max, IHMProgressMonitor pm)
      TODO Daniele doc
      static double sumDoublematrixColumns​(int coolIndex, double[][] matrixToSum, double[][] resultMatrix, int firstRowIndex, int lastRowIndex, IHMProgressMonitor pm)
      Sum columns.
      static WritableRaster sumDownstream​(javax.media.jai.iterator.RandomIter flowIter, javax.media.jai.iterator.RandomIter mapToSumIter, int width, int height, Double upperThreshold, Double lowerThreshold, IHMProgressMonitor pm)
      Calculates the sum of the values of a specified quantity from every point to the outlet.
      static void topologicalOutletdistance​(javax.media.jai.iterator.RandomIter flowIter, javax.media.jai.iterator.RandomIter pitIter, javax.media.jai.iterator.WritableRandomIter distanceToOutIter, RegionMap region, IHMProgressMonitor pm)
      Calculates the distance of every pixel of the basin from the outlet (in meter), calculated along the drainage directions
      static double varianceDoublematrixColumn​(double[][] matrix, int column, double mean)
      Return the variance of a column of a matrix.
      static double[] vectorizeDoubleMatrix​(RenderedImage input)
      Takes a input raster and vectorializes it.
      static boolean verifyDoubleStation​(double[] xStation, double[] yStation, double[] zStation, double[] hStation, double xTmp, double yTmp, double zTmp, double hTmp, int i, boolean doMean)
      Deprecated.
      This is no longer used.
      static double widthInterpolate​(double[][] data, double x, int nx, int ny)
      Linear interpolation between two values
    • Field Detail

      • DIR

        public static int[][] DIR
        Deprecated.
        this should not be used. Use the FlowNode concept instead.
      • DEFAULTPIXELANCHOR

        public static org.opengis.referencing.datum.PixelInCell DEFAULTPIXELANCHOR
    • Constructor Detail

      • ModelsEngine

        public ModelsEngine()
    • Method Detail

      • go_downstream

        public static boolean go_downstream​(int[] colRow,
                                            double flowdirection)
        Moves one pixel downstream.
        Parameters:
        colRow - the array containing the column and row of the current pixel. It will be modified here to represent the next downstream pixel.
        flowdirection - the current flowdirection number.
        Returns:
        true if everything went well.
      • go_upstream_a

        public static void go_upstream_a​(int[] p,
                                         javax.media.jai.iterator.RandomIter flowRandomIter,
                                         javax.media.jai.iterator.RandomIter tcaRandomIter,
                                         javax.media.jai.iterator.RandomIter lRandomIter,
                                         int[] param)
        Moves one pixel upstream.
        Parameters:
        p -
        flowRandomIter -
        tcaRandomIter -
        lRandomIter -
        param -
      • goUpStreamOnNetFixed

        public static void goUpStreamOnNetFixed​(int[] colRow,
                                                javax.media.jai.iterator.RandomIter flowIterator,
                                                javax.media.jai.iterator.RandomIter netnumIterator,
                                                int[] param)
        Moves one pixel upstream following the supplied network. TODO Daniele doc
        Parameters:
        colRow -
        flowIterator -
        netnumIterator -
        param -
      • net2ShapeOnly

        public static org.geotools.data.simple.SimpleFeatureCollection net2ShapeOnly​(RenderedImage flowImage,
                                                                                     WritableRaster netNumImage,
                                                                                     org.geotools.coverage.grid.GridGeometry2D gridGeometry,
                                                                                     List<Integer> nstream,
                                                                                     IHMProgressMonitor pm)
                                                                              throws IOException,
                                                                                     org.opengis.referencing.operation.TransformException
        It create the shape-file of channel network
        Parameters:
        flowImage - the map of flow direction.
        netNumImage - the map of netnumbering.
        gridGeometry - the GridGeometry2D of the flow coverage.
        nstream -
        pm - the progress monitor.
        Returns:
        the extracted features.
        Throws:
        IOException
        org.opengis.referencing.operation.TransformException
      • net2ShapeGeometries

        public static List<org.locationtech.jts.geom.MultiLineString> net2ShapeGeometries​(javax.media.jai.iterator.WritableRandomIter flowIter,
                                                                                          javax.media.jai.iterator.RandomIter netNumIter,
                                                                                          int[] nstream,
                                                                                          org.geotools.coverage.grid.GridGeometry2D gridGeometry,
                                                                                          IHMProgressMonitor pm)
                                                                                   throws IOException,
                                                                                          org.opengis.referencing.operation.TransformException
        Create the line geometries of channel network
        Parameters:
        flowIter - the flow map.
        netNumIter - the netnumbering map.
        nstream -
        pm - the progress monitor.
        gridGeometry - the gridgeometry.
        Returns:
        the geometries of the network.
        Throws:
        IOException
        org.opengis.referencing.operation.TransformException
      • sourcesNet

        public static boolean sourcesNet​(javax.media.jai.iterator.RandomIter flowIterator,
                                         int[] colRow,
                                         int num,
                                         javax.media.jai.iterator.RandomIter netNum)
        Controls if the considered point is a source in the network map.
        Parameters:
        flowIterator - iterator of flowdirections map
        colRow - the col and row of the point to check.
        num - channel number
        netNum - iterator of the netnumbering map.
        Returns:
      • vectorizeDoubleMatrix

        public static double[] vectorizeDoubleMatrix​(RenderedImage input)
        Takes a input raster and vectorializes it.
        Parameters:
        input -
        Returns:
      • split2realvectors

        public static double split2realvectors​(double[] U,
                                               double[] T,
                                               SplitVectors theSplit,
                                               int binNum,
                                               int num_max,
                                               IHMProgressMonitor pm)
        TODO Daniele doc
        Parameters:
        U -
        T -
        theSplit -
        binNum -
        num_max -
        Returns:
      • calculateNthMoment

        public static double calculateNthMoment​(double[] values,
                                                int validValues,
                                                double mean,
                                                double momentOrder,
                                                IHMProgressMonitor pm)
        Calculates the nth moment of a set of values.
        Parameters:
        values - the array of values.
        validValues - the number of valid values in the array.
        mean - the mean to use.
        momentOrder - the moment order to calculate.
        pm - the monitor.
        Returns:
        the nth moment value.
      • netNumbering

        public static WritableRaster netNumbering​(org.geotools.coverage.grid.GridCoverage2D flowGC,
                                                  org.geotools.coverage.grid.GridCoverage2D netGC,
                                                  org.geotools.coverage.grid.GridCoverage2D tcaGC,
                                                  List<org.locationtech.jts.geom.Geometry> points,
                                                  List<NetLink> netLinksList,
                                                  IHMProgressMonitor pm)
                                           throws Exception
        Calculate the map of netnumbering.
        Parameters:
        flowGC - the map of flowdirection.
        netGC - the map of network.
        tcaGC - the optional map of tca.
        tcaThreshold - the threshold on the tca.
        pointsFC - optional feature collection of points in which to split the net.
        pm - the monitor.
        Returns:
        the raster of netnumbering.
        Throws:
        Exception
      • extractSubbasins

        public static WritableRaster extractSubbasins​(javax.media.jai.iterator.WritableRandomIter flowIter,
                                                      int flowNovalue,
                                                      javax.media.jai.iterator.RandomIter netIter,
                                                      javax.media.jai.iterator.WritableRandomIter netNumberIter,
                                                      int rows,
                                                      int cols,
                                                      IHMProgressMonitor pm)
        Extract the subbasins of a raster map.
        Parameters:
        flowIter - the map of flowdirections.
        netIter - the network map.
        netNumberIter - the netnumber map.
        rows - rows of the region.
        cols - columns of the region.
        pm -
        Returns:
        the map of extracted subbasins.
      • markHillSlopeWithLinkValue

        public static void markHillSlopeWithLinkValue​(javax.media.jai.iterator.RandomIter flowIter,
                                                      int flowNovalue,
                                                      javax.media.jai.iterator.RandomIter attributeIter,
                                                      javax.media.jai.iterator.WritableRandomIter markedIter,
                                                      int cols,
                                                      int rows,
                                                      IHMProgressMonitor pm)
        Marks a map on the hillslope with the values on the channel of an attribute map.
        Parameters:
        flowIter - map of flow direction with the network cells all marked as FlowNode.NETVALUE. This is very important!
        attributeIter - map of attributes.
        markedIter - the map to be marked.
        cols - region cols.
        rows - region rows.
        pm - monitor.
      • isSourcePixel

        public static boolean isSourcePixel​(javax.media.jai.iterator.RandomIter flowIter,
                                            int col,
                                            int row)
        Verifies if the point is a source pixel in the supplied flow raster.
        Parameters:
        flowIter - the iterator of the flowdirections.
        col - the col of the point to check.
        row - the row of the point to check.
        Returns:
        true if the point identified by col and row is a source pixel.
      • getFlowDirection

        public static int getFlowDirection​(int i,
                                           int j)
        Returns the flow direction value for a given point as indexes (i, j) of the dir matrix.
        Parameters:
        i -
        j -
        Returns:
      • widthInterpolate

        public static double widthInterpolate​(double[][] data,
                                              double x,
                                              int nx,
                                              int ny)
        Linear interpolation between two values
        Parameters:
        data - - matrix of values to interpolate
        x - - value to interpolate
        nx - - column of data in which you find the x values
        ny - - column of data in which you find the y values
        Returns:
      • henderson

        public static double henderson​(double[][] data,
                                       int tp)
        Interpolates the width function in a given tp.
        Parameters:
        data -
        tp -
        Returns:
      • gamma

        public static double gamma​(double x)
        The Gamma function.
        Parameters:
        x -
        Returns:
        the calculated gamma function.
      • sumDownstream

        public static WritableRaster sumDownstream​(javax.media.jai.iterator.RandomIter flowIter,
                                                   javax.media.jai.iterator.RandomIter mapToSumIter,
                                                   int width,
                                                   int height,
                                                   Double upperThreshold,
                                                   Double lowerThreshold,
                                                   IHMProgressMonitor pm)
        Calculates the sum of the values of a specified quantity from every point to the outlet.

        During the calculation the drainage directions are followed.

        Parameters:
        flowIter - the map of flowdirections.
        mapToSumIter - the map for which to sum downstream.
        width - the width of the resulting map.
        height - the height of the resulting map.
        upperThreshold - the upper threshold, values above that are excluded.
        lowerThreshold - the lower threshold, values below that are excluded.
        pm - the monitor.
        Returns:
        The map of downstream summed values.
      • calcInverseSunVector

        public static double[] calcInverseSunVector​(double[] sunVector)
        Calculating the inverse of the sun vector.
        Parameters:
        sunVector -
        Returns:
      • calcNormalSunVector

        public static double[] calcNormalSunVector​(double[] sunVector)
        Calculating the normal to the sun vector.
        Parameters:
        sunVector -
        Returns:
      • scalarProduct

        public static double scalarProduct​(double[] a,
                                           double[] b)
        Compute the dot product.
        Parameters:
        a - is a vector.
        b - is a vector.
        Returns:
        the dot product of a and b.
      • calculateFactor

        public static WritableRaster calculateFactor​(int h,
                                                     int w,
                                                     double[] sunVector,
                                                     double[] inverseSunVector,
                                                     double[] normalSunVector,
                                                     WritableRaster demWR,
                                                     double dx)
        Evaluate the shadow map calling the shadow method.
        Parameters:
        h - the height of the raster.
        w - the width of the raster.
        sunVector -
        inverseSunVector -
        normalSunVector -
        demWR - the elevation map.
        dx - the resolution of the elevation map.
        Returns:
        the shadow map.
      • verifyDoubleStation

        public static boolean verifyDoubleStation​(double[] xStation,
                                                  double[] yStation,
                                                  double[] zStation,
                                                  double[] hStation,
                                                  double xTmp,
                                                  double yTmp,
                                                  double zTmp,
                                                  double hTmp,
                                                  int i,
                                                  boolean doMean)
                                           throws Exception
        Deprecated.
        This is no longer used.
        Verify if the current station (i) is already into the arrays.
        Parameters:
        xStation - the x coordinate of the stations
        yStation - the y coordinate of the stations
        zStation - the z coordinate of the stations
        hStation - the h value of the stations
        xTmp -
        yTmp -
        zTmp -
        hTmp -
        i - the current index
        doMean - if the h value of a double station have different value then do the mean.
        Returns:
        true if there is already this station.
        Throws:
        Exception
      • meanDoublematrixColumn

        public static double meanDoublematrixColumn​(double[][] matrix,
                                                    int column)
        Return the mean of a column of a matrix.
        Parameters:
        matrix - matrix of the value to calculate.
        column - index of the column to calculate the variance.
        Returns:
        mean.
      • varianceDoublematrixColumn

        public static double varianceDoublematrixColumn​(double[][] matrix,
                                                        int column,
                                                        double mean)
        Return the variance of a column of a matrix.
        Parameters:
        matrix - matrix of the value to calculate.
        column - index of the column to calculate the variance.
        mean - the mean value of the column.
        Returns:
        variance.
      • sumDoublematrixColumns

        public static double sumDoublematrixColumns​(int coolIndex,
                                                    double[][] matrixToSum,
                                                    double[][] resultMatrix,
                                                    int firstRowIndex,
                                                    int lastRowIndex,
                                                    IHMProgressMonitor pm)
        Sum columns.

        Store in a matrix (at index coulumn), the sum of the column of another matrix. It's necessary to specify the initial and final index of the coluns to sum.

        Parameters:
        coolIndex - index of the matrix2 where to put the result.
        matrixToSum - contains the value to sum.
        resultMatrix - where to put the result.
        firstRowIndex - initial index of the colum to sum.
        lastRowIndex - final index of the colum to sum.
        Returns:
        maximum value of the colum index of the matrix2.
      • topologicalOutletdistance

        public static void topologicalOutletdistance​(javax.media.jai.iterator.RandomIter flowIter,
                                                     javax.media.jai.iterator.RandomIter pitIter,
                                                     javax.media.jai.iterator.WritableRandomIter distanceToOutIter,
                                                     RegionMap region,
                                                     IHMProgressMonitor pm)
        Calculates the distance of every pixel of the basin from the outlet (in meter), calculated along the drainage directions
        Parameters:
        flowIter - the flow map.
        pitIter - the pit map (if available distance is calculated in 3d).
        distanceToOutIter - the resulting outlet distance map.
        region - the region parameters.
        pm - the monitor.
      • outletdistance

        public static void outletdistance​(javax.media.jai.iterator.RandomIter flowIter,
                                          javax.media.jai.iterator.WritableRandomIter distanceToOutIter,
                                          RegionMap region,
                                          IHMProgressMonitor pm)
        Calculates the distance of every pixel of the basin from the outlet (in map units), calculated along the drainage directions
        Parameters:
        flowIter - the flow map.
        distanceToOutIter - the resulting outlet distance map.
        region - the region parameters.
        pm - the monitor.
      • approximate2Multiple

        public static double approximate2Multiple​(double valueToApproximate,
                                                  double divisor)
        Approximate a value to a multiple of a divisor value.

        It evaluates a multiple of the divisor (which is the nearest number to valueToApproximate).

        Parameters:
        valueToApproximate - value to approximate
        divisor - the unit value, it's the divisor number.
        Returns:
        the largest value less than or equal to valueToApproximate and divisible to divisor.