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.data.shapefile.shp;
018:
019: import java.awt.Rectangle;
020: import java.awt.geom.AffineTransform;
021: import java.net.URL;
022:
023: import junit.framework.TestCase;
024:
025: import org.geotools.data.Query;
026: import org.geotools.data.shapefile.Lock;
027: import org.geotools.data.shapefile.ShapefileDataStore;
028: import org.geotools.data.shapefile.ShapefileDataStoreFactory;
029: import org.geotools.data.shapefile.ShapefileRendererUtil;
030: import org.geotools.map.DefaultMapContext;
031: import org.geotools.map.MapContext;
032: import org.geotools.referencing.CRS;
033: import org.geotools.referencing.ReferencingFactoryFinder;
034: import org.geotools.referencing.crs.DefaultGeographicCRS;
035: import org.geotools.referencing.operation.matrix.GeneralMatrix;
036: import org.geotools.referencing.operation.transform.IdentityTransform;
037: import org.geotools.renderer.lite.RendererUtilities;
038: import org.geotools.renderer.shape.LabelingTest;
039: import org.geotools.renderer.shape.ShapefileRenderer;
040: import org.geotools.renderer.shape.TestUtilites;
041: import org.geotools.resources.TestData;
042: import org.geotools.styling.Style;
043: import org.opengis.referencing.crs.CoordinateReferenceSystem;
044: import org.opengis.referencing.operation.MathTransform;
045:
046: import com.vividsolutions.jts.geom.Coordinate;
047: import com.vividsolutions.jts.geom.Envelope;
048: import com.vividsolutions.jts.geom.Geometry;
049:
050: /**
051: * Tests multilinehandler class
052: *
053: * @author jeichar
054: * @since 2.1.x
055: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/shapefile-renderer/src/test/java/org/geotools/data/shapefile/shp/JTSMultiLineHandlerTest.java $
056: */
057: public class JTSMultiLineHandlerTest extends TestCase {
058:
059: private static final boolean INTERACTIVE = false;
060:
061: public void testRead() throws Exception {
062: URL url = TestData.getResource(LabelingTest.class,
063: "streams.shp");
064: ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory()
065: .createDataStore(url);
066:
067: Envelope env = ds.getFeatureSource().getBounds();
068: MathTransform mt = null;
069: AffineTransform transform = RendererUtilities
070: .worldToScreenTransform(env, new Rectangle(512, 512));
071: MathTransform at = ReferencingFactoryFinder
072: .getMathTransformFactory(null).createAffineTransform(
073: new GeneralMatrix(transform));
074: if (mt == null) {
075: mt = at;
076: } else {
077: mt = ReferencingFactoryFinder.getMathTransformFactory(null)
078: .createConcatenatedTransform(mt, at);
079: }
080:
081: ShapefileReader reader = new ShapefileReader(
082: ShapefileRendererUtil.getShpReadChannel(ds), new Lock());
083: reader
084: .setHandler(new org.geotools.renderer.shape.shapehandler.jts.MultiLineHandler(
085: reader.getHeader().getShapeType(), env, mt,
086: false, new Rectangle(512, 512)));
087: Object shape = reader.nextRecord().shape();
088: assertNotNull(shape);
089: assertTrue(shape instanceof Geometry);
090: Coordinate[] coords = ((Geometry) shape).getCoordinates();
091: for (int i = 0; i < coords.length; i++) {
092: Coordinate coordinate = coords[i];
093: assertNotNull(coordinate);
094: }
095:
096: int i = 0;
097: while (reader.hasNext()) {
098: i++;
099: shape = reader.nextRecord().shape();
100: assertNotNull(shape);
101: assertTrue(shape instanceof Geometry);
102: if (i == 0) {
103: Geometry geom = (Geometry) shape;
104: assertEquals(13, geom.getCoordinates().length);
105: }
106: }
107: assertEquals(ds.getFeatureSource().getCount(Query.ALL) - 1, i);
108: }
109:
110: public void testDecimation() throws Exception {
111: URL url = TestData
112: .getResource(LabelingTest.class, "theme1.shp");
113: ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory()
114: .createDataStore(url);
115:
116: Envelope env = new Envelope(-7.105552354197932,
117: 8.20555235419793, -3.239388966356115, 4.191388966388683);
118: CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
119: MathTransform mt = CRS.findMathTransform(crs,
120: DefaultGeographicCRS.WGS84);
121: AffineTransform at = RendererUtilities.worldToScreenTransform(
122: env, new Rectangle(300, 300));
123: MathTransform worldToScreen = ReferencingFactoryFinder
124: .getMathTransformFactory(null).createAffineTransform(
125: new GeneralMatrix(at));
126: mt = ReferencingFactoryFinder.getMathTransformFactory(null)
127: .createConcatenatedTransform(mt, worldToScreen);
128: ShapefileReader reader = new ShapefileReader(
129: ShapefileRendererUtil.getShpReadChannel(ds), new Lock());
130: reader
131: .setHandler(new org.geotools.renderer.shape.shapehandler.jts.MultiLineHandler(
132: reader.getHeader().getShapeType(), env, mt,
133: false, new Rectangle(300, 300)));
134: Geometry shape = (Geometry) reader.nextRecord().shape();
135: assertEquals(3, shape.getCoordinates().length);
136:
137: shape = (Geometry) reader.nextRecord().shape();
138: assertEquals(2, shape.getCoordinates().length);
139:
140: shape = (Geometry) reader.nextRecord().shape();
141: assertEquals(2, shape.getCoordinates().length);
142: }
143:
144: public void disabledtestFeatureNearBoundry() throws Exception {
145: ShapefileDataStore ds = (ShapefileDataStore) TestUtilites
146: .getDataStore("theme1.shp");
147: Style style = TestUtilites.createTestStyle(null, "theme1");
148: assertNotNull(style);
149: MapContext map = new DefaultMapContext();
150: map.addLayer(ds.getFeatureSource(), style);
151: ShapefileRenderer renderer = new ShapefileRenderer(map);
152: Envelope env = new Envelope(-5, 6, -1.4, 0);
153: TestUtilites.INTERACTIVE = INTERACTIVE;
154: TestUtilites
155: .showRender("testLineLabeling", renderer, 2000, env);
156: }
157:
158: public void testBBoxIntersectSegment() throws Exception {
159: org.geotools.renderer.shape.shapehandler.jts.MultiLineHandler handler = new org.geotools.renderer.shape.shapehandler.jts.MultiLineHandler(
160: ShapeType.ARC, new Envelope(0, 10, 0, 10),
161: IdentityTransform.create(2), false, new Rectangle(0, 0,
162: 10, 10));
163: assertTrue("point contained in bbox", handler
164: .bboxIntersectSegment(false, new double[] { 1, 1 }, 2));
165: assertFalse("point outside of bbox", handler
166: .bboxIntersectSegment(false, new double[] { -1, 1 }, 2));
167: assertTrue("Line enters bbox", handler.bboxIntersectSegment(
168: false, new double[] { -1, 1, 1, 1 }, 4));
169: assertTrue("line crosses bbox, no vertices contained", handler
170: .bboxIntersectSegment(false, new double[] { -1, 1, 11,
171: 1 }, 4));
172: assertFalse("line misses bbox", handler.bboxIntersectSegment(
173: false, new double[] { -1, -1, 11, -1 }, 4));
174: assertTrue(
175: "line diagonally crosses bbox, no vertices contained",
176: handler.bboxIntersectSegment(false, new double[] { 2,
177: -2, 12, 6 }, 4));
178: assertFalse("diagonal line misses bbox, no vertices contained",
179: handler.bboxIntersectSegment(false, new double[] { 8,
180: -4, 14, 2 }, 4));
181: }
182: }
|