PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/geotools-9.2/modules/plugin/imagemosaic/src/test/java/org/geotools/gce/imagemosaic/CatalogBuilderTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 348 lines | 243 code | 57 blank | 48 comment | 12 complexity | c6aba578d0d1c4f62c5d3deac527924a MD5 | raw file
  1. /*
  2. * GeoTools - The Open Source Java GIS Toolkit
  3. * http://geotools.org
  4. *
  5. * (C) 2007-2008, Open Source Geospatial Foundation (OSGeo)
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation;
  10. * version 2.1 of the License.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. */
  17. package org.geotools.gce.imagemosaic;
  18. import java.awt.Dimension;
  19. import java.awt.Rectangle;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.FileNotFoundException;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.net.InetAddress;
  26. import java.util.Arrays;
  27. import java.util.Properties;
  28. import javax.media.jai.PlanarImage;
  29. import org.apache.commons.io.IOUtils;
  30. import org.geotools.coverage.grid.GridCoverage2D;
  31. import org.geotools.coverage.grid.GridEnvelope2D;
  32. import org.geotools.coverage.grid.GridGeometry2D;
  33. import org.geotools.coverage.grid.io.AbstractGridFormat;
  34. import org.geotools.gce.imagemosaic.catalog.GranuleCatalog;
  35. import org.geotools.gce.imagemosaic.catalogbuilder.CatalogBuilder;
  36. import org.geotools.gce.imagemosaic.catalogbuilder.CatalogBuilder.ExceptionEvent;
  37. import org.geotools.gce.imagemosaic.catalogbuilder.CatalogBuilder.ProcessingEvent;
  38. import org.geotools.gce.imagemosaic.catalogbuilder.CatalogBuilder.ProcessingEventListener;
  39. import org.geotools.gce.imagemosaic.catalogbuilder.CatalogBuilderConfiguration;
  40. import org.geotools.geometry.GeneralEnvelope;
  41. import org.geotools.test.TestData;
  42. import org.junit.Assert;
  43. import org.junit.Before;
  44. import org.junit.Test;
  45. import org.opengis.parameter.GeneralParameterValue;
  46. import org.opengis.parameter.ParameterValue;
  47. /**
  48. * Testing {@link CatalogBuilder} and its related subclasses.
  49. *
  50. * @author Simone Giannecchini, GeoSolutions SAS
  51. *
  52. *
  53. *
  54. *
  55. * @source $URL$
  56. */
  57. public class CatalogBuilderTest extends Assert {
  58. /** Used to avoid errors if building on a system where hostname is not defined */
  59. private boolean hostnameDefined;
  60. @Before
  61. public void setup() {
  62. try {
  63. InetAddress.getLocalHost();
  64. hostnameDefined = true;
  65. } catch (Exception ex) {
  66. hostnameDefined = false;
  67. }
  68. }
  69. private final class CatalogBuilderListener extends ProcessingEventListener{
  70. @Override
  71. public void exceptionOccurred(ExceptionEvent event) {
  72. throw new RuntimeException(event.getException());
  73. }
  74. @Override
  75. public void getNotification(ProcessingEvent event) {
  76. }
  77. }
  78. @Test
  79. public void catalogBuilderConfiguration() throws Exception{
  80. // create a stub configuration
  81. final CatalogBuilderConfiguration c1= new CatalogBuilderConfiguration();
  82. c1.setIndexName("index");
  83. c1.setLocationAttribute("location");
  84. c1.setAbsolute(true);
  85. c1.setRootMosaicDirectory(TestData.file(this,"/rgb").toString());
  86. c1.setIndexingDirectories(Arrays.asList(TestData.file(this,"/rgb").toString()));
  87. assertNotNull(c1.toString());
  88. // create a second stub configuration
  89. final CatalogBuilderConfiguration c2= new CatalogBuilderConfiguration();
  90. c2.setIndexName("index");
  91. c2.setLocationAttribute("location");
  92. c2.setAbsolute(true);
  93. c2.setRootMosaicDirectory(TestData.file(this,"/rgb").toString());
  94. c2.setIndexingDirectories(Arrays.asList(TestData.file(this,"/rgb").toString()));
  95. assertTrue(c1.equals(c2));
  96. assertEquals(c1.hashCode(), c2.hashCode());
  97. CatalogBuilderConfiguration c3 = c2.clone();
  98. assertTrue(c3.equals(c2));
  99. assertEquals(c3.hashCode(), c2.hashCode());
  100. //check errors
  101. final CatalogBuilderConfiguration c4= new CatalogBuilderConfiguration();
  102. assertNotNull(c4.toString());
  103. }
  104. @Test
  105. public void buildCatalog() throws FileNotFoundException, IOException{
  106. if (hostnameDefined){
  107. CatalogBuilder builder = null;
  108. ImageMosaicReader reader = null;
  109. ParameterValue<GridGeometry2D> gg = null;
  110. GeneralEnvelope envelope = null;
  111. Dimension dim = null;
  112. Rectangle rasterArea = null;
  113. GridEnvelope2D range = null;
  114. GridCoverage2D coverage = null;
  115. final ParameterValue<Boolean> useJai = AbstractGridFormat.USE_JAI_IMAGEREAD.createValue();
  116. useJai.setValue(false);
  117. final ParameterValue<String> tileSize = AbstractGridFormat.SUGGESTED_TILE_SIZE.createValue();
  118. tileSize.setValue("128,128");
  119. //build a relative index and then make it run
  120. CatalogBuilderConfiguration c1= new CatalogBuilderConfiguration();
  121. c1.setIndexName("shpindex");
  122. c1.setLocationAttribute("location");
  123. c1.setAbsolute(false);
  124. c1.setRootMosaicDirectory(TestData.file(this,"/overview").toString());
  125. c1.setIndexingDirectories(Arrays.asList(TestData.file(this,"/overview/0").toString()));
  126. assertNotNull(c1.toString());
  127. //build the index
  128. builder= new CatalogBuilder(c1);
  129. builder.addProcessingEventListener(new CatalogBuilderListener());
  130. builder.run();
  131. final File relativeMosaic=TestData.file(this,"/overview/"+c1.getIndexName()+".shp");
  132. assertTrue(relativeMosaic.exists());
  133. assertTrue(new ImageMosaicFormat().accepts(relativeMosaic));
  134. reader = (ImageMosaicReader) new ImageMosaicReader(relativeMosaic);
  135. // limit yourself to reading just a bit of it
  136. gg = AbstractGridFormat.READ_GRIDGEOMETRY2D.createValue();
  137. envelope = reader.getOriginalEnvelope();
  138. dim= new Dimension();
  139. dim.setSize(reader.getOriginalGridRange().getSpan(0)/2.0, reader.getOriginalGridRange().getSpan(1)/2.0);
  140. rasterArea=(( GridEnvelope2D)reader.getOriginalGridRange());
  141. rasterArea.setSize(dim);
  142. range= new GridEnvelope2D(rasterArea);
  143. gg.setValue(new GridGeometry2D(range,envelope));
  144. // use imageio with defined tiles
  145. // Test the output coverage
  146. coverage = (GridCoverage2D) reader.read(new GeneralParameterValue[] {gg,useJai ,tileSize});
  147. Assert.assertNotNull(coverage);
  148. PlanarImage.wrapRenderedImage( coverage.getRenderedImage()).getTiles();
  149. //caching should be false by default
  150. Properties props= new Properties();
  151. InputStream in= null;
  152. try{
  153. in= TestData.openStream(this, "/overview/"+c1.getIndexName()+".properties");
  154. assertNotNull("unable to find mosaic properties file",in);
  155. props.load(in);
  156. assertTrue(props.containsKey("Caching"));
  157. assertTrue(props.getProperty("Caching").equalsIgnoreCase("false"));
  158. } finally {
  159. if(in!=null){
  160. IOUtils.closeQuietly(in);
  161. }
  162. }
  163. // dispose
  164. coverage.dispose(true);
  165. reader.dispose();
  166. //build an absolute index and then make it run
  167. CatalogBuilderConfiguration c2= new CatalogBuilderConfiguration();
  168. c2.setIndexName("shpindex_absolute");
  169. c2.setLocationAttribute("location");
  170. c2.setAbsolute(true);
  171. c2.setCaching(true);
  172. c2.setRootMosaicDirectory(TestData.file(this,"/overview").toString());
  173. c2.setIndexingDirectories(Arrays.asList(TestData.file(this,"/overview/0").toString()));
  174. assertNotNull(c2.toString());
  175. //build the index
  176. builder= new CatalogBuilder(c2);
  177. builder.addProcessingEventListener(new CatalogBuilderListener());
  178. builder.run();
  179. final File absoluteMosaic=TestData.file(this,"/overview/"+c2.getIndexName()+".shp");
  180. assertTrue(absoluteMosaic.exists());
  181. //caching should be false by default
  182. props= new Properties();
  183. in= null;
  184. try{
  185. in= TestData.openStream(this, "/overview/"+c2.getIndexName()+".properties");
  186. assertNotNull("unable to find mosaic properties file",in);
  187. props.load(in);
  188. assertTrue(props.containsKey("Caching"));
  189. assertTrue(props.getProperty("Caching").equalsIgnoreCase("true"));
  190. } finally {
  191. if(in!=null){
  192. IOUtils.closeQuietly(in);
  193. }
  194. }
  195. assertTrue(new ImageMosaicFormat().accepts(absoluteMosaic));
  196. reader = (ImageMosaicReader) new ImageMosaicReader(absoluteMosaic);
  197. // limit yourself to reading just a bit of it
  198. gg = AbstractGridFormat.READ_GRIDGEOMETRY2D.createValue();
  199. envelope = reader.getOriginalEnvelope();
  200. dim= new Dimension();
  201. dim.setSize(reader.getOriginalGridRange().getSpan(0)/2.0, reader.getOriginalGridRange().getSpan(1)/2.0);
  202. rasterArea=(( GridEnvelope2D)reader.getOriginalGridRange());
  203. rasterArea.setSize(dim);
  204. range= new GridEnvelope2D(rasterArea);
  205. gg.setValue(new GridGeometry2D(range,envelope));
  206. // use imageio with defined tiles
  207. // Test the output coverage
  208. coverage = (GridCoverage2D) reader.read(new GeneralParameterValue[] {gg,useJai ,tileSize});
  209. Assert.assertNotNull(coverage);
  210. PlanarImage.wrapRenderedImage( coverage.getRenderedImage()).getTiles();
  211. // dispose
  212. coverage.dispose(true);
  213. reader.dispose();
  214. }
  215. }
  216. @Test
  217. public void buildCachingIndex() throws FileNotFoundException, IOException {
  218. if (hostnameDefined){
  219. CatalogBuilder builder = null;
  220. ImageMosaicReader reader = null;
  221. FileInputStream inStream = null;
  222. CatalogBuilderConfiguration c1 = new CatalogBuilderConfiguration();
  223. c1.setIndexName("shpindex");
  224. c1.setLocationAttribute("location");
  225. c1.setAbsolute(false);
  226. c1.setRootMosaicDirectory(TestData.file(this, "/caching").toString());
  227. c1.setIndexingDirectories(Arrays.asList(TestData.file(this,"/caching").toString()));
  228. Properties prop = new Properties();
  229. try {
  230. c1.setCaching(false);
  231. // build the index
  232. builder = new CatalogBuilder(c1);
  233. builder.addProcessingEventListener(new CatalogBuilderListener());
  234. builder.run();
  235. final File relativeMosaic = TestData.file(this, "/caching/" + c1.getIndexName() + ".shp");
  236. final File propertiesFile = TestData.file(this, "/caching/" + c1.getIndexName() + ".properties");
  237. assertTrue(relativeMosaic.exists());
  238. inStream = new FileInputStream(propertiesFile);
  239. prop.load(inStream);
  240. String value = prop.getProperty("Caching");
  241. assertNotNull(value);
  242. assertTrue (value.toLowerCase().equals("false"));
  243. assertTrue(new ImageMosaicFormat().accepts(relativeMosaic));
  244. reader = (ImageMosaicReader) new ImageMosaicReader(relativeMosaic);
  245. GranuleCatalog catalog = reader.rasterManager.granuleCatalog;
  246. assertTrue(catalog.getClass().toString().endsWith("GTDataStoreGranuleCatalog"));
  247. } finally {
  248. if (inStream != null){
  249. IOUtils.closeQuietly(inStream);
  250. }
  251. try {
  252. if (reader != null){
  253. reader.dispose();
  254. }
  255. } catch (Throwable t){
  256. //Eat exception
  257. }
  258. }
  259. try {
  260. c1.setCaching(true);
  261. // build the index
  262. builder = new CatalogBuilder(c1);
  263. builder.addProcessingEventListener(new CatalogBuilderListener());
  264. builder.run();
  265. final File relativeMosaic = TestData.file(this, "/caching/" + c1.getIndexName() + ".shp");
  266. final File propertiesFile = TestData.file(this, "/caching/" + c1.getIndexName() + ".properties");
  267. inStream = new FileInputStream(propertiesFile);
  268. prop.load(inStream);
  269. String value = prop.getProperty("Caching");
  270. assertNotNull(value);
  271. assertTrue (value.toLowerCase().equals("true"));
  272. assertTrue(relativeMosaic.exists());
  273. assertTrue(new ImageMosaicFormat().accepts(relativeMosaic));
  274. reader = (ImageMosaicReader) new ImageMosaicReader(relativeMosaic);
  275. GranuleCatalog catalog = reader.rasterManager.granuleCatalog;
  276. assertTrue(catalog.getClass().toString().endsWith("STRTreeGranuleCatalog"));
  277. } finally {
  278. if (inStream != null){
  279. IOUtils.closeQuietly(inStream);
  280. }
  281. try {
  282. if (reader != null){
  283. reader.dispose();
  284. }
  285. } catch (Throwable t){
  286. //Eat exception
  287. }
  288. }
  289. }
  290. }
  291. }