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>