Show direction of linestring on map - auto zoom on map

后端 未结 1 1895
一整个雨季
一整个雨季 2021-01-14 16:04

I have this code, which plots on a map a linestring which is the track of 2 coordinate points that the user supplies.

public class Quickstart {

public stati         


        
相关标签:
1条回答
  • 2021-01-14 16:51

    For the style you need something like the SLD described here, in code that becomes:

        // Create style for the line
        // Style style = SLD.createSimpleStyle(TYPE, Color.red);
        org.geotools.styling.Style style = SLD.createLineStyle(Color.red, 2.0f);
        StyleBuilder sb = new StyleBuilder();
        FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
        PointSymbolizer point = sb.createPointSymbolizer();
        Mark mark = sb.createMark("shape://oarrow");
        mark.setFill(sb.createFill(Color.RED));
        mark.setStroke(sb.createStroke(Color.red));
    
        Graphic graphic = sb.createGraphic(null, mark, null);
        graphic.setRotation(ff.function("endAngle", ff.property("line")));
        point.setGraphic(graphic);
    
        point.setGeometry(ff.function("endpoint", ff.property("line")));
    
        Rule rule = sb.createRule(point);
        style.getFeatureTypeStyles()[0].addRule(rule );
        Layer layer = new FeatureLayer(featureCollection, style);
    

    Zooming into the line is just a case of setting the map viewport to the bounds of the line:

        MapViewport viewport = new MapViewport(featureCollection.getBounds());
        map.setViewport(viewport );
    

    If you want you might want to grow those bounds by a little (10%?) so that you can see the surroundings too.

    EDIT

    To avoid the deprecated methods in StyleBuilder you can use:

    style.featureTypeStyles().get(0).rules().add(rule);
    

    Expanding the bounding box is just a case of adding some distance to the envelope:

        ReferencedEnvelope bounds = featureCollection.getBounds();
        double delta = bounds.getWidth()/20.0; //5% on each side
        bounds.expandBy(delta );
        MapViewport viewport = new MapViewport(bounds);
        map.setViewport(viewport );
    
    0 讨论(0)
提交回复
热议问题