1

I want to print a map with a raster image as base-map using Geotools.
The problem is that the raster image doesn't take the margins into consideration and starts always at the point (0,0) as you can see in the image below

My map Pdf

My code :

protected void RasterConnect() {
         List<Parameter<?>> list = new ArrayList<Parameter<?>>();
         list.add(new Parameter<File>("image", File.class, "Image",
                       "GeoTiff or World+Image to display as basemap", new KVP(
                                    Parameter.EXT, "tif", Parameter.EXT, "jpg")));

         JParameterListWizard wizard = new JParameterListWizard("Image Lab",
                       "Fill in the following layers", list);
         int finish = wizard.showModalDialog();

         if (finish != JWizard.FINISH) {
                System.exit(0);
         }
         File rasterFile = (File) wizard.getConnectionParameters().get("image");


         AbstractGridFormat format = GridFormatFinder.findFormat(rasterFile);
         reader = format.getReader(rasterFile);

         // Initially display the raster in greyscale using the
         // data from the first image band
         Style rasterStyle = createGreyscaleStyle(1);

         Layer rasterLayer = new GridReaderLayer(reader, rasterStyle);
         map.addLayer(rasterLayer);
   }

public void pdf(Object object) throws FileNotFoundException,
                DocumentException {

         int marge = Mediator.getMarge();
         int scale = Mediator.getScale();
         float widthA4 = Mediator.getSize().getWidth();
         float heightA4 = Mediator.getSize().getHeight();
         float heightA4MeterWithoutMargin = com.itextpdf.text.Utilities
                       .pointsToMillimeters(heightA4 - marge * 2) / 1000;

         Document doc = new Document(Mediator.getSize().rotate(), 10f, 10f,
                       100f, 0f);

         PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(
                       "d:\\test4.pdf"));
         doc.open();
         PdfContentByte cb = writer.getDirectContent();
         Graphics2D graphics = cb.createGraphics(heightA4, widthA4);
         GTRenderer draw = new StreamingRenderer();
         Rectangle outputArea = new Rectangle(marge, marge,
                       (int) (heightA4 - marge * 2), (int) (widthA4 - marge * 2));

         ReferencedEnvelope referencedEnvelope=referencedEnvelopeToScale(scale,marge, widthA4,heightA4,heightA4MeterWithoutMargin);
         map.getViewport().setBounds(referencedEnvelope);



         draw.setMapContent(map);

       draw.paint(graphics, outputArea, referencedEnvelope);


         // Grid
         com.itextpdf.text.Rectangle rect = createRectangleBorder(marge,
                       outputArea);
         cb.rectangle(rect);

         MyFooter event = showInfoPaper(outputArea, scale, "map");
         writer.setPageEvent(event);

         createCrossCoord(marge, scale,  writer, cb,
                       referencedEnvelope);

         cb.fill();

         // cleanup
         graphics.dispose();

         // cleanup
         doc.close();
         writer.close();

   }

PS : Vector layers work perfectly, the problem occurs only with raster.

  • can you add the code which loads the image? all the printing code looks fine – Ian Turton Mar 10 '16 at 17:39
  • Ok thank's, I've added the function that loads the raster file – Youssef Mar 14 '16 at 11:34
  • what is in referencedEnvelopeToScale(...)? – Ian Turton Mar 15 '16 at 7:22
  • it's the function I used to center my map on the paper, I give it the margin to start at (50,50) (in the attached image) rather than 0,0 – Youssef Mar 15 '16 at 9:49
  • @Youssef I am creating print printing service using code almost same as your but my map data get affected by margin i provide and hides the data on right side so i think uisng referencedEnvelopeToScale will resolve this , can u provide the code for this – Haider Dec 12 '18 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.