java实现读取本地shp数据文件

本文主要讲解java读取本地shp数据。可以参考GeoServer的源码。

1、pom.xml依赖
    
        19.1
    

    
        
            osgeo
            Open Source Geospatial Foundation Repository
            http://download.osgeo.org/webdav/geotools/
        
        
            geosolutions
            geosolutions repository
            http://maven.geo-solutions.it/
            
                true
            
            
                true
            
        
    

    
        
            org.slf4j
            jcl-over-slf4j
            1.7.5
        
        
            org.slf4j
            slf4j-api
            1.7.5
        
        
            org.slf4j
            slf4j-log4j12
            1.7.5
        

        
            nl.pdok
            geoserver-manager
            1.7.0-pdok2
        



        
            junit
            junit
            4.11
            test
        
        
            org.geotools
            gt-shapefile
            ${geotools.version}
        
        
            org.geotools
            gt-swing
            ${geotools.version}
        
        
            org.geotools
            gt-jdbc
            ${geotools.version}
        
        
            org.geotools.jdbc
            gt-jdbc-postgis
            ${geotools.version}
        

        
            org.geotools
            gt-epsg-hsql
            ${geotools.version}
        
    
2、java代码
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 Temp2 {
    public static void main(String[] args){
        String path1 = "E:\\Test\\TestOpenLayers\\data\\wafangdian_0\\wafangdianshi_0.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("UTF-8"));
            featureSource = store.getFeatureSource();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return featureSource ;
    }
}

你可能感兴趣的:(java实现读取本地shp数据文件)