Hi. I am alban I 'm a french beginner in Geotools.


For my current project I shall build a png image only containing point for

each cities of 5000hab in a defined square.

So only black points and all the image should be transparent.



I got my features in a collection, I got my Enveloppe on the earth.

Then I build a MapContent then I add the layer of all features, styling the

layer with black points.

then I save the MapContent using ImageWriter and GTRender.



My code is very very similar to the geotools tutorial.

But in result I only get a white png file with no point.

*******************************************************

/**

     *  Features to map content.

     *

     * @param envelope the feature envelope

     * @param featuresSource the features source

     * @param imageWidth the image width

     * @param imageHeight the image height

     * @return the map content

     * @throws CartographyException the cartography exception

     */

    public static MapContent featuresToMapContent(final Envelope2D envelope,

final FeatureCollection<SimpleFeatureType,

                 SimpleFeature> featuresSource, final int imageWidth, final
int

imageHeight) throws CartographyException {



         // Create a map viewport

         final MapViewport viewport = new MapViewport() ;

         viewport.setBounds(new ReferencedEnvelope(envelope));



viewport.setCoordinateReferenceSystem(envelope.getCoordinateReferenceSystem());

         viewport.setScreenArea(new Rectangle(imageWidth, imageHeight));



         // create Mapcontent

         final MapContent map = new MapContent();

         map.setTitle("Map");

         map.setViewport(viewport);



         // Create a basic Style to render the features

         final Style style = ShapeUtils.createPointStyle(Color.BLACK, 5);

         style.featureTypeStyles();



         // Add the features and the associated Style object to

         // the MapContent as a new Layer

         final Layer layer = new FeatureLayer(featuresSource, style,

"featuresLayer");

         layer.setVisible(true);

         layer.setSelected(true);



         //add layer and dispose Map

         map.addLayer(layer);

         map.dispose();



         return map;

    }









    /**

     * Save image.

     *

     * @param map the map

     * @param outPutFile the output png file

     * @param imageWidth the image width

     * @param imageHeight the image height

     * @throws CartographyException the cartography exception

     */

    public static void saveImage(final MapContent map, final File

outPutFile, final int imageWidth, final int imageHeight) throws

CartographyException {

         try {

                 // get the renderer

                 final GTRenderer renderer = new StreamingRenderer();

                 renderer.setMapContent(map);



                 // define bounds : mapbounds and screenToDisplay

                 final ReferencedEnvelope mapBounds =
map.getViewport().getBounds();

                 final Rectangle imageBounds = new

Rectangle(0,0,imageWidth,imageHeight);



                 // create the image

                 final BufferedImage image = new
BufferedImage(imageBounds.width,

imageBounds.height, BufferedImage.TYPE_INT_RGB);

                 final Graphics2D gr = image.createGraphics();

                 gr.setPaint(Color.WHITE);

                 gr.fill(imageBounds);

                 gr.dispose();



                 // paint the image

                 final AffineTransform world2screen =

RendererUtilities.worldToScreenTransform(mapBounds, imageBounds);

                 renderer.paint(gr, imageBounds, mapBounds, world2screen);

                 ImageIO.write(image, "png", outPutFile);

         } catch (IOException e) {

                 throw new CartographyException(e);

         }

    }

************************************************





I read almost all internet on this subject and I don't find my mistake. If

you would have a look it will be greate for me. Thanks a lot !

Alban
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
GeoTools-GT2-Users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to