0

I am trying to read the sample GML file in Java using GeoTools 29.0 but getting issue for finding data store. Can you share a sample on how to read GML file or parse it to WKT/GeoJSON in Java using GeoTools 29.0?

Deps

    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-main</artifactId>
        <version>29.0</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-opengis</artifactId>
        <version>29.0</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-epsg-hsql</artifactId>
        <version>29.0</version>
    </dependency>
    <dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-xml</artifactId>
    <version>29.0</version>
    </dependency>

with repo as

    <repository>
        <id>osgeo</id>
        <name>OSGeo Release Repository</name>
        <url>https://repo.osgeo.org/repository/release/</url>
        <snapshots><enabled>false</enabled></snapshots>
        <releases><enabled>true</enabled></releases>
    </repository>

and my code for reading looks like this

    // Create a connection parameters map
    Map<String, Object> connectionParameters = new HashMap<>();
    connectionParameters.put("url", new File(gmlFilePath).toURI().toURL());

    // Open the GML data store
    DataStore dataStore = DataStoreFinder.getDataStore(connectionParameters);

    // Get the feature collection to read features from the GML file
    String typeName = dataStore.getTypeNames()[0];
    SimpleFeatureCollection featureCollection = dataStore.getFeatureSource(typeName).getFeatures();

    // Get the feature iterator
    try (FeatureIterator<SimpleFeature> featureIterator = featureCollection.features()) {
        // Iterate through features
        while (featureIterator.hasNext()) {
            SimpleFeature feature = featureIterator.next();
            // Process the feature as needed
            System.out.println(feature.toString());
        }
    }

But, I am getting

Null Pointer exception at DataStoreFinder.getDataStore(connectionParameters)

Looks like is not able to get any valid datastore for the GML files. How can I read the GML file properly then with GeoTools?

5
  • 1
    And what result do you get from that code? We need the Question to contain what you have tried and where you are stuck.
    – Vince
    Commented Jul 10, 2023 at 21:36
  • I am getting datastore not found. Commented Jul 11, 2023 at 2:47
  • I repeat: We need the Question to contain what you have tried and where you are stuck. Comments are not in the Question. Edit the Question.
    – Vince
    Commented Jul 11, 2023 at 5:00
  • Welcome to Geographic Information Systems! Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short tour for more about how the site works
    – Ian Turton
    Commented Jul 11, 2023 at 7:27
  • The duplicated issue has an example with 15.2 version after which many braking changes were introduced so I want the example with latest version of geotools. The classes in that answer example are no longer available in latest versions. Commented Jul 13, 2023 at 15:13

0

Browse other questions tagged or ask your own question.