java实现读取postgis表数据

本文讲解java实现读取postgis中的表数据。

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.DataStoreFinder;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.jdbc.JDBCDataStore;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.filter.Filter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/*
* 读取postgis表数据
* */
public class Temp3 {
    public static void main(String[] args){
        String host = "localhost";
        String schema = "public" ;
        String database = "postgis_24_sample" ;
        String user = "postgres" ;
        String pass = "19920318" ;
        String tablename = "wafangdianshi" ;
        int port = 5432;
        //读取
        SimpleFeatureCollection colls1 = readPostgisTable(host, port, user, pass, database, schema, tablename);
        if(colls1 == null){
            System.out.println("请检查参数,确保jdbc连接正常以及表存在.");
            return;
        }
        //拿到所有features
        SimpleFeatureIterator iters = colls1.features();
        //遍历打印
        while(iters.hasNext()){
            SimpleFeature sf = iters.next();
            System.out.println(sf.getID() + " , " + sf.getAttributes());
        }
    }


    public static SimpleFeatureCollection  readPostgisTable(String host , int port , String user , String pass , String dbname, String schema , String tablename ){
        return readPostgisTable(host, port, user, pass, dbname, schema, tablename , null);
    }

    public static SimpleFeatureCollection  readPostgisTable(String host , int port , String user , String pass , String dbname, String schema , String tablename , Filter filter){
        Map params = new HashMap();
        params.put("dbtype", "postgis");
        params.put("host", host);
        params.put("port", port);
        params.put("schema", schema);
        params.put("database", dbname);
        params.put("user", user);
        params.put("passwd", pass);
        try {
            JDBCDataStore dataStore = (JDBCDataStore) DataStoreFinder.getDataStore(params);
            return readDatastore(dataStore, tablename, filter);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static SimpleFeatureCollection readDatastore(JDBCDataStore store ,String typeName , Filter filter){
        try {
            SimpleFeatureSource featureSource = store.getFeatureSource(typeName);
            return filter != null ? featureSource.getFeatures(filter) : featureSource.getFeatures();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

你可能感兴趣的:(java实现读取postgis表数据)