<del id="d4fwx"><form id="d4fwx"></form></del>
      <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

            <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
          • java  中Excel轉(zhuǎn)shapefile的實例詳解

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

            目前創(chuàng)新互聯(lián)建站已為近千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站改版維護、企業(yè)網(wǎng)站設(shè)計、廣豐網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

            概述:

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

            截圖:

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

             原始Excel文件

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

            運行耗時

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

            運行結(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ū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

            網(wǎng)站欄目:java  中Excel轉(zhuǎn)shapefile的實例詳解
            文章分享:http://www.jbt999.com/article34/ihpepe.html

            成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、網(wǎng)站設(shè)計公司網(wǎng)站營銷、服務(wù)器托管外貿(mào)建站、建站公司

            廣告

            聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

            網(wǎng)站托管運營

              <del id="d4fwx"><form id="d4fwx"></form></del>
              <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

                    <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
                  • 火爆全网嫖妓达人金先生约战极品S空 | 日韩操逼视频 | 欧美一级A片在线观看 | 一级日逼片 | 深夜操逼 |