打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
提取shape文件数据到csv

pom文件:

<dependencies>

<dependency>

<groupId>nl.cloudfarming.client</groupId>

<artifactId>lib-geotools</artifactId>

<version>2.7.5</version>

</dependency>

<dependency>

<groupId>org.geotools</groupId>

<artifactId>gt-geojson</artifactId>

<version>18.0</version>

</dependency>

</dependencies>

java代码:

import org.geotools.data.shapefile.ShapefileDataStore;

import org.geotools.data.simple.SimpleFeatureCollection;

import org.geotools.data.simple.SimpleFeatureIterator;

import org.geotools.data.simple.SimpleFeatureSource;

import org.opengis.feature.simple.SimpleFeature;

import org.opengis.feature.type.AttributeType;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStreamWriter;

import java.math.BigDecimal;

import java.nio.charset.Charset;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class ShapeToCsv {

    public static void main(String[] args) {

        long startTime = System.currentTimeMillis();

        System.out.println(startTime);

        System.out.println(shape2Geojson("E:\\jingwei\\china_2000.shp","E:\\csv\\北京.csv"));

        long endTime = System.currentTimeMillis();

        System.out.println(endTime-startTime);

    }

    /**

     * shp转换为Geojson

     * @param shpPath

     * @return

     */

    public static int shape2Geojson(String shpPath, String csvPath){

        int size = 0;

        try{

            BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvPath),"UTF-8"));

            File file = new File(shpPath);

            ShapefileDataStore shpDataStore = null;

            shpDataStore = new ShapefileDataStore(file.toURL());

            // 设置编码,防止中文乱码

            shpDataStore.setStringCharset(Charset.forName("utf8"));

            String typeName = shpDataStore.getTypeNames()[0];

            SimpleFeatureSource featureSource = null;

            featureSource =  shpDataStore.getFeatureSource (typeName);

            SimpleFeatureCollection result = featureSource.getFeatures();

            Map<String, Class> mapFields = new HashMap();

            List<AttributeType > attributeTypes = result.getSchema().getTypes();

            // geometry的类型

            String geometryType = result.getSchema().getGeometryDescriptor().getType().getName().toString();

            List<String> csvHeader = new ArrayList<>();

            for (int i=0;i<attributeTypes.size();i++){

                AttributeType attributeType = attributeTypes.get(i);

                String key = attributeType.getName().toString();

                Class classType = attributeType.getBinding();

                if (geometryType.equals(key)){

                    continue;

                }

                csvHeader.add(key);

                out.write(key + ",");

                mapFields.put(key,classType);

            }

            out.write("coords");

            out.newLine();

            SimpleFeatureIterator itertor = result.features();

            while (itertor.hasNext()) {

                SimpleFeature feature = itertor.next();

                for(int i=0;i<csvHeader.size();i++){

                    String key = csvHeader.get(i);

                    String classType = mapFields.get(key).getTypeName();

                    String oneCsvData = feature.getAttribute(csvHeader.get(i)).toString();

                    if (classType.contains("Double")){

                        BigDecimal bd = new BigDecimal(oneCsvData);

                        oneCsvData = bd.toPlainString();

                    }

                    if(oneCsvData.contains(",")){

                        oneCsvData = "\"" + oneCsvData + "\"";

                    }

                    out.write(oneCsvData);

                    out.write(",");

                }

                String coords = feature.getDefaultGeometry().toString();

                if(coords.contains(",")){

                    coords = "\"" + coords + "\"";

                }

                out.write(coords);

                out.newLine();

                size++;

            }

            itertor.close();

            out.flush();

            out.close();

        }

        catch(Exception e){

            System.out.println("转换失败");

            e.printStackTrace();

        }

        return size;

    }

}

————————————————

版权声明:本文为CSDN博主「纯洁的小魔鬼」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/xyy1028/article/details/102655954

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Feature Tutorial
Java 中文排序
读取和处理csv文件为sql文件
基于crawler4j、jsoup、javacsv的爬虫实践
java操作CSV文件
java连接DB2数据库示例代码
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服