001: package org.geotools.renderer.lite;
002:
003: import java.awt.Graphics2D;
004: import java.awt.Rectangle;
005: import java.awt.image.BufferedImage;
006:
007: import junit.framework.TestCase;
008:
009: import org.geotools.feature.AttributeType;
010: import org.geotools.feature.AttributeTypeFactory;
011: import org.geotools.feature.Feature;
012: import org.geotools.feature.FeatureCollection;
013: import org.geotools.feature.FeatureCollections;
014: import org.geotools.feature.FeatureType;
015: import org.geotools.feature.FeatureTypes;
016: import org.geotools.feature.IllegalAttributeException;
017: import org.geotools.geometry.jts.ReferencedEnvelope;
018: import org.geotools.map.DefaultMapContext;
019: import org.geotools.map.MapContext;
020: import org.geotools.referencing.CRS;
021: import org.geotools.referencing.crs.DefaultGeographicCRS;
022: import org.geotools.renderer.RenderListener;
023: import org.geotools.styling.Style;
024: import org.geotools.styling.StyleBuilder;
025: import org.opengis.referencing.crs.CoordinateReferenceSystem;
026:
027: import com.vividsolutions.jts.geom.Coordinate;
028: import com.vividsolutions.jts.geom.Envelope;
029: import com.vividsolutions.jts.geom.GeometryFactory;
030: import com.vividsolutions.jts.geom.LineString;
031:
032: /**
033: * Tests for rendering and reprojection
034: *
035: * @author wolf
036: *
037: */
038: public class ReprojectionTest extends TestCase {
039:
040: private FeatureType pointFeautureType;
041:
042: private GeometryFactory gf = new GeometryFactory();
043:
044: protected int errors;
045:
046: protected void setUp() throws Exception {
047: super .setUp();
048: AttributeType[] attributes = new AttributeType[] { AttributeTypeFactory
049: .newAttributeType("geom", LineString.class, false, 0,
050: null, DefaultGeographicCRS.WGS84) };
051: pointFeautureType = FeatureTypes.newFeatureType(attributes,
052: "Lines");
053: }
054:
055: public FeatureCollection createLineCollection() throws Exception {
056: FeatureCollection fc = FeatureCollections.newCollection();
057: fc.add(createLine(-177, 0, -177, 10));
058: fc.add(createLine(-177, 0, -200, 0));
059: fc.add(createLine(-177, 0, -177, 100));
060:
061: return fc;
062: }
063:
064: private Feature createLine(double x1, double y1, double x2,
065: double y2) throws IllegalAttributeException {
066: Coordinate[] coords = new Coordinate[] {
067: new Coordinate(x1, y1), new Coordinate(x2, y2) };
068: return pointFeautureType.create(new Object[] { gf
069: .createLineString(coords) });
070: }
071:
072: private Style createLineStyle() {
073: StyleBuilder sb = new StyleBuilder();
074: return sb.createStyle(sb.createLineSymbolizer());
075: }
076:
077: public void testSkipProjectionErrors() throws Exception {
078: // build map context
079: MapContext mapContext = new DefaultMapContext(
080: DefaultGeographicCRS.WGS84);
081: mapContext.addLayer(createLineCollection(), createLineStyle());
082:
083: // build projected envelope to work with (small one around the area of
084: // validity of utm zone 1, which being a Gauss projection is a vertical
085: // slice parallel to the central meridian, -177°)
086: ReferencedEnvelope reWgs = new ReferencedEnvelope(new Envelope(
087: -180, -170, 20, 40), DefaultGeographicCRS.WGS84);
088: CoordinateReferenceSystem utm1N = CRS.decode("EPSG:32601");
089: System.out.println(CRS.getGeographicBoundingBox(utm1N));
090: ReferencedEnvelope reUtm = reWgs.transform(utm1N, true);
091:
092: BufferedImage image = new BufferedImage(200, 200,
093: BufferedImage.TYPE_4BYTE_ABGR);
094:
095: // setup the renderer and listen for errors
096: StreamingRenderer sr = new StreamingRenderer();
097: sr.setContext(mapContext);
098: sr.addRenderListener(new RenderListener() {
099:
100: public void featureRenderer(Feature feature) {
101: }
102:
103: public void errorOccurred(Exception e) {
104: errors++;
105: }
106:
107: });
108: errors = 0;
109: sr.paint((Graphics2D) image.getGraphics(), new Rectangle(200,
110: 200), reUtm);
111: // we should get two errors since there are two features that cannot be
112: // projected
113: // but the renderer itself should not throw exceptions
114: assertEquals(2, errors);
115: }
116: }
|