GIS:生成Shp文件

news/2025/2/23 16:16:36
  /**
     * 生成shape文件
     *
     * @param shpPath 生成shape文件路径(包含文件名称)
     * @param encode  编码
     * @param geoType 图幅类型,Point和Rolygon
     * @param geoms   图幅集合
     */
    public static void write2Shape(String shpPath, String encode, String geoType, List<Geometry> geoms) {
        try {
            //创建shape文件对象
            File file = new File(shpPath);
            Map<String, Serializable> params = new HashMap<>();
            params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
            ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
            //定义图形信息和属性信息
            SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
            tb.setCRS(DefaultGeographicCRS.WGS84);
            tb.setName("shapefile");

            if ("Polygon".equals(geoType)) {
                tb.add("the_geom", Polygon.class);
            } else if ("MultiPolygon".equals(geoType)) {
                tb.add("the_geom", MultiPolygon.class);
            } else if ("Point".equals(geoType)) {
                tb.add("the_geom", Point.class);
            } else if ("MultiPoint".equals(geoType)) {
                tb.add("the_geom", MultiPoint.class);
            } else if ("LineString".equals(geoType)) {
                tb.add("the_geom", LineString.class);
            } else if ("MultiLineString".equals(geoType)) {
                tb.add("the_geom", MultiLineString.class);
            } else {
                throw new Exception("Geometry中没有该类型:" + geoType);
            }
            ds.createSchema(tb.buildFeatureType());
            //设置编码
            ds.setCharset(Charset.forName(encode));
            //设置Writer
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
            for (Geometry geom : geoms) {
                SimpleFeature feature = writer.next();
                feature.setAttribute("the_geom", geom);
            }
            writer.write();
            writer.close();
            ds.dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 public static void main(String[] args) throws Exception {
            List<String> list = new ArrayList<>();
            list.add("POLYGON ((116.21278950384274 39.90557982319698, 116.21177234433465 39.90610963061354, 116.21106912279264 39.90264172209895, 116.21399502548638 39.902612822554126, 116.21629305278306 39.905011479365406, 116.21278950384274 39.90557982319698))");
            list.add("POLYGON((113.38185597038 34.54828048706,113.38224220848 34.548355588913,113.38249970055 34.548108825684,113.38237095451 34.54787279129,113.38208127594 34.547786960602,113.38185597038 34.54828048706))");

            List<Geometry> geometryList = new ArrayList<>();
            for (String str : list) {
                Geometry geom = wktToGeom(str);
                geometryList.add(geom);
            }

            String url = "F://tmp//ceshi2222.shp";
            ShapeUtil.write2Shape(url, "utf-8", "Polygon", geometryList);
        }

说几个重要的类:
**DataStore:**访问和存储矢量格式空间数据的引擎,对应关系数据库中database的概念,可以用来更新 ,删除,获取SimpleFeatureType(类比数据库中一张具体表)
**FeatureSource:**与DataStore相比,粒度更细,内部操作都是针对这张表的。
**FeatureStore:**子接口,对数据本身进行设置。
**SimpleFeature:**类比数据库中一条记录,与SimpleFeatureType进行了绑定。
**SimpleFeatureType:**对SimpleFeature进行数据结构约束,类比关系数据库的表结构。
**FeatureCollection:**存储Feature对象的集合类,注意两点:1.其迭代器使用完毕必须显示关闭,2.存储在同一个FeatureCollection中的对象具有相同的SimpleFeatureType。


http://www.niftyadmin.cn/n/5003547.html

相关文章

springcloud3 注册中心以及cloud启动原理总结(含面试)

一 Springcloud微服务面试题 1.1 为何使用注册中心 1)问题描述 在多个单体微服务之间&#xff0c;可以直接通过http请求进行通信&#xff0c;但是存在以下问题&#xff1a; 1.调用服务提供者时需要写ip和端口&#xff0c;如果出现ip和端口进行了修改&#xff0c;没有及时告…

Web自动化测试进阶 —— Selenium模拟鼠标操作

鼠标操作事件 在实际的web产品测试中&#xff0c;对于鼠标的操作&#xff0c;不单单只有click()&#xff0c;有时候还要用到右击、双击、拖动等操作&#xff0c;这些操作包含在ActionChains类中。 ActionChains类中鼠标操作常用方法&#xff1a; 首先导入ActionChains类&…

16字节协议的串口通信

1.协议要求 协议为帧传输&#xff0c;一共16字节。主要是2字节的固定帧头 EB 90&#xff0c;2字节的帧计数(用来计数发出的帧),10字节的数据和2字节的校验位 帧头&#xff1a;2字节&#xff0c;固定值 8’HEB、8’H90 帧计数&#xff1a;2字节&#xff0c;用来说明发出去帧是…

高光谱图像超分辨率-总

高光谱图像超分辨率 高光谱图像超分辨率 高光谱图像超分辨率一、基础内容1.1 高光谱图像特点1.2 研究现状1.3 高光谱图像数据集1.4 评价指标1.5 Wald**协议**二、文献阅读清单2.1 综述+先锋工作1.提出解混的思想。2.随机混合模型在高光谱分辨率增强中的应用。3.遥感中的多光谱和…

如何查找GNU C语言参考手册

快捷通道 标准C/C参考手册 GNU C参考手册HTML版 GNU C参考手册PDF版本 HTML版本部分目录预览 从GNU官网找那个GNU C参考手册 访问gnu.org 点击软件 下滑找到gnu-c-manual或者在这个页面Ctrl-f搜索"manual" 点进去即可看到HTML版本和PDF版本

Linux系统Redis的集群搭建

redis的集群搭建 此案例在3台虚拟机上每台机搭建一个master主节点和一个slave从节点&#xff0c;共3个master节点&#xff0c;3个slave从节点&#xff0c;共计6个redis节点&#xff1b;&#xff08;集群至少需要存在3个主节点&#xff0c;如果只有2个&#xff0c;则其中一个主节…

C语言malloc函数学习

malloc的全称是memory allocation&#xff0c;中文叫动态内存分配&#xff0c;用于申请一块连续的指定大小的内存块区域&#xff0c;以void*类型返回分配的内存区域地址&#xff1b; 函数原型为void *malloc(unsigned int size)&#xff0c;在内存的动态存储区中分配一个长度为…

Chrome 和 Edge 上出现“status_breakpoint”错误解决办法

文章目录 STATUS_BREAKPOINTSTATUS_BREAKPOINT报错解决办法Chrome浏览器 Status_breakpoint 错误修复- 将 Chrome 浏览器更新到最新版本- 卸载不再使用的扩展程序和应用程序- 安装计算机上可用的任何更新&#xff0c;尤其是 Windows 10- 重启你的电脑。 Edge浏览器 Status_brea…