This is an old version of LandSerf. For the latest release please visit www.landserf.org

GISFrame unselected spatial object retrieval

The Problem

Some processing classes create spatial objects that are added to a GISFrame but are neither primary or secondary spatial objects. Therefore they cannot be retrieved using GISFrame's getRaster1(), getRaster2(), getVectorMap1() or getVectorMap2() methods. This is not a problem if a GUIFrame is used to hold spatial objects, since a user can select any stored object graphically. However it does not allow non-selected spatial objects to be retrieved programmatically.

The 2.1 API does not provide any other methods for retrieving spatial objects from a GISFrame. This has been addressed in the 2.2 API, but until that is available, the following workaround is suggested.

The Solution

By overriding the SimpleGISFrame class, we can 'capture' spatial objects as they are added, and expose them to the overriding class. One way of doing this is to create a publicly accessible collection of all raster and vector maps that get added to the GISFrame. All we then have to do is to override SimpleGISFrame's methods for adding new spatial objects, and in addition to the normal behaviour, make sure we add the new objects to the publicly accessible collection. The only other addition we have to make is to allow items to be removed from these collections when the removeSpatialObject() method is called.

import jwo.landserf.structure.*;    // For spatial object class.
import jwo.landserf.gui.*;          // For GISFrame.

import java.util.*;

// **********************************************************
/** Workaround class that exposes all raster and vector maps
  * that are added to a GISFrame. 
  * @author Jo Wood
  * @version 1.0, 15th November, 2004
  */
// **********************************************************

public class SimpleGISFrame2 extends SimpleGISFrame
{
    // ----------------------- Object variables ------------------------
    
    private Vector rasterMaps;      // Collection of all raster maps in GISFrame.
    private Vector vectorMaps;      // Collection of all vector maps in GISFrame.
    
    // ------------------------- Constructor --------------------------- 
    
    /** Creates a simple GISFrame that can hold a collection of spatial objects. 
      */
    public SimpleGISFrame2()
    {
        super();
        rasterMaps = new Vector();
        vectorMaps = new Vector();
    }
    
    // -------------------------- New methods --------------------------
    
    /** Returns a collection of all the stored raster maps associated 
      * with this GISFrame.
      * @return Collection of raster maps associated with this GISFrame.
      */
    public Vector getRasterMaps()
    {
        return rasterMaps;
    }
    
    /** Returns a collection of all the stored vector maps associated 
      * with this GISFrame.
      * @return Collection of vector maps associated with this GISFrame.
      */
    public Vector getVectorMaps()
    {
        return vectorMaps;
    }
      
    // ---------------------- Overridden methods -----------------------
    
    /** Adds a new raster to the collection stored in the GIS frame.
      * @param newRast new Raster to add.
      * @param selection Selection type (PRIMARY or SECONDARY).
      */
    public void addRaster(RasterMap newRast, int selection)
    {
        super.addRaster(newRast,selection);       
        rasterMaps.add(newRast);    
    }
    
    /** Adds a new vector map to the collection stored in the GIS frame.
      * @param newVect new vector map to add.
      * @param selection Selection type (PRIMARY or SECONDARY.
      */
    public void addVectorMap(VectorMap newVect, int selection)
    {
        super.addVectorMap(newVect,selection);       
        vectorMaps.add(newVect);     
    }
    
    /** Removes the given spatial object from those stored by this GISFrame
      * @param spObj Spatial object to remove.
      */
    public void removeSpatialObject(SpatialObject spObj)
    {
        if (spObj instanceof RasterMap)
        {
            rasterMaps.remove(spObj);
            
            if (getRaster1() == spObj)
                setRaster1(null);
            else
                if (getRaster2() == spObj)
                    setRaster2(null);  
        }
        else
        {
            vectorMaps.remove(spObj);
            
            if (getVectorMap1() == spObj)
                setVectorMap1(null);
            else
                if (getVectorMap2() == spObj)
                    setVectorMap2(null);  
        }   
    }
}

This class can be used instead of SimpleGISFrame whenever you need to supply a GISFrame to a spatial processing class.