为什么此代码无法在GeoTools中生成点层 [英] Why can't this code produce a points layer in GeoTools

查看:249
本文介绍了为什么此代码无法在GeoTools中生成点层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试使用Geotools API向地图添加点集合.我一直在尽可能地遵循此示例,

I am testing adding a collection of points to a map utilizing the Geotools API. I've been following this example as best I could Problem creating a point and adding it to FeatureCollection, as the example code is old, and things like FeatureCollections is deprecated. I tried using DefaultFeatureCollection instance instead, and I am not sure if I am using it correctly, and that is why the points do not appear on the map. What am I doing wrong? Here is some of my code:

private void plotMarkers() {
    final SimpleFeatureType TYPE = this.createFeatureType();
    final SimpleFeatureBuilder BLDR = new SimpleFeatureBuilder(TYPE);

    DefaultFeatureCollection features = new DefaultFeatureCollection();

    // arbitrary start position
    Coordinate pos = new Coordinate(0, 0);
    final double pointSpacing = 1.0;
    String title = "Test";
    features.add(creatureFeature(BLDR, pos, title));

    // display points on screen
    Style style = SLD.createPointStyle("circle", Color.RED, Color.RED, 1.0f, 5.0f);
    Layer layer = new FeatureLayer(features, style);

    this.getMapContent().addLayer(layer);
}

推荐答案

也许可以帮助您使其正常工作

Maybe this can help you to make it work

private MapContent map;
private static Style pointStyle = SLD.createPointStyle("Circle", Color.RED, Color.RED, 0.5f, POINT_SIZE);
public static void CreatePoints(double X, double Y){
        createPointLayer();
        createFeatures(X,Y);
}
static void createFeatures(double X, double Y) {
    Point point = geometryFactory.createPoint(new Coordinate(X, Y));
    pointCollection.add(SimpleFeatureBuilder.build(pointType, new Object[]{point}, null));

    //create map layer event
    MapLayerEvent mple = new MapLayerEvent(pointLayer, MapLayerEvent.DATA_CHANGED);
    //create maplayer list event
    MapLayerListEvent mplle = new MapLayerListEvent(map, pointLayer, map.layers().indexOf(pointLayer), mple);

    okvir.mapPane.layerChanged(mplle);
    System.out.println(MessageFormat.format("Created Point: {0}", point));
}


private static void createPointLayer() {
    if (pointType == null) {
        pointFeatureTypeBuilder.setName("PointCreated");
        pointFeatureTypeBuilder.setCRS(map.getCoordinateReferenceSystem());
        pointFeatureTypeBuilder.add("the_geom", Point.class);
        pointType = pointFeatureTypeBuilder.buildFeatureType();
        pointCollection = new DefaultFeatureCollection(null, pointType);
    }
    pointLayer = new FeatureLayer(pointCollection, pointStyle);
    map.addLayer(pointLayer);
}

这篇关于为什么此代码无法在GeoTools中生成点层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