一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - java 中Excel轉(zhuǎn)shape file的實(shí)例詳解

java 中Excel轉(zhuǎn)shape file的實(shí)例詳解

2020-12-30 11:35gisshixisheng Java教程

這篇文章主要介紹了java 中Excel轉(zhuǎn)shape file的實(shí)例詳解的相關(guān)資料,希望通過本文大家能實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下

java  中excel轉(zhuǎn)shape file的實(shí)例詳解

概述:

本文講述如何結(jié)合geotools和poi實(shí)現(xiàn)excel到shp的轉(zhuǎn)換,再結(jié)合前文shp到geojson數(shù)據(jù)的轉(zhuǎn)換,即可實(shí)現(xiàn)用戶上傳excel數(shù)據(jù)并在web端的展示功能。

截圖:

java 中Excel轉(zhuǎn)shape file的實(shí)例詳解

 原始excel文件

java 中Excel轉(zhuǎn)shape file的實(shí)例詳解

運(yùn)行耗時(shí)

java 中Excel轉(zhuǎn)shape file的實(shí)例詳解

運(yùn)行結(jié)果

代碼:

?
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);
    //獲取總列數(shù)
    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);
      //獲取總列數(shù)
      int colnum = sheet.getrow(0).getphysicalnumberofcells();
      // 得到總行數(shù)
      int rownum = sheet.getlastrownum();
 
      list list = getexcelheader();
      //創(chuàng)建shape文件對(duì)象
      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());
      //設(shè)置編碼
      charset charset = charset.forname("gbk");
      ds.setcharset(charset);
      //設(shè)置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("共耗時(shí)" + (system.currenttimemillis() - start) + "ms");
  }
}

說明:

1、轉(zhuǎn)換僅限點(diǎn)對(duì)象的轉(zhuǎn)換;
2、保留所有excel相關(guān)的屬性,lon、lat字段是必須要有的;
3、對(duì)于中文字段,做了取首字母的處理;

 如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

原文鏈接:http://blog.csdn.net/gisshixisheng/article/details/77877031

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 九九精品免费视频 | 91.久久| 成人中文字幕在线高清 | 操小女人 | 天堂网在线.www天堂在线视频 | 非洲黑人女bbwxxxx | 日本午夜vr影院新入口 | coolgay男男gayxxx chinese壮直男gay老年人 chinese野外gay军人 | 天天快乐高清在线观看 | 含羞草国产亚洲精品岁国产精品 | 三级全黄的视频 | 日本一区二区三区精品 | 波多野结衣小说 | 欧美亚洲另类综合 | 啪啪链接 | 久久这里有精品 | 亚洲四虎 | 免费日批软件 | 日日网| 亚洲国产剧情中文视频在线 | 天天操网 | katsuniav在线播放 | 91精品国产高清久久久久久 | 日韩精品一区二区三区视频 | 草莓香蕉绿巨人丝瓜榴莲18 | 歪歪私人影院成人毛片 | 欧美在线一级视频 | 2019aw网站 | 午夜dj影院在线观看完整版 | 国产欧美日韩图片一区二区 | 亚洲欧美国产精品完整版 | 欧美猛男同志video在线 | 欧美特级特黄a大片免费 | 久99久热只有精品国产99 | 草草免费观看视频在线 | 高清不卡日本v在线二区 | 国产一二在线观看视频网站 | 91桃花视频| 99久久精品自在自看国产 | 婷婷丁香色综合狠狠色 | 国产在线视频欧美亚综合 |