java实现List对象转geojson文本返回前端

1.业务需求

查询带有经纬度数据的list列表,将其转为geojson格式给前端。

2.GeoJson格式说明

GeoJSON是一种对各种地理数据结构进行编码的格式,基于Javascript对象表示法(JavaScript Object Notation, 简称JSON)的地理空间信息数据交换格式。GeoJSON对象可以表示几何、特征或者特征集合。GeoJSON支持下面这几种几何类型:点、线、面、多点、多线、多面和几何集合。GeoJSON里的特征包含一个几何对象和其他属性,特征集合表示一系列特征。

3.JAVA代码实现:

java 复制代码
public Map getSectionStringGeojson(DataBaseSection dataBaseSection) {
        Map map = new HashMap();
        //查询断面数据
        List<DataBaseSection> dataBaseSectionList = dataBaseSectionMapper.selectDataBaseSectionListNoGeom(dataBaseSection);
        if(dataBaseSectionList == null || dataBaseSectionList.size() == 0) {
            return map;
        }
        //使用stream方法,获取dataBaseSectionList中lgtd的最大值和最小值
        BigDecimal maxLgtd = dataBaseSectionList.stream().map(DataBaseSection::getLgtd).max(BigDecimal::compareTo).get();
        BigDecimal minLgtd = dataBaseSectionList.stream().map(DataBaseSection::getLgtd).min(BigDecimal::compareTo).get();
        BigDecimal maxLttd = dataBaseSectionList.stream().map(DataBaseSection::getLttd).max(BigDecimal::compareTo).get();
        BigDecimal minLttd = dataBaseSectionList.stream().map(DataBaseSection::getLttd).min(BigDecimal::compareTo).get();
        map.put("maxLgtd", maxLgtd);
        map.put("minLgtd", minLgtd);
        map.put("maxLttd", maxLttd);
        map.put("minLttd", minLttd);
        //创建一个空的 SimpleFeatureTypeBuilder 对象,并添加属性字段:
        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
        builder.setName("Section");
        builder.add("geometry", Point.class);
        builder.add("bscd", String.class);
        builder.add("bscnm", String.class);
        builder.add("sttp", String.class);
        builder.add("fcd", String.class);
        builder.add("ocd", String.class);
        //使用 SimpleFeatureBuilder 创建一个 SimpleFeatureType 对象:
        SimpleFeatureType featureType = builder.buildFeatureType();
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
        DefaultFeatureCollection featureCollection = new DefaultFeatureCollection();
        //遍历查询结果集,将每一行数据转换为一个 SimpleFeature 对象,并将其添加到 SimpleFeatureCollection 中:
        for(int i = 0; i < dataBaseSectionList.size(); i++) {
            DataBaseSection obj = dataBaseSectionList.get(i);
            //存储数据
            double x = obj.getLgtd().doubleValue();
            double y = obj.getLttd().doubleValue();
            // 创建一个 Point 对象
            GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
            Coordinate coordinate = new Coordinate(x, y);
            Point point = geometryFactory.createPoint(coordinate);
            // 将坐标和属性数据设置到 SimpleFeature 对象中
            featureBuilder.add(point);
            featureBuilder.add(obj.getBscd());
            featureBuilder.add(obj.getBscnm());
            featureBuilder.add(obj.getSttp());
            featureBuilder.add(obj.getFcd());
            featureBuilder.add(obj.getOcd());
            SimpleFeature feature = featureBuilder.buildFeature(null);
            // 将 SimpleFeature 对象添加到 SimpleFeatureCollection 中
            featureCollection.add(feature);
        }
        //使用 FeatureJSON 将 SimpleFeatureCollection 转换为 GeoJSON 字符串:
        String geojson = "";
        try {
            if(featureCollection != null && featureCollection.size() > 0){
                geojson = new FeatureJSON().toString(featureCollection);
            }
        }catch (IOException e){
            System.out.println("基础断面geojson文件保存异常!");
        }
        map.put("geojson", geojson);
        return map;
    }
相关推荐
GISer_Jing3 分钟前
[总结篇]个人网站
前端·javascript
eternal__day16 分钟前
Spring Cloud 多机部署与负载均衡实战详解
java·spring boot·后端·spring cloud·负载均衡
颜淡慕潇20 分钟前
Redis 实现分布式锁:深入剖析与最佳实践(含Java实现)
java·redis·分布式
疯狂的沙粒25 分钟前
在web-view 加载的本地及远程HTML中调用uniapp的API及网页和vue页面是如何通讯的?
前端·uni-app·html
程序员秘密基地26 分钟前
基于vscode,idea,java,html,css,vue,echart,maven,springboot,mysql数据库,在线考试系统
java·vue.js·spring boot·spring·web app
何中应27 分钟前
【设计模式-5】设计模式的总结
java·后端·设计模式
小妖66629 分钟前
html 滚动条滚动过快会留下边框线
前端·html
heroboyluck43 分钟前
Svelte 核心语法详解:Vue/React 开发者如何快速上手?
前端·svelte
海的诗篇_44 分钟前
前端开发面试题总结-JavaScript篇(二)
开发语言·前端·javascript·typescript
吾日三省吾码1 小时前
Spring 团队详解:AOT 缓存实践、JSpecify 空指针安全与支持策略升级
java·spring·缓存