首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法使用geoTools获取FeaturesCollection中的内容

无法使用geoTools获取FeaturesCollection中的内容
EN

Stack Overflow用户
提问于 2020-01-25 18:21:43
回答 1查看 422关注 0票数 1

我正在使用geoTools库。我的目标是从geoServer返回特性。我已正确连接到dataStore,但无法接收集合功能的内容。

当我尝试的时候:

代码语言:javascript
运行
复制
SimpleFeatureCollection collection = featureSource.getFeatures();

在欺骗我的代码时,collectionFeature的响应包含cashedSize = -1typeContent包含正确的propertyNames,但没有特征数据。

我认为问题出在依赖项中,但我无法修复它。

这是我的代码:

代码语言:javascript
运行
复制
CRS.decode("EPSG:4326", true);
// URL 
StringBuilder completeUrl = new StringBuilder();
completeUrl.append(url);
completeUrl.append("/geoserver/wfs?SERVICE=wfs&version=");
completeUrl.append(version);
completeUrl.append("&REQUEST=GetCapabilities");
String getCapabilities = completeUrl.toString();
logger.info("URL" + getCapabilities);
// Connexion to WFS
Map<String, String> connectionParameters = new HashMap<String, String>();
connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", getCapabilities); 
 connectionParameters.put("WFSDataStoreFactory:MAXFEATURES", "50");
 DataStore dataStore = DataStoreFinder.getDataStore(connectionParameters);
SimpleFeatureSource featureSource = dataStore.getFeatureSource(layer);
SimpleFeatureCollection collection = featureSource.getFeatures();   
return  collection;             `

和我的POM.xml:

代码语言:javascript
运行
复制
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>


            <!-- https://mvnrepository.com/artifact/org.geotools/gt-shapefile -->
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>22.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


            <!-- https://mvnrepository.com/artifact/org.geotools/gt-main -->
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-main</artifactId>
            <version>21.1</version>
        </dependency>   



        <!-- https://mvnrepository.com/artifact/org.geotools/gt-cql -->
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-cql</artifactId>
            <version>21.1</version>
        </dependency>

            <!-- https://mvnrepository.com/artifact/org.geotools/gt-opengis -->
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-opengis</artifactId>
            <version>21.1</version>
        </dependency>   

        <!-- https://mvnrepository.com/artifact/org.geotools/gt-epsg-hsql -->
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-hsql</artifactId>
            <version>22.1</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.geotools/gt-wfs -->
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-wfs</artifactId>
        <version>11.2</version>
    </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>maven2-repository.dev.java.net</id>
            <name>Java.net repository</name>
            <url>http://download.java.net/maven/2</url>
        </repository>
        <repository>
            <id>osgeo</id>
            <name>Open Source Geospatial Foundation Repository</name>
            <url>http://download.osgeo.org/webdav/geotools/</url>
        </repository>
        <repository>
            <id>opengeo</id>
            <name>OpenGeo Maven Repository</name>
            <url>http://repo.opengeo.org</url>
        </repository>
    </repositories>
</project>

谢谢

EN

回答 1

Stack Overflow用户

发布于 2020-01-26 03:13:11

如果计算功能的数量开销太大,功能集合可能会返回-1。这取决于数据存储,例如Postgis可以快速计数,而geojson不能,因为它必须读取和处理整个文件。

尝试通过从集合中获取迭代器来使用集合。

更新

更仔细地查看您的pom.xml会发现另一个问题:

代码语言:javascript
运行
复制
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-epsg-hsql</artifactId>
        <version>22.1</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.geotools/gt-wfs -->
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-wfs</artifactId>
    <version>11.2</version>
</dependency>

您正在混合GeoTools的版本,这可能会很糟糕,而且您可能希望使用较新的gt-wfs-ng模块,而不是gt-wfs模块。

更新2

我在本地GeoServer中使用了以下代码,没有出现任何问题。

代码语言:javascript
运行
复制
    String getCapabilities = "http://localhost:8080/geoserver/ows?service=wfs&version=1.1.0&request=GetCapabilities";
    Map<String, Serializable> connectionParameters = new HashMap<>();
    connectionParameters.put(WFSDataStoreFactory.URL.key, getCapabilities);
    connectionParameters.put(WFSDataStoreFactory.TIMEOUT.key, 10000000);
    WFSDataStoreFactory dsf = new WFSDataStoreFactory();
    try {
      WFSDataStore dataStore = dsf.createDataStore(connectionParameters);
      String types[] = dataStore.getTypeNames();
      for (int i = 0; i < types.length; i++) {
        System.out.println(types[i]);
        String name = types[i];
        Query query = new Query(name);
        SimpleFeatureSource source = dataStore.getFeatureSource(name);
        SimpleFeatureType schema = source.getSchema();
        // System.out.println(schema);
        query.setMaxFeatures(10);
        SimpleFeatureCollection fc = source.getFeatures(query);
        try (SimpleFeatureIterator itr = fc.features()) {
          while (itr.hasNext()) {
            SimpleFeature sf = itr.next();
            System.out.println(sf);
          }
        }
      }
    } catch (IOException ex) {
      ex.printStackTrace();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59908331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档