ShapeFile工具类
来源:互联网 发布:37传奇霸业网络异常 编辑:程序博客网 时间:2024/06/06 13:21
使用GeoTools读取ShapeFile文件,测试版本为geotools-16.1。
所需jar包有
package com.test;import java.io.File;import java.nio.charset.Charset;import java.util.ArrayList;import java.util.List;import org.geotools.data.shapefile.ShapefileDataStore;import org.geotools.data.shapefile.ShapefileDataStoreFactory;import org.geotools.data.shapefile.files.ShpFiles;import org.geotools.data.shapefile.shp.ShapefileReader;import org.geotools.data.simple.SimpleFeatureCollection;import org.geotools.data.simple.SimpleFeatureSource;import com.vividsolutions.jts.geom.Geometry;import com.vividsolutions.jts.geom.GeometryFactory;public class ShapeFileUtils { /** * 获取shape文件feature集合 * @param shapePath shape文件路径 * @param charSet 读取shape文件编码 * @return SimpleFeatureCollection */ public static SimpleFeatureCollection ReadShapeFileFeatures(String shapePath, String charSet){ SimpleFeatureCollection sfc = null; ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory(); ShapefileDataStore sds = null; try { sds = (ShapefileDataStore)dataStoreFactory.createDataStore(new File(shapePath).toURI().toURL()); sds.setCharset(Charset.forName(charSet)); SimpleFeatureSource featureSource = sds.getFeatureSource(); sfc = featureSource.getFeatures(); } catch (Exception e) { e.printStackTrace(); }finally{ sds.dispose(); } return sfc; } /** * 读取ShapeFile中的空间数据 * @param shapeFilePath * @return List<Geometry> */ public static List<Geometry> getGeometriesFromShapeFile(String shapeFilePath){ List<Geometry> result = new ArrayList<Geometry>(); try { ShpFiles file = new ShpFiles(shapeFilePath); ShapefileReader reader = new ShapefileReader(file, false, false, new GeometryFactory()); while(reader.hasNext()){ result.add((Geometry)reader.nextRecord().shape()); } reader.close(); } catch (Exception e) { e.printStackTrace(); } return result; }}
测试类如下。
package com.test;import org.geotools.data.simple.SimpleFeatureCollection;import org.geotools.data.simple.SimpleFeatureIterator;import org.opengis.feature.simple.SimpleFeature;import com.tongtu.mobile.cuanalysis.utils.ShapeFileUtils;import com.vividsolutions.jts.geom.Geometry;public class ShapeFileTest { public static void main(String[] args) throws Exception{ SimpleFeatureCollection sfc = ShapeFileUtils.ReadShapeFileFeatures("D:\\ziptest\\bou.shp", "GBK"); SimpleFeatureIterator iterator = sfc.features(); while(iterator.hasNext()) { SimpleFeature feature = iterator.next(); System.out.println(feature.getAttribute("NAME"));//获取属性名称 Geometry g = (Geometry) feature.getDefaultGeometry();//获取空间数据 System.out.println(g.toString()); } iterator.close(); }}
工具类持续完善中……
参考资料:http://www.cnblogs.com/cugwx/p/3719195.html
0 0
- ShapeFile工具类
- 非ArcGIS客户端查看Shapefile工具“Shapefile information Viewer”
- Shapefile、要素类和layer之间的区别和联系
- 使用PostGIS 2.0 Shapefile and DBF Loader Exporter工具导入shp文件
- 谈谈Shapefile
- Shapefile简述
- 关于shapefile
- 剖析Shapefile
- Shapefile文件
- 剖析Shapefile
- 解剖Shapefile
- Shapefile数据结构
- Shapefile综述
- Shapefile文件组成,Shapefile文件的修复
- 读取shapefile格式文件
- 读取shapefile格式文件
- shapefile文件格式的读取
- 打开shapefile格式文件代码
- Python Set
- 让Struts2与Spring进行整合原因及原理
- android新特性:TextInputLayout使用方法
- Kubernetes and Container Operations
- 小程序的坑
- ShapeFile工具类
- 从零到日志采集索引可视化、监控报警、rpc trace跟踪-日志索引
- c++双向链表构成的队列
- 关于Hadoop中是否还有hadoop-core-xxx.jar包吗
- Android新特性之CardView的简单使用
- ffmpeg 和 x264的参数对照
- ButterKnife8.5.1最新版本使用详细步骤
- 11. Container With Most Water
- 自定义注解