Why can't this code produce a points layer in GeoTools
Asked Answered
C

1

0

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);
}
Clarettaclarette answered 10/4, 2015 at 17:22 Comment(4)
you'll need to show us at least the createFeatures method before we can helpFaria
Can you not visit the link I provided? It's on that site. osgeo-org.1560.x6.nabble.com/…Clarettaclarette
we also need to see how you've set up the map - o,o may be off the mapFaria
this.setMapContent(new MapContent()); in the constructor.Clarettaclarette
S
1

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);
}
Separation answered 24/6, 2016 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.