当前位置: 首页 > news >正文

GIS:生成Shp文件

  /*** 生成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));//设置WriterFeatureWriter<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.lryc.cn/news/161326.html

相关文章:

  • 【日常笔记】使用Server过程中可能遇到的一些问题
  • 【Mysql】给查询记录增加序列号方法
  • Linux 安装elasticsearch-7.5.1
  • ElementUI浅尝辄止26:Notification 通知
  • IDEA新建的Moudle失效显示为灰色
  • Protobuf的简单使用
  • OpenCV 12(图像直方图)
  • LeetCode 面试题 03.06. 动物收容所
  • 快速理解DDD领域驱动设计架构思想-基础篇 | 京东物流技术团队
  • C++学习笔记(堆栈、指针、命名空间、编译步骤)
  • Rust Yew应用开发的事件初探
  • 高并发下单例线程安全
  • 【EKF】EKF原理
  • 蓝桥杯官网填空题(古堡算式)
  • Python---集合set
  • LORA项目源码解读
  • Azure + React + ASP.NET Core 项目笔记一:项目环境搭建(一)
  • html 学习 之 文本标签
  • 联发科3纳米芯片预计2024年量产,此前称仍未获批给华为供货
  • 搭建vue3项目并git管理
  • 【Azure OpenAI】OpenAI Function Calling 101
  • 立晶半导体Cubic Lattice Inc 专攻音频ADC,音频DAC,音频CODEC,音频CLASS D等CL7016
  • 【Flutter】支持多平台 多端保存图片到本地相册 (兼容 Web端 移动端 android 保存到本地)
  • postgresql 安装教程
  • 手写数据库连接池
  • 在CentOS7上增加swap空间
  • @Autowired和@Resource
  • QTableView通过setColumnWidth设置了列宽无效的问题
  • 【用unity实现100个游戏之10】复刻经典俄罗斯方块游戏
  • Docker容器内数据备份到系统本地