0

I am trying to import shapefile into Oracle db using GeoTools, but when I am trying to create a new schema, it will giving me following error.

Aug 01, 2021 8:24:13 PM org.geotools.jdbc.JDBCDataStore getMapping WARNING: No mapping for org.locationtech.jts.geom.MultiPolygon Aug 01, 2021 8:24:13 PM org.geotools.jdbc.JDBCDataStore getSQLTypeNames WARNING: Fetching fields from Database Exception in thread "main" java.lang.NoClassDefFoundError: org/geotools/factory/Hints

I am using following jars for this.

gt-jdbc-oracle-2.6.1
ojdbc8-12.2.0.1

Not able to found gt-epsg-oracle jar not sure if this is happening because of this jar.

Below is the code which I am using:

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.Transaction;
import org.geotools.data.collection.ListFeatureCollection;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.simple.SimpleFeatureStore;
import org.geotools.feature.FeatureCollection;
import org.geotools.jdbc.JDBCDataStoreFactory;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.identity.FeatureId;

public class WriteToDatabase {
    private static DataStore dataStore;

    public WriteToDatabase(Map<String, Object> params) throws IOException {

        dataStore = DataStoreFinder.getDataStore(params);
        if(dataStore != null && dataStore.getTypeNames() != null)
            System.out.println("Oracle connected");
    }

    public boolean isReady() {
        return dataStore != null;
    }

    public boolean writeFeatures(FeatureCollection<SimpleFeatureType, SimpleFeature> features) {

        if (dataStore == null) {
            throw new IllegalStateException("Datastore can not be null when writing");
        }
        SimpleFeatureType schema = features.getSchema();

        try {

            /*
             * Write the features
             */
            Transaction transaction = new DefaultTransaction("create");

            String[] typeNames = dataStore.getTypeNames();
            // first check if we need to create table
            boolean exists = false;
            String schemaName = schema.getName().getLocalPart();
            for (String name : typeNames) {

                if (schemaName.equalsIgnoreCase(name)) {
                    exists = true;
                }
            }
            if (!exists) {
                **dataStore.createSchema(schema);**
            }
            // we should probably check the schema matches the existing table.
            // but we don't
            SimpleFeatureSource featureSource = dataStore.getFeatureSource(schema.getName().getLocalPart());
            if (featureSource instanceof SimpleFeatureStore) {
                SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;

                featureStore.setTransaction(transaction);
                try {
                    List<FeatureId> ids = featureStore.addFeatures(features);
                    transaction.commit();
                } catch (Exception problem) {
                    problem.printStackTrace();
                    transaction.rollback();
                } finally {
                    transaction.close();
                }
                dataStore.dispose();
                return true;
            } else {
                dataStore.dispose();
                System.err.println("Database not writable");
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String args[]) throws IOException {
        File file;
        String path = "D:\\shppoc.shp";
        ShapefileDataStore shp = new ShapefileDataStore(new File(path).toURL());

        shp.setCharset(Charset.forName("UTF-8"));
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(JDBCDataStoreFactory.USER.key, "GEO_LOAD");
        params.put(JDBCDataStoreFactory.PASSWD.key, "test");
        params.put(JDBCDataStoreFactory.HOST.key, "test");
        params.put(JDBCDataStoreFactory.PORT.key, "1521");
        params.put(JDBCDataStoreFactory.DATABASE.key, "/test1");
        params.put(JDBCDataStoreFactory.DBTYPE.key, "oracle");
        // create some random features and write them out;
        List<SimpleFeature> feats = new ArrayList<SimpleFeature>();

        SimpleFeatureIterator fi = shp.getFeatureSource().getFeatures().features();
        SimpleFeatureType sourceSchema = shp.getSchema();

        while (fi.hasNext()) {
            SimpleFeature source = (SimpleFeature) fi.next();
            feats.add(source);
        }

        WriteToDatabase writer = new WriteToDatabase(params);
        System.out.println("oracle.getTypeNames()"+dataStore.getTypeNames());
        String typeName = shp.getTypeNames()[0].toUpperCase();

        System.out.println("shp.getTypeNames()[0]===="+shp.getTypeNames()[0]);

        FeatureCollection<SimpleFeatureType, SimpleFeature> features = new ListFeatureCollection(sourceSchema, feats);
        writer.writeFeatures(features);
    }
}

Attached is the pom , Also 2 jar i am adding externally because of not able to found in maven repository

gt-jdbc-oracle-2.6.1 ojdbc8-12.2.0.1

<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.geotools</groupId>
    <artifactId>tutorial</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>tutorial</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <geotools.version>26-SNAPSHOT</geotools.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.tukaani/xz -->
        <dependency>
            <groupId>org.tukaani</groupId>
            <artifactId>xz</artifactId>
            <version>1.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
            <version>1.21</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-hsql</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-extension</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-referencing</artifactId>
            <version>${geotools.version}</version>
        </dependency>

        <!-- <dependency> <groupId>org.geotools</groupId> <artifactId>gt-epsg-oracle</artifactId> 
            <version>20.4</version> </dependency> -->
        <!-- <dependency> <groupId>org.locationtech</groupId> <artifactId>jts</artifactId> 
            <version>1.13</version> </dependency> -->
        <!-- Provides support for PostGIS. Note the different groupId -->
        <dependency>
            <groupId>org.geotools.jdbc</groupId>
            <artifactId>gt-jdbc-postgis</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-swing</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>com.vividsolutions</groupId>
            <artifactId>jts</artifactId>
            <version>1.13</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/oracle/ojdbc6 -->

    </dependencies>
    <repositories>
        <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>
        <repository>
            <id>osgeo-snapshot</id>
            <name>OSGeo Snapshot Repository</name>
            <url>https://repo.osgeo.org/repository/snapshot/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>

    </repositories>

    <build>
        <plugins>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
2
  • GeoTools is currently on version 25.x - so I would first make sure I was using a version written in the last decade and then edit your question to show the dependencies in your pom.xml and the version of Java you are using.
    – Ian Turton
    Commented Aug 1, 2021 at 15:44
  • I am currently using the latest version of geotools i.e 26-SNAPSHOT. Updated the pom in question and I am using 2 external jar mentioned in the question too. Commented Aug 1, 2021 at 15:48

1 Answer 1

1

You are trying to mix in a old jar (gt-jdbc-oracle-2.6.1) with modern GeoTools (26-SNAPSHOT are you actively debugging the GeoTools code or just trying to make life hard for yourself?).

THIS WILL NOT WORK

There is a reason we use Maven to control dependencies in Java projects and it is to explicitly prevent this from happening. So change your pom.xml to handle all your dependencies including gt-jdbc-oracle as explained in the documentation.

<dependency>
   <groupId>org.geotools.jdbc</groupId>
   <artifactId>gt-jdbc-oracle</artifactId>
   <version>${geotools.version}</version>
 </dependency>

I don't have an oracle database to hand so I can't test your code but using the right dependency should help.

1
  • Fixing the external jar version worked for me. Thanks Ian Commented Aug 2, 2021 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.