Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import java.io.Serializable;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Map;
- import org.geotools.data.DataStore;
- import org.geotools.data.DataUtilities;
- import org.geotools.data.DefaultTransaction;
- import org.geotools.data.FeatureStore;
- import org.geotools.data.Transaction;
- import org.geotools.data.oracle.OracleNGDataStoreFactory;
- import org.geotools.data.shapefile.ShapefileDataStore;
- import org.geotools.feature.FeatureIterator;
- import org.geotools.feature.simple.SimpleFeatureBuilder;
- import org.geotools.jdbc.JDBCDataStoreFactory;
- import org.opengis.feature.simple.SimpleFeature;
- import org.opengis.feature.simple.SimpleFeatureType;
- import org.opengis.feature.type.AttributeDescriptor;
- import org.opengis.filter.Filter;
- public class OracleImporter {
- public static void main(String[] args) throws IOException {
- String path = "/home/aaime/devel/gs2.0.x/data/release/data/shapefiles/states.shp";
- ShapefileDataStore shp = new ShapefileDataStore(new File(path).toURL());
- Map<Serializable, Object> params = new HashMap<Serializable, Object>();
- params.put(JDBCDataStoreFactory.USER.key, "usr");
- params.put(JDBCDataStoreFactory.PASSWD.key, "pwd");
- params.put(JDBCDataStoreFactory.HOST.key, "localhost");
- params.put(JDBCDataStoreFactory.PORT.key, OracleNGDataStoreFactory.PORT.sample);
- params.put(JDBCDataStoreFactory.DATABASE.key, "xe");
- params.put(JDBCDataStoreFactory.DBTYPE.key, "oracle");
- DataStore oracle = new OracleNGDataStoreFactory().createDataStore(params);
- if(oracle != null && oracle.getTypeNames() != null)
- System.out.println("Oracle connected");
- String typeName = shp.getTypeNames()[0].toUpperCase();
- if(!Arrays.asList(oracle.getTypeNames()).contains(typeName))
- oracle.createSchema(shp.getSchema());
- FeatureStore oraStore = (FeatureStore) oracle.getFeatureSource(typeName);
- oraStore.removeFeatures(Filter.INCLUDE);
- SimpleFeatureType targetSchema = (SimpleFeatureType) oraStore.getSchema();
- SimpleFeatureBuilder builder = new SimpleFeatureBuilder(targetSchema);
- FeatureIterator fi = shp.getFeatureSource().getFeatures().features();
- SimpleFeatureType sourceSchema = shp.getSchema();
- Transaction t = new DefaultTransaction();
- oraStore.setTransaction(t);
- while(fi.hasNext()) {
- SimpleFeature source = (SimpleFeature) fi.next();
- for(AttributeDescriptor ad : sourceSchema.getAttributeDescriptors()) {
- String attribute = ad.getLocalName();
- builder.set(attribute.toUpperCase(), source.getAttribute(attribute));
- }
- oraStore.addFeatures(DataUtilities.collection(builder.buildFeature(null)));
- }
- t.commit();
- t.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement