001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, 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; either
009: * version 2.1 of the License, or (at your option) any later version.
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: package org.geotools.renderer.lite;
017:
018: import java.io.IOException;
019:
020: import junit.framework.TestCase;
021:
022: import org.geotools.data.memory.MemoryDataStore;
023: import org.geotools.feature.AttributeType;
024: import org.geotools.feature.AttributeTypeFactory;
025: import org.geotools.feature.Feature;
026: import org.geotools.feature.FeatureCollection;
027: import org.geotools.feature.FeatureType;
028: import org.geotools.feature.FeatureTypes;
029: import org.geotools.referencing.crs.DefaultGeographicCRS;
030: import org.geotools.test.TestData;
031: import org.geotools.styling.SLDParser;
032: import org.geotools.styling.Style;
033: import org.geotools.styling.StyleFactory;
034: import org.geotools.styling.StyleFactoryFinder;
035: import org.opengis.referencing.crs.CoordinateReferenceSystem;
036:
037: import com.vividsolutions.jts.geom.Coordinate;
038: import com.vividsolutions.jts.geom.GeometryFactory;
039: import com.vividsolutions.jts.geom.LineString;
040: import com.vividsolutions.jts.geom.LinearRing;
041: import com.vividsolutions.jts.geom.Point;
042: import com.vividsolutions.jts.geom.Polygon;
043:
044: /**
045: * Tests the StreamingRenderer labelling algorithms
046: *
047: * @author jeichar
048: * @since 0.9.0
049: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/render/src/test/java/org/geotools/renderer/lite/LabelingTest.java $
050: */
051: public class LabelingTest extends TestCase {
052:
053: private long timout = 3000;
054: private static final int CENTERX = 160;
055: private static final int CENTERY = 40;
056:
057: /*
058: * @see TestCase#setUp()
059: */
060: protected void setUp() throws Exception {
061: super .setUp();
062: }
063:
064: /*
065: * @see TestCase#tearDown()
066: */
067: protected void tearDown() throws Exception {
068: super .tearDown();
069: }
070:
071: public void testPointLabeling() throws Exception {
072: // FeatureCollection collection=createPointFeatureCollection();
073: // Style style=loadStyle("PointStyle.sld");
074: // assertNotNull(style);
075: // MapContext map = new DefaultMapContext();
076: // map.addLayer(collection, style);
077: //
078: // StreamingRenderer renderer=new StreamingRenderer();
079: // renderer.setContext(map);
080: // Envelope env = map.getLayerBounds();
081: // int boundary=10;
082: // env = new Envelope(env.getMinX() - boundary, env.getMaxX() + boundary,
083: // env.getMinY() - boundary, env.getMaxY() + boundary);
084: // Rendering2DTest.INTERACTIVE=INTERACTIVE;
085: // Rendering2DTest.showRender("testPointLabeling", renderer, timout, env);
086: }
087:
088: private Style loadStyle(String sldFilename) throws IOException {
089: StyleFactory factory = StyleFactoryFinder.createStyleFactory();
090:
091: java.net.URL surl = TestData.getResource(this , sldFilename);
092: SLDParser stylereader = new SLDParser(factory, surl);
093:
094: Style style = stylereader.readXML()[0];
095: return style;
096: }
097:
098: private FeatureCollection createPointFeatureCollection()
099: throws Exception {
100: AttributeType[] types = new AttributeType[2];
101:
102: GeometryFactory geomFac = new GeometryFactory();
103: CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
104:
105: MemoryDataStore data = new MemoryDataStore();
106: data.addFeature(createPointFeature(0, 0, "LongLabel1", crs,
107: geomFac, types));
108: data.addFeature(createPointFeature(2, 2, "LongLabel2", crs,
109: geomFac, types));
110: data.addFeature(createPointFeature(0, 2, "LongLabel3", crs,
111: geomFac, types));
112: // data.addFeature(createPointFeature(2,0,"Label4",crs, geomFac, types));
113: data.addFeature(createPointFeature(0, 4, "LongLabel6", crs,
114: geomFac, types));
115:
116: return data.getFeatureSource(Rendering2DTest.POINT)
117: .getFeatures();
118: }
119:
120: private Feature createPointFeature(int x, int y, String name,
121: CoordinateReferenceSystem crs, GeometryFactory geomFac,
122: AttributeType[] types) throws Exception {
123: Coordinate c = new Coordinate(x, y);
124: Point point = geomFac.createPoint(c);
125: if (crs != null)
126: types[0] = AttributeTypeFactory.newAttributeType("point",
127: point.getClass(), false, 0, null, crs);
128: else
129: types[0] = AttributeTypeFactory.newAttributeType("centre",
130: point.getClass());
131: types[1] = AttributeTypeFactory.newAttributeType("name",
132: String.class);
133: FeatureType pointType = FeatureTypes.newFeatureType(types,
134: Rendering2DTest.POINT);
135: Feature pointFeature = pointType.create(new Object[] { point,
136: name });
137: return pointFeature;
138: }
139:
140: public void testLineLabeling() throws Exception {
141: // FeatureCollection collection=createLineFeatureCollection();
142: // Style style=loadStyle("LineStyle.sld");
143: // assertNotNull(style);
144: // MapContext map = new DefaultMapContext();
145: // map.addLayer(collection, style);
146: //
147: // StreamingRenderer renderer=new StreamingRenderer();
148: // renderer.setContext(map);
149: // Envelope env = map.getLayerBounds();
150: // int boundary=10;
151: // Rendering2DTest.INTERACTIVE=INTERACTIVE;
152: // env = new Envelope(env.getMinX() - boundary, env.getMaxX() + boundary,
153: // env.getMinY() - boundary, env.getMaxY() + boundary);
154: // Rendering2DTest.showRender("testLineLabeling", renderer, timout, env);
155: }
156:
157: private FeatureCollection createLineFeatureCollection()
158: throws Exception {
159: AttributeType[] types = new AttributeType[2];
160:
161: GeometryFactory geomFac = new GeometryFactory();
162: CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
163:
164: MemoryDataStore data = new MemoryDataStore();
165: data.addFeature(createLineFeature(10, 0, 0, 10, "LongLabel1",
166: crs, geomFac, types));
167: data.addFeature(createLineFeature(10, 10, 0, 0, "LongLabel2",
168: crs, geomFac, types));
169: // data.addFeature(createPointFeature(0,2,"LongLabel3",crs, geomFac, types));
170: // data.addFeature(createPointFeature(2,0,"Label4",crs, geomFac, types));
171: // data.addFeature(createPointFeature(0,4,"LongLabel6",crs, geomFac, types));
172:
173: return data.getFeatureSource(Rendering2DTest.LINE)
174: .getFeatures();
175: }
176:
177: private Feature createLineFeature(int startx, int starty, int endx,
178: int endy, String name, CoordinateReferenceSystem crs,
179: GeometryFactory geomFac, AttributeType[] types)
180: throws Exception {
181: Coordinate[] c = new Coordinate[] {
182: new Coordinate(startx, starty),
183: new Coordinate(endx, endy) };
184: LineString line = geomFac.createLineString(c);
185: if (crs != null)
186: types[0] = AttributeTypeFactory.newAttributeType("line",
187: line.getClass(), false, 0, null, crs);
188: else
189: types[0] = AttributeTypeFactory.newAttributeType("centre",
190: line.getClass());
191: types[1] = AttributeTypeFactory.newAttributeType("name",
192: String.class);
193: FeatureType pointType = FeatureTypes.newFeatureType(types,
194: Rendering2DTest.LINE);
195: Feature pointFeature = pointType.create(new Object[] { line,
196: name });
197:
198: return pointFeature;
199: }
200:
201: public void testPolyLabeling() throws Exception {
202: // FeatureCollection collection=createPolyFeatureCollection();
203: // Style style=loadStyle("PolyStyle.sld");
204: // assertNotNull(style);
205: // MapContext map = new DefaultMapContext();
206: // map.addLayer(collection, style);
207: // StreamingRenderer renderer=new StreamingRenderer();
208: // renderer.setContext(map);
209: // Envelope env = map.getLayerBounds();
210: // int boundary=10;
211: // env = new Envelope(env.getMinX() - boundary, env.getMaxX() + boundary,
212: // env.getMinY() - boundary, env.getMaxY() + boundary);
213: // Rendering2DTest.INTERACTIVE=INTERACTIVE;
214: // Rendering2DTest.showRender("testPolyLabeling", renderer, timout, env);
215: }
216:
217: private FeatureCollection createPolyFeatureCollection()
218: throws Exception {
219: AttributeType[] types = new AttributeType[2];
220:
221: GeometryFactory geomFac = new GeometryFactory();
222: CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
223:
224: MemoryDataStore data = new MemoryDataStore();
225: data.addFeature(createPolyFeature(CENTERX + 5, CENTERY + 0,
226: CENTERX + 10, CENTERY + 10, "LongLabel1", crs, geomFac,
227: types));
228: data.addFeature(createPolyFeature(CENTERX + 0, CENTERY + 0,
229: CENTERX + 10, CENTERY + 10, "LongLabel2", crs, geomFac,
230: types));
231:
232: return data.getFeatureSource(Rendering2DTest.POLYGON)
233: .getFeatures();
234: }
235:
236: private Feature createPolyFeature(int startx, int starty,
237: int width, int height, String name,
238: CoordinateReferenceSystem crs, GeometryFactory geomFac,
239: AttributeType[] types) throws Exception {
240: Coordinate[] c = new Coordinate[] {
241: new Coordinate(startx, starty),
242: new Coordinate(startx + width, starty),
243: new Coordinate(startx + width, starty + height),
244: new Coordinate(startx, starty), };
245: LinearRing line = geomFac.createLinearRing(c);
246: Polygon poly = geomFac.createPolygon(line, null);
247: if (crs != null)
248: types[0] = AttributeTypeFactory.newAttributeType("polygon",
249: poly.getClass(), false, 0, null, crs);
250: else
251: types[0] = AttributeTypeFactory.newAttributeType("centre",
252: line.getClass());
253: types[1] = AttributeTypeFactory.newAttributeType("name",
254: String.class);
255: FeatureType pointType = FeatureTypes.newFeatureType(types,
256: Rendering2DTest.POLYGON);
257: Feature pointFeature = pointType.create(new Object[] { poly,
258: name });
259:
260: return pointFeature;
261: }
262: }
|