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源码,说实话,研读源码真是一个痛苦的过程,好多晦涩的语法搞得人晕晕乎乎.这两天算是理解了一小部分,这里先记录下来,也给需要的园友一个参考,奈何博主技术有限,如有理解不妥之 ... 
随机推荐
- 常用sql 增删改、批量、合并、去重、增列、
			自己总结的一些常用sql :插入.删除.批量更新.判重.新增列.数据库连接数 ---------------- 批量插入跨数据库 insert into ejpms.dbo.role (Name,In ... 
- 机械迷城MAC下载及攻略
			点击下载 无意间在verycd上看到这个游戏,很好玩的一个游戏. 画风非常可爱,有点复古风. 这里是 机械迷城 的专题频道 http://pc.pcgames.com.cn/pczq/jxmc/ 
- 数值和字符串相互转换(C++ 数据类型转换技巧)
			类型转换是将一种类型的值映射为另一种类型的值.进行数据类型的转换. 是在实际代码编写中经常遇到的问题,特别是字符串和其他类型的转换. 1.将字符串转换为整数 (1).转换函数// 双精度函数doubl ... 
- 每隔10秒钟打印一个“Helloworld”
			/** * 每隔10秒钟打印一个“Helloworld” */ public class Test03 { public static void main(String[] args) throws ... 
- python单线程解决并发
			1.单线程解决并发 方式一 import socket import select # 百度创建连接:非阻塞 client1 = socket.socket() client1.setblocking ... 
- 我的Android进阶之旅------>关于使用CSDN-markdown编辑器来编写博客
			关于使用MarkDown编辑器的原因 什么是 Markdown 制作一份待办事宜 Todo 列表 书写一个质能守恒公式LaTeX 高亮一段代码code 高效绘制 流程图 高效绘制序列图 绘制表格 更详 ... 
- unity3d相关资源
			http://pan.baidu.com/s/1kTG9DVD GUI源码 
- PyNest——part 3: connecting networks with synapses
			part 3: connecting networks with synapses parameterising synapse models NEST提供了各种不同的突触模型. 您可以使用命令nes ... 
- Java排序算法总结(转载)
			排序算法 平均时间复杂度 冒泡排序 O(n2) 选择排序 O(n2) 插入排序 O(n2) 希尔排序 O(n1.5) 快速排序 O(N*logN) 归并排序 O(N*logN) 堆排序 O(N*log ... 
- jQuery Mobile 手动显示ajax加载器
			在jquery mobile开发中,经常需要调用ajax方法,异步获取数据,如果异步获取数据方法由于网速等等的原因,会有一个反应时间,如果能在点击按钮后数据处理期间,给一个正在加载的提示,客户体验会更 ... 
