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

做元器件上什么网站做网站公司的排名

做元器件上什么网站,做网站公司的排名,桂林生活网爆料,优化关键词哪家好前言 在GIS开发中,经常需要进行数据的转换处理。在之前的文章中讲了如何使用GeoTools读取Shapefile数据,并且展示了将Shapefile数据导入PostGIS空间数据库的多种方式。但是还缺少Shapefile数据转换来源的操作。 本篇教程在之前文章的基础上讲解如何将CSV…

前言

在GIS开发中,经常需要进行数据的转换处理。在之前的文章中讲了如何使用GeoTools读取Shapefile数据,并且展示了将Shapefile数据导入PostGIS空间数据库的多种方式。但是还缺少Shapefile数据转换来源的操作。

本篇教程在之前文章的基础上讲解如何将CSV文件转换为我们熟悉的Shapefile数据。

开发环境

本文使用开发环境如下,仅供参考。
:::block-1
时间:2025年

GeoTools:34-SNAPSHOT

IDE:IDEA2025.1.2

JDK:17

:::

1. 准备CSV文件

CSV(Comma-Separated Values)文件是一种纯文本格式,用于存储表格数据(如电子表格或数据库)。它以结构简单、兼容性广泛而著称,是数据交换中最常用的格式之一。

CSV文本结构:

Name,Age,Occupation
Alice,28,Engineer
Bob,32,Designer
Charlie,45,Manager

CSV表格结构:
image

2. 安装依赖

在之前开发的基础上增加gt-epsg-hsql依赖包。

<dependency><groupId>org.geotools</groupId><artifactId>gt-shapefile</artifactId><version>${geotools.version}</version>
</dependency>
<dependency><groupId>org.geotools</groupId><artifactId>gt-epsg-hsql</artifactId><version>${geotools.version}</version>
</dependency>

3. 读取CSV文件

使用showOpenFile方法打开文件选择框,然后使用createType构造要素结构,第一个参数"location"为要素类型,第二个参数为要素属性。the_geom字段表明数据几何类型为Pointsrid表明数据坐标系为4326以及后面的字段名称和对应字段类型。

// 设置外观
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());// 选择文件
File file = JFileDataStoreChooser.showOpenFile("csv",null);
if(file == null ){return;
}// 创建要素类型
final SimpleFeatureType TYPE = DataUtilities.createType("location","the_geom:Point:srid=4326,"+"name:String,"+"number:Integer"
);

现在可以读取CSV数据并构造Features,使用GeometryFactory来创建几何属性。

// 创建要素
List<SimpleFeature> features = new ArrayList<>();
// GeometryFactory 用来为要素创建几何属性
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);try(BufferedReader reader = new BufferedReader(new FileReader(file))){// 读取第一行头部数据String line = reader.readLine();for(line = reader.readLine(); line != null; line = reader.readLine()){if(line.trim().length()>0){String[] tokens =  line.split("\,");double latitude = Double.parseDouble(tokens[0]);double longitude = Double.parseDouble(tokens[1]);String name = tokens[2].trim();int number = Integer.parseInt(tokens[3].trim());// 构造点Point point = geometryFactory.createPoint(new Coordinate(longitude,latitude));featureBuilder.add(point);featureBuilder.add(name);featureBuilder.add(number);SimpleFeature feature = featureBuilder.buildFeature(null);features.add(feature);}}
}

4. 创建Shapefile

ShapefileDataStoreFactory创建Shp工厂,在createDataStore参数中将属性"create spatial index"设置为true标明为Shp数据创建空间索引。

// 从要素集创建Shapefile
File newFile = getNewShapeFile(file);
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();Map<String, Serializable> params = new HashMap<>();
params.put("url",newFile.toURI().toURL());
params.put("create spatial index",Boolean.TRUE);ShapefileDataStore dataStore =  (ShapefileDataStore) dataStoreFactory.createDataStore(params);// TYPE 用作描述文件内容的模板
dataStore.createSchema(TYPE);

通过确认FeatureSource对象实现了FeatureStore方法来检查是否具有读写权限,使用ListFeatureCollection包装FeatureCollection对象。最后使用transaction.comit()一次性安全地写出所有数据。

// 输出要素数据到Shapefile
Transaction transaction = new DefaultTransaction("create");
String typeName = dataStore.getTypeNames()[0];SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
SimpleFeatureType featureType = featureSource.getSchema();if(featureSource instanceof SimpleFeatureStore){SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;SimpleFeatureCollection featureCollection = new ListFeatureCollection(featureType,features);featureStore.setTransaction(transaction);try {featureStore.addFeatures(featureCollection);transaction.commit();}catch (Exception e){e.printStackTrace();transaction.rollback();}finally {transaction.close();}System.exit(0);
}else {System.out.println(typeName + "缺少读|写权限!!");System.exit(1);
}

5. Shapefile输出位置

使用getNewShapeFile方法选择Shp输出位置。

// 提示输出Shapefile
private static File getNewShapeFile(File csvFile){String path = csvFile.getAbsolutePath();String newPath = path.substring(0,path.length()-4)+".shp";JFileDataStoreChooser chooser = new JFileDataStoreChooser(".shp");chooser.setDialogTitle("保存 ShapeFile");chooser.setSelectedFile(new File(newPath));int returnVal = chooser.showSaveDialog(null);if(returnVal != JFileDataStoreChooser.APPROVE_OPTION){System.exit(0);}File newFile = chooser.getSelectedFile();if(newFile.equals(csvFile)){System.out.println("Error:不能替换" + csvFile);System.exit(0);}return newFile;
}
http://www.dtcms.com/a/438048.html

相关文章:

  • hadoop-hdfs-secondaryNameNode
  • 每日一个网络知识点:OSI参考模型
  • 怎么在国外网站做推广wordpress企业主题制作视频教程
  • K8s不同工作负载对应LOL里哪位英雄
  • 【探寻C++之旅】第十六章:unordered系列的认识与模拟实现
  • 用terraform 创建一个GKE private cluster
  • [优选算法专题三.二分查找——NO.22寻找峰值]
  • 中国建设银行官方网站下载北京企业做网站
  • [优选算法专题三.二分查找——NO.24搜索旋转排序数组中的最⼩值]
  • 微服务项目->在线oj系统(Java-Spring)--竞赛管理
  • 苏州市吴江太湖新城建设局网站网站模版建设教程
  • 【AI Design】如何利用 Paraflow 从创意到产品设计规范
  • 360免费建站网址是什么深圳网站推广哪家好
  • 【Linux系列】并发世界的基石:透彻理解 Linux 进程 — 进程概念
  • Spring AI alibaba 工具调用
  • 机器学习基础入门(第三篇):监督学习详解与经典算法
  • 做产品的淘宝客网站网站建设的素材处理方式
  • 【专业词典】FAST
  • 诸城网站建设wordpress退出维护
  • 预约记录自动关联功能测试
  • 进程“悄悄话”函数——`socketpair`
  • QT肝8天14--编辑用户
  • Redis Zset的底层秘密:跳表(Skip List)的精妙设计
  • 广州金融网站建设2017网站开发语言排名
  • C++ priority_queue优先级队列
  • Kafka 授权与 ACL 深入实践
  • 西宁市住房和城乡建设局网站做一个个人网站
  • 瑞安做网站多少钱东莞网站建设找谁
  • 谷歌云+Apache Airflow,数据处理自动化的强力武器
  • 小红书自动化运营:智能体+RPA自动化+MCP实现采集仿写和自动发布