@Test public void testCreateSchema() throws Exception { File dir = File.createTempFile("foo", "shp", new File("target")); dir.delete(); dir.mkdir(); DataStore ds = new DirectoryDataStore(dir, getFileStoreFactory()); assertEquals(0, ds.getTypeNames().length); SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.setName("foo"); tb.add("geom", Polygon.class); tb.add("bar", Integer.class); ds.createSchema(tb.buildFeatureType()); SimpleFeatureType ft = ds.getSchema("foo"); assertNotNull(ft); // clean up ds.dispose(); for (File f : dir.listFiles()) { f.delete(); } dir.delete(); }
@Override protected void setUpInternal(SystemTestData data) throws Exception { // run all the tests against a store that can do native paging (h2) and one that // can't (property) Catalog cat = getCatalog(); DataStoreInfo ds = cat.getFactory().createDataStore(); ds.setName("foo"); ds.setWorkspace(cat.getDefaultWorkspace()); Map params = ds.getConnectionParameters(); params.put("dbtype", "h2"); params.put("database", getTestData().getDataDirectoryRoot().getAbsolutePath()); cat.add(ds); FeatureSource fs1 = getFeatureSource(SystemTestData.FIFTEEN); FeatureSource fs2 = getFeatureSource(SystemTestData.SEVEN); DataStore store = (DataStore) ds.getDataStore(null); SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.init((SimpleFeatureType) fs1.getSchema()); tb.add("num", Integer.class); tb.remove("boundedBy"); store.createSchema(tb.buildFeatureType()); tb.init((SimpleFeatureType) fs2.getSchema()); tb.add("num", Integer.class); tb.remove("boundedBy"); store.createSchema(tb.buildFeatureType()); CatalogBuilder cb = new CatalogBuilder(cat); cb.setStore(ds); FeatureStore fs = (FeatureStore) store.getFeatureSource("Fifteen"); addFeatures(fs, fs1.getFeatures()); FeatureTypeInfo ft = cb.buildFeatureType(fs); cat.add(ft); fs = (FeatureStore) store.getFeatureSource("Seven"); addFeatures(fs, fs2.getFeatures()); ft = cb.buildFeatureType(fs); cat.add(ft); }
/** * Builds a FeatureStore for the output Feature. * * @param store * @param schema * @return * @throws IOException */ protected FeatureStore<SimpleFeatureType, SimpleFeature> createOutputWriter( DataStore store, SimpleFeatureType schema, Transaction transaction) throws IOException { String destTypeName = schema.getTypeName(); boolean createSchema = true; for (String typeName : store.getTypeNames()) { if (typeName.equalsIgnoreCase(destTypeName)) { createSchema = false; destTypeName = typeName; } } // check for case changing in typeName if (createSchema) { store.createSchema(schema); for (String typeName : store.getTypeNames()) { if (!typeName.equals(destTypeName) && typeName.equalsIgnoreCase(destTypeName)) { destTypeName = typeName; } } } FeatureStore<SimpleFeatureType, SimpleFeature> result = (FeatureStore<SimpleFeatureType, SimpleFeature>) store.getFeatureSource(destTypeName); result.setTransaction(transaction); return result; }
/** Executes the export command using the provided options. */ @Override protected final void runInternal(GeogigCLI cli) throws IOException { if (args.isEmpty()) { printUsage(cli); throw new CommandFailedException(); } String path = args.get(0); String tableName = args.get(1); checkParameter(tableName != null && !tableName.isEmpty(), "No table name specified"); DataStore dataStore = getDataStore(); ObjectId featureTypeId = null; if (!Arrays.asList(dataStore.getTypeNames()).contains(tableName)) { SimpleFeatureType outputFeatureType; if (sFeatureTypeId != null) { // Check the feature type id string is a correct id Optional<ObjectId> id = cli.getGeogig().command(RevParse.class).setRefSpec(sFeatureTypeId).call(); checkParameter(id.isPresent(), "Invalid feature type reference", sFeatureTypeId); TYPE type = cli.getGeogig().command(ResolveObjectType.class).setObjectId(id.get()).call(); checkParameter( type.equals(TYPE.FEATURETYPE), "Provided reference does not resolve to a feature type: ", sFeatureTypeId); outputFeatureType = (SimpleFeatureType) cli.getGeogig() .command(RevObjectParse.class) .setObjectId(id.get()) .call(RevFeatureType.class) .get() .type(); featureTypeId = id.get(); } else { try { SimpleFeatureType sft = getFeatureType(path, cli); outputFeatureType = new SimpleFeatureTypeImpl( new NameImpl(tableName), sft.getAttributeDescriptors(), sft.getGeometryDescriptor(), sft.isAbstract(), sft.getRestrictions(), sft.getSuper(), sft.getDescription()); } catch (GeoToolsOpException e) { throw new CommandFailedException("No features to export.", e); } } try { dataStore.createSchema(outputFeatureType); } catch (IOException e) { throw new CommandFailedException("Cannot create new table in database", e); } } else { if (!overwrite) { throw new CommandFailedException("The selected table already exists. Use -o to overwrite"); } } SimpleFeatureSource featureSource = dataStore.getFeatureSource(tableName); if (!(featureSource instanceof SimpleFeatureStore)) { throw new CommandFailedException("Can't write to the selected table"); } SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource; if (overwrite) { try { featureStore.removeFeatures(Filter.INCLUDE); } catch (IOException e) { throw new CommandFailedException("Error truncating table: " + e.getMessage(), e); } } ExportOp op = cli.getGeogig() .command(ExportOp.class) .setFeatureStore(featureStore) .setPath(path) .setFilterFeatureTypeId(featureTypeId) .setAlter(alter); if (defaultType) { op.exportDefaultFeatureType(); } try { op.setProgressListener(cli.getProgressListener()).call(); } catch (IllegalArgumentException iae) { throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae); } catch (GeoToolsOpException e) { switch (e.statusCode) { case MIXED_FEATURE_TYPES: throw new CommandFailedException( "The selected tree contains mixed feature types. Use --defaulttype or --featuretype <feature_type_ref> to export.", e); default: throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e); } } cli.getConsole().println(path + " exported successfully to " + tableName); }
private SimpleFeatureType importDataIntoStore( SimpleFeatureCollection features, String name, DataStoreInfo storeInfo) throws IOException, ProcessException { SimpleFeatureType targetType; // grab the data store DataStore ds = (DataStore) storeInfo.getDataStore(null); // decide on the target ft name SimpleFeatureType sourceType = features.getSchema(); if (name != null) { SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.init(sourceType); tb.setName(name); sourceType = tb.buildFeatureType(); } // create the schema ds.createSchema(sourceType); // try to get the target feature type (might have slightly different // name and structure) targetType = ds.getSchema(sourceType.getTypeName()); if (targetType == null) { // ouch, the name was changed... we can only guess now... // try with the typical Oracle mangling targetType = ds.getSchema(sourceType.getTypeName().toUpperCase()); } if (targetType == null) { throw new WPSException( "The target schema was created, but with a name " + "that we cannot relate to the one we provided the data store. Cannot proceeed further"); } else { // check the layer is not already there String newLayerName = storeInfo.getWorkspace().getName() + ":" + targetType.getTypeName(); LayerInfo layer = catalog.getLayerByName(newLayerName); // todo: we should not really reach here and know beforehand what the targetType // name is, but if we do we should at least get a way to drop it if (layer != null) { throw new ProcessException( "Target layer " + newLayerName + " already exists in the catalog"); } } // try to establish a mapping with old and new attributes. This is again // just guesswork until we have a geotools api that will give us the // exact mapping to be performed Map<String, String> mapping = buildAttributeMapping(sourceType, targetType); // start a transaction and fill the target with the input features Transaction t = new DefaultTransaction(); SimpleFeatureStore fstore = (SimpleFeatureStore) ds.getFeatureSource(targetType.getTypeName()); fstore.setTransaction(t); SimpleFeatureIterator fi = features.features(); SimpleFeatureBuilder fb = new SimpleFeatureBuilder(targetType); while (fi.hasNext()) { SimpleFeature source = fi.next(); fb.reset(); for (String sname : mapping.keySet()) { fb.set(mapping.get(sname), source.getAttribute(sname)); } SimpleFeature target = fb.buildFeature(null); fstore.addFeatures(DataUtilities.collection(target)); } t.commit(); t.close(); return targetType; }