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