GeoTools - drawing points on image

烈酒焚心 提交于 2019-12-12 02:25:29

问题


I'm using geoTools 14.1

I'm trying to plot on an image some points

This is the code I'm using:

    double[][] points = new double[8][2];
    points[0] = new double[]{45.46433710338643, 9.190417528152478};
    points[1] = new double[]{45.46195085146914, 9.189746320685355};
    points[2] = new double[]{45.460062304163635, 9.19015527826191};
    points[3] = new double[]{45.472950871127445, 9.17363731952788};
    points[4] = new double[]{45.4737153001908,9.203728795018847};
    points[5] = new double[]{45.4849795331724,9.20162835217198};
    points[6] = new double[]{45.48560542313713,9.195953607559215};
    points[7] = new double[]{45.48348421787171,9.188765287399292};
    final SimpleFeatureType TYPE = DataUtilities.createType("Location",
                            "location:Point:srid=3857,"+// <- the geometry attribute: Point type
                            "nome:String," + // <- a String attribute
                            "id:Integer" // a number attribute
                    );
    FeatureCollection<SimpleFeatureType, SimpleFeature> collection = new DefaultFeatureCollection();
    GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
    CoordinateReferenceSystem pointSrc = CRS.decode("EPSG:4326");
    CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:3857");
    MathTransform transform = CRS.findMathTransform(pointSrc, targetCRS);
    for (int i = 0; i < points.length; i++)
    {
    double[] coords = points[i];
    Point point = geometryFactory.createPoint(new Coordinate(coords[0], coords[1]));
    Point converted = (Point) JTS.transform( point, transform);
    featureBuilder.add(converted);
    featureBuilder.add("Punto "+i);
    featureBuilder.add(i);
    SimpleFeature feature = featureBuilder.buildFeature(""+i);
    logger.info(""+feature+" feature.getDefaultGeometry() "+feature.getDefaultGeometry()+ " feature.getDefaultGeometryProperty() "+feature.getDefaultGeometryProperty());
    ((DefaultFeatureCollection)collection).add(feature);                
    }
    String wellKnownName = "Circle";
    Style style = SLD.createPointStyle(wellKnownName,Color.RED,Color.RED,0f,10f);
    FeatureLayer fl = new FeatureLayer(collection, style);
    fl.setVisible(true);
    fl.setSelected(true);
    logger.info(""+fl);
    mapcontent.addLayer((org.geotools.map.Layer)fl);                        //"1010177.1917802,5688070.7096562,1029133.5747922,5704122.4855938"
    ReferencedEnvelope bounds = new ReferencedEnvelope(1010177.1917802,1029133.5747922,5688070.7096562, 5704122.4855938, targetCRS);
   BufferedImage ret = buildImage(mapcontent, 5000, 5000, bounds, Color.white);
   ImageIO.write((RenderedImage) ret, "png", new File("/home/angelo/Scrivania/immagineResult.png"));

It seems to me all correct, but the generated image contains no point.

This is the generated image

As you can see it's all white; I was expecting only 8 red circles on the image... Is there any error in my code? Am I doing anything wrong?

Thank you Angelo

UPDATE: added build image method

public BufferedImage buildImage(final MapContent map, final int imageWidth,final int imageHeight,ReferencedEnvelope bounds,Color bgcolor) {
    GTRenderer renderer = new StreamingRenderer();
    renderer.setMapContent(map);

    renderer.setMapContent(map);
    Rectangle imageBounds = null;
    ReferencedEnvelope mapBounds = bounds;

    try {
        if(bounds==null) mapBounds = map.getMaxBounds();

        imageBounds = new Rectangle(imageWidth, imageHeight);
    } catch (Exception e) {
         failed to access map layers
        throw new RuntimeException(e);
    }

    BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_4BYTE_ABGR);

    Graphics2D gr = image.createGraphics();
    int type = AlphaComposite.SRC;
    gr.setComposite(AlphaComposite.getInstance(type));

    Color c = new Color(bgcolor.getRed(), bgcolor.getGreen(), bgcolor.getBlue(), 0);
    gr.setBackground(bgcolor);
    gr.setColor(c);
    gr.fillRect(0, 0, image.getWidth(), image.getHeight());
    type = AlphaComposite.SRC_OVER;
    gr.setComposite(AlphaComposite.getInstance(type));
    try {
        renderer.paint(gr, imageBounds, bounds);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return image;
}

回答1:


After a lot of playing I have found the problem :-) There is no bug in your code! There are in fact 8 red dots in that white image, but they are very hard to find!

As the above image shows at 2000% zoom (and panning along the top edge) you will find a dot (I'm assuming the others are there) - the simple answer is either to make the dots 10 times bigger (100px) or the image much smaller (500x500) and in both cases the dots are immediately visible.



来源:https://stackoverflow.com/questions/34037972/geotools-drawing-points-on-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!