geoserver源码学习与扩展——CSV转shapefile文件
基于geotools实现csv转换为shapefile文件。
1、读取CSV文件,将其装入FeatureCollection;
2、利用ShapefileDumper类将FeatureCollection转存到硬盘;
/*
* transform CSV to FeatureCollection
*/
1 public void processCSVFile(File csvFile){ ListFeatureCollection collection;
try{
typeName = csvFile.getName().replace(".", "_");
typeName = replaceBlank(typeName);
BufferedReader reader = new BufferedReader(new FileReader(csvFile));
try{
/* First line of the data file is the header */
String line = reader.readLine();
System.out.println("Header: " + line); /*
* We use the DataUtilities class to create a FeatureType that will describe the data in our
* shapefile.
*
* See also the createFeatureType method below for another, more flexible approach.
*/
String[] strArray = line.split("\\,");
int latDex = -1;
int longDex = -1;
List<String> typeSpec = new ArrayList<String>();
typeSpec.add("the_geom:Point:srid="+ SRID); // <- the geometry attribute: Point type
for(int i = 0; i < strArray.length; i ++){
if(strArray[i].contains("Lat")){
latDex = i;
}
else if(strArray[i].contains("Long")){
longDex = i;
}
else{
typeSpec.add(strArray[i] + ":String"); //all String type
}
}
if(CreateCluster)
typeSpec.add("cluster:String"); //add cluster field
String typeSpecs = String.join(",", typeSpec);
final SimpleFeatureType TYPE = DataUtilities.createType(typeName,
typeSpecs // other attribute
);
/*
* GeometryFactory will be used to create the geometry attribute of each feature (a Point
* object for the location)
*/
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
/*
* We create a FeatureCollection into which we will put each Feature created from a record
* in the input csv data file
*/
collection = new ListFeatureCollection(TYPE); for (line = reader.readLine(); line != null; line = reader.readLine()) {
if (line.trim().length() > 0) { // skip blank lines
String tokens[] = line.split("\\,");
double latitude = Double.parseDouble(tokens[latDex]);
double longitude = Double.parseDouble(tokens[longDex]); /* Longitude (= x coord) first ! */
Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude)); featureBuilder.add(point);;
for(int i = 0; i < tokens.length; i ++){
if(latDex != i && longDex != i){
String strVal = tokens[i].trim();
featureBuilder.add(strVal);
}
} if(CreateCluster)
featureBuilder.add(""); //add cluster field value
SimpleFeature feature = featureBuilder.buildFeature(null);
collection.add(feature);
}
}
} finally {
reader.close();
}
writeShapeFile(collection); }
catch(Exception ex){
System.out.println(ex.getMessage());
}
}
CSV文件并不包含各字段的类型说明,将经纬度字段转换为Geometry,其他字段都为String类型;
另外,DataUtilities.createType方法创建要素方案时,将所有的String类型的字段长度都设为了java环境下的默认长度254,若字段太多或要素太多会导致shapefile中的dbf文件过大,可参考DataUtilities类,编写自定义类将String类型长度设为自定义长度;
/*
* FeatureCollection to shapefile
*/
private void writeShapeFile(SimpleFeatureCollection collection){
final File baseDir = resourceLoader.getBaseDirectory();
File shpDir = new File(baseDir.getAbsolutePath() + "/Data/upload");
if(!shpDir.exists()){
shpDir.mkdirs();
}
if(!shpDir.exists())
return; ShapefileDumper dumper = new ShapefileDumper(shpDir);
dumper.setMaxDbfSize(maxDbfSize);
dumper.setMaxShpSize(maxShpSize);
dumper.setCharset(Charset.forName(charSet)); // target charset try {
// if an empty result out of feature type with unknown geometry is created, the
// zip file will be empty and the zip output stream will break
boolean shapefileCreated = false;
shapefileCreated |= dumper.dump(collection); // take care of the case the output is completely empty
if(!shapefileCreated) {
createEmptyWarning(shpDir);
} }
catch(Exception ex){
System.out.println(ex.getMessage());
} } private void createEmptyWarning(File tempDir) throws IOException {
PrintWriter pw = null;
try {
pw = new PrintWriter(new File(tempDir, "README.TXT"));
pw.print("The CSV Upload result is empty, and the geometric type of the features is unknwon:"
+ "an empty point shapefile has been created");
} finally {
pw.close();
}
}
geoserver源码学习与扩展——CSV转shapefile文件的更多相关文章
- geoserver源码学习与扩展——自动发布shapefile图层
geoserver通过工作空间Workspace-数据源DataStore-图层Layer管理地理数据,这些信息都通过Catalog进行组织和管理,要完成自动发布只需要在Catalog中增加相应的信息 ...
- geoserver源码学习与扩展——跨域访问配置
在 geoserver源码学习与扩展——restAPI访问 博客中提到了geoserver的跨域参数设置,本文详细讲一下geoserver的跨域访问配置. geoserver的跨域访问依赖java-p ...
- geoserver源码学习与扩展——kml/kmz转shapefile文件
geoserver通过工作空间Workspace-数据源DataStore-图层Layer管理地理数据,默认只支持shapefile格式的文件发布,不支持kml/kmz.csv的文件格式,所以存在将这 ...
- geoserver源码学习与扩展——restAPI访问
产生这篇文章的想法是在前端通过js调用restAPI时,总是不成功,发送ajax请求时还总是出现类似跨域的问题,后来查找才发现,默认情况下restAPI的访问都需要管理员权限,而通过ajax请求传输用 ...
- geoserver源码学习与扩展——增加服务接口
参看:http://www.cnblogs.com/sillyemperor/archive/2011/01/11/1929420.html 上文写的很详细了.
- ddms(基于 Express 的表单管理系统)源码学习
ddms是基于express的一个表单管理系统,今天抽时间看了下它的代码,其实算不上源码学习,只是对它其中一些小的开发技巧做一些记录,希望以后在项目开发中能够实践下. 数据层封装 模块只对外暴露mod ...
- MVC系列——MVC源码学习:打造自己的MVC框架(四:了解神奇的视图引擎)
前言:通过之前的三篇介绍,我们基本上完成了从请求发出到路由匹配.再到控制器的激活,再到Action的执行这些个过程.今天还是趁热打铁,将我们的View也来完善下,也让整个系列相对完整,博主不希望烂尾. ...
- MVC系列——MVC源码学习:打造自己的MVC框架(三:自定义路由规则)
前言:上篇介绍了下自己的MVC框架前两个版本,经过两天的整理,版本三基本已经完成,今天还是发出来供大家参考和学习.虽然微软的Routing功能已经非常强大,完全没有必要再“重复造轮子”了,但博主还是觉 ...
- MVC系列——MVC源码学习:打造自己的MVC框架(一:核心原理)
前言:最近一段时间在学习MVC源码,说实话,研读源码真是一个痛苦的过程,好多晦涩的语法搞得人晕晕乎乎.这两天算是理解了一小部分,这里先记录下来,也给需要的园友一个参考,奈何博主技术有限,如有理解不妥之 ...
随机推荐
- JavaScript学习笔记-构造函数
什么是构造函数 简单说构造函数是类函数,函数名与类名完全相同,且无返回值.构造函数是类的一个特殊成员函数. JavaScript构造函数 * 在JavaScript的世界里没有类的概念,JavaScr ...
- 【JS】JS中对于this的理解
一.对this的产生原因分析和了解 第一:this指的是函数运行时所在的环境(即调用的对象). 第二:JavaScript 语言之所以有this的设计,跟内存里面的数据结构有关系(内存存储详细理解参考 ...
- MonogoDB----Date()
以字符串或日期对象的形式返回日期. 使用new Date(),插入的是一个isodate类型:而使用Date()插入的是一个字符串类型. 那isodate是什么日期类型的?我们看这2个值,它比字符串大 ...
- As of Flume 1.4.0, Avro is the default RPC protocol.
Flume 1.8.0 Developer Guide — Apache Flume http://flume.apache.org/FlumeDeveloperGuide.html The remo ...
- Spring - Netty (整合)
写在前面 大家好,我是作者尼恩.目前和几个小伙伴一起,组织了一个高并发的实战社群[疯狂创客圈].正在开始 高并发.亿级流程的 IM 聊天程序 学习和实战,此文是: 疯狂创客圈 Java ...
- rainbow table 彩虹表
RainbowTable 的使用和性能的小测试 - SV的边界 - CSDN博客 https://blog.csdn.net/cecilulysess/article/details/4804707 ...
- tortoisegit错误: disconnected - no supported authentication methods available(server sent: publickey)
修改小乌龟的 SSH客户端:
- Java 之继承和 final 关键字
继承的概述 继承的特点 super 关键字 函数覆盖 子类的实例化过程 final 关键字 1. 继承的概述 继承是类与类之间的关系. 继承的好处: 提高了代码的复用性 让类与类之间产生了关系, 给第 ...
- 解决: ./netapp.bin: error while loading shared libraries: libcaffe.so.1.0.0: cannot open shared object file: No such file or directory 运行时报错(caffe)
caffe安装好后lib没有配置到/usr/lib或/usr/local/lib中,需手动配置: export LD_LIBRARY_PATH=/path_to_your_caffe/build/li ...
- django 多数据库时 ORM语句 选择数据库
多数据库时ORM语句选择数据库 不需要save的操作: 查询(删除的话查询语句后面加一个.delete()即可,修改的话在后面加一个.update()即可): models.表名.objects.us ...