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.renderer.shape;
018:
019: import java.awt.Rectangle;
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.OutputStream;
026: import java.util.logging.Level;
027: import java.util.logging.Logger;
028:
029: import junit.framework.TestCase;
030:
031: import org.geotools.TestData;
032: import org.geotools.data.DataUtilities;
033: import org.geotools.data.DefaultTransaction;
034: import org.geotools.data.FeatureStore;
035: import org.geotools.data.Transaction;
036: import org.geotools.data.shapefile.ShapefileDataStore;
037: import org.geotools.data.shapefile.ShapefileRendererUtil;
038: import org.geotools.data.shapefile.dbf.IndexedDbaseFileReader;
039: import org.geotools.data.shapefile.shp.ShapefileReader;
040: import org.geotools.feature.Feature;
041: import org.geotools.feature.FeatureCollection;
042: import org.geotools.feature.FeatureIterator;
043: import org.geotools.feature.FeatureType;
044: import org.geotools.filter.FidFilter;
045: import org.geotools.filter.Filter;
046: import org.geotools.map.DefaultMapContext;
047: import org.geotools.map.MapContext;
048: import org.geotools.referencing.operation.transform.IdentityTransform;
049: import org.geotools.renderer.RenderListener;
050: import org.geotools.styling.Style;
051: import org.opengis.referencing.operation.MathTransform;
052:
053: import com.vividsolutions.jts.geom.Envelope;
054:
055: /**
056: * Tests ShapeRenderer class
057: *
058: * @author jeichar
059: * @since 2.1.x
060: * @source $URL:
061: * http://svn.geotools.org/geotools/branches/2.2.x/ext/shaperenderer/test/org/geotools/renderer/shape/ShapeRendererTest.java $
062: */
063: public class ShapeRendererTest extends TestCase {
064: private static final boolean INTERACTIVE = false;
065:
066: private static final MathTransform IDENTITY = IdentityTransform
067: .create(2);
068:
069: private File shp2;
070:
071: private File shx2;
072:
073: private File prj2;
074:
075: private File dbf2;
076:
077: private String typename;
078:
079: private File directory;
080:
081: protected void setUp() throws Exception {
082: org.geotools.util.logging.Logging.getLogger("org.geotools")
083: .setLevel(Level.FINE);
084: File shp = new File(TestData.url(Rendering2DTest.class,
085: "theme1.shp").getFile());
086: File shx = new File(TestData.url(Rendering2DTest.class,
087: "theme1.shx").getFile());
088: File prj = new File(TestData.url(Rendering2DTest.class,
089: "theme1.prj").getFile());
090: File dbf = new File(TestData.url(Rendering2DTest.class,
091: "theme1.dbf").getFile());
092:
093: directory = TestData.file(Rendering2DTest.class, ".");
094:
095: shp2 = File.createTempFile("theme2", ".shp", directory);
096: typename = shp2.getName().substring(0,
097: shp2.getName().lastIndexOf("."));
098: shx2 = new File(directory, typename + ".shx");
099: prj2 = new File(directory, typename + ".prj");
100: dbf2 = new File(directory, typename + ".dbf");
101:
102: copy(shp, shp2);
103: copy(shx, shx2);
104: copy(prj, prj2);
105: copy(dbf, dbf2);
106: }
107:
108: protected void tearDown() throws Exception {
109: dbf2.deleteOnExit();
110: shx2.deleteOnExit();
111: shp2.deleteOnExit();
112: prj2.deleteOnExit();
113: File fix = new File(directory, typename + ".fix");
114: File qix = new File(directory, typename + ".qix");
115:
116: if (shp2.exists() && !shp2.delete())
117: System.out.println("failed to delete: "
118: + shp2.getAbsolutePath());
119: if (shx2.exists() && !shx2.delete())
120: System.out.println("failed to delete: "
121: + shx2.getAbsolutePath());
122:
123: if (prj2.exists() && !prj2.delete())
124: System.out.println("failed to delete: "
125: + prj2.getAbsolutePath());
126:
127: if (dbf2.exists() && !dbf2.delete())
128: System.out.println("failed to delete: "
129: + dbf2.getAbsolutePath());
130:
131: if (fix.exists() && !fix.delete()) {
132: fix.deleteOnExit();
133: System.out.println("failed to delete: "
134: + fix.getAbsolutePath());
135: }
136: if (qix.exists() && !qix.delete()) {
137: qix.deleteOnExit();
138: System.out.println("failed to delete: "
139: + qix.getAbsolutePath());
140: }
141: }
142:
143: void copy(File src, File dst) throws IOException {
144: InputStream in = null;
145: OutputStream out = null;
146:
147: try {
148: in = new FileInputStream(src);
149: out = new FileOutputStream(dst, false);
150:
151: // Transfer bytes from in to out
152: byte[] buf = new byte[1024];
153: int len;
154:
155: while ((len = in.read(buf)) > 0) {
156: out.write(buf, 0, len);
157: }
158: } finally {
159: if (in != null) {
160: in.close();
161: }
162:
163: if (out != null) {
164: out.close();
165: }
166: }
167: }
168:
169: public void testCreateFeature() throws Exception {
170: ShapefileRenderer renderer = new ShapefileRenderer(null);
171: Style style = LabelingTest.loadStyle("LineStyle.sld");
172: ShapefileDataStore ds = TestUtilites.getDataStore(shp2
173: .getName());
174: IndexedDbaseFileReader reader = ShapefileRendererUtil
175: .getDBFReader(ds);
176: renderer.dbfheader = reader.getHeader();
177: FeatureType type = renderer.createFeatureType(null, style, ds);
178: assertEquals("NAME", type.getAttributeType(0).getName());
179: assertEquals(2, type.getAttributeCount());
180: Envelope bounds = ds.getFeatureSource().getBounds();
181: ShapefileReader shpReader = ShapefileRendererUtil.getShpReader(
182: ds, bounds, new Rectangle(0, 0,
183: (int) bounds.getWidth(), (int) bounds
184: .getHeight()), IDENTITY, false, false);
185: Feature feature = renderer.createFeature(type, shpReader
186: .nextRecord(), reader, "id");
187: shpReader.close();
188: reader.close();
189:
190: assertEquals("id", feature.getID());
191: assertEquals("dave street", feature.getAttribute(0));
192: }
193:
194: public void testRemoveTransaction() throws Exception {
195: ShapefileDataStore ds = TestUtilites.getDataStore(shp2
196: .getName());
197: Style st = TestUtilites.createTestStyle(null, typename);
198: final FeatureStore store = (FeatureStore) ds.getFeatureSource();
199: Transaction t = new DefaultTransaction();
200: store.setTransaction(t);
201: FeatureCollection collection = store.getFeatures();
202: FeatureIterator iter = collection.features();
203: FidFilter createFidFilter = TestUtilites.filterFactory
204: .createFidFilter(iter.next().getID());
205: collection.close(iter);
206: store.removeFeatures(createFidFilter);
207:
208: MapContext context = new DefaultMapContext();
209: context.addLayer(store, st);
210: ShapefileRenderer renderer = new ShapefileRenderer(context);
211: TestUtilites.CountingRenderListener listener = new TestUtilites.CountingRenderListener();
212: renderer.addRenderListener(listener);
213: Envelope env = context.getLayerBounds();
214: int boundary = 7;
215: TestUtilites.INTERACTIVE = INTERACTIVE;
216: env = new Envelope(env.getMinX() - boundary, env.getMaxX()
217: + boundary, env.getMinY() - boundary, env.getMaxY()
218: + boundary);
219: TestUtilites.showRender("testTransaction", renderer, 2000, env);
220: assertEquals(2, listener.count);
221: t.commit();
222:
223: collection = store.getFeatures();
224: iter = collection.features();
225: final Feature feature = iter.next();
226: collection.close(iter);
227:
228: // now add a new feature new fid should be theme2.4 remove it and assure
229: // that it is not rendered
230: store.addFeatures(DataUtilities
231: .collection(new Feature[] { store.getSchema().create(
232: feature.getAttributes(new Object[feature
233: .getNumberOfAttributes()]),
234: "newFeature") })); //$NON-NLS-1$
235: t.commit();
236: listener.count = 0;
237: TestUtilites.showRender("testTransaction", renderer, 2000, env);
238: assertEquals(3, listener.count);
239:
240: iter = store.getFeatures().features();
241: Feature last = null;
242: while (iter.hasNext()) {
243: last = iter.next();
244: }
245: iter.close();
246:
247: store.removeFeatures(TestUtilites.filterFactory
248: .createFidFilter(last.getID()));
249:
250: listener.count = 0;
251: TestUtilites.showRender("testTransaction", renderer, 2000, env);
252: assertEquals(2, listener.count);
253:
254: }
255:
256: public void testAddTransaction() throws Exception {
257: final ShapefileDataStore ds = TestUtilites.getDataStore(shp2
258: .getName());
259: Style st = TestUtilites.createTestStyle(null, typename);
260: FeatureStore store = (FeatureStore) ds.getFeatureSource();
261: Transaction t = new DefaultTransaction();
262: store.setTransaction(t);
263: FeatureCollection collection = store.getFeatures();
264: FeatureIterator iter = collection.features();
265: final Feature feature = iter.next();
266: collection.close(iter);
267:
268: store.addFeatures(DataUtilities.collection(new Feature[] { ds
269: .getSchema().create(
270: feature.getAttributes(new Object[feature
271: .getNumberOfAttributes()]),
272: "newFeature") }));
273:
274: MapContext context = new DefaultMapContext();
275: context.addLayer(store, st);
276: ShapefileRenderer renderer = new ShapefileRenderer(context);
277: TestUtilites.CountingRenderListener listener = new TestUtilites.CountingRenderListener();
278: renderer.addRenderListener(listener);
279: Envelope env = context.getLayerBounds();
280: int boundary = 7;
281: TestUtilites.INTERACTIVE = INTERACTIVE;
282: env = new Envelope(env.getMinX() - boundary, env.getMaxX()
283: + boundary, env.getMinY() - boundary, env.getMaxY()
284: + boundary);
285: TestUtilites.showRender("testTransaction", renderer, 2000, env);
286:
287: assertEquals(4, listener.count);
288: }
289:
290: public void testModifyTransaction() throws Exception {
291: ShapefileDataStore ds = TestUtilites.getDataStore(shp2
292: .getName());
293: Style st = TestUtilites.createTestStyle(null, typename);
294: FeatureStore store = (FeatureStore) ds.getFeatureSource();
295: Transaction t = new DefaultTransaction();
296: store.setTransaction(t);
297: store.modifyFeatures(ds.getSchema().getAttributeType("NAME"),
298: "bleep", Filter.NONE);
299:
300: MapContext context = new DefaultMapContext();
301: context.addLayer(store, st);
302: ShapefileRenderer renderer = new ShapefileRenderer(context);
303: TestUtilites.CountingRenderListener listener = new TestUtilites.CountingRenderListener();
304: renderer.addRenderListener(listener);
305: renderer.addRenderListener(new RenderListener() {
306:
307: public void featureRenderer(Feature feature) {
308: assertEquals("bleep", feature.getAttribute("NAME"));
309: }
310:
311: public void errorOccurred(Exception e) {
312: assertFalse(true);
313: }
314:
315: });
316: Envelope env = context.getLayerBounds();
317: int boundary = 7;
318: TestUtilites.INTERACTIVE = INTERACTIVE;
319: env = new Envelope(env.getMinX() - boundary, env.getMaxX()
320: + boundary, env.getMinY() - boundary, env.getMaxY()
321: + boundary);
322: TestUtilites.showRender("testTransaction", renderer, 2000, env);
323:
324: assertEquals(3, listener.count);
325: }
326:
327: }
|