001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002, 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.awt.Rectangle;
020: import java.awt.geom.AffineTransform;
021: import java.net.URL;
022:
023: import junit.framework.TestCase;
024:
025: import org.geotools.data.Query;
026: import org.geotools.data.shapefile.Lock;
027: import org.geotools.data.shapefile.ShapefileDataStore;
028: import org.geotools.data.shapefile.ShapefileDataStoreFactory;
029: import org.geotools.data.shapefile.ShapefileRendererUtil;
030: import org.geotools.geometry.jts.ReferencedEnvelope;
031: import org.geotools.referencing.CRS;
032: import org.geotools.referencing.ReferencingFactoryFinder;
033: import org.geotools.referencing.crs.DefaultGeographicCRS;
034: import org.geotools.referencing.operation.matrix.AffineTransform2D;
035: import org.geotools.referencing.operation.matrix.GeneralMatrix;
036: import org.geotools.referencing.operation.matrix.XAffineTransform;
037: import org.geotools.renderer.lite.RendererUtilities;
038: import org.geotools.renderer.shape.LabelingTest;
039: import org.geotools.resources.TestData;
040: import org.opengis.referencing.crs.CoordinateReferenceSystem;
041: import org.opengis.referencing.operation.MathTransform;
042: import org.opengis.referencing.operation.MathTransform2D;
043:
044: import com.vividsolutions.jts.geom.Coordinate;
045: import com.vividsolutions.jts.geom.Envelope;
046: import com.vividsolutions.jts.geom.Geometry;
047: import com.vividsolutions.jts.geom.Polygon;
048:
049: /**
050: * @TODO class description
051: *
052: * @author jeichar
053: * @since 2.1.x
054: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/shapefile-renderer/src/test/java/org/geotools/data/shapefile/shp/JTSPolygonHandlerTest.java $
055: */
056: public class JTSPolygonHandlerTest extends TestCase {
057:
058: public void testRead() throws Exception {
059: URL url = TestData.getResource(LabelingTest.class, "lakes.shp");
060: ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory()
061: .createDataStore(url);
062:
063: ReferencedEnvelope env = (ReferencedEnvelope) ds
064: .getFeatureSource().getBounds();
065:
066: AffineTransform transform = RendererUtilities
067: .worldToScreenTransform(env, new Rectangle(500, 500));
068: MathTransform mt = ReferencingFactoryFinder
069: .getMathTransformFactory(null).createAffineTransform(
070: new GeneralMatrix(transform));
071:
072: ShapefileReader reader = new ShapefileReader(
073: ShapefileRendererUtil.getShpReadChannel(ds), new Lock());
074: reader
075: .setHandler(new org.geotools.renderer.shape.shapehandler.jts.PolygonHandler(
076: reader.getHeader().getShapeType(), env, mt,
077: false));
078: Object shape = reader.nextRecord().shape();
079: assertNotNull(shape);
080: assertTrue(shape instanceof Geometry);
081: Coordinate[] coords = ((Geometry) shape).getCoordinates();
082: for (int i = 0; i < coords.length; i++) {
083: Coordinate coordinate = coords[i];
084: assertNotNull(coordinate);
085: }
086: int i = 0;
087: while (reader.hasNext()) {
088: i++;
089: shape = reader.nextRecord().shape();
090: assertNotNull(shape);
091: assertTrue(shape instanceof Geometry);
092: }
093: assertEquals(ds.getFeatureSource().getCount(Query.ALL) - 1, i);
094: }
095:
096: public void testPolgyonPartDecimation() throws Exception {
097: URL url = TestData.getResource(LabelingTest.class,
098: "smallMultiPoly.shp");
099: ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory()
100: .createDataStore(url);
101:
102: Envelope env = new Envelope(-116.61514977458947,
103: -115.06357335975156, 31.826799280244018,
104: 32.590528603609826);
105: // Envelope env=new Envelope(-180,180,-90,90);
106: // CoordinateReferenceSystem crs=ds.getSchema().getDefaultGeometry().getCoordinateSystem();
107: CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
108: MathTransform mt = CRS.findMathTransform(crs,
109: DefaultGeographicCRS.WGS84);
110: AffineTransform at = RendererUtilities.worldToScreenTransform(
111: env, new Rectangle(300, 300));
112: mt = ReferencingFactoryFinder.getMathTransformFactory(null)
113: .createConcatenatedTransform(
114: mt,
115: ReferencingFactoryFinder
116: .getMathTransformFactory(null)
117: .createAffineTransform(
118: new GeneralMatrix(at)));
119:
120: ShapefileReader reader = new ShapefileReader(
121: ShapefileRendererUtil.getShpReadChannel(ds), new Lock());
122: reader
123: .setHandler(new org.geotools.renderer.shape.shapehandler.jts.PolygonHandler(
124: reader.getHeader().getShapeType(), env, mt,
125: false));
126: Object shape = reader.nextRecord().shape();
127: assertNotNull(shape);
128: assertTrue(shape instanceof Geometry);
129: Geometry geom = (Geometry) shape;
130: assertEquals(1, geom.getNumGeometries());
131: assertEquals(5, geom.getCoordinates().length);
132: }
133:
134: }
|