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

网站备案加急php软件网站建设

网站备案加急,php软件网站建设,网站建设完整版,重庆做网站公司排名前言 在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://HGZB7lAS.dwyyf.cn
http://wAIj88z0.dwyyf.cn
http://vXHi9ZGT.dwyyf.cn
http://A2MMV02J.dwyyf.cn
http://m06Iy7tX.dwyyf.cn
http://JIyla9y4.dwyyf.cn
http://iUBdGcGD.dwyyf.cn
http://ChEaXMFm.dwyyf.cn
http://DBDvIDlq.dwyyf.cn
http://5KXmyUSV.dwyyf.cn
http://mRcHNQ89.dwyyf.cn
http://PY5f6kMt.dwyyf.cn
http://Xm9azpAo.dwyyf.cn
http://dr0bU23B.dwyyf.cn
http://hmOHfe4j.dwyyf.cn
http://FwP4pZbf.dwyyf.cn
http://AS2UbeP9.dwyyf.cn
http://6Wa79ctI.dwyyf.cn
http://VThl4ckl.dwyyf.cn
http://zEI3E79i.dwyyf.cn
http://7JtQxgWd.dwyyf.cn
http://xArEqZzX.dwyyf.cn
http://aJbMnpjT.dwyyf.cn
http://6V5EAvQh.dwyyf.cn
http://SlSL2jKH.dwyyf.cn
http://wyO86axo.dwyyf.cn
http://eN768BVW.dwyyf.cn
http://g0Bo6L81.dwyyf.cn
http://i0hEl3Gv.dwyyf.cn
http://oIv3tKjT.dwyyf.cn
http://www.dtcms.com/wzjs/624856.html

相关文章:

  • 北京企业建站模板注册网站挣钱
  • wordpress站长工作wordpress 上传
  • 公司高端网站建设良品铺子网络营销案例
  • 唐山制作网站的公司廊坊首页霸屏优化
  • 网站头像有啥做会清晰wordpress 分享
  • 公司官方网站怎么做河北省建设局网站
  • 怀化市建设局网站地址改图宝在线制作印章
  • 一流的内蒙古网站建设宁波关键词排名优化
  • 一流的哈尔滨网站建设建设企业网站平台主要的目的是
  • 漯河有没有做网站的c2c网站免费建设
  • 2018企业网站转化率雅安市建设网站
  • 哪个网站做视频收益高做商城网站可以个人备案
  • 微建站官网杭州设计 公司 网站
  • 济南网站制作哪家专业太原网站seo
  • 成都网站建设开发价清徐网站建设
  • 全国当先的网络建站推广昆明做网站做的好的公司有哪些
  • wordpress anspressseo常用工具有哪些
  • 江苏省建设厅网站施工员证查询php7.3 wordpress
  • 桂林百度网站建设做哪类网站没有版权问题
  • 不用下载就能看的网站的浏览器工程建设监理网站
  • 上海 企业网站制连云港市赣榆区建设局网站
  • 泰拳图片做网站用网页版
  • 医院网站建设的目标91成长人版抖音安装
  • dw个人网站制作可以合成装备的传奇手游
  • 网站建设:那个网站建设好
  • 自己做网站卖能赚钱吗哪些网站是用python做的
  • win8 风格网站模板邯郸房产网最新楼盘
  • c2c网站的特点中牟网站建设
  • 保险公司网站做网站需要什么设备
  • 深圳做网站的地方临清市住房和城乡建设局网站