基于GDAL2.2.3读取gdb数据的方法

基于GDAL2.2.3读取gdb数据的方法

安装开发环境参考地址:
https://blog.csdn.net/hsg77/article/details/134340631?spm=1001.2014.3001.5501

新建java项目后
pom.xml内容如下所示:

特别注意:
添加repository两项, 不然下载不了geotools工具包

<repositories>
    <!--    添加POM环境下载地址:(非常重要)-->
    <repository>
      <id>osgeo</id>
      <name>Geotools repository</name>
      <url>https://repo.osgeo.org/repository/release/</url>
    </repository>
    <repository>
      <id>osgeo</id>
      <name>Open Source Geospatial Foundation Repository</name>
      <url>http://download.osgeo.org/webdav/geotools/</url>
    </repository>
  </repositories>

添加测试代码:

package org.example;

import org.gdal.osr.*;
import org.gdal.ogr.*;
import org.gdal.gdal.*;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.opengis.feature.simple.SimpleFeature;

/**
 * Hello world!
 *
 */
public class App 
{
    
    
    public static void main( String[] args ) throws Exception
    {
    
    
        System.out.println( "Hello World!" );
        //System.load("gdalalljni.dll");
        System.out.println(System.getProperty("java.library.path"));

        // 设置java.library.path=path/to/gdal/library
        //System.setProperty("java.library.path", "C:\\Program Files\\GDAL");
        //
        System.out.println(System.getProperty("java.library.path"));
        //
        ogr.RegisterAll();
        int c=ogr.GetDriverCount();
        System.out.println(c);
        for(int i=0;i<c;i++)
        {
    
    
            System.out.println(ogr.GetDriver(i).getName());
        }
        gdal.GDALDestroyDriverManager();

        //
        String gdbPath="D:\\data\\hsg\\test\\testXZQ.gdb";
        gdalShape g=new gdalShape(gdalDataType.gdb,gdbPath);
        g.init();

        SimpleFeatureIterator items=null;
        SimpleFeatureSource featSource=g.getFeatureSource("XZQ");
        try {
    
    
            items = featSource.getFeatures().features();
            while(items.hasNext())
            {
    
    
                SimpleFeature feat=items.next();
                System.out.println(feat.getAttribute("XJQYDM"));
                System.out.println(feat.getAttribute("XJQYMC"));
            }
        }catch (Exception ex)
        {
    
    
            ex.printStackTrace();
        }
        finally {
    
    
            items.close();
        }

    }
}

测试输出结果为:

51X002001
XXX区莲花街道
51X002002
XXX区三贤祠街道
51X002003
XXX区资溪街道
...

java调用gdal库读取gdb文件中空间图层数据源代码如下所示:

package org.example;

import org.gdal.gdal.gdal;
import org.gdal.ogr.ogr;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;

import org.opengis.feature.simple.SimpleFeature;

import java.util.HashMap;
import java.util.Map;

public class gdalShape {
    
    
    private gdalDataType dataType=gdalDataType.shape;
    private String dataPath="";
    private DataStore dataStore=null;
    //dataPath=shpPath/gdbPath/geojsonPath/esrijsonPath/...
    public gdalShape(gdalDataType dataType,String dataPath)
    {
    
    
        this.dataType=dataType;
        this.dataPath=dataPath;
    }
    public gdalShape()
    {
    
    
    }
    public void init() throws Exception
    {
    
    
        Map cMap=new HashMap();
        switch(this.dataType)
        {
    
    
            case shape:
                cMap.put("DriverName","ESRI Shapefile");
                break;
            case gdb:
                cMap.put("DriverName","OpenFileGDB");
                break;
            case geojson:
                cMap.put("DriverName","GeoJSON");
                break;
            case esrijson:
                cMap.put("DriverName","EsriJSON");
                break;
            default:
                cMap.put("DriverName","ESRI Shapefile");
                break;
        }
        cMap.put("DatasourceName",this.dataPath);
        this.dataStore= DataStoreFinder.getDataStore(cMap);
    }
    public SimpleFeatureSource getFeatureSource(String typeName) throws Exception
    {
    
    
        if(this.dataStore!=null)
        {
    
    
            return this.dataStore.getFeatureSource(typeName);
        }
        return null;
    }

