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: /*
018: * StylingTest.java JUnit based test Created on April 12, 2002, 1:18 PM
019: */
020: package org.geotools.renderer.shape;
021:
022: import java.awt.Rectangle;
023: import java.awt.image.BufferedImage;
024: import java.io.IOException;
025: import java.util.logging.Logger;
026:
027: import junit.framework.Test;
028: import junit.framework.TestCase;
029: import junit.framework.TestSuite;
030: import junit.textui.TestRunner;
031:
032: import org.geotools.data.DataStore;
033: import org.geotools.data.FeatureSource;
034: import org.geotools.data.shapefile.ShapefileDataStore;
035: import org.geotools.filter.IllegalFilterException;
036: import org.geotools.geometry.jts.ReferencedEnvelope;
037: import org.geotools.map.DefaultMapContext;
038: import org.geotools.map.MapContext;
039: import org.geotools.referencing.ReferencingFactoryFinder;
040: import org.geotools.referencing.crs.DefaultEngineeringCRS;
041: import org.geotools.referencing.crs.DefaultGeographicCRS;
042: import org.geotools.styling.SLDParser;
043: import org.geotools.styling.Style;
044: import org.geotools.styling.StyleFactory;
045: import org.geotools.styling.StyleFactoryFinder;
046: import org.geotools.styling.StyledLayerDescriptor;
047: import org.geotools.styling.UserLayer;
048: import org.geotools.test.TestData;
049: import org.opengis.referencing.crs.CoordinateReferenceSystem;
050: import org.opengis.referencing.crs.EngineeringCRS;
051:
052: import com.vividsolutions.jts.geom.Envelope;
053:
054: /**
055: * DOCUMENT ME!
056: *
057: * @author jamesm
058: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/shapefile-renderer/src/test/java/org/geotools/renderer/shape/Rendering2DTest.java $
059: */
060: public class Rendering2DTest extends TestCase {
061: /** path for test data */
062:
063: // private java.net.URL base = getClass().getResource("/testData/");
064: /** The logger for the rendering module. */
065: public static final Logger LOGGER = org.geotools.util.logging.Logging
066: .getLogger("org.geotools.rendering");
067: static final String LINE = "linefeature";
068: static final String POLYGON = "polygonfeature";
069: static final String POINT = "pointfeature";
070: static final String RING = "ringfeature";
071: static final String COLLECTION = "collfeature";
072:
073: public Rendering2DTest(java.lang.String testName) {
074: super (testName);
075: }
076:
077: public static void main(java.lang.String[] args) {
078: TestRunner.run(suite());
079: }
080:
081: public static Test suite() {
082: TestSuite suite = new TestSuite(Rendering2DTest.class);
083:
084: return suite;
085: }
086:
087: Style loadTestStyle() throws IOException {
088: StyleFactory factory = StyleFactoryFinder.createStyleFactory();
089:
090: java.net.URL surl = TestData.getResource(this , "test-sld.xml");
091: SLDParser stylereader = new SLDParser(factory, surl);
092: StyledLayerDescriptor sld = stylereader.parseSLD();
093:
094: UserLayer layer = (UserLayer) sld.getStyledLayers()[0];
095:
096: Style style = layer.getUserStyles()[0];
097:
098: return style;
099: }
100:
101: Style createTestStyle() throws IllegalFilterException {
102: return TestUtilites.createTestStyle(null, null);
103: }
104:
105: public void testSimpleRender() throws Exception {
106: // same as the datasource test, load in some features into a table
107: System.err.println("starting rendering2DTest");
108:
109: DataStore ds = TestUtilites.getPolygons();
110: FeatureSource source = ds
111: .getFeatureSource(ds.getTypeNames()[0]);
112: Style style = createTestStyle();
113:
114: MapContext map = new DefaultMapContext(
115: DefaultGeographicCRS.WGS84);
116: map.addLayer(source, style);
117:
118: ShapefileRenderer renderer = new ShapefileRenderer(map);
119: ReferencedEnvelope env = map.getLayerBounds();
120: TestUtilites
121: .showRender("testSimpleRender", renderer, 1000, env);
122: }
123:
124: public void testSimpleLineRender() throws Exception {
125: // same as the datasource test, load in some features into a table
126: System.err.println("starting rendering2DTest");
127:
128: ShapefileRenderer renderer = createLineRenderer(TestUtilites
129: .getLines());
130: MapContext map = renderer.getContext();
131:
132: map
133: .setAreaOfInterest(map.getLayer(0).getFeatureSource()
134: .getBounds(), map.getLayer(0)
135: .getFeatureSource().getSchema()
136: .getDefaultGeometry().getCoordinateSystem());
137:
138: ReferencedEnvelope env = map.getLayerBounds();
139: env = new ReferencedEnvelope(env.getMinX() - 20,
140: env.getMaxX() + 20, env.getMinY() - 20,
141: env.getMaxY() + 20, env.getCoordinateReferenceSystem());
142: map.setAreaOfInterest(env);
143: TestUtilites.showRender("testSimpleLineRender", renderer, 3000,
144: env);
145: }
146:
147: public void testSimplePolygonRender() throws Exception {
148: // same as the datasource test, load in some features into a table
149: System.err.println("starting rendering2DTest");
150:
151: ShapefileRenderer renderer = createLineRenderer(TestUtilites
152: .getPolygons());
153: MapContext map = renderer.getContext();
154:
155: map
156: .setAreaOfInterest(map.getLayer(0).getFeatureSource()
157: .getBounds(), map.getLayer(0)
158: .getFeatureSource().getSchema()
159: .getDefaultGeometry().getCoordinateSystem());
160:
161: ReferencedEnvelope env = map.getLayerBounds();
162: env = new ReferencedEnvelope(env.getMinX() - 20,
163: env.getMaxX() + 20, env.getMinY() - 20,
164: env.getMaxY() + 20, env.getCoordinateReferenceSystem());
165: map.setAreaOfInterest(env);
166: TestUtilites.showRender("testSimpleLineRender", renderer, 3000,
167: env);
168: }
169:
170: public void testSimplePolygonRenderZoomedOut() throws Exception {
171: // same as the datasource test, load in some features into a table
172: System.err.println("starting rendering2DTest");
173:
174: ShapefileRenderer renderer = createLineRenderer(TestUtilites
175: .getPolygons());
176: MapContext map = renderer.getContext();
177:
178: map
179: .setAreaOfInterest(map.getLayer(0).getFeatureSource()
180: .getBounds(), map.getLayer(0)
181: .getFeatureSource().getSchema()
182: .getDefaultGeometry().getCoordinateSystem());
183:
184: ReferencedEnvelope env = map.getLayerBounds();
185: env = new ReferencedEnvelope(env.getMinX() - 200000, env
186: .getMaxX() + 200000, env.getMinY() - 200000, env
187: .getMaxY() + 200000, env.getCoordinateReferenceSystem());
188: map.setAreaOfInterest(env);
189: TestUtilites.showRender("testSimpleLineRender", renderer, 3000,
190: env);
191: }
192:
193: public void testSimpleLineRenderLargebbox() throws Exception {
194: // same as the datasource test, load in some features into a table
195: System.err.println("starting rendering2DTest");
196:
197: ShapefileRenderer renderer = createLineRenderer(TestUtilites
198: .getLines());
199: MapContext map = renderer.getContext();
200:
201: map
202: .setAreaOfInterest(map.getLayer(0).getFeatureSource()
203: .getBounds(), map.getLayer(0)
204: .getFeatureSource().getSchema()
205: .getDefaultGeometry().getCoordinateSystem());
206:
207: ReferencedEnvelope env = map.getLayerBounds();
208: env = new ReferencedEnvelope(env.getMinX() - env.getWidth(),
209: env.getMaxX() + env.getWidth(), env.getMinY()
210: - env.getHeight(), env.getMaxY()
211: + env.getHeight(), env
212: .getCoordinateReferenceSystem());
213: map.setAreaOfInterest(env);
214: TestUtilites.showRender("testSimpleLineRender", renderer, 3000,
215: env);
216: }
217:
218: public void testSimplePointRender() throws Exception {
219: // same as the datasource test, load in some features into a table
220: System.err.println("starting rendering2DTest");
221:
222: DataStore ds = TestUtilites.getPoints();
223: FeatureSource source = ds
224: .getFeatureSource(ds.getTypeNames()[0]);
225: Style style = createTestStyle();
226:
227: MapContext map = new DefaultMapContext(
228: DefaultGeographicCRS.WGS84);
229: map.addLayer(source, style);
230:
231: ShapefileRenderer renderer = new ShapefileRenderer(map);
232: ReferencedEnvelope env = map.getLayerBounds();
233: env = new ReferencedEnvelope(env.getMinX() - 20,
234: env.getMaxX() + 20, env.getMinY() - 20,
235: env.getMaxY() + 20, env.getCoordinateReferenceSystem());
236: TestUtilites.showRender("testSimplePointRender", renderer,
237: 1000, env);
238: }
239:
240: public void testReprojection() throws Exception {
241: DataStore ds = TestUtilites.getPolygons();
242: FeatureSource source = ds
243: .getFeatureSource(ds.getTypeNames()[0]);
244: Style style = createTestStyle();
245: CoordinateReferenceSystem crs = ReferencingFactoryFinder
246: .getCRSFactory(null)
247: .createFromWKT(
248: "PROJCS[\"NAD_1983_UTM_Zone_10N\",GEOGCS[\"GCS_North_American_1983\",DATUM[\"D_North_American_1983\",TOWGS84[0,0,0,0,0,0,0],SPHEROID[\"GRS_1980\",6378137,298.257222101]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"False_Easting\",500000],PARAMETER[\"False_Northing\",0],PARAMETER[\"Central_Meridian\",-123],PARAMETER[\"Scale_Factor\",0.9996],PARAMETER[\"Latitude_Of_Origin\",0],UNIT[\"Meter\",1]]");
249:
250: MapContext map = new DefaultMapContext(crs);
251: map.addLayer(source, style);
252:
253: ShapefileRenderer renderer = new ShapefileRenderer(map);
254:
255: ReferencedEnvelope bounds = map.getLayerBounds();
256:
257: TestUtilites.showRender("testReprojection", renderer, 1000,
258: bounds);
259:
260: }
261:
262: public void testLineReprojection() throws Exception {
263: // same as the datasource test, load in some features into a table
264: System.err.println("starting rendering2DTest");
265:
266: ShapefileRenderer renderer = createLineRenderer(TestUtilites
267: .getLines());
268: ReferencedEnvelope env = renderer.getContext()
269: .getAreaOfInterest();
270:
271: // INTERACTIVE=true;
272: TestUtilites.showRender("testSimpleLineRender", renderer, 3000,
273: env);
274: }
275:
276: public void testNullCRSPoly() throws Exception {
277: // same as the datasource test, load in some features into a table
278: System.err.println("starting testLiteRender2");
279:
280: ShapefileDataStore ds = TestUtilites
281: .getDataStore("smallMultiPoly.shp");
282: FeatureSource source = ds
283: .getFeatureSource(ds.getTypeNames()[0]);
284: Style style = TestUtilites.createTestStyle(ds.getSchema()
285: .getTypeName(), null);
286:
287: MapContext map = new DefaultMapContext(
288: DefaultGeographicCRS.WGS84);
289: map.addLayer(source, style);
290:
291: ShapefileRenderer renderer = new ShapefileRenderer(map);
292: ReferencedEnvelope env = map.getLayerBounds();
293:
294: // renderer.setOptimizedDataLoadingEnabled(true);
295: env = new ReferencedEnvelope(env.getMinX() - 1,
296: env.getMaxX() + 1, env.getMinY() - 1,
297: env.getMaxY() + 1, env.getCoordinateReferenceSystem());
298: TestUtilites
299: .showRender("testReprojection", renderer, 1000, env);
300:
301: // System.in.read();
302: }
303:
304: public void testNullCRSLine() throws Exception {
305: // same as the datasource test, load in some features into a table
306: System.err.println("starting rendering2DTest");
307:
308: ShapefileRenderer renderer = createLineRenderer(TestUtilites
309: .getDataStore("lineNoCRS.shp"));
310: ReferencedEnvelope env = renderer.getContext()
311: .getAreaOfInterest();
312:
313: // INTERACTIVE=true;
314: TestUtilites.showRender("testSimpleLineRender", renderer, 3000,
315: env);
316: }
317:
318: public void testEnvelopePerformance() throws Exception {
319: ShapefileRenderer renderer = createLineRenderer(TestUtilites
320: .getLines());
321: MapContext context = renderer.getContext();
322:
323: TestUtilites.CountingRenderListener l1 = new TestUtilites.CountingRenderListener();
324: renderer.addRenderListener(l1);
325:
326: BufferedImage image = new BufferedImage(300, 300,
327: BufferedImage.TYPE_3BYTE_BGR);
328: renderer.paint(image.createGraphics(), new Rectangle(300, 300),
329: context.getAreaOfInterest());
330: renderer.removeRenderListener(l1);
331:
332: TestUtilites.CountingRenderListener l2 = new TestUtilites.CountingRenderListener();
333: renderer.addRenderListener(l2);
334:
335: ReferencedEnvelope old = context.getAreaOfInterest();
336: double w = old.getWidth() / 4;
337: double h = old.getHeight() / 4;
338: ReferencedEnvelope env = new ReferencedEnvelope(old
339: .getCenter(0)
340: - w, old.getCenter(0) + w, old.getCenter(1) - h, old
341: .getCenter(1)
342: + h, old.getCoordinateReferenceSystem());
343: renderer.paint(image.createGraphics(), new Rectangle(300, 300),
344: env);
345: assertTrue(l1.count > l2.count);
346: }
347:
348: /**
349: * DOCUMENT ME!
350: *
351: * @param ds DOCUMENT ME!
352: *
353: * @return
354: *
355: * @throws Exception
356: */
357: private ShapefileRenderer createLineRenderer(ShapefileDataStore ds)
358: throws Exception {
359: FeatureSource source = ds
360: .getFeatureSource(ds.getTypeNames()[0]);
361: Style style = TestUtilites.createTestStyle(null, ds
362: .getTypeNames()[0]);
363:
364: MapContext map = new DefaultMapContext(
365: DefaultGeographicCRS.WGS84);
366: map.addLayer(source, style);
367:
368: ShapefileRenderer renderer = new ShapefileRenderer(map);
369: ReferencedEnvelope env = map.getLayerBounds();
370: if (env.getCoordinateReferenceSystem() == null)
371: env = new ReferencedEnvelope((Envelope) env,
372: DefaultEngineeringCRS.GENERIC_2D);
373: map.setAreaOfInterest(env);
374:
375: return renderer;
376: }
377: }
|