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.jts;
018:
019: import org.geotools.data.shapefile.shp.ShapeType;
020: import org.geotools.geometry.jts.LiteCoordinateSequence;
021: import org.geotools.geometry.jts.LiteCoordinateSequenceFactory;
022: import org.opengis.referencing.operation.MathTransform;
023: import org.opengis.referencing.operation.TransformException;
024:
025: import com.vividsolutions.jts.geom.Coordinate;
026: import com.vividsolutions.jts.geom.CoordinateSequence;
027: import com.vividsolutions.jts.geom.Envelope;
028: import com.vividsolutions.jts.geom.GeometryFactory;
029: import com.vividsolutions.jts.geom.LinearRing;
030: import com.vividsolutions.jts.geom.Polygon;
031:
032: /**
033: * A ShapeHandler that reads PointHandler objects from a file. It returns a SimpleGeometry and decimates all points that
034: * map to the same screen location.
035: *
036: * @author jeichar
037: * @since 2.1.x
038: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/shapefile-renderer/src/main/java/org/geotools/renderer/shape/shapehandler/jts/PolygonHandler.java $
039: */
040: public class PolygonHandler extends
041: org.geotools.renderer.shape.shapehandler.simple.PolygonHandler {
042: private static final LinearRing[] HOLES = new LinearRing[0];
043: private static final GeometryFactory factory = new GeometryFactory(
044: new LiteCoordinateSequenceFactory());
045:
046: public PolygonHandler(ShapeType type, Envelope env,
047: MathTransform mt, boolean hasOpacity)
048: throws TransformException {
049: super (type, env, mt, hasOpacity);
050: }
051:
052: protected Object createGeometry(ShapeType type, Envelope geomBBox,
053: double[][] transformed) {
054: Polygon[] poly = new Polygon[transformed.length];
055: for (int i = 0; i < transformed.length; i++) {
056: LinearRing ring = factory
057: .createLinearRing(new LiteCoordinateSequence(
058: transformed[i]));
059: poly[i] = factory.createPolygon(ring, HOLES);
060: }
061:
062: return factory.createMultiPolygon(poly);
063: }
064:
065: private static class PolygonCoodinateSequence implements
066: CoordinateSequence {
067:
068: final double[] coords;
069: volatile Coordinate[] array;
070:
071: public PolygonCoodinateSequence(double[] ds) {
072: this .coords = ds;
073: }
074:
075: public Envelope expandEnvelope(Envelope env) {
076: for (int i = 0; i < coords.length; i += 2) {
077: env.expandToInclude(coords[i], coords[i + 1]);
078: }
079: return env;
080: }
081:
082: public Coordinate getCoordinate(int i) {
083: int offset = i * 2;
084: return new Coordinate(coords[offset], coords[offset + 1]);
085: }
086:
087: public void getCoordinate(int index, Coordinate coord) {
088: int offset = index * 2;
089: coord.x = coords[offset];
090: coord.y = coords[offset + 1];
091: }
092:
093: public Coordinate getCoordinateCopy(int i) {
094: return getCoordinate(i);
095: }
096:
097: public int getDimension() {
098: return 2;
099: }
100:
101: public double getOrdinate(int index, int ordinateIndex) {
102: return coords[index * 2 + ordinateIndex];
103: }
104:
105: public double getX(int index) {
106: return coords[index * 2];
107: }
108:
109: public double getY(int index) {
110: return coords[index * 2 + 1];
111: }
112:
113: public void setOrdinate(int index, int ordinateIndex,
114: double value) {
115: throw new UnsupportedOperationException();
116: }
117:
118: public int size() {
119: return coords.length / 2;
120: }
121:
122: public Coordinate[] toCoordinateArray() {
123: if (array == null) {
124: synchronized (this ) {
125: if (array == null) {
126: array = new Coordinate[size()];
127: for (int i = 0; i < array.length * 2; i += 2) {
128: array[i / 2] = new Coordinate(coords[i],
129: coords[i + 1]);
130: }
131: }
132: }
133: }
134: return array;
135: }
136:
137: public Object clone() {
138: PolygonCoodinateSequence other;
139: try {
140: other = (PolygonCoodinateSequence) super .clone();
141: return other;
142: } catch (CloneNotSupportedException e) {
143: return null;
144: }
145: }
146:
147: }
148: }
|