Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

GEOT-4236

  • Loading branch information...
commit 5f6c25284c4336c7f3c7e04e4f6a41753f153347 1 parent 2a7194e
@simboss simboss authored
View
23 modules/plugin/geotiff/src/main/java/org/geotools/gce/geotiff/GeoTiffReader.java
@@ -124,6 +124,12 @@
/** Logger for the {@link GeoTiffReader} class. */
private Logger LOGGER = org.geotools.util.logging.Logging.getLogger(GeoTiffReader.class.toString());
+
+ /** With this java switch I can control whether or not an external PRJ files takes precedence over the internal CRS definition*/
+ public static final String OVERRIDE_CRS_SWITCH = "org.geotools.gce.geotiff.override.crs";
+
+ /** With this java switch I can control whether or not an external PRJ files takes precedence over the internal CRS definition*/
+ static boolean OVERRIDE_INNER_CRS=Boolean.valueOf(System.getProperty(GeoTiffReader.OVERRIDE_CRS_SWITCH, "False"));
/** SPI for creating tiff readers in ImageIO tools */
private final static TIFFImageReaderSpi READER_SPI = new TIFFImageReaderSpi();
@@ -296,12 +302,19 @@ private void getHRInfo(Hints hints) throws DataSourceException {
if (LOGGER.isLoggable(Level.FINE))
LOGGER.log(Level.FINE, "Using forced coordinate reference system");
} else {
- // check metadata first
- if (metadata.hasGeoKey()&& gtcs != null)
- crs = gtcs.createCoordinateSystem(metadata);
+
+ // check external prj first
+ crs = getCRS(source);
+
+ // now, if we did not want to override the inner CRS or we did not have any external PRJ at hand
+ // let's look inside the geotiff
+ if (!OVERRIDE_INNER_CRS|| crs==null){
+ if(metadata.hasGeoKey()&& gtcs != null){
+ crs = gtcs.createCoordinateSystem(metadata);
+ }
+ }
+
- if (crs == null)
- crs = getCRS(source);
}
if (crs == null){
View
71 modules/plugin/geotiff/src/test/java/org/geotools/gce/geotiff/GeoTiffReaderTest.java
@@ -22,6 +22,7 @@
import java.awt.Rectangle;
import java.awt.image.RenderedImage;
import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Logger;
@@ -35,11 +36,13 @@
import org.geotools.coverage.grid.io.AbstractGridFormat;
import org.geotools.coverage.grid.io.imageio.IIOMetadataDumper;
import org.geotools.data.DataSourceException;
+import org.geotools.data.PrjFileReader;
import org.geotools.factory.Hints;
import org.geotools.geometry.GeneralEnvelope;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultEngineeringCRS;
import org.geotools.test.TestData;
+import org.junit.Ignore;
import org.junit.Test;
import org.opengis.parameter.GeneralParameterValue;
import org.opengis.parameter.ParameterValue;
@@ -59,6 +62,74 @@
private final static Logger LOGGER = org.geotools.util.logging.Logging
.getLogger(GeoTiffReaderTest.class.toString());
+ /**
+ * Testing proper CRS override with PRJ.
+ *
+ * @throws IllegalArgumentException
+ * @throws IOException
+ * @throws FactoryException
+ */
+ @Test
+ public void prjOverrideTesting1() throws IllegalArgumentException, IOException,
+ FactoryException {
+ // NO override
+ GeoTiffReader.OVERRIDE_INNER_CRS=true;
+
+ //
+ // PRJ override
+ //
+ final File noCrs = TestData.file(GeoTiffReaderTest.class, "sample.tif");
+ final AbstractGridFormat format = new GeoTiffFormat();
+ assertTrue(format.accepts(noCrs));
+ GeoTiffReader reader = (GeoTiffReader) format.getReader(noCrs);
+ CoordinateReferenceSystem crs=reader.getCrs();
+
+ final File prj= TestData.file(GeoTiffReaderTest.class, "sample.prj");
+ final CoordinateReferenceSystem crs_=new PrjFileReader(new FileInputStream(prj).getChannel()).getCoordinateReferenceSystem();
+ assertTrue(CRS.equalsIgnoreMetadata(crs, crs_));
+ GridCoverage2D coverage=reader.read(null);
+ assertTrue(CRS.equalsIgnoreMetadata(coverage.getCoordinateReferenceSystem(), crs_));
+
+ coverage.dispose(true);
+
+ }
+
+ /**
+ * Testing proper CRS override with PRJ.
+ *
+ * @throws IllegalArgumentException
+ * @throws IOException
+ * @throws FactoryException
+ */
+ @Test
+ public void prjOverrideTesting2() throws IllegalArgumentException, IOException,
+ FactoryException {
+ // NO override
+ GeoTiffReader.OVERRIDE_INNER_CRS=false;
+ //
+ // PRJ override
+ //
+ final File noCrs = TestData.file(GeoTiffReaderTest.class, "sample.tif");
+
+
+ final File prj= TestData.file(GeoTiffReaderTest.class, "sample.prj");
+ final CoordinateReferenceSystem crs_=new PrjFileReader(new FileInputStream(prj).getChannel()).getCoordinateReferenceSystem();
+
+
+ // getting a reader
+ GeoTiffReader reader = new GeoTiffReader(noCrs);
+
+ // reading the coverage
+ GridCoverage2D coverage1 = (GridCoverage2D) reader.read(null);
+
+ // check coverage and crs
+ assertNotNull(coverage1);
+ assertNotNull(coverage1.getCoordinateReferenceSystem());
+ assertNotSame(coverage1.getCoordinateReferenceSystem(),crs_);
+ reader.dispose();
+
+ coverage1.dispose(true);
+ }
/**
* Test for reading bad/strange geotiff files
*
View
1  modules/plugin/geotiff/src/test/resources/org/geotools/gce/geotiff/test-data/sample.prj
@@ -0,0 +1 @@
+PROJCS["KosovaRef01",GEOGCS["GCS_GRS_1980",DATUM["D_ETRS_89",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.017453292519943295]],PROJECTION["Gauss_Kruger"],PARAMETER["False_Easting",7500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",21],PARAMETER["Scale_Factor",0.9999],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]
View
BIN  modules/plugin/geotiff/src/test/resources/org/geotools/gce/geotiff/test-data/sample.tif
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.