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