⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rastermap.java

📁 Java source code for the Ant Colony Optimization Problem.
💻 JAVA
字号:
package jwo.jpss.spatial;       // Part of the spatial modelling package.

import jwo.jpss.spatial.gui.*;  // For GUI representation.
import javax.swing.JPanel;

//  **************************************************************
/** Models a GIS raster map. Stores the raster resolution and
  * dimensions as well as the raster values.
  * @author Jo Wood
  * @version 1.3, 20th October, 2001
  */
//  **************************************************************

public class RasterMap extends SpatialObject
{   
    // ----------------- Object variables ------------------
    
    private float[][] raster;           // Raster values.
    private float     resolution;       // Raster resolution.
    private int       numRows,numCols;  // Dimensions of raster.
    private float     max,min;          // Max and min attribute values.
    private boolean   recalcRange;      // Determines if range should be calculated.
    
    // -------------------- Constructors --------------------

    /** Creates a raster map with the given dimensions.
      * @param numRows Number of rows in raster.
      * @param numCols Number of columns in raster.
      * @param res Size of 1 side of a raster cell.
      * @param fp Location of bottom left corner of raster.
      */
    public RasterMap(int numRows, int numCols, float res, Footprint fp)
    {
        this(numRows,numCols,res,fp, new Header("Simple raster"));
    }
    /** Creates a raster map with the given dimensions.
      * @param numRows Number of rows in raster.
      * @param numCols Number of columns in raster.
      * @param res Size of 1 side of a raster cell.
      * @param fp Location of bottom left corner of raster.
      * @param header Header associated with this raster.
      */
    public RasterMap(int numRows, int numCols, 
                     float res, Footprint fp, Header header)
    {    
        // Store incoming messages as object variables.
        this.numRows    = numRows;
        this.numCols    = numCols;
        this.resolution = res;
        setBounds(new Footprint(fp.getXOrigin(),fp.getYOrigin(),
                                resolution*numCols,resolution*numRows));

        setHeader(header);
               
        // Declare and initialise new raster array.
        raster = new float[numRows][numCols];

        for (int row=0; row<numRows; row++)
            for (int col=0; col<numCols; col++)
                raster[row][col] = 0.0f;

        max = 0;
        min = 0;
        recalcRange = false;
    } 

    // ---------------------- Methods ----------------------

    /** Creates a panel with graphical representation of this raster.
      * @return Graphical representation of this raster map.
      */
    public SpatialPanel createPanel()
    {
        return new RasterPanel(this);
    }  

    // ------------------ Accessor Methods -----------------

    /** Reports the number of rows in the raster.
      * @return Number of raster rows.
      */
    public int getNumRows()
    {
        return numRows;
    }

    /** Reports the number of columns in the raster.
      * @return Number of raster columns.
      */
    public int getNumCols()
    {
        return numCols;
    }

    /** Reports the resolution of the raster.
      * @return Resolution of the raster in georeferenced units.
      */
    public float getResolution()
    {
        return resolution;
    }

    /** Reports the maximimum attribute value of the raster.
      * @return Maximum raster value.
      */
    public float getMaxAttribute()
    {
        if (recalcRange)
        {
            for (int row=0; row<numRows; row++)
                for (int col=0; col<numCols; col++)
                {
                    if (raster[row][col] > max)
                        max = raster[row][col];
                    if (raster[row][col] < min)
                        min = raster[row][col];
                }
            recalcRange = false;
        }
        return max;
    }

    /** Reports the minimum attribute value of the raster.
      * @return Minimum raster value.
      */
    public float getMinAttribute()
    {
        if (recalcRange)
        {
            for (int row=0; row<numRows; row++)
                for (int col=0; col<numCols; col++)
                {
                    if (raster[row][col] > max)
                        max = raster[row][col];
                    if (raster[row][col] < min)
                        min = raster[row][col];
                }
            recalcRange = false;
        }
        return min;
    }

    /** Reports the attribute of the raster at the given location.
      * @param fp Location to query.
      * @return Attribute at given location or NO_VALUE if none defined.
      */
    public float getAttribute(Footprint fp)
    {        
        int col = Math.round((fp.getXOrigin()-getBounds().getXOrigin())/resolution);
        int row = Math.round((fp.getYOrigin()-getBounds().getYOrigin())/resolution);
        return getAttribute(row,col);
    }
    
    /** Reports the attribute of the raster at the given array coordinates.
      * @param row Array row coordinate of cell to query.
      * @param col Array column coordinate of cell to query.
      * @return attribute of the raster at query location or NO_VALUE if
      *         out of bounds.
      */
    public float getAttribute(int row, int col)
    {
        if ((row <0) || (row >=numRows) || (col <0) || (col>=numCols))
            return OUT_OF_BOUNDS;
        else
            return raster[row][col];
    }

    // ------------------- Mutator Methods -----------------

    /** Sets a new resolution for the raster.
      * @param resolution New resolution for raster in georeferenced units.
      */
    public void setResolution(float resolution)
    {
        this.resolution = resolution;
    }

    /** Sets the attribute of the raster at the given array coordinates.
      * @param row Array row coordinate of cell to set.
      * @param col Array column coordinate of cell to set.
      * @return attribute of the raster at query location.
      */
    public void setAttribute(int row, int col, float attribute)
    {
        if ((row <0) || (row >=numRows) || (col <0) || (col>=numCols))
            return;  // Coordinates out of bounds.

        raster[row][col] = attribute;
        recalcRange = true;
    }

    /** Sets the attribute of the raster at the given georeferenced
      * coordinates.
      * @param fp Location of attribute to set.
      * @param attribute New attribute of the raster cell.
      */
    public void setAttribute(Footprint fp, float attribute)
    {
        int col = Math.round((fp.getXOrigin()-getBounds().getXOrigin())/resolution);
        int row = Math.round((fp.getYOrigin()-getBounds().getYOrigin())/resolution);
        setAttribute(row,col,attribute);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -