Search in sources :

Example 6 with DataStore

use of org.apache.sis.storage.DataStore in project sis by apache.

the class WritableStore method add.

/**
 * Create a new file for the given resource.
 * This implementation uses the provider specified at creation time.
 */
@Override
public synchronized Resource add(final Resource resource) throws DataStoreException {
    ArgumentChecks.ensureNonNull("resource", resource);
    if (!(resource instanceof FeatureSet)) {
        throw new DataStoreException(message(Resources.Keys.CanNotStoreResourceType_2, new Object[] { FolderStoreProvider.NAME, StoreUtilities.getInterface(resource.getClass()) }));
    }
    /*
         * If we determined in a previous method invocation that the given provider can not write feature set,
         * we are better to fail now instead than polluting the directory with files that we can not use.
         */
    if (isReadOnly) {
        throw new ReadOnlyStorageException(messages().getString(Resources.Keys.StoreIsReadOnly));
    }
    /*
         * Infer a filename from the resource identifier, if one can be found.
         * A suffix is added to the filename if available (some formats may have no suffix at all).
         */
    String filename = StoreUtilities.getIdentifier(resource.getMetadata());
    if (filename == null) {
        throw new DataStoreException(message(Resources.Keys.MissingResourceIdentifier_1, StoreUtilities.getLabel(resource)));
    }
    final String[] suffixes = StoreUtilities.getFileSuffixes(componentProvider.getClass());
    if (suffixes.length != 0) {
        filename += '.' + suffixes[0];
    }
    /*
         * Create new store/resource for write access, provided that no store already exist for the path.
         * We use the CREATE_NEW option in order to intentionally fail if the resource already exists.
         */
    final Path path = location.resolve(filename);
    if (!children.containsKey(path)) {
        final StorageConnector connector = new StorageConnector(path);
        connector.setOption(OptionKey.LOCALE, locale);
        connector.setOption(OptionKey.TIMEZONE, timezone);
        connector.setOption(OptionKey.ENCODING, encoding);
        connector.setOption(OptionKey.OPEN_OPTIONS, new StandardOpenOption[] { StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE });
        final DataStore store = componentProvider.open(connector);
        if (children.putIfAbsent(path, store) == null) {
            // TODO: handle transactional case.
            if (store instanceof WritableFeatureSet) {
                StoreUtilities.copy((FeatureSet) resource, (WritableFeatureSet) store);
                // Clear cache. TODO: we should do something more efficient.
                components = null;
                return store;
            }
            /*
                 * If the data store is not a WritableFeatureSet, current implementation can not use it.
                 * Files created by this failed attempt may remain; instead of trying to delete them with
                 * uncertain consequences, we set a flag for avoiding to pollute further the directory.
                 */
            isReadOnly = true;
            children.remove(path, store);
            final String name = store.getDisplayName();
            store.close();
            throw new DataStoreException(message(Resources.Keys.NotAWritableFeatureSet_1, name));
        }
        store.close();
    }
    throw new DataStoreException(message(Resources.Keys.ResourceAlreadyExists_1, path));
}
Also used : Path(java.nio.file.Path) StorageConnector(org.apache.sis.storage.StorageConnector) WritableFeatureSet(org.apache.sis.storage.WritableFeatureSet) DataStoreException(org.apache.sis.storage.DataStoreException) ReadOnlyStorageException(org.apache.sis.storage.ReadOnlyStorageException) DataStore(org.apache.sis.storage.DataStore) FeatureSet(org.apache.sis.storage.FeatureSet) WritableFeatureSet(org.apache.sis.storage.WritableFeatureSet)

Example 7 with DataStore

use of org.apache.sis.storage.DataStore in project sis by apache.

the class StoreProvider method open.

/**
 * Returns a CSV {@link Store} implementation from the given parameters.
 *
 * @return a data store implementation associated with this provider for the given parameters.
 * @throws DataStoreException if an error occurred while creating the data store instance.
 */
@Override
public DataStore open(final ParameterValueGroup parameters) throws DataStoreException {
    ArgumentChecks.ensureNonNull("parameter", parameters);
    final StorageConnector connector = connector(this, parameters);
    final Parameters pg = Parameters.castOrWrap(parameters);
    connector.setOption(DataOptionKey.ENCODING, pg.getValue(ENCODING));
    connector.setOption(DataOptionKey.FOLIATION_REPRESENTATION, pg.getValue(FOLIATION));
    return new Store(this, connector);
}
Also used : StorageConnector(org.apache.sis.storage.StorageConnector) Parameters(org.apache.sis.parameter.Parameters) DataStore(org.apache.sis.storage.DataStore) URIDataStore(org.apache.sis.internal.storage.URIDataStore)

