【问题标题】:Why can't this code produce a points layer in GeoTools为什么这段代码不能在 GeoTools 中生成点图层
【发布时间】:2015-04-10 17:22:03
【问题描述】:

我正在测试使用 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);
}

【问题讨论】:

  • 您至少需要向我们展示 createFeatures 方法,我们才能提供帮助
  • 你不能访问我提供的链接吗?在那个网站上。 osgeo-org.1560.x6.nabble.com/…
  • 我们还需要看看您是如何设置地图的 - o,o 可能不在地图上
  • this.setMapContent(new MapContent()); 在构造函数中。

标签: gis point coordinate geotools


【解决方案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);
}

【讨论】:

    猜你喜欢
    • 2011-02-21
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    相关资源
    最近更新 更多