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;
018:
019: import java.awt.BasicStroke;
020: import java.awt.Color;
021: import java.awt.Frame;
022: import java.awt.Graphics;
023: import java.awt.Graphics2D;
024: import java.awt.GraphicsEnvironment;
025: import java.awt.Panel;
026: import java.awt.Shape;
027: import java.awt.event.WindowAdapter;
028: import java.awt.event.WindowEvent;
029: import java.awt.geom.GeneralPath;
030: import java.awt.geom.PathIterator;
031:
032: import junit.framework.TestCase;
033:
034: import org.geotools.data.shapefile.shp.ShapeType;
035:
036: import com.vividsolutions.jts.geom.Envelope;
037:
038: /**
039: * Test The PolygonShape class
040: *
041: * @author jeichar
042: *
043: * @since 2.1.x
044: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/shapefile-renderer/src/test/java/org/geotools/renderer/shape/PolygonShapeTest.java $
045: */
046: public class PolygonShapeTest extends TestCase {
047: private static final boolean DISPLAY = false;
048:
049: public static void testPolygonWithHoles() throws Exception {
050: double[] coord1 = new double[] { 0.0, 0.0, 0.0, 200.0, 200,
051: 200.0, 200.0, 0.0, 0.0, 0.0 };
052: double[] coord2 = new double[] { 50.0, 50.0, 50.0, 150.0,
053: 150.0, 150.0, 150.0, 50.0 };
054: double[][] coords = new double[][] { coord1, coord2 };
055:
056: SimpleGeometry geom = new SimpleGeometry(ShapeType.ARC, coords,
057: new Envelope(0, 15, 0, 15));
058:
059: final PolygonShape shape = new PolygonShape(geom);
060:
061: PathIterator i = shape.getPathIterator(null);
062:
063: double[] tmp = new double[6];
064: int result = i.currentSegment(tmp);
065: assertFalse(i.isDone());
066: assertEquals(0.0, tmp[0], 0.001);
067: assertEquals(0.0, tmp[1], 0.001);
068: assertEquals(PathIterator.SEG_MOVETO, result);
069:
070: i.next();
071:
072: tmp = new double[6];
073: result = i.currentSegment(tmp);
074:
075: assertFalse(i.isDone());
076: assertEquals(0.0, tmp[0], 0.001);
077: assertEquals(200.0, tmp[1], 0.001);
078: assertEquals(PathIterator.SEG_LINETO, result);
079:
080: i.next();
081:
082: tmp = new double[6];
083: result = i.currentSegment(tmp);
084:
085: assertFalse(i.isDone());
086: assertEquals(200.0, tmp[0], 0.001);
087: assertEquals(200.0, tmp[1], 0.001);
088: assertEquals(PathIterator.SEG_LINETO, result);
089:
090: i.next();
091:
092: tmp = new double[6];
093: result = i.currentSegment(tmp);
094:
095: assertFalse(i.isDone());
096: assertEquals(200.0, tmp[0], 0.001);
097: assertEquals(0.0, tmp[1], 0.001);
098: assertEquals(PathIterator.SEG_LINETO, result);
099:
100: i.next();
101:
102: tmp = new double[6];
103: result = i.currentSegment(tmp);
104:
105: assertFalse(i.isDone());
106: assertEquals(0.0, tmp[0], 0.001);
107: assertEquals(0.0, tmp[1], 0.001);
108: assertEquals(PathIterator.SEG_CLOSE, result);
109:
110: i.next();
111:
112: tmp = new double[6];
113: result = i.currentSegment(tmp);
114:
115: assertFalse(i.isDone());
116: assertEquals(50.0, tmp[0], 0.001);
117: assertEquals(50.0, tmp[1], 0.001);
118: assertEquals(PathIterator.SEG_MOVETO, result);
119:
120: i.next();
121:
122: tmp = new double[6];
123: result = i.currentSegment(tmp);
124:
125: assertFalse(i.isDone());
126: assertEquals(50.0, tmp[0], 0.001);
127: assertEquals(150.0, tmp[1], 0.001);
128: assertEquals(PathIterator.SEG_LINETO, result);
129:
130: i.next();
131:
132: tmp = new double[6];
133: result = i.currentSegment(tmp);
134:
135: assertFalse(i.isDone());
136: assertEquals(150.0, tmp[0], 0.001);
137: assertEquals(150.0, tmp[1], 0.001);
138: assertEquals(PathIterator.SEG_LINETO, result);
139:
140: i.next();
141:
142: tmp = new double[6];
143: result = i.currentSegment(tmp);
144:
145: // assertFalse( i.isDone() );
146: assertEquals(150.0, tmp[0], 0.001);
147: assertEquals(50.0, tmp[1], 0.001);
148: assertEquals(PathIterator.SEG_CLOSE, result);
149:
150: if (DISPLAY) {
151: display("Holes", shape, 500, 500);
152: }
153: }
154:
155: public static void testMultiPolygonNoOverlap() throws Exception {
156: double[] coord1 = new double[] { 0.0, 0.0, 0.0, 100.0, 100,
157: 100.0, 100.0, 0.0, 0.0, 0.0 };
158: double[] coord2 = new double[] { 150.0, 150.0, 150.0, 250.0,
159: 250.0, 250.0, 250.0, 150.0 };
160: double[][] coords = new double[][] { coord1, coord2 };
161:
162: SimpleGeometry geom = new SimpleGeometry(ShapeType.ARC, coords,
163: new Envelope(0, 15, 0, 15));
164:
165: final PolygonShape shape = new PolygonShape(geom);
166:
167: if (DISPLAY) {
168: display("MultiNoOverlap", shape, 500, 500);
169: }
170: }
171:
172: public static void testMultiPolygonOverlap() throws Exception {
173: double[] coord1 = new double[] { 0.0, 0.0, 0.0, 100.0, 100,
174: 100.0, 100.0, 0.0, 0.0, 0.0 };
175: double[] coord2 = new double[] { 50.0, 50.0, 50.0, 250.0,
176: 250.0, 250.0, 250.0, 50.0 };
177: double[][] coords = new double[][] { coord1, coord2 };
178:
179: SimpleGeometry geom = new SimpleGeometry(ShapeType.ARC, coords,
180: new Envelope(0, 15, 0, 15));
181:
182: final PolygonShape shape = new PolygonShape(geom);
183:
184: if (DISPLAY) {
185: display("MultiOverlap", shape, 500, 500);
186: }
187: }
188:
189: public void testPolygonPoint() throws InterruptedException {
190: if (!DISPLAY) {
191: return;
192: }
193: final GeneralPath shape = new GeneralPath();
194:
195: shape.moveTo(10.1f, 10.1f);
196: shape.lineTo(10.2f, 10.1f);
197: shape.lineTo(10.2f, 10.2f);
198: shape.lineTo(10.1f, 10.2f);
199: shape.lineTo(10.1f, 10.1f);
200:
201: Frame frame = new Frame("");
202: frame.addWindowListener(new WindowAdapter() {
203: public void windowClosing(WindowEvent e) {
204: e.getWindow().dispose();
205: }
206: });
207:
208: Panel p = new Panel() {
209: /** <code>serialVersionUID</code> field */
210: private static final long serialVersionUID = 1L;
211:
212: public void paint(Graphics g) {
213: Graphics2D g2 = (Graphics2D) g;
214: g2.setPaint(Color.BLUE);
215: g2.setStroke(new BasicStroke(1));
216: (g2).fill(shape);
217: g2.setPaint(Color.BLACK);
218: (g2).draw(shape);
219: }
220: };
221:
222: frame.add(p);
223: frame.setSize(100, 100);
224: frame.setVisible(true);
225: Thread.sleep(1000);
226: }
227:
228: public static Frame display(String testName, final Shape shape,
229: int w, int h) throws Exception {
230: if (GraphicsEnvironment.isHeadless()) {
231: return null;
232: }
233: Frame frame = new Frame(testName);
234: frame.addWindowListener(new WindowAdapter() {
235: public void windowClosing(WindowEvent e) {
236: e.getWindow().dispose();
237: }
238: });
239:
240: Panel p = new Panel() {
241: /** <code>serialVersionUID</code> field */
242: private static final long serialVersionUID = 1L;
243:
244: public void paint(Graphics g) {
245: Graphics2D g2 = (Graphics2D) g;
246: g2.setPaint(Color.BLUE);
247: (g2).fill(shape);
248: g2.setPaint(Color.BLACK);
249: (g2).draw(shape);
250: }
251: };
252:
253: frame.add(p);
254: frame.setSize(w, h);
255: frame.setVisible(true);
256: Thread.sleep(1000);
257:
258: return frame;
259: }
260: }
|