博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java读取SHP格式文件,解决中文乱码
阅读量:1823 次
发布时间:2019-04-25

本文共 2420 字,大约阅读时间需要 8 分钟。

导入jar

org.geotools
gt-shapefile
19.1
org.geotools
gt-swing
19.1
org.geotools
gt-jdbc
19.1
org.geotools.jdbc
gt-jdbc-postgis
19.1
org.geotools
gt-epsg-hsql
19.1

创建工具类

import org.geotools.data.FileDataStore;import org.geotools.data.FileDataStoreFinder;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.filter.Filter;import java.io.File;import java.io.IOException;import java.nio.charset.Charset;/* * 读取本地shp文件 * */public class ReadShepUtil {    public static void main(String[] args){        String path1 = "F:\\test\\test.shp" ;        //读取shp        SimpleFeatureCollection colls1 = readShp(path1);        //拿到所有features        SimpleFeatureIterator iters = colls1.features();        //遍历打印输出        while(iters.hasNext()){            SimpleFeature sf = iters.next();            System.out.println(sf.getID() + " , " + sf.getAttributes());        }    }    public static SimpleFeatureCollection  readShp(String path ){        return readShp(path, null);    }    public static SimpleFeatureCollection  readShp(String path , Filter filter){        SimpleFeatureSource featureSource = readStoreByShp(path);        if(featureSource == null) return null;        try {            return filter != null ? featureSource.getFeatures(filter) : featureSource.getFeatures() ;        } catch (IOException e) {            e.printStackTrace();        }        return null ;    }    public static  SimpleFeatureSource readStoreByShp(String path ){        File file = new File(path);        FileDataStore store;        SimpleFeatureSource featureSource = null;        try {            store = FileDataStoreFinder.getDataStore(file);             //解决中文乱码            ((ShapefileDataStore) store).setCharset(Charset.forName("GBK"));            featureSource = store.getFeatureSource();        } catch (IOException e) {            e.printStackTrace();        }        return featureSource ;    }}

 

转载地址:http://ayskf.baihongyu.com/

你可能感兴趣的文章
[网鼎杯 2020 朱雀组]phpweb
查看>>
[BJDCTF2020]Cookie is so stable
查看>>
[SUCTF 2019]Pythonginx
查看>>
[极客大挑战 2019]RCE ME
查看>>
HackTheBox-------ScriptKiddie
查看>>
Shell学习
查看>>
[Zer0pts2020]Can you guess it?
查看>>
Jenkins资料整理
查看>>
ArrayList源码常用方法注意点
查看>>
MySQL资料整理
查看>>
Redis常用文章整理
查看>>
RocketMQ资料整理
查看>>
慢sql统计
查看>>
基于webRTC的1V1在线视频聊天(网页版DEMO)
查看>>
Disconf数据安全保护设计方案
查看>>
HttpClient获取302重定向的新网址方法
查看>>
Java 函数优雅之道【大厂规范】
查看>>
第三方接口调用规范
查看>>
java中调用js函数的方法
查看>>
可落地的云游戏解决方案
查看>>