最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年09月08日 14:15:00   投稿:lqh  
這篇文章主要介紹了java 中Excel轉(zhuǎn)shape file的實例詳解的相關(guān)資料,希望通過本文大家能實現(xiàn)這樣的功能,需要的朋友可以參考下

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

概述:

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

截圖:

 原始Excel文件

運行耗時

運行結(jié)果

代碼:

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文件對象
      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("共耗時" + (System.currentTimeMillis() - start) + "ms");
  }
}

說明:

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

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

相關(guān)文章

  • SpringBoot注冊Servlet的三種方法詳解

    SpringBoot注冊Servlet的三種方法詳解

    這篇文章主要介紹了SpringBoot注冊Servlet的三種方法詳解,教你如何Spring Boot 注冊 Servlet、Filter、Listener,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • Java 將List中的實體類按照某個字段進行分組并存放至Map中操作

    Java 將List中的實體類按照某個字段進行分組并存放至Map中操作

    這篇文章主要介紹了Java 將List中的實體類按照某個字段進行分組并存放至Map中操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • 簡單實現(xiàn)Java版學生管理系統(tǒng)

    簡單實現(xiàn)Java版學生管理系統(tǒng)

    這篇文章主要為大家詳細介紹了簡單實現(xiàn)Java版學生管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • IDEA中如何引入spring的命名空間

    IDEA中如何引入spring的命名空間

    這篇文章主要介紹了IDEA中如何引入spring的命名空間問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • mybatis中嵌套使用foreach需要注意的坑

    mybatis中嵌套使用foreach需要注意的坑

    在使用MyBatis進行數(shù)據(jù)庫操作時,如果需要在一個大的foreach標簽中嵌套另一個foreach,需要注意內(nèi)層foreach的item屬性不能與外層重復(fù),如果出現(xiàn)重復(fù),會導致綁定參數(shù)時找不到正確的參數(shù),從而拋出異常
    2024-09-09
  • Java后端SSM框架圖片上傳功能實現(xiàn)方法解析

    Java后端SSM框架圖片上傳功能實現(xiàn)方法解析

    這篇文章主要介紹了Java后端SSM框架圖片上傳功能實現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • Java并發(fā)容器ConcurrentLinkedQueue解析

    Java并發(fā)容器ConcurrentLinkedQueue解析

    這篇文章主要介紹了Java并發(fā)容器ConcurrentLinkedQueue解析,
    2023-12-12
  • Java語言----三種循環(huán)語句的區(qū)別介紹

    Java語言----三種循環(huán)語句的區(qū)別介紹

    下面小編就為大家?guī)硪黄狫ava語言----三種循環(huán)語句的區(qū)別介紹。小編舉得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-07-07
  • java教程之java程序編譯運行圖解(java程序運行)

    java教程之java程序編譯運行圖解(java程序運行)

    最近重新復(fù)習了一下java基礎(chǔ),在使用javap的過程中遇到了一些問題,這里便講講對于一個類文件如何編譯、運行、反編譯的。也讓自己加深一下印象
    2014-03-03
  • 將JSON字符串數(shù)組轉(zhuǎn)對象集合方法步驟

    將JSON字符串數(shù)組轉(zhuǎn)對象集合方法步驟

    這篇文章主要給大家介紹了關(guān)于將JSON字符串數(shù)組轉(zhuǎn)對象集合的方法步驟,文中通過代碼示例介紹的非常詳細,對大家的學習或者工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08

最新評論

酒泉市| 安化县| 湖州市| 新田县| 化隆| 彭山县| 临城县| 确山县| 泸溪县| 利辛县| 会同县| 姜堰市| 苗栗县| 化隆| 烟台市| 肥东县| 罗城| 年辖:市辖区| 河间市| 昭平县| 老河口市| 曲周县| 青州市| 天峻县| 太和县| 安多县| 彩票| 西畴县| 南城县| 高青县| 武汉市| 荔波县| 腾冲县| 义乌市| 松溪县| 宣汉县| 长垣县| 叙永县| 红安县| 沿河| 宜宾市|