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