001: package org.geotools.data.crs;
002:
003: import junit.framework.TestCase;
004:
005: import org.geotools.data.FeatureReader;
006: import org.geotools.data.memory.MemoryDataStore;
007: import org.geotools.feature.AttributeType;
008: import org.geotools.feature.AttributeTypeFactory;
009: import org.geotools.feature.Feature;
010: import org.geotools.feature.FeatureType;
011: import org.geotools.feature.FeatureTypeBuilder;
012: import org.geotools.referencing.crs.DefaultEngineeringCRS;
013: import org.geotools.referencing.crs.DefaultGeographicCRS;
014: import org.opengis.referencing.crs.CoordinateReferenceSystem;
015:
016: import com.vividsolutions.jts.geom.Coordinate;
017: import com.vividsolutions.jts.geom.GeometryFactory;
018: import com.vividsolutions.jts.geom.Point;
019:
020: public class ForceCoordinateSystemFeatureReaderTest extends TestCase {
021:
022: private static final String FEATURE_TYPE_NAME = "testType";
023:
024: protected void setUp() throws Exception {
025: super .setUp();
026: }
027:
028: /**
029: * create a datastore with 1 feature in it.
030: * @param crs the CRS of the featuretype
031: * @param p the point to add, should be same CRS as crs
032: * @return
033: * @throws Exception
034: */
035: private MemoryDataStore createDatastore(
036: CoordinateReferenceSystem crs, Point p) throws Exception {
037:
038: AttributeType attType = AttributeTypeFactory.newAttributeType(
039: "geom", Point.class, true, 100, null, crs);
040: FeatureType ft = FeatureTypeBuilder.newFeatureType(
041: new AttributeType[] { attType }, FEATURE_TYPE_NAME);
042:
043: Feature[] features = new Feature[] { ft
044: .create(new Object[] { p }) };
045:
046: return new MemoryDataStore(features);
047: }
048:
049: public void testSameCRS() throws Exception {
050: CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
051: GeometryFactory fac = new GeometryFactory();
052: Point p = fac.createPoint(new Coordinate(10, 10));
053:
054: MemoryDataStore ds = createDatastore(crs, p);
055:
056: FeatureReader original = ds.getFeatureReader(FEATURE_TYPE_NAME);
057:
058: ForceCoordinateSystemFeatureReader modified = new ForceCoordinateSystemFeatureReader(
059: ds.getFeatureReader(FEATURE_TYPE_NAME), crs);
060:
061: Feature f1 = original.next();
062: Feature f2 = modified.next();
063:
064: assertEquals(f1, f2);
065:
066: assertFalse(original.hasNext());
067: assertFalse(modified.hasNext());
068:
069: assertNull(modified.schema);
070: }
071:
072: public void testDifferentCRS() throws Exception {
073: CoordinateReferenceSystem srcCRS = DefaultGeographicCRS.WGS84;
074: GeometryFactory fac = new GeometryFactory();
075: Point p = fac.createPoint(new Coordinate(10, 10));
076:
077: MemoryDataStore ds = createDatastore(srcCRS, p);
078:
079: FeatureReader original = ds.getFeatureReader(FEATURE_TYPE_NAME);
080:
081: CoordinateReferenceSystem destCRS = DefaultEngineeringCRS.CARTESIAN_2D;
082: ForceCoordinateSystemFeatureReader modified = new ForceCoordinateSystemFeatureReader(
083: ds.getFeatureReader(FEATURE_TYPE_NAME), destCRS);
084:
085: Feature f1 = original.next();
086: Feature f2 = modified.next();
087:
088: assertEquals(f1.getDefaultGeometry().getCoordinate(), f2
089: .getDefaultGeometry().getCoordinate());
090: assertFalse(f1.getFeatureType().getDefaultGeometry()
091: .getCoordinateSystem().equals(
092: f2.getFeatureType().getDefaultGeometry()
093: .getCoordinateSystem()));
094: assertEquals(srcCRS, f1.getFeatureType().getDefaultGeometry()
095: .getCoordinateSystem());
096: assertEquals(destCRS, f2.getFeatureType().getDefaultGeometry()
097: .getCoordinateSystem());
098:
099: assertFalse(original.hasNext());
100: assertFalse(modified.hasNext());
101:
102: assertNotNull(modified.schema);
103: }
104:
105: public void testNullDestination() throws Exception {
106: CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
107: GeometryFactory fac = new GeometryFactory();
108: Point p = fac.createPoint(new Coordinate(10, 10));
109:
110: MemoryDataStore ds = createDatastore(crs, p);
111:
112: try {
113: new ForceCoordinateSystemFeatureReader(ds
114: .getFeatureReader(FEATURE_TYPE_NAME),
115: (CoordinateReferenceSystem) null);
116: fail(); // should throw a nullpointer exception.
117: } catch (NullPointerException e) {
118: // good
119: }
120:
121: }
122:
123: public void testNullSource() throws Exception {
124: CoordinateReferenceSystem srcCRS = null;
125: GeometryFactory fac = new GeometryFactory();
126: Point p = fac.createPoint(new Coordinate(10, 10));
127:
128: MemoryDataStore ds = createDatastore(srcCRS, p);
129:
130: FeatureReader original = ds.getFeatureReader(FEATURE_TYPE_NAME);
131:
132: CoordinateReferenceSystem destCRS = DefaultEngineeringCRS.CARTESIAN_2D;
133: ForceCoordinateSystemFeatureReader modified = new ForceCoordinateSystemFeatureReader(
134: ds.getFeatureReader(FEATURE_TYPE_NAME), destCRS);
135:
136: Feature f1 = original.next();
137: Feature f2 = modified.next();
138:
139: assertEquals(f1.getDefaultGeometry().getCoordinate(), f2
140: .getDefaultGeometry().getCoordinate());
141: assertFalse(f2.getFeatureType().getDefaultGeometry()
142: .getCoordinateSystem().equals(
143: f1.getFeatureType().getDefaultGeometry()
144: .getCoordinateSystem()));
145: assertEquals(srcCRS, f1.getFeatureType().getDefaultGeometry()
146: .getCoordinateSystem());
147: assertEquals(destCRS, f2.getFeatureType().getDefaultGeometry()
148: .getCoordinateSystem());
149:
150: assertFalse(original.hasNext());
151: assertFalse(modified.hasNext());
152:
153: assertNotNull(modified.schema);
154: }
155: }
|