91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java  中Excel轉shape file的實例詳解

發布時間:2020-10-18 11:12:29 來源:腳本之家 閱讀:161 作者:lqh 欄目:編程語言

java  中Excel轉shape file的實例詳解

概述:

本文講述如何結合geotools和POI實現Excel到shp的轉換,再結合前文shp到geojson數據的轉換,即可實現用戶上傳excel數據并在web端的展示功能。

截圖:

java  中Excel轉shape file的實例詳解

 原始Excel文件

java  中Excel轉shape file的實例詳解

運行耗時

java  中Excel轉shape file的實例詳解

運行結果

代碼:

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、對于中文字段,做了取首字母的處理;

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

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

汝南县| 仪陇县| 鄂托克旗| 峡江县| 赣州市| 成武县| 湄潭县| 沈丘县| 中方县| 砀山县| 南投县| 泽普县| 金寨县| 正安县| 贡山| 商水县| 乐亭县| 武冈市| 唐山市| 罗甸县| 任丘市| 安吉县| 武穴市| 靖边县| 柳河县| 凤台县| 八宿县| 杂多县| 望都县| 长兴县| 芷江| 清涧县| 吉隆县| 武夷山市| 德州市| 交口县| 南投市| 南城县| 平塘县| 庄河市| 湟中县|