From: 咳咳>唉倪 <565...@qq...> - 2013-08-05 13:17:58
|
Hello~ I am very new to geotools,and I have to create a geotiff from a bufferedimage. Then during the progress I get several problems and I can't solve it by myself immediately. So,if it is convenient for you to help me with that ,I would be very grateful. Q1:I have tried to write the bufferedimage to "tif" first,then use geotools to convert "tif" to "geotiff",but after I get the "geotiff" and open it with the arcMap , arcMap point me "The following data sources use a geographic coordinate system that is different from the one used by the data frame you are adding the data into".Really don't understand why this happened. Q2:And the Properties of my "geotiff" shows that the Statistics have not been calculated, it seems like that I didn't set the bands ,and I need to set the bands so others could use the geotiff to build a pyramid .All I know is that I need to do something with the "Sampledimensions" before I create a GridCoverage ,but I don't know how to do it. BTW,I open the "tif" with the arcMap,it has a RGBA composite. I fould the function in http://docs.geotools.org/latest/userguide/tutorial/raster/image.html can createRGBstyle for displaying the map,but I need to create a geotiff file,not just dispalying it. What am I doing wrong ? What I can do ? Thanks in advance!!! 1:bufferedimage to tif 2:tif to geotiff public void CreateGeotiffFromTiff(HttpServletRequest request, ServletOutputStream out){ byte[] bytes=null; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); double xmax=148.384765624315; double ymax=55.990478515196884; double xmin=59.615234375685; double ymin=0.5095214848031198; double width=xmax-xmin; double height=ymax-ymin; RenderedImage mapraster = JAI.create("ImageRead", new File("c:/img/tiff/theImageMaptiff.tif")); //show image try { ImageIO.write(mapraster, "png", outputStream); bytes=outputStream.toByteArray(); out.write(bytes); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } GridCoverageFactory gcf = new GridCoverageFactory(); CoordinateReferenceSystem finalCRS = null; try { finalCRS = CRS.decode("EPSG:4326",true); } catch (NoSuchAuthorityCodeException e) { System.out.println(e.getMessage()); // TODO Auto-generated catch block e.printStackTrace(); } catch (FactoryException e) { // TODO Auto-generated catch block System.out.println(e.getMessage()); e.printStackTrace(); } Envelope2D finalEnvelope = new Envelope2D(finalCRS,xmin,ymin,width,height); //creating final coverage GridCoverage cov = gcf.create("geotifftest",mapraster, finalEnvelope); GridCoverage2D cov2d= (GridCoverage2D)Operations.DEFAULT.resample(cov,finalCRS); //creating final geotiff file File outfile = new File("C:/img/try/CreareGeotiff.tif"); CoverageProcessor processor = new CoverageProcessor(null); ParameterValueGroup params = processor.getOperation("Resample").getParameters(); params.parameter("CoordinateReferenceSystem").setValue(finalCRS); params.parameter("Source").setValue(cov2d); params.parameter("InterpolationType").setValue(Interpolation.getInstance(Interpolation.INTERP_NEAREST)); cov2d=(GridCoverage2D)processor.doOperation(params); GeoTiffWriter gtw = null; try { gtw = new GeoTiffWriter(outfile); gtw.write(gc1, null); gtw.write(cov2d, (GeneralParameterValue[])params.values().toArray(new GeneralParameterValue[1])); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IndexOutOfBoundsException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } gtw.dispose(); cov2d.dispose(true); } } |