java读取SHP格式文件,解决中文乱码
发布日期:2021-06-28 18:30:42
浏览次数:4
分类:技术文章
本文共 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 ; }}
转载地址:https://blog.csdn.net/xljx_1/article/details/101218135 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月08日 17时45分30秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
转载:如何下载网页上的视频?You-get
2019-04-29
视频下载网址
2019-04-29
转载:Centos7安装Python3.7
2019-04-29
转载:Centos7系统切换Python2到Python3.x
2019-04-29
廖雪峰的Git教程
2019-04-29
转载:python 怎么查看函数的用法
2019-04-29
sql学习笔记:缩略版
2019-04-29
UML类图中的关系和表示方法(转载)
2019-04-29
eclipse使用技巧、快捷键
2019-04-29
博主的一点碎碎念201907
2019-04-29
java学习笔记:JDBC入门,JDBC工具类JDBCUtils
2019-04-29
java学习笔记:Statement详解
2019-04-29
java学习笔记:PreparedStatement解决sql注入
2019-04-29
java学习笔记:JDBC批处理
2019-04-29
java学习笔记:JavaBean
2019-04-29
java学习笔记:连接池
2019-04-29
java学习笔记:分层思想
2019-04-29
java学习笔记:事务管理
2019-04-29
java学习笔记:详解匿名内部类(转载)
2019-04-29