Example 8 with DataStore

use of org.apache.sis.storage.DataStore in project sis by apache.

the class StoreFormat method log.

/**
 * Reports a warning for a WKT that can not be read. This method should be invoked only when the CRS
 * can not be created at all; it should not be invoked if the CRS has been created with some warnings.
 */
final void log(final Exception e) {
    final DataStore store = listeners.getSource();
    listeners.warning(Resources.forLocale(store.getLocale()).getString(Resources.Keys.CanNotReadCRS_WKT_1, store.getDisplayName()), e);
}
Also used : DataStore(org.apache.sis.storage.DataStore)

Example 9 with DataStore

use of org.apache.sis.storage.DataStore in project sis by apache.

the class ReferencingFunctions method getIdentifiedObject.

/**
 * Gets the CRS or other kind of object from the given code.
 * If the code is a URN, then it can be any kind of object.
 * Otherwise a Coordinate Reference System is assumed.
 * This method caches the result.
 *
 * @param  codeOrPath  the code allocated by an authority, or the path to a file.
 * @param  type        how to interpret {@code codeOrPath}, or {@code null} for guessing.
 * @return the identified object for the given code.
 * @throws FactoryException if an error occurred while creating the object.
 * @throws DataStoreException if an error occurred while reading a data file.
 */
private IdentifiedObject getIdentifiedObject(final String codeOrPath, CodeType type) throws FactoryException, DataStoreException {
    final CacheKey<IdentifiedObject> key = new CacheKey<>(IdentifiedObject.class, codeOrPath, null, null);
    IdentifiedObject object = key.peek();
    if (object == null) {
        final Cache.Handler<IdentifiedObject> handler = key.lock();
        try {
            object = handler.peek();
            if (object == null) {
                if (type == null) {
                    type = CodeType.guess(codeOrPath);
                }
                if (type.equals(CodeType.URN)) {
                    object = CRS.getAuthorityFactory(null).createObject(codeOrPath);
                } else if (type.isCRS) {
                    object = CRS.forCode(codeOrPath);
                } else {
                    /*
                         * Apparently not an AUTHORITY:CODE string.
                         * Try to read a dataset from a file or URL, then get its CRS.
                         */
                    final Metadata metadata;
                    try (DataStore store = DataStores.open(codeOrPath)) {
                        metadata = store.getMetadata();
                    }
                    if (metadata != null) {
                        for (final ReferenceSystem rs : metadata.getReferenceSystemInfo()) {
                            if (rs instanceof CoordinateReferenceSystem) {
                                return rs;
                            } else if (object == null) {
                                // Will be used as a fallback if we find no CRS.
                                object = rs;
                            }
                        }
                    }
                    if (object == null) {
                        throw new FactoryException(Errors.getResources(getJavaLocale()).getString(Errors.Keys.UnspecifiedCRS));
                    }
                }
            }
        } finally {
            handler.putAndUnlock(object);
        }
    }
    return object;
}
Also used : FactoryException(org.opengis.util.FactoryException) DataStore(org.apache.sis.storage.DataStore) Metadata(org.opengis.metadata.Metadata) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) IdentifiedObject(org.opengis.referencing.IdentifiedObject) AbstractIdentifiedObject(org.apache.sis.referencing.AbstractIdentifiedObject) ReferenceSystem(org.opengis.referencing.ReferenceSystem) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Cache(org.apache.sis.util.collection.Cache)

Aggregations

DataStore (org.apache.sis.storage.DataStore)9 DataStoreException (org.apache.sis.storage.DataStoreException)6 Path (java.nio.file.Path)4 IOException (java.io.IOException)3 StorageConnector (org.apache.sis.storage.StorageConnector)3 URIDataStore (org.apache.sis.internal.storage.URIDataStore)2 UnsupportedStorageException (org.apache.sis.storage.UnsupportedStorageException)2 File (java.io.File)1 UncheckedIOException (java.io.UncheckedIOException)1 DirectoryIteratorException (java.nio.file.DirectoryIteratorException)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 ArrayList (java.util.ArrayList)1 ResourceOnFileSystem (org.apache.sis.internal.storage.ResourceOnFileSystem)1 UnmodifiableArrayList (org.apache.sis.internal.util.UnmodifiableArrayList)1 DefaultMetadata (org.apache.sis.metadata.iso.DefaultMetadata)1 Parameters (org.apache.sis.parameter.Parameters)1 AbstractIdentifiedObject (org.apache.sis.referencing.AbstractIdentifiedObject)1 DataStoreProvider (org.apache.sis.storage.DataStoreProvider)1 FeatureSet (org.apache.sis.storage.FeatureSet)1