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源码,说实话,研读源码真是一个痛苦的过程,好多晦涩的语法搞得人晕晕乎乎.这两天算是理解了一小部分,这里先记录下来,也给需要的园友一个参考,奈何博主技术有限,如有理解不妥之 ...
随机推荐
- vue+node+mongoDB 火车票H5(四)---完成静态页面
各项配置都好了,就可以开始写静态页面了,先别急着写,看一下页面又哪些公用的部分可以提取出来的,统一放到components组件文件夹中 header头部文件夹放一些头部常用组件,如首页的banner切 ...
- 160802、1.06s删除10w条重复记录只保留一条(mysql)及linux删除乱码文件
上次发表过的一遍删除重复记录的文章,其中最优的方案三删除1w条也花了0.07秒而2w条就已经花掉了4秒.今天进行了进一步优化,测试删除10w条只花了1.06秒.速度提升了很多. 建表语句 CREATE ...
- FineReport---样式
1.单元格样式 单元格样式说明 2.预定义样式 预定义样式说明 这里发现,改了样式,服务器更新Congfig,需要重启服务器,这样比较麻烦 我的操作是,先设置预定义样式,然后再点击自定义样式,操作是就 ...
- iOS与导航相关的都在这
// 设置导航背景图片 (一旦设置背景图片(变为不透明),透明层不起作用) [self.navigationBar setBackgroundImage:[UIImage imageNamed:@&q ...
- 12.php中无比坑爹的构造函数。
当你在php类中,写一个构造方法时,记得,一定要用__这是两个下划线,而不是一个.......... <?php class Car { // function _construct() { / ...
- 服务器1M带宽同时能承受多少人在线
最近网站的流量一直在增长,这个肯定是好事.不过也有个麻烦的问题,目前本站用的的虚拟空间,每月流量30G,虽然95%的图片都已外链,但流量还是很吃紧,日均2000ip,4月份流量34g左右,单JS的响应 ...
- 深入Redis内部-Redis 源码讲解(转)
Redis作为 NoSQL 数据库的杰出代表,一直广受关注,其轻量级的敏捷架构,向来有存储中的瑞士军刀之称.下面推荐的一篇文章,从源码的角度讲解了Redis 的整个工作流程,是了解 Redis 流程的 ...
- time 模块,random模块,os模块
一 :time 模块 python中,通常有几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行“type(t ...
- Linux上free命令的输出及其他
一.明确概念 A buffer is something that has yet to be "written" to disk. A cache is something t ...
- EXP直接导出压缩问津,IMP直接导入压缩文件的方法
在10G之前,甚至在10G的Oracle环境中,有很多数据量不大,重要性不太高的系统依然采用EXP/IMP逻辑导出备份方式,或者,作为辅助备份方式. 通常情况下,我们都是这样操作的:1.exp导出2. ...