001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2002, Refractions Reserach Inc.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.graph.build.polygon;
018:
019: import java.util.StringTokenizer;
020:
021: import org.geotools.graph.build.basic.BasicGraphBuilder;
022: import org.geotools.graph.structure.Graph;
023: import org.geotools.graph.structure.Node;
024:
025: import com.vividsolutions.jts.geom.Coordinate;
026: import com.vividsolutions.jts.geom.GeometryFactory;
027: import com.vividsolutions.jts.geom.Polygon;
028:
029: import junit.framework.TestCase;
030:
031: public class PolygonGraphGeneratorTest extends TestCase {
032:
033: static GeometryFactory gf = new GeometryFactory();
034: PolygonGraphGenerator gg;
035:
036: protected void setUp() throws Exception {
037: super .setUp();
038: PolygonGraphGenerator.PolygonRelationship rel = new PolygonGraphGenerator.PolygonRelationship() {
039:
040: public boolean related(Polygon p1, Polygon p2) {
041: return p1.intersects(p2);
042: }
043:
044: public boolean equal(Polygon p1, Polygon p2) {
045: return p1.equals(p2);
046: }
047:
048: };
049:
050: gg = new PolygonGraphGenerator(new BasicGraphBuilder(), rel);
051: }
052:
053: public void testAdd() {
054: Polygon p1 = createPolygon("0 0,1 1,2 2,0 0");
055: Polygon p2 = createPolygon("3 3,4 4,5 5,3 3");
056: Polygon p3 = createPolygon("6 6,7 7,8 8,6 6");
057:
058: Node n1 = (Node) gg.add(p1);
059: Node n2 = (Node) gg.add(p2);
060: Node n3 = (Node) gg.add(p3);
061:
062: assertNotNull(n1);
063: assertEquals(n1.getObject(), p1);
064:
065: assertNotNull(n2);
066: assertEquals(n2.getObject(), p2);
067:
068: assertNotNull(n3);
069: assertEquals(n3.getObject(), p3);
070:
071: Graph g = gg.getGraph();
072: assertEquals(3, g.getNodes().size());
073: assertEquals(0, g.getEdges().size());
074: }
075:
076: public void testRelationships() {
077: Polygon p1 = createPolygon("0 0,5 0,5 5,0 5,0 0");
078: Polygon p2 = createPolygon("4 4,9 4,9 9,4 9,4 4");
079: Polygon p3 = createPolygon("2 2,7 2,7 -3,2 -3,2 2");
080:
081: Node n1 = (Node) gg.add(p1);
082: Node n2 = (Node) gg.add(p2);
083: Node n3 = (Node) gg.add(p3);
084:
085: assertNotNull(n1.getEdge(n2));
086: assertNotNull(n2.getEdge(n1));
087: assertNotNull(n1.getEdge(n3));
088: assertNotNull(n2.getEdge(n1));
089:
090: assertNull(n2.getEdge(n3));
091: assertNull(n2.getEdge(n2));
092:
093: Graph g = gg.getGraph();
094: assertEquals(3, g.getNodes().size());
095: assertEquals(2, g.getEdges().size());
096: }
097:
098: protected Polygon createPolygon(String coordinates) {
099: StringTokenizer tokens = new StringTokenizer(coordinates, ",");
100: Coordinate[] c = new Coordinate[tokens.countTokens()];
101:
102: int i = 0;
103: while (tokens.hasMoreTokens()) {
104: String token = tokens.nextToken();
105: String[] oordinates = token.split(" ");
106: c[i++] = new Coordinate(Double.parseDouble(oordinates[0]),
107: Double.parseDouble(oordinates[1]));
108: }
109:
110: return gf.createPolygon(gf.createLinearRing(c), null);
111: }
112: }
|