    //测试读取shape文件
    public void testReadShape() throws  Exception
    {
    
    
        SimpleFeatureIterator items=null;
        SimpleFeatureSource featSource=this.getFeatureSource("xzq");
        try {
    
    
            items = featSource.getFeatures().features();
            while(items.hasNext())
            {
    
    
                SimpleFeature feat=items.next();
                System.out.println(feat.getAttribute("xzqmc"));
            }
        }catch (Exception ex)
        {
    
    
            ex.printStackTrace();
        }
        finally {
    
    
            items.close();
        }
    }


    public void test_gdal_getDrivers()
    {
    
    
        ogr.RegisterAll();
        int c=ogr.GetDriverCount();
        System.out.println(c);
        for(int i=0;i<c;i++)
        {
    
    
            System.out.println(ogr.GetDriver(i).getName());
        }
        gdal.GDALDestroyDriverManager();
    }
}

pom.xml完整内容如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>cwgis-gdal</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>cwgis-gdal</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>



  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>


    <dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-main</artifactId>
      <version>24.0</version>
    </dependency>
    <dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-geojson</artifactId>
      <version>24.0</version>
    </dependency>
    <dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-cql</artifactId>
      <version>24.0</version>
    </dependency>
    <dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-swing</artifactId>
      <version>24.0</version>
    </dependency>
    <dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-process-feature</artifactId>
      <version>24.0</version>
    </dependency>
    <dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-process-raster</artifactId>
      <version>24.0</version>
    </dependency>
    <dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-geotiff</artifactId>
      <version>24.0</version>
    </dependency>
    <dependency>
      <groupId>org.geotools.jdbc</groupId>
      <artifactId>gt-jdbc-postgis</artifactId>
      <version>24.0</version>
    </dependency>
    <dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-epsg-hsql</artifactId>
      <version>24.0</version>
    </dependency>
    <dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-shapefile</artifactId>
      <version>24.0</version>
    </dependency>

    <dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-ogr-jni</artifactId>
      <version>24.0</version>
      <exclusions>
        <exclusion>
          <groupId>org.gdal</groupId>
          <artifactId>gdal</artifactId>
        </exclusion></exclusions>
    </dependency>
    <dependency>
      <groupId>org.gdal</groupId>
      <artifactId>gdal</artifactId>
      <version>2.2.3</version>
      <scope>system</scope>
      <systemPath>${project.basedir}/src/main/resources/gdal/gdal.jar</systemPath>
    </dependency>

  </dependencies>

  <build>
    <resources>
      <!-- 将gdal_data文件夹放到classpath路径下 -->
      <resource>
        <directory>${project.basedir}/src/main/gdal_data</directory>
      </resource>
    </resources>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
  <distributionManagement>
    <repository>
      <id>releases</id>
      <url>https://s01.oss.sonatype.org/content/repositories/releases/</url>
    </repository>
    <snapshotRepository>
      <id>snapshots</id>
      <url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
    </snapshotRepository>
  </distributionManagement>
  <repositories>
    <!--    添加POM环境下载地址:(非常重要)-->
    <repository>
      <id>osgeo</id>
      <name>Geotools repository</name>
      <url>https://repo.osgeo.org/repository/release/</url>
    </repository>
    <repository>
      <id>osgeo</id>
      <name>Open Source Geospatial Foundation Repository</name>
      <url>http://download.osgeo.org/webdav/geotools/</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>public</id>
      <name>aliyun nexus</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </pluginRepository>
  </pluginRepositories>
</project>

本blog地址:https://blog.csdn.net/hsg77
—the—end—

猜你喜欢

转载自blog.csdn.net/hsg77/article/details/134427216