001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032: package com.vividsolutions.jump.workbench.ui.snap;
033:
034: import com.vividsolutions.jts.geom.Envelope;
035: import com.vividsolutions.jts.geom.Geometry;
036: import com.vividsolutions.jts.geom.GeometryCollection;
037: import com.vividsolutions.jts.geom.GeometryFactory;
038: import com.vividsolutions.jts.geom.Polygon;
039: import com.vividsolutions.jts.index.strtree.STRtree;
040: import com.vividsolutions.jts.util.Assert;
041:
042: import com.vividsolutions.jump.feature.Feature;
043: import com.vividsolutions.jump.workbench.model.CategoryEvent;
044: import com.vividsolutions.jump.workbench.model.FeatureEvent;
045: import com.vividsolutions.jump.workbench.model.Layer;
046: import com.vividsolutions.jump.workbench.model.LayerEvent;
047: import com.vividsolutions.jump.workbench.model.LayerListener;
048: import com.vividsolutions.jump.workbench.ui.LayerViewPanel;
049: import com.vividsolutions.jump.workbench.ui.ViewportListener;
050:
051: import java.util.ArrayList;
052: import java.util.Iterator;
053:
054: public class VisiblePointsAndLinesCache {
055: private static final String PANEL_PROPERTY_KEY = "VISIBLE_POINTS_AND_LINES_CACHE";
056: private LayerListener layerListener = new LayerListener() {
057: public void layerChanged(LayerEvent e) {
058: invalidate();
059: }
060:
061: public void featuresChanged(FeatureEvent e) {
062: }
063:
064: public void categoryChanged(CategoryEvent e) {
065: }
066: };
067:
068: private ViewportListener viewportListener = new ViewportListener() {
069: public void zoomChanged(Envelope modelEnvelope) {
070: invalidate();
071: }
072: };
073:
074: private LayerViewPanel panel;
075: private GeometryFactory factory = new GeometryFactory();
076: private STRtree tree = null;
077:
078: private VisiblePointsAndLinesCache(LayerViewPanel panel) {
079: this .panel = panel;
080: panel.getViewport().addListener(viewportListener);
081: panel.getLayerManager().addLayerListener(layerListener);
082: }
083:
084: private void invalidate() {
085: tree = null;
086: }
087:
088: public STRtree getTree() {
089: if (tree == null) {
090: Envelope viewportEnvelope = panel.getViewport()
091: .getEnvelopeInModelCoordinates();
092: tree = new STRtree();
093: for (Iterator i = panel.getLayerManager().iterator(); i
094: .hasNext();) {
095: Layer layer = (Layer) i.next();
096: if (!layer.isVisible()) {
097: continue;
098: }
099: for (Iterator j = layer.getFeatureCollectionWrapper()
100: .query(viewportEnvelope).iterator(); j
101: .hasNext();) {
102: Feature feature = (Feature) j.next();
103: Geometry geometry = feature.getGeometry();
104: tree.insert(geometry.getEnvelopeInternal(),
105: toPointsAndLines(geometry));
106: }
107: }
108: }
109:
110: return tree;
111: }
112:
113: private Geometry toPointsAndLines(Geometry g) {
114: if (g.getDimension() <= 1) {
115: return g;
116: }
117: if (g instanceof GeometryCollection) {
118: GeometryCollection oldCollection = (GeometryCollection) g;
119: ArrayList newCollection = new ArrayList();
120: for (int i = 0; i < oldCollection.getNumGeometries(); i++) {
121: newCollection.add(toPointsAndLines(oldCollection
122: .getGeometryN(i)));
123: }
124:
125: return factory
126: .createGeometryCollection((Geometry[]) newCollection
127: .toArray(new Geometry[] {}));
128: }
129: Assert.isTrue(g instanceof Polygon);
130:
131: return ((Polygon) g).getBoundary();
132: }
133:
134: public static VisiblePointsAndLinesCache instance(
135: LayerViewPanel panel) {
136: if (panel.getBlackboard().get(PANEL_PROPERTY_KEY) == null) {
137: return (VisiblePointsAndLinesCache) panel.getBlackboard()
138: .get(PANEL_PROPERTY_KEY,
139: new VisiblePointsAndLinesCache(panel));
140: }
141:
142: return (VisiblePointsAndLinesCache) panel.getBlackboard().get(
143: PANEL_PROPERTY_KEY);
144: }
145: }
|