geotools style解析-使用纯色填充

渲染结果

在这里插入图片描述

代码

public class MyStyle1 {

    // 静态变量:样式工厂,用于创建样式相关对象
    static StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory();
    // 静态变量:过滤器工厂,用于创建过滤器相关对象
    static FilterFactory filterFactory = CommonFactoryFinder.getFilterFactory();

    // 主方法,程序入口
    public static void main(String[] args) throws Exception {
        // 创建MyStyle1实例并调用其方法
        MyStyle1 myStyle1 = new MyStyle1();
        myStyle1.displayShapefile();
    }

    // 显示Shapefile的方法
    private void displayShapefile() throws Exception {
        // 使用文件选择器打开一个shp文件,限定文件类型为"shp"
        File file = JFileDataStoreChooser.showOpenFile("shp", null);
        // 如果用户没有选择文件,则直接返回
        if (file == null) {
            return;
        }

        // 获取数据存储对象
        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        // 从数据存储中获取要素源
        FeatureSource featureSource = store.getFeatureSource();

        // 创建地图内容对象并设置标题
        MapContent map = new MapContent();
        map.setTitle("Style");

        // 创建多边形样式
        Style style = createPolygonStyle();

        // 使用要素源和样式创建要素图层
        FeatureLayer layer = new FeatureLayer(featureSource, style);
        // 将图层添加到地图中
        map.addLayer(layer);

        // 显示地图窗口
        JMapFrame.showMap(map);
    }

    // 创建多边形样式的方法
    private Style createPolygonStyle() {
        // 创建描边样式:蓝色、线宽1、不透明度1(完全不透明)
        Stroke stroke = styleFactory.createStroke(
                filterFactory.literal(Color.BLUE),  // 颜色
                filterFactory.literal(1),         // 线宽
                filterFactory.literal(1));        // 不透明度

        // 创建填充样式:青色、不透明度0.5(半透明)
        Fill fill = styleFactory.createFill(
                filterFactory.literal(Color.CYAN), // 颜色
                filterFactory.literal(0.5));       // 不透明度

        // 创建多边形符号化器,使用上面定义的描边和填充
        PolygonSymbolizer sym = styleFactory.createPolygonSymbolizer(stroke, fill, null);

        // 创建规则并添加符号化器
        Rule rule = styleFactory.createRule();
        rule.symbolizers().add(sym);
        
        // 创建要素类型样式并添加规则
        FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle(rule);
        
        // 创建样式对象并添加要素类型样式
        Style style = styleFactory.createStyle();
        style.featureTypeStyles().add(fts);

        // 返回创建的样式
        return style;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值