I've been trying to find a quick way to create Polygons from selected
LineStrings in one of my OpenJUMP GIS projects. I've been using the
suggestion to set up a temporary Layer were I paste a copy of the
LineStrings I've selected, then I use the polygonize tool to actually
create the Polygons.
I thought I would take a "quick" look at OpenJUMP's selection code to
see if I could determine how to implement a CursorTool similar to the
select tool that allows a little more control over selection. This
includes the ability to have "sticky" selections, the ability to
remove selected items from a sticky selection, and the ability to
automatically copy selected items to a new or designated layer with a
single click.
I think I've got a good handle on how the selection code works, excpet
for one problem that I can't figure out in the SelectTool class. The
class defines and uses an instance of the AbstractSelection class
named "selection". It is declared as a protexted variable in the
class, but I can't for the life of me figure out where the variable is
initialized. It isn't in the constructor of the SelectTool class, it
isn't in the variable declaration, and I couldn't find an
initialization reading through the file, searching for instances of
the "new" keyword in the file, or using the "references" tool in
Eclipse.
Is there some black magic going on in the code that I don't know
about. If the "selection" variable isn't initialized how come the
calls to its methods in the class don't throw a null pointer
exception? I know I am missing something, but I'm not sure what I am
missing.
I appreciate any help.
The .java file for the SelectTool class is attached.
The Sunburned Surveyor
/*
* The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
* for visualizing and manipulating spatial features with geometry and
attributes.
*
* Copyright (C) 2003 Vivid Solutions
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* For more information, contact:
*
* Vivid Solutions
* Suite #1A
* 2328 Government Street
* Victoria BC V8T 5G5
* Canada
*
* (250)385-6040
* www.vividsolutions.com
*/
package com.vividsolutions.jump.workbench.ui.cursortool;
import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.geom.NoninvertibleTransformException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import javax.swing.Icon;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jump.feature.Feature;
import com.vividsolutions.jump.geom.EnvelopeUtil;
import com.vividsolutions.jump.util.CollectionMap;
import com.vividsolutions.jump.workbench.model.FenceLayerFinder;
import com.vividsolutions.jump.workbench.model.Layer;
import com.vividsolutions.jump.workbench.ui.AbstractSelection;
import com.vividsolutions.jump.workbench.ui.LayerViewPanel;
public abstract class SelectTool extends DragTool {
public Cursor getCursor() {
return Cursor.getDefaultCursor();
}
public void mouseClicked(MouseEvent e) {
try {
super.mouseClicked(e);
setViewSource(e.getPoint());
setViewDestination(e.getPoint());
fireGestureFinished();
} catch (Throwable t) {
getPanel().getContext().handleThrowable(t);
}
}
protected void gestureFinished() throws NoninvertibleTransformException {
reportNothingToUndoYet();
if (!wasShiftPressed()) {
getPanel().getSelectionManager().clear();
}
Map layerToFeaturesInFenceMap =
getPanel().visibleLayerToFeaturesInFenceMap(
EnvelopeUtil.toGeometry(getBoxInModelCoordinates()));
Collection layers = layerToFeaturesInFenceMap.keySet();
if (selectedLayersOnly()) {
layers.retainAll(Arrays.asList(getTaskFrame().getLayerNamePanel().getSelectedLayers()));
}
for (Iterator i = layers.iterator(); i.hasNext();) {
Layer layer = (Layer) i.next();
if (layer.getName().equals(FenceLayerFinder.LAYER_NAME)) {
continue;
}
//Disable panel updates -- we'll manually repaint the selection and
//fire the selection-changed event. [Jon Aquino]
boolean originalPanelUpdatesEnabled =
getPanel().getSelectionManager().arePanelUpdatesEnabled();
getPanel().getSelectionManager().setPanelUpdatesEnabled(false);
try {
CollectionMap featureToItemsToSelectMap =
featureToItemsInFenceMap(
(Collection) layerToFeaturesInFenceMap.get(layer),
layer,
false);
CollectionMap featureToItemsToUnselectMap =
featureToItemsInFenceMap(
(Collection) layerToFeaturesInFenceMap.get(layer),
layer,
true);
selection.selectItems(layer, featureToItemsToSelectMap);
if (wasShiftPressed()) {
selection.unselectItems(layer, featureToItemsToUnselectMap);
}
} finally {
getPanel().getSelectionManager().setPanelUpdatesEnabled(
originalPanelUpdatesEnabled);
}
}
getPanel().getSelectionManager().updatePanel();
}
protected boolean selectedLayersOnly() {
return wasControlPressed();
}
private String rendererID;
protected SelectTool(String rendererID) {
this.rendererID = rendererID;
}
protected AbstractSelection selection;
/**
* @param selected whether to return selected items or deselected items
*/
private CollectionMap featureToItemsInFenceMap(
Collection features,
Layer layer,
boolean selected)
throws NoninvertibleTransformException {
CollectionMap featureToSelectedItemsMap =
selection.getFeatureToSelectedItemCollectionMap(layer);
CollectionMap featureToItemsInFenceMap = new CollectionMap();
for (Iterator i = features.iterator(); i.hasNext();) {
Feature feature = (Feature) i.next();
Collection selectedItems =
featureToSelectedItemsMap.getItems(feature);
Collection itemsToReturn = itemsInFence(feature);
if (selected) {
itemsToReturn.retainAll(selectedItems);
} else {
itemsToReturn.removeAll(selectedItems);
}
featureToItemsInFenceMap.put(feature, itemsToReturn);
}
return featureToItemsInFenceMap;
}
private Collection itemsInFence(Feature feature) throws
NoninvertibleTransformException {
ArrayList itemsInFence = new ArrayList();
Geometry fence = EnvelopeUtil.toGeometry(getBoxInModelCoordinates());
for (Iterator i = selection.items(feature.getGeometry()).iterator();
i.hasNext();) {
Geometry selectedItem = (Geometry) i.next();
if (LayerViewPanel.intersects(selectedItem, fence)) {
itemsInFence.add(selectedItem);
}
}
return itemsInFence;
}
public Icon getIcon() {
return null;
}
}
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Jump-pilot-devel mailing list
Jump-pilot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel