众所周知Geotools作为开源的Java GIS三方库,已经成为GIS服务器端的主流开源库,其功能非常强大,涉及到GIS业务的方方面面,其中就包括GIS数据的读写,今天小编就借助Geotools来实现shape数据的写入。

Geotools对于shape数据写入,主要提供了SimpleFeatureStore和FeatureWriter两个主要操作类,下面小编就根据这两个类实现shape数据的写入,废话不多说,直接上代码:

import org.geotools.data.*;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.simple.SimpleFeatureStore;
import org.opengis.feature.simple.SimpleFeature; import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map; public class ShapwWriterTest { public static void main(String[] args) throws IOException {
File file = new File("D:\\data\\line_sheng.shp");
ShapefileDataStore shapefileDataStore = new ShapefileDataStore(file.toURI().toURL());
SimpleFeatureSource simpleFeatureSource = shapefileDataStore.getFeatureSource();
int count = simpleFeatureSource.getFeatures().size();
for(int i = 0;i<2; i++){
//分批插入(没啥逻辑,主要是验证多次写入同一个shp)
Query query = createQuery(i*(count / 2),count / 2);
SimpleFeatureCollection simpleFeatureCollection = simpleFeatureSource.getFeatures(query);
addFeature2Shp(simpleFeatureCollection,"D:\\data\\line_sheng_1.shp");
}
} /**
* 将simplefearurecollection写入目标shape
* @param simpleFeatureCollection
* @param filePath
* @throws IOException
*/
public static void addFeature2Shp(SimpleFeatureCollection simpleFeatureCollection, String filePath) throws IOException {
File file = new File(filePath);
ShapefileDataStore shapefileDataStore = null;
if (file.exists()){
shapefileDataStore = (ShapefileDataStore) DataStoreFinder.getDataStore(Collections.singletonMap("url",file.toURI().toURL()));
}else{
ShapefileDataStoreFactory shapefileDataStoreFactory = new ShapefileDataStoreFactory();
shapefileDataStore = (ShapefileDataStore) shapefileDataStoreFactory.createNewDataStore(Collections.singletonMap("url",file.toURI().toURL()));
shapefileDataStore.setCharset(Charset.defaultCharset());
shapefileDataStore.createSchema(simpleFeatureCollection.getSchema());
}
//获取simplefeaturestore
writerFeature(simpleFeatureCollection, shapefileDataStore);
//writerFeature1(simpleFeatureCollection,shapefileDataStore);
} /**
* 使用SimpleFeatureStore写入shape文件
* @param simpleFeatureCollection
* @param shapefileDataStore
* @throws IOException
*/
private static void writerFeature(SimpleFeatureCollection simpleFeatureCollection, ShapefileDataStore shapefileDataStore) throws IOException {
SimpleFeatureStore simpleFeatureStore = (SimpleFeatureStore) shapefileDataStore.getFeatureSource(shapefileDataStore.getTypeNames()[0]);
Transaction transaction = new DefaultTransaction("create");
simpleFeatureStore.setTransaction(transaction);
try {
simpleFeatureStore.addFeatures(simpleFeatureCollection);
transaction.commit();
} catch (Exception e) {
transaction.rollback();
} finally {
transaction.close();
}
} /**
* 使用FeatureWriter来写feature
* @param simpleFeatureCollection
* @param shapefileDataStore
* @throws IOException
*/
private static void writerFeature1(SimpleFeatureCollection simpleFeatureCollection, ShapefileDataStore shapefileDataStore) throws IOException {
FeatureWriter featureWriter = shapefileDataStore.getFeatureWriterAppend(Transaction.AUTO_COMMIT);
SimpleFeatureIterator simpleFeatureIterator = simpleFeatureCollection.features();
while(simpleFeatureIterator.hasNext()){
SimpleFeature simpleFeature = simpleFeatureIterator.next();
SimpleFeature simpleFeature1 = (SimpleFeature) featureWriter.next();
simpleFeature1.setAttributes(simpleFeature.getAttributes());
}
featureWriter.write();
featureWriter.close();
simpleFeatureIterator.close();
} private static Query createQuery(int startIndex,int queryCount){
Query query = new Query();
query.setStartIndex(startIndex);
query.setMaxFeatures(queryCount);
return query;
} /**
* 总结geotools 读取shape的几种方式
*/
private static void testReaderShape(String filePath) throws IOException {
//第一种方式
ShapefileDataStore shapefileDataStore = new ShapefileDataStore(new File(filePath).toURI().toURL());
/**
* 使用上述这种方式读shape的话,其中的很多参数都是默认的,最主要的是它的编码是StandardCharsets.ISO_8859_1
* 因此我们需要单独设置下
*/
shapefileDataStore.setCharset(Charset.forName("UTF-8")); //第二种ShapefileDataStoreFactory
ShapefileDataStoreFactory shapefileDataStoreFactory = new ShapefileDataStoreFactory();
Map<String,?> paramMap = new HashMap<>();
/**
* 通常有那些参数,我们可以通过下面的这个函数去查看,这里面
*/
shapefileDataStoreFactory.createNewDataStore(paramMap);
//第三种方式,这种方式可适用于各种基于SPI模式的文件读写
DataStoreFinder.getDataStore(paramMap);
} }

好了,今天关于Geotools写入shape的代码就分享到这里,而关于shape文件的操作,还有很多内容,其中最主要的过滤(Filter)后续也会出个专题来记录下,毕竟这里的东西很多。

Geotools实现shape文件的写入的更多相关文章

  1. JAVA用geotools读写shape格式文件

    转自:http://toplchx.iteye.com/blog/1335007 JAVA用geotools读写shape格式文件 (对应geotools版本:2.7.2) (后面添加对应geotoo ...

  2. JAVA用geotools读取shape格式文件

    Shapefile属于一种矢量图形格式,它能够保存几何图形的位置及相关属性.但这种格式没法存储地理数据的拓扑信息. 其中,要组成一个Shapefile,有三个文件是必不可少的,它们分别是". ...

  3. 【C#】C#中使用GDAL3(二):Windows下读写Shape文件及超详细解决中文乱码问题

    转载请注明原文地址:https://www.cnblogs.com/litou/p/15035790.html 本文为<C#中使用GDAL3>的第二篇,总目录地址:https://www. ...

  4. Java使用Geotools读取shape矢量数据

    作为GIS开发者而言,矢量数据是我们经常要用到的,而shape数据是矢量数据中最常用的格式,因此解析shape数据也是作为GIS软件开发人员必备的基础技能,而GeoTools无疑是Java最好用来处理 ...

  5. java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

  6. Android从网络某个地址下载文件、写入SD卡

    首先创建一个HttpDownloader类,获取下载文件的网络地址,将文件下载下来以String流的方式返回: public String download(String urlStr){ //url ...

  7. 自定义shape文件

    1.shape文件 btn_bg.xml文件内容 <?xml version="1.0" encoding="utf-8"?> <shape ...

  8. DWG2SHP DXF2SHP 如何把AutoCAD的DWG,DXF文件转换为Esri ArcGIS的Shape文件

    dwg是AutoCAD创立的一种图纸保存格式,已经成为二维CAD的标准格式,很多其他CAD为了兼容AutoCAD,也直接使用dwg作为默认工作文件. 地图shape文件由ESRI开发,一个ESRI的s ...

  9. 看到shape文件可以加载到GOOGLE EARTH上的方法,有空可以试试

    引用 Shape文件转为KMZ并在Google Earth中显示 (1)在ArcGIS中加载一个Shape文件,笔者加载的是某个地区的道路(双线道路)图层 (2)在ArcToolbox中,依次展开Co ...

  10. Python中,添加写入数据到已经存在的Excel的xls文件,即打开excel文件,写入新数据

    背景 Python中,想要打开已经存在的excel的xls文件,然后在最后新的一行的数据. 折腾过程 1.找到了参考资料: writing to existing workbook using xlw ...

随机推荐

  1. 2021-08-30:给定两个字符串str1和str2,在str1中寻找一个最短子串,能包含str2的所有字符,字符顺序无所谓,str1的这个最短子串也可以包含多余的字符。返回这个最短包含子串。

    2021-08-30:给定两个字符串str1和str2,在str1中寻找一个最短子串,能包含str2的所有字符,字符顺序无所谓,str1的这个最短子串也可以包含多余的字符.返回这个最短包含子串. 福大 ...

  2. json在线效验检测工具

    json在线效验检测工具:https://www.sojson.com/ 解析结果: { 'os_type': 'Windows', 'os_release': '10 64bit 10.0.1904 ...

  3. drf——restful规范、序列化反序列化、drf介绍和快速使用、drf之APIView源码

    1.restful规范 # restful是一种定义API接口的设计风格,API接口的编写规范,尤其适用于前后端分离的应用模式中 这种风格的理念人为后端开发任务就是提供数据的,对外提供的是数据资源的访 ...

  4. AcWing 1230. K倍区间

    给定一个长度为 N 的数列,A1,A2,-AN,如果其中一段连续的子序列 Ai,Ai+1,-Aj 之和是 K 的倍数,我们就称这个区间 [i,j] 是 K倍区间. 你能求出数列中总共有多少个 K倍区间 ...

  5. 商业智能 (BI) 对企业中每个员工的 5 大好处

    本文由葡萄城技术团队于博客园原创并首发.葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 众所周知,商业智能 (BI) 是探索企业数据价值的强大工具,能够帮助企业做出明智的决策.提高绩效 ...

  6. UART-UART非常见波特率调试应用笔记

    UART非常见波特率调试 应用笔记 串口通信中的波特率选择,对于确保可靠的数据传输至关重要.波特率是衡量单位时间内传输的比特数,常见的波特率包括300.1200.2400.9600.115200等.不 ...

  7. ELK日志收集记录

    logstash在需要收集日志的服务器里运行,将日志数据发送给es 在kibana页面查看es的数据 es和kibana安装: Install Elasticsearch with RPM | Ela ...

  8. Java:错误:不支持发行版本5

    #解决方案1 1.点击File--Project Structure 2.点击Project 3.查看jdk版本是否和安装的一样 4.点击Modules 查看版本 5.点击Preferences--B ...

  9. Kafka 如何保证消息不被重复消费?或者说,如何保证消息消费的幂等性?

    如何保证消息不被重复消费?或者说,如何保证消息消费的幂等性? >幂等性,通俗点说,就一个数据,或者一个请求,给你重复来多次,你得确保对应的数据是不会改变的,不能出错. kafka 的机制:  K ...

  10. 如何制作 GitHub 个人主页

    人们在网上首先发现你的地方是哪里?也许你的社交媒体是人们搜索你时首先发现的东西,亦也许是你为自己创建的投资组合网站.然而,如果你使用GitHub来分享你的代码并参与开源项目,那么你的GitHub个人主 ...