001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package org.geotools.renderer.shape.shapehandler.simple;
018:
019: import java.awt.Rectangle;
020: import java.nio.ByteBuffer;
021: import java.util.logging.Logger;
022:
023: import org.geotools.data.shapefile.shp.ShapeHandler;
024: import org.geotools.data.shapefile.shp.ShapeType;
025: import org.geotools.renderer.shape.GeometryHandlerUtilities;
026: import org.geotools.renderer.shape.ScreenMap;
027: import org.geotools.renderer.shape.ShapefileRenderer;
028: import org.geotools.renderer.shape.SimpleGeometry;
029: import org.opengis.referencing.operation.MathTransform;
030: import org.opengis.referencing.operation.TransformException;
031:
032: import com.vividsolutions.jts.geom.Envelope;
033:
034: /**
035: * A ShapeHandler that reads PointHandler objects from a file. It returns a
036: * SimpleGeometry and decimates all points that map to the same screen location.
037: *
038: * @author jeichar
039: * @since 2.1.x
040: * @source $URL:
041: * http://svn.geotools.org/geotools/branches/2.2.x/ext/shaperenderer/src/org/geotools/renderer/shape/PointHandler.java $
042: */
043: public class PointHandler implements ShapeHandler {
044: private ShapeType type;
045:
046: private Envelope bbox;
047:
048: private MathTransform mt;
049:
050: private ScreenMap screenMap;
051:
052: Logger LOGGER = ShapefileRenderer.LOGGER;
053:
054: int skipped = 0;
055:
056: /**
057: * Create new instance
058: *
059: * @param type
060: * the type of shape.
061: * @param env
062: * the area that is visible. If shape is not in area then skip.
063: * @param mt
064: * the transform to go from data to the envelope (and that should
065: * be used to transform the shape coords)
066: * @param hasOpacity
067: */
068: public PointHandler(ShapeType type, Envelope env,
069: Rectangle screenSize, MathTransform mt, boolean hasOpacity)
070: throws TransformException {
071: if (mt == null) {
072: throw new NullPointerException();
073: }
074: this .type = type;
075: this .bbox = env;
076: this .mt = mt;
077: screenMap = GeometryHandlerUtilities.calculateScreenSize(
078: screenSize, hasOpacity);
079: }
080:
081: /**
082: * @see org.geotools.data.shapefile.shp.ShapeHandler#getShapeType()
083: */
084: public ShapeType getShapeType() {
085: return type;
086: }
087:
088: /*
089: * (non-Javadoc)
090: *
091: * @see org.geotools.data.shapefile.shp.ShapeHandler#read(java.nio.ByteBuffer,
092: * org.geotools.data.shapefile.shp.ShapeType)
093: */
094: public Object read(ByteBuffer buffer, ShapeType type) {
095: if (type == ShapeType.NULL) {
096: LOGGER.finest("shape type is NULL");
097: return null;
098: }
099:
100: double[][] coords = new double[1][];
101: coords[0] = new double[] { buffer.getDouble(),
102: buffer.getDouble() };
103: Envelope geomBBox = new Envelope(coords[0][0], coords[0][0],
104: coords[0][1], coords[0][1]);
105:
106: if (!bbox.intersects(geomBBox)) {
107: LOGGER.finest("Point doesn't intersect with envelope");
108: return null;
109: }
110:
111: double[][] transformed = new double[1][];
112: transformed[0] = new double[2];
113:
114: if (!mt.isIdentity()) {
115: try {
116: mt.transform(coords[0], 0, transformed[0], 0, 1);
117: } catch (Exception e) {
118: ShapefileRenderer.LOGGER
119: .severe("could not transform coordinates"
120: + e.getLocalizedMessage());
121: }
122: } else {
123: transformed = coords;
124: }
125:
126: if (screenMap.get((int) (transformed[0][0]),
127: (int) transformed[0][1])) {
128: LOGGER.finest("Point already rendered" + transformed[0][0]
129: + " " + transformed[0][1]);
130: return null;
131: }
132: screenMap.set((int) (transformed[0][0]),
133: (int) (transformed[0][1]), true);
134: return createGeometry(type, geomBBox, transformed);
135: }
136:
137: protected Object createGeometry(ShapeType type, Envelope geomBBox,
138: double[][] transformed) {
139: return new SimpleGeometry(type, transformed, geomBBox);
140: }
141:
142: /**
143: * @see org.geotools.data.shapefile.shp.ShapeHandler#write(java.nio.ByteBuffer,
144: * java.lang.Object)
145: */
146: public void write(ByteBuffer buffer, Object geometry) {
147: // This handler doesnt write
148: throw new UnsupportedOperationException(
149: "This handler is only for reading");
150: }
151:
152: /**
153: * @see org.geotools.data.shapefile.shp.ShapeHandler#getLength(java.lang.Object)
154: */
155: public int getLength(Object geometry) {
156: // TODO Auto-generated method stub
157: return 0;
158: }
159:
160: }
|