[Schmitzm-commits] r1350 - in trunk: src/schmitzm/geotools/io src/schmitzm/io src_junit/schmitzm/geotools src_junit/schmitzm/geotools/io src_junit/schmitzm/swing testresources/shapes testresources/shapes/lineBrokenQix

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Dec 17 21:28:23 CET 2010


Author: alfonx
Date: 2010-12-17 21:27:53 +0100 (Fri, 17 Dec 2010)
New Revision: 1350

Added:
   trunk/src_junit/schmitzm/geotools/io/
   trunk/src_junit/schmitzm/geotools/io/GeoImportUtilTest.java
   trunk/testresources/shapes/lineBrokenQix/
   trunk/testresources/shapes/lineBrokenQix/Deu_Fluesse.dbf
   trunk/testresources/shapes/lineBrokenQix/Deu_Fluesse.fix
   trunk/testresources/shapes/lineBrokenQix/Deu_Fluesse.prj
   trunk/testresources/shapes/lineBrokenQix/Deu_Fluesse.qix
   trunk/testresources/shapes/lineBrokenQix/Deu_Fluesse.shp
   trunk/testresources/shapes/lineBrokenQix/Deu_Fluesse.shx
Modified:
   trunk/src/schmitzm/geotools/io/GeoImportUtil.java
   trunk/src/schmitzm/io/IOUtil.java
   trunk/src_junit/schmitzm/swing/TestingUtil.java
Log:
AS and GP can now handle Shapefiles with broken (too old) .qix indexes. Thanks to Olaf Knopp for reporting.

Modified: trunk/src/schmitzm/geotools/io/GeoImportUtil.java
===================================================================
--- trunk/src/schmitzm/geotools/io/GeoImportUtil.java	2010-12-17 00:20:36 UTC (rev 1349)
+++ trunk/src/schmitzm/geotools/io/GeoImportUtil.java	2010-12-17 20:27:53 UTC (rev 1350)
@@ -47,7 +47,9 @@
 import java.net.URL;
 import java.nio.charset.Charset;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
+import java.util.NoSuchElementException;
 import java.util.StringTokenizer;
 import java.util.Vector;
 import java.util.zip.ZipEntry;
@@ -66,7 +68,9 @@
 import org.geotools.data.DataStoreFinder;
 import org.geotools.data.DataUtilities;
 import org.geotools.data.shapefile.ShapefileDataStore;
+import org.geotools.data.shapefile.ShpFileType;
 import org.geotools.data.shapefile.ShpFiles;
+import org.geotools.data.shapefile.indexed.IndexedShapefileDataStore;
 import org.geotools.data.shapefile.shp.ShapefileReader;
 import org.geotools.factory.Hints;
 import org.geotools.feature.FeatureCollection;
@@ -538,7 +542,6 @@
 	 */
 	public static FeatureCollection<SimpleFeatureType, SimpleFeature> readFeaturesFromShapeURL(
 			URL shpURL, URL prjUrl) throws IOException {
-		// ShapefileDataStore store = new ShapefileDataStore(url);
 
 		DataStore store = readDataStoreFromShape(shpURL, prjUrl);
 
@@ -1594,4 +1597,44 @@
 		throw new NotImplementedException();
 	}
 
+	/**
+	 * Some Shapefiles have broken .qix files that make Geotools fail to read
+	 * the layer. THis method tests for that specific Exception.
+	 * 
+	 * "WARNUNG: Old qix file format; this file format is deprecated; It is
+	 * strongly recommended to regenerate it in new format."
+	 * 
+	 * @throws IOException
+	 */
+	public static boolean isOldBrokenQix(DataStore ds) throws IOException {
+
+		if (!(ds instanceof IndexedShapefileDataStore))
+			return false;
+		IndexedShapefileDataStore idxShpDs = (IndexedShapefileDataStore) ds;
+		if (!idxShpDs.isIndexed())
+			return false;
+		if (!idxShpDs.indexUseable(ShpFileType.QIX))
+			return false;
+
+		// We have a Shapefile with a .qix index. Test whether we can read at
+		// least one feature. It the QIX is broken, it will fail now.
+
+		FeatureCollection<SimpleFeatureType, SimpleFeature> features = idxShpDs
+				.getFeatureSource().getFeatures();
+		Iterator<SimpleFeature> it = features.iterator();
+		try {
+			while (it.hasNext()) {
+				SimpleFeature next = it.next();
+				return false;
+			}
+			return false;
+		} catch (NoSuchElementException e) {
+			return true;
+		} finally {
+			features.close(it);
+		}
+
+		// idxShpDs.buildQuadTree();
+	}
+
 }

Modified: trunk/src/schmitzm/io/IOUtil.java
===================================================================
--- trunk/src/schmitzm/io/IOUtil.java	2010-12-17 00:20:36 UTC (rev 1349)
+++ trunk/src/schmitzm/io/IOUtil.java	2010-12-17 20:27:53 UTC (rev 1350)
@@ -279,6 +279,7 @@
 	/**
 	 * @deprecated use getParentUrl()
 	 */
+	@Deprecated
 	public static URL getParentURL(URL url) throws MalformedURLException {
 		return getParentUrl(url);
 	}
@@ -662,6 +663,7 @@
 	 * 
 	 * @deprecated Use DataUtilties.url2file
 	 */
