001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, 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; either
009: * version 2.1 of the License, or (at your option) any later version.
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: * Created on 27 May 2002, 15:40
017: */
018: package org.geotools.geometry.jts;
019:
020: import java.awt.geom.AffineTransform;
021: import java.awt.geom.Rectangle2D;
022:
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026:
027: import org.geotools.geometry.jts.LiteShape2;
028: import org.geotools.referencing.operation.transform.ProjectiveTransform;
029: import org.opengis.referencing.FactoryException;
030: import org.opengis.referencing.operation.TransformException;
031:
032: import com.vividsolutions.jts.geom.Coordinate;
033: import com.vividsolutions.jts.geom.CoordinateSequence;
034: import com.vividsolutions.jts.geom.GeometryFactory;
035: import com.vividsolutions.jts.geom.LineString;
036: import com.vividsolutions.jts.geom.LinearRing;
037: import com.vividsolutions.jts.geom.Polygon;
038: import com.vividsolutions.jts.geom.TopologyException;
039:
040: /**
041: *
042: * @author jamesm,iant
043: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/test/java/org/geotools/geometry/jts/LiteShapeTest.java $
044: */
045: public class LiteShapeTest extends TestCase {
046: private java.net.URL base = getClass().getResource("testData/");
047:
048: public LiteShapeTest(java.lang.String testName) {
049: super (testName);
050:
051: }
052:
053: public static void main(java.lang.String[] args) {
054: junit.textui.TestRunner.run(suite());
055: }
056:
057: public static Test suite() {
058: TestSuite suite = new TestSuite(LiteShapeTest.class);
059: return suite;
060: }
061:
062: public void testLineShape() throws TransformException,
063: FactoryException {
064: GeometryFactory geomFac = new GeometryFactory();
065: LineString lineString = makeSampleLineString(geomFac, 0, 0);
066: LiteShape2 lineShape = new LiteShape2(lineString,
067: ProjectiveTransform.create(new AffineTransform()),
068: new Decimator(ProjectiveTransform
069: .create(new AffineTransform())), false);
070:
071: assertFalse(lineShape.contains(0, 0));
072: assertTrue(lineShape.contains(60, 60));
073: assertFalse(lineShape.contains(50, 50, 10, 10));
074: assertTrue(lineShape.contains(new java.awt.Point(60, 60)));
075: assertFalse(lineShape
076: .contains(new java.awt.geom.Rectangle2D.Float(50, 50,
077: 10, 10)));
078: assertTrue(lineShape.getBounds2D().equals(
079: new Rectangle2D.Double(50, 50, 80, 250)));
080: assertTrue(lineShape.getBounds().equals(
081: new java.awt.Rectangle(50, 50, 80, 250)));
082: assertTrue(lineShape.intersects(0, 0, 100, 100));
083: assertTrue(lineShape.intersects(new Rectangle2D.Double(0, 0,
084: 100, 100)));
085: assertFalse(lineShape.intersects(55, 55, 3, 100));
086: assertFalse(lineShape.intersects(new Rectangle2D.Double(55, 55,
087: 3, 100)));
088: }
089:
090: public void testPolygonShape() throws TransformException,
091: FactoryException {
092: GeometryFactory geomFac = new GeometryFactory();
093: Polygon polygon = makeSamplePolygon(geomFac, 0, 0);
094: LiteShape2 lineShape = new LiteShape2(polygon,
095: ProjectiveTransform.create(new AffineTransform()),
096: new Decimator(ProjectiveTransform
097: .create(new AffineTransform())), false);
098:
099: assertFalse(lineShape.contains(0, 0));
100: assertTrue(lineShape.contains(100, 100));
101: assertFalse(lineShape.contains(50, 50, 10, 10));
102: assertTrue(lineShape.contains(100, 100, 10, 10));
103: assertTrue(lineShape.contains(new java.awt.Point(70, 90)));
104: assertFalse(lineShape
105: .contains(new java.awt.geom.Rectangle2D.Float(50, 50,
106: 10, 10)));
107: assertTrue(lineShape.getBounds2D().equals(
108: new Rectangle2D.Double(60, 70, 70, 50)));
109: assertTrue(lineShape.getBounds().equals(
110: new java.awt.Rectangle(60, 70, 70, 50)));
111: assertTrue(lineShape.intersects(0, 0, 100, 100));
112: assertTrue(lineShape.intersects(new Rectangle2D.Double(0, 0,
113: 100, 100)));
114: assertFalse(lineShape.intersects(55, 55, 3, 100));
115: assertFalse(lineShape.intersects(new Rectangle2D.Double(55, 55,
116: 3, 100)));
117: }
118:
119: public void testCloning() throws TransformException,
120: FactoryException {
121: LiteCoordinateSequenceFactory csFac = new LiteCoordinateSequenceFactory();
122: GeometryFactory geomFac = new GeometryFactory(csFac);
123: CoordinateSequence cs = csFac.create(4, 2);
124: cs.setOrdinate(0, 0, 10);
125: cs.setOrdinate(0, 1, 10);
126: cs.setOrdinate(1, 0, 12);
127: cs.setOrdinate(1, 1, 12);
128: cs.setOrdinate(2, 0, 14);
129: cs.setOrdinate(2, 1, 12);
130: cs.setOrdinate(3, 0, 30);
131: cs.setOrdinate(3, 1, 10);
132:
133: LineString ls = geomFac.createLineString(cs);
134: LineString copy = (LineString) ls.clone();
135: LiteShape2 ltCloning = new LiteShape2(ls, ProjectiveTransform
136: .create(AffineTransform.getScaleInstance(10, 10)),
137: new Decimator(4, 4), true);
138: assertTrue(ls.equals(copy));
139:
140: LiteShape2 ltNotCloning = new LiteShape2(ls,
141: ProjectiveTransform.create(AffineTransform
142: .getScaleInstance(10, 10)),
143: new Decimator(4, 4), true, false);
144: assertFalse(ls.equals(copy));
145: }
146:
147: private LineString makeSampleLineString(
148: final GeometryFactory geomFac, double xoff, double yoff) {
149: Coordinate[] linestringCoordinates = new Coordinate[8];
150: linestringCoordinates[0] = new Coordinate(50.0d + xoff,
151: 50.0d + yoff);
152: linestringCoordinates[1] = new Coordinate(60.0d + xoff,
153: 50.0d + yoff);
154: linestringCoordinates[2] = new Coordinate(60.0d + xoff,
155: 60.0d + yoff);
156: linestringCoordinates[3] = new Coordinate(70.0d + xoff,
157: 60.0d + yoff);
158: linestringCoordinates[4] = new Coordinate(70.0d + xoff,
159: 70.0d + yoff);
160: linestringCoordinates[5] = new Coordinate(80.0d + xoff,
161: 70.0d + yoff);
162: linestringCoordinates[6] = new Coordinate(80.0d + xoff,
163: 80.0d + yoff);
164: linestringCoordinates[7] = new Coordinate(130.0d + xoff,
165: 300.0d + yoff);
166: LineString line = geomFac
167: .createLineString(linestringCoordinates);
168:
169: return line;
170: }
171:
172: private com.vividsolutions.jts.geom.Polygon makeSamplePolygon(
173: final GeometryFactory geomFac, double xoff, double yoff) {
174: Coordinate[] polygonCoordinates = new Coordinate[10];
175: polygonCoordinates[0] = new Coordinate(70 + xoff, 70 + yoff);
176: polygonCoordinates[1] = new Coordinate(60 + xoff, 90 + yoff);
177: polygonCoordinates[2] = new Coordinate(60 + xoff, 110 + yoff);
178: polygonCoordinates[3] = new Coordinate(70 + xoff, 120 + yoff);
179: polygonCoordinates[4] = new Coordinate(90 + xoff, 110 + yoff);
180: polygonCoordinates[5] = new Coordinate(110 + xoff, 120 + yoff);
181: polygonCoordinates[6] = new Coordinate(130 + xoff, 110 + yoff);
182: polygonCoordinates[7] = new Coordinate(130 + xoff, 90 + yoff);
183: polygonCoordinates[8] = new Coordinate(110 + xoff, 70 + yoff);
184: polygonCoordinates[9] = new Coordinate(70 + xoff, 70 + yoff);
185: try {
186: LinearRing ring = geomFac
187: .createLinearRing(polygonCoordinates);
188: com.vividsolutions.jts.geom.Polygon polyg = geomFac
189: .createPolygon(ring, null);
190: return polyg;
191: } catch (TopologyException te) {
192: fail("Error creating sample polygon for testing " + te);
193: }
194: return null;
195: }
196:
197: }
|