java实现读取postgis表数据

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

1、pom.xml依赖
    <properties>
        <geotools.version>19.1</geotools.version>
    </properties>

    <repositories>
        <repository>
            <id>osgeo</id>
            <name>Open Source Geospatial Foundation Repository</name>
            <url>http://download.osgeo.org/webdav/geotools/</url>
        </repository>
        <repository>
            <id>geosolutions</id>
            <name>geosolutions repository</name>
            <url>http://maven.geo-solutions.it/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>

        <dependency>
            <groupId>nl.pdok</groupId>
            <artifactId>geoserver-manager</artifactId>
            <version>1.7.0-pdok2</version>
        </dependency>



        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-swing</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-jdbc</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools.jdbc</groupId>
            <artifactId>gt-jdbc-postgis</artifactId>
            <version>${geotools.version}</version>
        </dependency>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-hsql</artifactId>
            <version>${geotools.version}</version>
        </dependency>
    </dependencies>
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<String, Object> params = new HashMap<String, Object>();
        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;
    }
}

转载自:https://blog.csdn.net/weixin_33973609/article/details/86854826

You may also like...