001: /*
002: * Geotools2 - 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;
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.io.IOException;
020: import java.util.ArrayList;
021:
022: import org.geotools.data.shapefile.TestCaseSupport;
023:
024: import com.vividsolutions.jts.geom.Coordinate;
025: import com.vividsolutions.jts.geom.Geometry;
026: import com.vividsolutions.jts.geom.GeometryFactory;
027: import com.vividsolutions.jts.geom.LinearRing;
028: import com.vividsolutions.jts.geom.PrecisionModel;
029:
030: /**
031: *
032: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/test/java/org/geotools/data/shapefile/shp/PolygonHandlerTest.java $
033: * @version $Id: PolygonHandlerTest.java 19207 2006-04-17 18:07:51Z jgarnett $
034: * @author Ian Schneider
035: */
036: public class PolygonHandlerTest extends TestCaseSupport {
037:
038: public PolygonHandlerTest(String testName) throws IOException {
039: super (testName);
040: }
041:
042: public static void main(java.lang.String[] args) {
043: junit.textui.TestRunner.run(suite(PolygonHandlerTest.class));
044: }
045:
046: public void testPolygonHandler() {
047: Coordinate[] c = new Coordinate[3];
048: c[0] = new Coordinate(0, 0, 0);
049: c[1] = new Coordinate(1, 1, Double.NaN);
050: c[2] = new Coordinate(1, 2, 3);
051: PolygonHandler handler = new PolygonHandler();
052: assertTrue(handler.getShapeType() == ShapeType.POLYGON);
053: for (int i = 0, ii = c.length; i < ii; i++) {
054: assertTrue(handler.pointInList(c[i], c));
055: }
056: }
057:
058: public void testHoleAssignment() {
059: java.awt.Dimension ps = new java.awt.Dimension(500, 500);
060: PrecisionModel precision = new PrecisionModel();
061:
062: ArrayList shells = new ArrayList();
063: ArrayList holes = new ArrayList();
064:
065: int x = 10;
066: int y = 10;
067:
068: shells.add(copyTo(x, y, ps.width - 2 * x, ps.height - 2 * y,
069: rectangle(precision, 0)));
070:
071: int w = 11;
072: int h = 11;
073: int s = 10;
074:
075: int nx = (ps.width - 2 * x) / (w + s);
076: int ny = (ps.height - 2 * y) / (h + s);
077:
078: for (int i = 0; i < nx; i++) {
079: for (int j = 0; j < ny; j++) {
080: holes.add(copyTo(x + s + i * (w + s), y + s + j
081: * (h + s), w, h, rectangle(precision, 0)));
082: }
083: }
084:
085: PolygonHandler ph = new PolygonHandler();
086: ArrayList assigned = ph.assignHolesToShells(shells, holes);
087: assertEquals(((ArrayList) assigned.get(0)).size(), holes.size());
088:
089: }
090:
091: public static Geometry rectangle(PrecisionModel pm, int SRID) {
092: Coordinate[] coords = new Coordinate[5];
093: for (int i = 0; i < coords.length; i++) {
094: coords[i] = new Coordinate();
095: }
096: return new GeometryFactory().createLinearRing(coords);
097: }
098:
099: public static Geometry copyTo(double x, double y, double w,
100: double h, Geometry g) {
101: if (g.getNumPoints() != 5)
102: throw new IllegalArgumentException(
103: "Geometry must have 5 points");
104: if (!LinearRing.class.isAssignableFrom(g.getClass()))
105: throw new IllegalArgumentException(
106: "Geometry must be linear ring");
107: Coordinate[] coords = g.getCoordinates();
108: coords[0].x = x;
109: coords[0].y = y;
110: coords[1].x = x + w;
111: coords[1].y = y;
112: coords[2].x = x + w;
113: coords[2].y = y + h;
114: coords[3].x = x;
115: coords[3].y = y + h;
116: coords[4].x = x;
117: coords[4].y = y;
118: return g;
119: }
120: }
|