ShapeFileUtils工具类

使用GeoTools读取ShapeFile文件,测试版本为geotools-16.1。
所需jar包有
这里写图片描述

package com.util;

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.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import org.opengis.feature.simple.SimpleFeature;

public class ShapeFileUtils {

    /**
     * 获取shape文件feature集合
     * @param shapePath shape文件路径
     * @param charSet 读取shape文件编码
     * @return SimpleFeatureCollection
     */
    public static SimpleFeatureCollection getFeatures(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> getGeometries(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;
    }

    public static void main(String[] args) {

        // 读取Feature测试
        SimpleFeatureCollection sfc = getFeatures("D:\\shapetest\\district\\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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值