- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章java 中Excel转shape file的实例详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
java 中excel转shape file的实例详解 。
概述:
本文讲述如何结合geotools和poi实现excel到shp的转换,再结合前文shp到geojson数据的转换,即可实现用户上传excel数据并在web端的展示功能.
截图:
原始excel文件 。
运行耗时 。
运行结果 。
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
package
com.lzugis.geotools;
import
com.lzugis.commonmethod;
import
com.vividsolutions.jts.geom.coordinate;
import
com.vividsolutions.jts.geom.geometryfactory;
import
com.vividsolutions.jts.geom.point;
import
org.apache.poi.hssf.usermodel.hssfcell;
import
org.apache.poi.hssf.usermodel.hssfrow;
import
org.apache.poi.hssf.usermodel.hssfsheet;
import
org.apache.poi.hssf.usermodel.hssfworkbook;
import
org.apache.poi.poifs.filesystem.poifsfilesystem;
import
org.geotools.data.featurewriter;
import
org.geotools.data.transaction;
import
org.geotools.data.shapefile.shapefiledatastore;
import
org.geotools.data.shapefile.shapefiledatastorefactory;
import
org.geotools.feature.simple.simplefeaturetypebuilder;
import
org.geotools.referencing.crs.defaultgeographiccrs;
import
org.opengis.feature.simple.simplefeature;
import
org.opengis.feature.simple.simplefeaturetype;
import
java.io.file;
import
java.io.fileinputstream;
import
java.io.inputstream;
import
java.io.serializable;
import
java.nio.charset.charset;
import
java.util.arraylist;
import
java.util.hashmap;
import
java.util.list;
import
java.util.map;
/**
* created by admin on 2017/9/6.
*/
public
class
xls2shape {
static
xls2shape xls2shp =
new
xls2shape();
private
static
string rootpath = system.getproperty(
"user.dir"
);
private
commonmethod cm =
new
commonmethod();
private
hssfsheet sheet;
private
class
getcelltype(hssfcell cell) {
if
(cell.getcelltype() == hssfcell.cell_type_string) {
return
string.
class
;
}
else
if
(cell.getcelltype() == hssfcell.cell_type_numeric) {
return
double
.
class
;
}
else
{
return
string.
class
;
}
}
private
object getcellvalue(hssfcell cell) {
if
(cell.getcelltype() == hssfcell.cell_type_string) {
return
cell.getrichstringcellvalue().getstring();
}
else
if
(cell.getcelltype() == hssfcell.cell_type_numeric) {
return
cell.getnumericcellvalue();
}
else
{
return
""
;
}
}
private
list<map<string, object>> getexcelheader() {
list<map<string, object>> list =
new
arraylist();
hssfrow header = sheet.getrow(
0
);
hssfrow value = sheet.getrow(
1
);
//获取总列数
int
colnum = header.getphysicalnumberofcells();
for
(
int
i =
0
; i < colnum; i++) {
hssfcell cellfield = header.getcell(i);
hssfcell cellvalue = value.getcell(i);
string fieldname = cellfield.getrichstringcellvalue().getstring();
fieldname = cm.getpinyinheadchar(fieldname);
class
fieldtype = getcelltype(cellvalue);
map<string, object> map =
new
hashmap<string, object>();
map.put(
"name"
, fieldname);
map.put(
"type"
, fieldtype);
list.add(map);
}
return
list;
}
public
void
excel2shape(string xlsfile, string shppath) {
poifsfilesystem fs;
hssfworkbook wb;
hssfrow row;
try
{
inputstream is =
new
fileinputstream(xlsfile);
fs =
new
poifsfilesystem(is);
wb =
new
hssfworkbook(fs);
sheet = wb.getsheetat(
0
);
//获取总列数
int
colnum = sheet.getrow(
0
).getphysicalnumberofcells();
// 得到总行数
int
rownum = sheet.getlastrownum();
list list = getexcelheader();
//创建shape文件对象
file file =
new
file(shppath);
map<string, serializable> params =
new
hashmap<string, serializable>();
params.put(shapefiledatastorefactory.urlp.key, file.touri().tourl());
shapefiledatastore ds = (shapefiledatastore)
new
shapefiledatastorefactory().createnewdatastore(params);
//定义图形信息和属性信息
simplefeaturetypebuilder tb =
new
simplefeaturetypebuilder();
tb.setcrs(defaultgeographiccrs.wgs84);
tb.setname(
"shapefile"
);
tb.add(
"the_geom"
, point.
class
);
for
(
int
i =
0
; i < list.size(); i++) {
map<string, object> map = (map<string, object>) list.get(i);
tb.add(map.get(
"name"
).tostring(), (
class
) map.get(
"type"
));
}
ds.createschema(tb.buildfeaturetype());
//设置编码
charset charset = charset.forname(
"gbk"
);
ds.setcharset(charset);
//设置writer
featurewriter<simplefeaturetype, simplefeature> writer = ds.getfeaturewriter(ds.gettypenames()[
0
], transaction.auto_commit);
//写下一条
simplefeature feature =
null
;
for
(
int
i =
1
; i < rownum; i++) {
row = sheet.getrow(i);
feature = writer.next();
map maplonlat =
new
hashmap();
for
(
int
j =
0
; j < colnum; j++) {
hssfcell cell = row.getcell(j);
map<string, object> mapfields = (map<string, object>) list.get(j);
string fieldname = mapfields.get(
"name"
).tostring();
feature.setattribute(fieldname, getcellvalue(cell));
if
(fieldname.tolowercase().equals(
"lon"
) || fieldname.tolowercase().equals(
"lat"
)) {
maplonlat.put(fieldname, getcellvalue(cell));
}
}
feature.setattribute(
"the_geom"
,
new
geometryfactory().createpoint(
new
coordinate((
double
) maplonlat.get(
"lon"
), (
double
) maplonlat.get(
"lat"
))));
}
writer.write();
writer.close();
ds.dispose();
}
catch
(exception e) {
e.printstacktrace();
}
}
public
static
void
main(string[] args) {
long
start = system.currenttimemillis();
string xlspath = rootpath +
"/data/xls/capital.xls"
,
shppath = rootpath +
"/out/capital.shp"
;
xls2shp.excel2shape(xlspath, shppath);
system.out.println(
"共耗时"
+ (system.currenttimemillis() - start) +
"ms"
);
}
}
|
说明:
1、转换仅限点对象的转换; 2、保留所有excel相关的属性,lon、lat字段是必须要有的; 3、对于中文字段,做了取首字母的处理; 。
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持! 。
原文链接:http://blog.csdn.net/gisshixisheng/article/details/77877031 。
最后此篇关于java 中Excel转shape file的实例详解的文章就讲到这里了,如果你想了解更多关于java 中Excel转shape file的实例详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我想知道是否可以访问放在 tomcat 的 conf 文件夹中的文件。通常我会在这个文件中放置多个 webapp 的配置,在 war 之外。 我想使用类路径独立于文件系统。 我过去使用过 lib 文件
我有一个 PowerShell 脚本,它获取文件列表并移动满足特定条件的文件。为什么即使对象为空,foreach 循环也会运行? 我假设如果 $i 不存在,它就不会运行。但是如果 $filePath
我已将 BasicAccountRule.drl 放置在我的 Web 应用程序中,位置为:C:/workspace/exim_design/src/main/resources/rules/drl/i
我使用 File.open('file.txt').class 和 File.open('file.txt').readlines.class 以及前者进行了检查一个返回 File,后者返回 Arra
我正在尝试使用 FileOutputStream 删除文件,在其中写入内容后。这是我用来编写的代码: private void writeContent(File file, String fileC
我正在尝试使用 flink 和 python 批处理 api 测试 Wordcount 经典示例。我的问题是,将数据源从 env.from_elements() 修改为 env.read_text()
我正在尝试制作一个可以同时处理多个不同文件的程序。我的想法是制作一个包含 20 个 FILE* 的数组,以便在我达到此限制时能够关闭其中一个并打开请求的新文件。 为此,我想到了一个函数,它选择一个选项
我有两个文件A和B文件A: 976464 792992 文件B TimeStamp,Record1,976464,8383,ABCD 我想搜索文件 A 和文件 B 中的每条记录并打印匹配的记录。打印的
我有一些保存在 map 中的属性文件。示例: Map map = new HashMap<>(); map.put("1", "One"); map.put("2", "Two"); map.put(
我正在尝试找出一个脚本文件,该文件接受一个包含文件列表的文件(每一行都是一个文件路径,即 path/to/file)并将它们合并到一个文件中。 例如: list.text -- path/to/fil
为了使用 File.CreateText() 和 File.AppendText() 你必须: 通过调用这些方法之一打开流 写消息 关闭流 处理流 为了使用 File.AppendAllText()
使用rsync时,如何在使用--files-from参数复制时重命名文件?我有大约190,000个文件,在从源复制到目标时,每个文件都需要重命名。我计划将文件列表放在一个文本文件中传递给--files
我在非服务器应用程序中使用 Spring(只需从 Eclipse 中某个类的 main() 编译并运行它)。 我的问题是作为 new FileSystemXmlApplicationContext 的
QNX (Neutrino 6.5.0) 使用 ksh 的开源实现作为其 shell 。许多提供的脚本,包括系统启动脚本,都使用诸如 if ! test /dev/slog -ef /dev/slog
当我尝试打开从我的应用程序下载的 xls 文件时,出现此错误: excel cannot open the file because the file format or file extension
有一些相关的概念,即文件指针、流和文件描述符。 我知道文件指针是指向数据类型 FILE 的指针(在例如 FILE.h 和 struct_FILE.h 中声明)。 我知道文件描述符是 int ,例如成员
好吧,这应该很容易... 我是groovy的新手,我希望实现以下逻辑: def testFiles = findAllTestFiles(); 到目前为止,我想出了下面的代码,该代码可以成功打印所有文
我理解为什么以下内容会截断文件的内容: Get-Content | Out-File 这是因为 Out-File 首先运行,它会在 Get-Content 有机会读取文件之前清空文件。 但是当我尝
您好,我正在尝试将文件位置表示为变量,因为最终脚本将在另一台机器上运行。这是我尝试过的代码,然后是我得到的错误。在我看来,python 是如何添加“\”的,这就是导致问题的原因。如果是这种情况,我如何
我有一个只包含一行的输入文件: $ cat input foo bar 我想在我的脚本中使用这一行,据我所知有 3 种方法: line=$(cat input) line=$( input"...,
我是一名优秀的程序员,十分优秀!