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

📄 gisvector.java

📁 Java source code for the Ant Colony Optimization Problem.
💻 JAVA
字号:
package jwo.jpss.spatial;   // Part of the spatial modelling package.
import java.awt.geom.*;     // For GeneralPath.
import java.io.*;           // For serialization.

//  **************************************************************
/** Models a GIS vector object.
  * @author Jo Wood
  * @version 2.4, 21st October, 2001
  */
//  **************************************************************

public class GISVector extends SpatialObject
{   
    // -------------------- Object variables ---------------------

    private transient GeneralPath coords;   // Vector geometry array.
    private Footprint serializedCoords[];
    private int type;          // Type of vector object.
    private int numCoords;     // Number of coordinates in vector.
    private float attribute;   // Attribute associated with vector.
    
    // ---------------------- Constructor ------------------------
     
    /** Creates an empty GIS vector object.
      */
    public GISVector()
    {
        type = POINT;
        attribute = 0;
        numCoords = 0;
        coords = new GeneralPath();
        setBounds(new Footprint(0,0,0,0));
    }

    /** Creates the GIS vector object with the given x and y coords.
      * @param xCoords Array holding x-coordinates of object.
      * @param yCoords Array holding y-coordinates of object.
      * @param type Type of vector object (POINT, LINE, AREA, VOLUME).
      * @param attrib Attribute associated with this GIS vector.
      */
    public GISVector(float[] xCoords, float yCoords[], int type, float attrib)
    {	           
        // Initialise vector information and check integrity.
        numCoords = xCoords.length;
        this.type = type;
        this.attribute = attrib;
        coords = new GeneralPath();
        float north = -Float.MAX_VALUE;
        float south = Float.MAX_VALUE;
        float east  = -Float.MAX_VALUE;
        float west  = Float.MAX_VALUE;

        if (yCoords.length != numCoords)
            System.err.println("Warning: Number of x and y coords do not match");

        if ((type==POINT) && (numCoords != 1))
            System.err.println("Warning: Point does not contain 1 coordinate pair");
        
        if ((type==LINE) && (numCoords < 2))
            System.err.println("Warning: Line contains less than 2 coordinates");

        if ((type==AREA) && (numCoords < 3))
            System.err.println("Warning: Area contains less than 3 coordinates");

        if ((type==VOLUME) && (numCoords < 4))
            System.err.println("Warning: Volume contains less than 4 coordinates");

        // Store the coordinates and calculate bounds of object.
        coords.moveTo(xCoords[0], yCoords[0]);

        for (int i=0; i<numCoords; i++)
        {
            coords.lineTo(xCoords[i],yCoords[i]);
            
            if (xCoords[i] < west)
                west = xCoords[i];
            if (xCoords[i] > east)
                east = xCoords[i];
            if (yCoords[i] < south)
                south = yCoords[i];
            if (yCoords[i] > north)
                north = yCoords[i];
        }

        if (type == AREA)
            coords.closePath();

        // Store minimum enclosing rectangle.
        setBounds(new Footprint(west,south,east-west,north-south));
    } 

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

    /** Adds the given footprint to the vector's coordinates and
      * updates the bounding area.
      * @param footprint Coordinates to add.
      */
    public void addCoords(Footprint fp)
    {
        coords.lineTo(fp.getXOrigin(), fp.getYOrigin());
        numCoords++;
        setBounds(getUnionMER(fp));
    }

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

    /** Returns a drawable set of coordinates representing vector object.
      * @return coordinates of the vector.
      */
    public GeneralPath getCoords()
    {
        return coords;
    }

    /** Reports the type of vector.
      * @return Type of vector (POINT, LINE, AREA or VOLUME).
      */
    public int getType()
    {
        return type;
    }

    /** Reports the attribute associated with the GIS vector.
      * @return Attribute associated with the GIS vector.
      */
    public float getAttribute()
    {
        return attribute;
    }

    /** Reports the attribute at the given point location.
      * @param fp Location to query.
      * @return Attribute associated with the GIS vector if given
      * location intersects with object, otherwise OUT_OF_BOUNDS.
      */
    public float getAttribute(Footprint fp)
    {
        if (coords.contains(fp.getXOrigin(), fp.getYOrigin()))
            return attribute;
        else
            return OUT_OF_BOUNDS;
    }

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

    /** Sets the type of vector.
      * @param Type of vector (POINT, LINE, AREA or VOLUME).
      */
    public void setType(int type)
    {
        this.type = type;
    }

    /** Sets the attribute of the GIS vector.
      * @param Attribute of the GIS vector.
      */
    public void setType(float attrib)
    {
        this.attribute = attrib;
    }

    // ---------------------- Private Methods -------------------------

    /** Serializes the GISVector. Converts the general path into a 
      * coordinate array.
      * @param s Stream to place serialization.
      */
    private void writeObject(ObjectOutputStream s) throws IOException 
    {
        // Save general path as an array of footprints.
        PathIterator i = coords.getPathIterator(new AffineTransform());
        float segment[] = new float[6];
        serializedCoords = new Footprint[numCoords];

        for (int seg=0; seg<numCoords; seg++)
        {
            i.currentSegment(segment);
            serializedCoords[seg] = new Footprint(segment[0],segment[1]);          
            i.next();
        }

        s.defaultWriteObject();
        serializedCoords = null;
    }

    /** Deserializes the GISVector. Converts the coordinate array into
      * a general path.
      * @param s Stream from which to read serialization.
      */
    private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException
    {
        s.defaultReadObject();

        // Reconstruct general path from coordinate array.
        coords = new GeneralPath();
        if (numCoords == 0)
            return;

        coords.moveTo(serializedCoords[0].getXOrigin(), 
                      serializedCoords[0].getYOrigin());

        for (int i=0; i<numCoords; i++)
        {
            coords.lineTo(serializedCoords[i].getXOrigin(), 
                          serializedCoords[i].getYOrigin());
        }

        if (type == AREA)
            coords.closePath();

        serializedCoords = null;
    }
}

⌨️ 快捷键说明

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