+	@Deprecated
 	public static File urlToFile(URL url) {
 		// // LOGGER.debug("\nconverting " + url.toString() + " to ");
 		// File f;

Added: trunk/src_junit/schmitzm/geotools/io/GeoImportUtilTest.java
===================================================================
--- trunk/src_junit/schmitzm/geotools/io/GeoImportUtilTest.java	2010-12-17 00:20:36 UTC (rev 1349)
+++ trunk/src_junit/schmitzm/geotools/io/GeoImportUtilTest.java	2010-12-17 20:27:53 UTC (rev 1350)
@@ -0,0 +1,56 @@
+package schmitzm.geotools.io;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import org.geotools.data.DataStore;
+import org.geotools.data.FeatureSource;
+import org.geotools.data.shapefile.indexed.IndexedShapefileDataStore;
+import org.geotools.feature.FeatureCollection;
+import org.junit.Test;
+import org.opengis.feature.simple.SimpleFeature;
+import org.opengis.feature.simple.SimpleFeatureType;
+
+import schmitzm.junit.TestingClass;
+import schmitzm.swing.TestingUtil;
+
+public class GeoImportUtilTest extends TestingClass {
+
+	@Test
+	public void testOpenBorkenQixShapefile1() throws IOException {
+		FeatureSource<SimpleFeatureType, SimpleFeature> fs = TestingUtil.TestDatasetsVector.lineBrokenQuix
+				.getFeatureSource();
+
+		FeatureCollection<SimpleFeatureType, SimpleFeature> features = fs
+				.getFeatures();
+		Iterator<SimpleFeature> it = features.iterator();
+		try {
+			while (it.hasNext()) {
+				SimpleFeature next = it.next();
+				fail("We expect this to crash with a WARNUNG: Old qix file format; this file format is deprecated; It is strongly recommended to regenerate it in new format.");
+			}
+		} catch (NoSuchElementException e) {
+			// This is expected
+		} finally {
+			features.close(it);
+		}
+	}
+
+	@Test
+	public void testOpenBorkenQixShapefile2() throws IOException {
+		DataStore ds = TestingUtil.TestDatasetsVector.lineBrokenQuix
+				.getDataStore();
+
+		final IndexedShapefileDataStore idxShpDs = (IndexedShapefileDataStore) ds;
+		assertTrue(GeoImportUtil.isOldBrokenQix(idxShpDs));
+
+		idxShpDs.buildQuadTree();
+
+		assertFalse(GeoImportUtil.isOldBrokenQix(idxShpDs));
+	}
+}


Property changes on: trunk/src_junit/schmitzm/geotools/io/GeoImportUtilTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id URL
Name: svn:eol-style
   + native

Modified: trunk/src_junit/schmitzm/swing/TestingUtil.java
===================================================================
--- trunk/src_junit/schmitzm/swing/TestingUtil.java	2010-12-17 00:20:36 UTC (rev 1349)
+++ trunk/src_junit/schmitzm/swing/TestingUtil.java	2010-12-17 20:27:53 UTC (rev 1350)
@@ -92,8 +92,14 @@
 		 * A tiny Shapefile with an .sld that contains a QuantilesClassification
 		 * with manually adapted colors
 		 */
-		polygonSnow("/shapes/polygonSnowShape/polygonLayerSnow.shp");
+		polygonSnow("/shapes/polygonSnowShape/polygonLayerSnow.shp"),
 
+		/**
+		 * A tiny Shapefile with an .sld that contains a QuantilesClassification
+		 * with manually adapted colors
+		 */
+		lineBrokenQuix("/shapes/lineBrokenQix/Deu_Fluesse.shp");
+
 		private final String resLoc;
 
 		private TestDatasetsVector(String resLoc) {

Added: trunk/testresources/shapes/lineBrokenQix/Deu_Fluesse.dbf
===================================================================
(Binary files differ)


Property changes on: trunk/testresources/shapes/lineBrokenQix/Deu_Fluesse.dbf
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: trunk/testresources/shapes/lineBrokenQix/Deu_Fluesse.fix
===================================================================
(Binary files differ)


Property changes on: trunk/testresources/shapes/lineBrokenQix/Deu_Fluesse.fix
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: trunk/testresources/shapes/lineBrokenQix/Deu_Fluesse.prj
===================================================================
--- trunk/testresources/shapes/lineBrokenQix/Deu_Fluesse.prj	2010-12-17 00:20:36 UTC (rev 1349)
+++ trunk/testresources/shapes/lineBrokenQix/Deu_Fluesse.prj	2010-12-17 20:27:53 UTC (rev 1350)
@@ -0,0 +1 @@
+GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]
\ No newline at end of file

Added: trunk/testresources/shapes/lineBrokenQix/Deu_Fluesse.qix
===================================================================
(Binary files differ)


Property changes on: trunk/testresources/shapes/lineBrokenQix/Deu_Fluesse.qix
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: trunk/testresources/shapes/lineBrokenQix/Deu_Fluesse.shp
===================================================================
(Binary files differ)


Property changes on: trunk/testresources/shapes/lineBrokenQix/Deu_Fluesse.shp
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: trunk/testresources/shapes/lineBrokenQix/Deu_Fluesse.shx
===================================================================
(Binary files differ)


Property changes on: trunk/testresources/shapes/lineBrokenQix/Deu_Fluesse.shx
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream



More information about the Schmitzm-commits mailing list