GeoTools - 在图像上绘制点

时间:2015-12-02 08:27:38

标签: geotools

我正在使用geoTools 14.1

我试图在图像上绘制一些点

这是我使用的代码:

    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"));

在我看来,这一切都是正确的,但生成的图像没有任何意义。

这是生成的图片enter image description here

你可以看到它全是白色的;我期待图像上只有8个红色圆圈......我的代码中是否有任何错误?我做错了吗?

谢谢 安吉洛

更新:添加了构建图像方法

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 个答案:

答案 0 :(得分:1)

经过大量的游戏,我发现了问题:-)你的代码中没有错误!实际上白色图像中有8个红点,但它们很难找到!

enter image description here

由于上图显示的是2000%变焦(并沿着上边缘平移),你会发现一个点(我假设其他人都在那里) - 简单的答案是让点数大10倍(100像素)或图像小得多(500x500),在这两种情况下,点都可以立即看到。