Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.27 KB | None | 0 0
  1. package com.ianturton.cookbook.raster;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. import org.geotools.coverage.GridSampleDimension;
  9. import org.geotools.coverage.grid.GridCoverage2D;
  10. import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;
  11. import org.geotools.coverage.grid.io.AbstractGridFormat;
  12. import org.geotools.coverage.grid.io.GridFormatFinder;
  13. import org.geotools.data.Parameter;
  14. import org.geotools.factory.CommonFactoryFinder;
  15. import org.geotools.map.FeatureLayer;
  16. import org.geotools.map.GridCoverageLayer;
  17. import org.geotools.map.Layer;
  18. import org.geotools.map.MapContent;
  19. import org.geotools.referencing.CRS;
  20. import org.geotools.referencing.CRS.AxisOrder;
  21. import org.geotools.referencing.crs.DefaultGeographicCRS;
  22. import org.geotools.styling.ChannelSelection;
  23. import org.geotools.styling.ContrastEnhancement;
  24. import org.geotools.styling.ContrastEnhancementImpl;
  25. import org.geotools.styling.RasterSymbolizer;
  26. import org.geotools.styling.SLD;
  27. import org.geotools.styling.SelectedChannelType;
  28. import org.geotools.styling.Style;
  29. import org.geotools.styling.StyleFactory;
  30. import org.geotools.swing.JMapFrame;
  31. import org.geotools.swing.data.JParameterListWizard;
  32. import org.geotools.swing.wizard.JWizard;
  33. import org.geotools.util.KVP;
  34. import org.opengis.filter.FilterFactory;
  35. import org.opengis.referencing.FactoryException;
  36. import org.opengis.referencing.crs.CoordinateReferenceSystem;
  37. import org.opengis.style.ContrastMethod;
  38.  
  39. public class RasterViewer {
  40.  
  41. private JMapFrame frame;
  42. private MapContent mapContent;
  43. private FeatureLayer layer;
  44. private Layer gridLayer;
  45. private Style style;
  46. private final static FilterFactory ff = CommonFactoryFinder.getFilterFactory2();
  47. private final static StyleFactory sf = CommonFactoryFinder.getStyleFactory();
  48. private static final int RED = 0;
  49. private static final int GREEN = 1;
  50. private static final int BLUE = 2;
  51.  
  52. public RasterViewer(File file, CoordinateReferenceSystem crs) {
  53.  
  54. frame = new JMapFrame();
  55. mapContent = new MapContent();
  56.  
  57. frame.setMapContent(mapContent);
  58. frame.enableToolBar(true);
  59. frame.enableStatusBar(true);
  60. frame.setSize(800, 400);
  61. AbstractGridFormat format = GridFormatFinder.findFormat(file);
  62. System.out.println("got format " + format);
  63. AbstractGridCoverage2DReader reader = format.getReader(file);
  64. System.out.println("got reader " + reader);
  65.  
  66. GridCoverage2D cov = null;
  67. try {
  68. cov = reader.read(null);
  69. } catch (IOException giveUp) {
  70. throw new RuntimeException(giveUp);
  71. }
  72. if (cov == null) {
  73. System.out.println("no coverage found");
  74. System.exit(0);
  75. }
  76. System.out.println("coverage " + cov.getProperties());
  77.  
  78. if (crs == null) {
  79. crs = cov.getCoordinateReferenceSystem();
  80. System.err.println("Set CRS to " + crs);
  81. }
  82. if (crs == null) {
  83. System.err.println("No CRS defaulting to WGS84");
  84. crs = DefaultGeographicCRS.WGS84;
  85. }
  86.  
  87. if (CRS.getAxisOrder(crs).equals(AxisOrder.EAST_NORTH)) {
  88. System.setProperty("org.geotools.referencing.forceXY", "true");
  89. }
  90. mapContent.getViewport().setCoordinateReferenceSystem(crs);
  91. Style rasterStyle = createRGBStyle(cov);
  92.  
  93. /*
  94. * try { File sld = new File("raster.sld"); SLDParser stylereader = new
  95. * SLDParser(sf, sld.toURI().toURL()); Style[] style =
  96. * stylereader.readXML(); rasterStyle= style[0];
  97. *
  98. * } catch (Exception e) { e.printStackTrace(); }
  99. */
  100. Layer rasterLayer = new GridCoverageLayer(cov, rasterStyle);
  101. if (rasterLayer.getBounds() == null) {
  102. System.out.println("bad raster layer ");
  103. System.exit(0);
  104. }
  105. mapContent.addLayer(rasterLayer);
  106. frame.setVisible(true);
  107. }
  108.  
  109. /**
  110. * This method examines the names of the sample dimensions in the provided
  111. * coverage looking for "red...", "green..." and "blue..." (case insensitive
  112. * match). If these names are not found it uses bands 1, 2, and 3 for the red,
  113. * green and blue channels. It then sets up a raster symbolizer and returns
  114. * this wrapped in a Style.
  115. *
  116. * @return a new Style object containing a raster symbolizer set up for RGB
  117. * image
  118. */
  119. private Style createRGBStyle(GridCoverage2D cov) {
  120.  
  121. // We need at least three bands to create an RGB style
  122. int numBands = cov.getNumSampleDimensions();
  123. if (numBands < 3) {
  124. return createGreyscaleStyle(1);
  125. }
  126. // Get the names of the bands
  127. String[] sampleDimensionNames = new String[numBands];
  128. for (int i = 0; i < numBands; i++) {
  129. GridSampleDimension dim = cov.getSampleDimension(i);
  130. sampleDimensionNames[i] = dim.getDescription().toString();
  131. }
  132. final int RED = 0, GREEN = 1, BLUE = 2;
  133. int[] channelNum = { -1, -1, -1 };
  134. // We examine the band names looking for "red...", "green...", "blue...".
  135. // Note that the channel numbers we record are indexed from 1, not 0.
  136. for (int i = 0; i < numBands; i++) {
  137. String name = sampleDimensionNames[i].toLowerCase();
  138. if (name != null) {
  139. if (name.matches("red.*")) {
  140. channelNum[RED] = i + 1;
  141. } else if (name.matches("green.*")) {
  142. channelNum[GREEN] = i + 1;
  143. } else if (name.matches("blue.*")) {
  144. channelNum[BLUE] = i + 1;
  145. }
  146. }
  147. }
  148. // If we didn't find named bands "red...", "green...", "blue..."
  149. // we fall back to using the first three bands in order
  150. if (channelNum[RED] < 0 || channelNum[GREEN] < 0 || channelNum[BLUE] < 0) {
  151. channelNum[RED] = 1;
  152. channelNum[GREEN] = 2;
  153. channelNum[BLUE] = 3;
  154. }
  155. // Now we create a RasterSymbolizer using the selected channels
  156. SelectedChannelType[] sct = new SelectedChannelType[cov.getNumSampleDimensions()];
  157. ContrastEnhancement ce = sf.contrastEnhancement(ff.literal(1.0), ContrastMethod.NORMALIZE);
  158. for (int i = 0; i < 3; i++) {
  159. sct[i] = sf.createSelectedChannelType(String.valueOf(channelNum[i]), ce);
  160. }
  161. RasterSymbolizer sym = sf.getDefaultRasterSymbolizer();
  162. ChannelSelection sel = sf.channelSelection(sct[RED], sct[GREEN], sct[BLUE]);
  163. sym.setChannelSelection(sel);
  164.  
  165. return SLD.wrapSymbolizers(sym);
  166. }
  167.  
  168. private Style createGreyscaleStyle(int band) {
  169. ContrastEnhancement ce = new ContrastEnhancementImpl();// sf.contrastEnhancement(ff.literal(1.0),
  170. // new Normalize());
  171. SelectedChannelType sct = sf.createSelectedChannelType(String.valueOf(band), ce);
  172.  
  173. RasterSymbolizer sym = sf.getDefaultRasterSymbolizer();
  174. ChannelSelection sel = sf.channelSelection(sct);
  175. sym.setChannelSelection(sel);
  176.  
  177. return SLD.wrapSymbolizers(sym);
  178. }
  179.  
  180. public static void main(String[] args) throws IOException {
  181. File file = null;
  182. String wkt = "";
  183. CoordinateReferenceSystem crs = null;
  184. if (args.length == 0) {
  185. // display a data store file chooser dialog for images
  186. List<Parameter<?>> list = new ArrayList<Parameter<?>>();
  187. list.add(new Parameter<File>("image", File.class, "Image", "GeoTiff or World+Image to display as basemap",
  188. new KVP(Parameter.EXT, "tif", Parameter.EXT, "jpg")));
  189. /*
  190. * list.add(new Parameter<File>("shape", File.class, "Shapefile",
  191. * "Shapefile contents to display", new KVP(Parameter.EXT, "shp")));
  192. */
  193. JParameterListWizard wizard = new JParameterListWizard("Image Lab", "Fill in the following layers", list);
  194. int finish = wizard.showModalDialog();
  195.  
  196. if (finish != JWizard.FINISH) {
  197. System.exit(0);
  198. }
  199. file = (File) wizard.getConnectionParameters().get("image");
  200. } else {
  201. System.out.println(args[0]);
  202. int count = 0;
  203. while (args[count].startsWith("-")) {
  204. System.out.println("checking " + args[count]);
  205. if (count < args.length && args[count].endsWith("proj")) {
  206. count++;
  207. wkt = args[count];
  208. count++;
  209. continue;
  210. }
  211. }
  212. file = new File(args[count]);
  213. System.out.println(file);
  214. if (file == null || !file.exists()) {
  215. System.err.println(file + " doesn't exist");
  216. return;
  217. }
  218.  
  219. }
  220. try {
  221. if (!wkt.isEmpty()) {
  222.  
  223. crs = CRS.decode(wkt);
  224.  
  225. }
  226. } catch (FactoryException e) {
  227. // TODO Auto-generated catch block
  228. e.printStackTrace();
  229. }
  230.  
  231. System.out.println(file);
  232. new RasterViewer(file, crs);
  233. }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement