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

服務(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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 海绵宝宝第二季全集免费观看 | 国产日韩精品一区二区在线观看播放 | 香蕉在线播放 | 日本 片 成人 在线 日b视频免费 | 香蕉精品高清在线观看视频 | 国产精品福利一区二区亚瑟 | 久青草国产在视频在线观看 | 4hc44四虎永久地址链接 | 好大好猛好深好爽视频 | 国产亚洲女人久久久久久 | 国产农村乱子伦精品视频 | 男人天堂a| 日韩欧美国产成人 | 欧美精选欧美极品 | 欧美一卡2卡3卡四卡海外精品 | 九9热这里真品 | 美女张开双腿让男人捅 | 日本中文字幕一区二区高清在线 | 四虎网址 | 99久久精品无码一区二区毛片 | 色老板最新网站视频地址 | 精品亚洲综合在线第一区 | 日本黄大片影院一区二区 | 免费视频大全 | 成人毛片1024你懂的 | 久久精品美女 | 免费国产高清精品一区在线 | 青青国产成人久久激情91麻豆 | 被高跟鞋调教丨vk | 色综合色狠狠天天综合色 | 99久久免费国产香蕉麻豆 | 欧美人畜 | 亚洲精品无码不卡在线观看 | 日本成年片高清在线观看 | 爱情岛论坛亚洲永久入口口 | 青春草在线观看精品免费视频 | 2020国产精品视频免费 | 免费看一级a一片毛片 | 精品9e精品视频在线观看 | 精品国产乱码久久久久久软件 | 高清国语自产拍免费视频国产 |