为什么此代码不能在GeoTools中生成点图层

时间:2015-04-10 17:22:03

标签: gis point coordinate geotools

我正在测试使用Geotools API将一组点添加到地图中。我一直在关注这个例子Problem creating a point and adding it to FeatureCollection,因为示例代码是旧的,并且不推荐使用FeatureCollections之类的东西。我尝试使用DefaultFeatureCollection实例,我不确定我是否正确使用它,这就是为什么这些点不会出现在地图上。我究竟做错了什么?以下是我的一些代码:

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

1 个答案:

答案 0 :(得分:1)

也许这可以帮助你让它发挥作用

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