Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.Serializable;
  4. import java.util.Arrays;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7.  
  8. import org.geotools.data.DataStore;
  9. import org.geotools.data.DataUtilities;
  10. import org.geotools.data.DefaultTransaction;
  11. import org.geotools.data.FeatureStore;
  12. import org.geotools.data.Transaction;
  13. import org.geotools.data.oracle.OracleNGDataStoreFactory;
  14. import org.geotools.data.shapefile.ShapefileDataStore;
  15. import org.geotools.feature.FeatureIterator;
  16. import org.geotools.feature.simple.SimpleFeatureBuilder;
  17. import org.geotools.jdbc.JDBCDataStoreFactory;
  18. import org.opengis.feature.simple.SimpleFeature;
  19. import org.opengis.feature.simple.SimpleFeatureType;
  20. import org.opengis.feature.type.AttributeDescriptor;
  21. import org.opengis.filter.Filter;
  22.  
  23.  
  24. public class OracleImporter {
  25.  
  26.     public static void main(String[] args) throws IOException {
  27.         String path = "/home/aaime/devel/gs2.0.x/data/release/data/shapefiles/states.shp";
  28.         ShapefileDataStore shp = new ShapefileDataStore(new File(path).toURL());
  29.        
  30.         Map<Serializable, Object> params = new HashMap<Serializable, Object>();
  31.         params.put(JDBCDataStoreFactory.USER.key, "usr");
  32.         params.put(JDBCDataStoreFactory.PASSWD.key, "pwd");
  33.         params.put(JDBCDataStoreFactory.HOST.key, "localhost");
  34.         params.put(JDBCDataStoreFactory.PORT.key, OracleNGDataStoreFactory.PORT.sample);
  35.         params.put(JDBCDataStoreFactory.DATABASE.key, "xe");
  36.         params.put(JDBCDataStoreFactory.DBTYPE.key, "oracle");
  37.        
  38.         DataStore oracle = new OracleNGDataStoreFactory().createDataStore(params);
  39.         if(oracle != null && oracle.getTypeNames() != null)
  40.             System.out.println("Oracle connected");
  41.        
  42.         String typeName = shp.getTypeNames()[0].toUpperCase();
  43.         if(!Arrays.asList(oracle.getTypeNames()).contains(typeName))
  44.             oracle.createSchema(shp.getSchema());
  45.        
  46.         FeatureStore oraStore = (FeatureStore) oracle.getFeatureSource(typeName);
  47.         oraStore.removeFeatures(Filter.INCLUDE);
  48.        
  49.         SimpleFeatureType targetSchema = (SimpleFeatureType) oraStore.getSchema();
  50.         SimpleFeatureBuilder builder = new SimpleFeatureBuilder(targetSchema);
  51.        
  52.         FeatureIterator fi = shp.getFeatureSource().getFeatures().features();
  53.         SimpleFeatureType sourceSchema = shp.getSchema();
  54.        
  55.         Transaction t = new DefaultTransaction();
  56.         oraStore.setTransaction(t);
  57.         while(fi.hasNext()) {
  58.             SimpleFeature source = (SimpleFeature) fi.next();
  59.        
  60.             for(AttributeDescriptor ad : sourceSchema.getAttributeDescriptors()) {
  61.                 String attribute = ad.getLocalName();
  62.                 builder.set(attribute.toUpperCase(), source.getAttribute(attribute));
  63.             }
  64.            
  65.             oraStore.addFeatures(DataUtilities.collection(builder.buildFeature(null)));
  66.         }
  67.         t.commit();
  68.         t.close();
  69.        
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement