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.feature;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import junit.framework.TestCase;
023:
024: import org.geotools.feature.AttributeTypeFactory;
025: import org.geotools.feature.Feature;
026: import org.geotools.feature.FeatureType;
027: import org.geotools.feature.FeatureTypeFactory;
028: import org.geotools.feature.IllegalAttributeException;
029: import org.geotools.feature.SchemaException;
030: import org.geotools.graph.build.line.LineStringGraphGenerator;
031: import org.geotools.graph.structure.Edge;
032:
033: import com.vividsolutions.jts.geom.Coordinate;
034: import com.vividsolutions.jts.geom.GeometryFactory;
035: import com.vividsolutions.jts.geom.LineString;
036:
037: public class FeatureGraphGeneratorTest extends TestCase {
038:
039: public void test() throws Exception {
040: LineStringGraphGenerator lsgg = new LineStringGraphGenerator();
041: FeatureGraphGenerator fgg = new FeatureGraphGenerator(
042: new LineStringGraphGenerator());
043:
044: LineString[] lines = lines();
045: Feature[] features = features(lines);
046:
047: List el1 = new ArrayList();
048: List el2 = new ArrayList();
049: for (int i = 0; i < lines.length; i++) {
050: el1.add(lsgg.add(lines[i]));
051: el2.add(fgg.add(features[i]));
052: }
053:
054: for (int i = 0; i < el1.size(); i++) {
055: Edge e1 = (Edge) el1.get(i);
056: Edge e2 = (Edge) el2.get(i);
057:
058: assertTrue(e1.getObject() instanceof LineString);
059: assertTrue(e2.getObject() instanceof Feature);
060:
061: LineString line = (LineString) e1.getObject();
062: Feature feature = (Feature) e2.getObject();
063:
064: assertEquals(line, feature.getDefaultGeometry());
065: }
066: }
067:
068: LineString[] lines() {
069: GeometryFactory gf = new GeometryFactory();
070: LineString[] lines = new LineString[5];
071:
072: for (int i = 0; i < lines.length; i++) {
073: lines[i] = gf.createLineString(new Coordinate[] {
074: new Coordinate(i, i + 1),
075: new Coordinate(i + 2, i + 3) });
076: }
077:
078: return lines;
079: }
080:
081: Feature[] features(LineString[] lines) throws SchemaException,
082: IllegalAttributeException {
083:
084: FeatureTypeFactory typeFactory = FeatureTypeFactory
085: .newInstance("test");
086: typeFactory.addType(AttributeTypeFactory.newAttributeType(
087: "the_geom", LineString.class));
088: typeFactory.addType(AttributeTypeFactory.newAttributeType("id",
089: Integer.class));
090:
091: FeatureType schema = typeFactory.getFeatureType();
092: Feature[] features = new Feature[lines.length];
093: for (int i = 0; i < lines.length; i++) {
094: features[i] = schema.create(new Object[] { lines[i],
095: new Integer(i) });
096: }
097:
098: return features;
099: }
100:
101: }
|