001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-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: * Created on April 12, 2002, 1:18 PM
017: */
018: package org.geotools.styling;
019:
020: import junit.framework.Test;
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: import org.geotools.data.memory.MemoryDataStore;
025: import org.geotools.factory.CommonFactoryFinder;
026: import org.geotools.feature.AttributeType;
027: import org.geotools.feature.AttributeTypeFactory;
028: import org.geotools.feature.Feature;
029: import org.geotools.feature.FeatureCollection;
030: import org.geotools.feature.FeatureType;
031: import org.geotools.feature.FeatureTypeFactory;
032: import org.opengis.filter.FilterFactory;
033: import org.opengis.filter.expression.PropertyName;
034:
035: import com.vividsolutions.jts.geom.Coordinate;
036: import com.vividsolutions.jts.geom.GeometryFactory;
037: import com.vividsolutions.jts.geom.Point;
038:
039: /**
040: * Test for text symbols.
041: *
042: * @author jamesm
043: *
044: * @task REVISIT: redo the Map stuff - I commented it out since DefaultMap is
045: * deprecated - cholmes.
046: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/test/java/org/geotools/styling/TextSymbolTest.java $
047: */
048: public class TextSymbolTest extends TestCase {
049: private static final FilterFactory filterFactory = CommonFactoryFinder
050: .getFilterFactory(null);
051:
052: /** factory for attributes */
053: private static AttributeTypeFactory attFactory = AttributeTypeFactory
054: .defaultInstance();
055: String dataFolder;
056:
057: public TextSymbolTest(java.lang.String testName) {
058: super (testName);
059: dataFolder = System.getProperty("dataFolder");
060:
061: if (dataFolder == null) {
062: //then we are being run by maven
063: dataFolder = System.getProperty("basedir");
064: dataFolder += "/tests/unit/testData";
065: }
066: }
067:
068: public static void main(java.lang.String[] args) {
069: junit.textui.TestRunner.run(suite());
070: }
071:
072: public static Test suite() {
073: TestSuite suite = new TestSuite(TextSymbolTest.class);
074:
075: return suite;
076: }
077:
078: public void testRender() throws Exception {
079: //System.out.println("\n\nTextSymbolTest\n");
080:
081: // Request extent
082: GeometryFactory geomFac = new GeometryFactory();
083: MemoryDataStore data = new MemoryDataStore();
084: AttributeType[] pointAttribute = new AttributeType[4];
085: pointAttribute[0] = AttributeTypeFactory.newAttributeType(
086: "centre", com.vividsolutions.jts.geom.Point.class);
087: pointAttribute[1] = AttributeTypeFactory.newAttributeType(
088: "size", Double.class);
089: pointAttribute[2] = AttributeTypeFactory.newAttributeType(
090: "rotation", Double.class);
091: pointAttribute[3] = AttributeTypeFactory.newAttributeType(
092: "symbol", String.class);
093:
094: FeatureTypeFactory feaTypeFactory = FeatureTypeFactory
095: .newInstance("test");
096: feaTypeFactory.addTypes(pointAttribute);
097: feaTypeFactory.setName("testPoint");
098:
099: FeatureType pointType = feaTypeFactory.getFeatureType();
100:
101: //FlatFeatureFactory pointFac = feaTypeFactory.(pointType);
102: Point point;
103: Feature pointFeature;
104:
105: // load font
106: String[] symbol = { "\uF04A", "\uF04B", "\uF059", "\uF05A",
107: "\uF06B", "\uF06C", "\uF06E" };
108: double size = 16;
109: double rotation = 0.0;
110: int rows = 8;
111:
112: for (int j = 0; j < rows; j++) {
113: for (int i = 0; i < symbol.length; i++) {
114: point = makeSamplePoint(geomFac,
115: ((double) i * 5.0) + 5.0, 5.0 + (j * 5));
116: pointFeature = pointType.create(new Object[] { point,
117: new Double(size), new Double(rotation),
118: symbol[i] });
119: data.addFeature(pointFeature);
120: }
121:
122: size += 2;
123: rotation += 45;
124: }
125:
126: String typeName = data.getTypeNames()[0];
127: FeatureCollection ft = data.getFeatureSource(typeName)
128: .getFeatures();
129:
130: //REVISIT: Removed since it is deprecated, not sure what this test is
131: //is doing, what should replace it. If someone with more knowledge of
132: //this stuff could update the tests that'd be great - ch.
133: //org.geotools.map.Map map = new DefaultMap();
134: //The following is complex, and should be built from
135: //an SLD document and not by hand
136: org.geotools.styling.FontImpl font = new org.geotools.styling.FontImpl();
137:
138: font.setFontFamily(filterFactory.literal(dataFolder
139: + "geog.ttf"));
140: font.setFontSize(filterFactory.property("size"));
141:
142: PropertyName symbExpr = filterFactory.property("symbol");
143: TextMark textMark = new TextMarkImpl(font, symbExpr);
144:
145: org.geotools.styling.FontImpl font2 = new org.geotools.styling.FontImpl();
146: font2.setFontFamily(filterFactory
147: .literal("MapInfo Cartographic"));
148: font2.setFontSize(filterFactory.property("size"));
149: textMark.addFont(font2);
150:
151: org.geotools.styling.FontImpl font3 = new org.geotools.styling.FontImpl();
152: font3.setFontFamily(filterFactory.literal("ESRI Cartography"));
153: font3.setFontSize(filterFactory.property("size"));
154: textMark.addFont(font3);
155:
156: GraphicImpl graphic = new GraphicImpl();
157: graphic.addSymbol(textMark);
158:
159: PointSymbolizerImpl pointsym = new PointSymbolizerImpl();
160: pointsym.setGeometryPropertyName("centre");
161: pointsym.setGraphic(graphic);
162:
163: RuleImpl rule = new RuleImpl();
164: rule.setSymbolizers(new Symbolizer[] { pointsym });
165:
166: FeatureTypeStyleImpl fts = new FeatureTypeStyleImpl();
167: fts.addRule(rule);
168: fts.setFeatureTypeName("testPoint");
169:
170: StyleImpl style = new StyleImpl();
171: style.addFeatureTypeStyle(fts);
172:
173: //map.addFeatureTable(ft,style);
174:
175: /*Java2DRenderer renderer = new org.geotools.renderer.Java2DRenderer();
176: Frame frame = new Frame("text symbol test");
177: frame.addWindowListener(new WindowAdapter() {
178: public void windowClosing(WindowEvent e) {e.getWindow().dispose(); }
179: });
180: Panel p = new Panel();
181: frame.add(p);
182: frame.setSize(300,300);
183: frame.setVisible(true);
184: renderer.setOutput(p.getGraphics(),p.getBounds());
185: map.render(renderer,ex.getBounds());//and finaly try and draw it!
186:
187: int w = 400, h =400;
188: BufferedImage image = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
189: Graphics g = image.getGraphics();
190: g.setColor(Color.white);
191: g.fillRect(0,0,w,h);
192: renderer.setOutput(g,new java.awt.Rectangle(0,0,w,h));
193: map.render(renderer,ex.getBounds());//and finaly try and draw it!
194: File file = new File(dataFolder, "TextSymbolTest.jpg");
195: FileOutputStream out = new FileOutputStream(file);
196: ImageIO.write(image, "JPEG", out);
197: Thread.sleep(5000);
198: frame.dispose();*/
199: }
200:
201: private Point makeSamplePoint(final GeometryFactory geomFac,
202: double x, double y) {
203: Coordinate c = new Coordinate(x, y);
204: Point point = geomFac.createPoint(c);
205:
206: return point;
207: }
208: }
|