Search in sources :

Example 1 with IndexType

use of org.geotoolkit.data.shapefile.indexed.IndexType in project geotoolkit by Geomatys.

the class ShapefileProvider method open.

/**
 * {@inheritDoc }
 */
@Override
public ShapefileFeatureStore open(final ParameterValueGroup params) throws DataStoreException {
    ensureCanProcess(params);
    final URI uri = (URI) params.parameter(PATH.getName().toString()).getValue();
    Boolean isMemoryMapped = (Boolean) params.parameter(MEMORY_MAPPED.getName().toString()).getValue();
    Charset dbfCharset = (Charset) params.parameter(DBFCHARSET.getName().toString()).getValue();
    Boolean isCreateSpatialIndex = (Boolean) params.parameter(CREATE_SPATIAL_INDEX.getName().toString()).getValue();
    if (isCreateSpatialIndex == null) {
        // should not be needed as default is TRUE
        isCreateSpatialIndex = Boolean.TRUE;
    }
    if (dbfCharset == null) {
        // this should not happen as Charset.forName("ISO-8859-1") was used
        // as the param default?
        dbfCharset = Charset.forName("ISO-8859-1");
    }
    if (isMemoryMapped == null) {
        isMemoryMapped = Boolean.FALSE;
    }
    // index loading hints
    final Boolean loadQix = (Boolean) params.parameter(LOAD_QIX.getName().toString()).getValue();
    final ShpFiles shpFiles;
    try {
        shpFiles = new ShpFiles(uri, (loadQix == null) ? false : loadQix);
    } catch (IllegalArgumentException ex) {
        throw new DataStoreException(ex.getMessage(), ex);
    }
    if (!shpFiles.exists(ShpFileType.SHP)) {
        throw new DataStoreException("Shapefile not found:" + shpFiles.get(ShpFileType.SHP));
    }
    final boolean isWritable = shpFiles.isWritable();
    final boolean useMemoryMappedBuffer = shpFiles.exists(ShpFileType.SHP) && isMemoryMapped;
    final boolean createIndex = isCreateSpatialIndex && isWritable;
    IndexType treeIndex = IndexType.NONE;
    if (isWritable) {
        if (createIndex) {
            // default
            treeIndex = IndexType.QIX;
        } else {
            // lets check and see if any index file is avaialble
            if (shpFiles.exists(ShpFileType.QIX)) {
                treeIndex = IndexType.QIX;
            }
        }
    }
    try {
        if (createIndex) {
            return new IndexedShapefileFeatureStore(uri, useMemoryMappedBuffer, createIndex, IndexType.QIX, dbfCharset);
        } else if (treeIndex != IndexType.NONE) {
            return new IndexedShapefileFeatureStore(uri, useMemoryMappedBuffer, false, treeIndex, dbfCharset);
        } else {
            return new ShapefileFeatureStore(uri, useMemoryMappedBuffer, dbfCharset);
        }
    } catch (MalformedURLException mue) {
        throw new DataStoreException("Url for shapefile malformed: " + uri, mue);
    }
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) MalformedURLException(java.net.MalformedURLException) ShpFiles(org.geotoolkit.data.shapefile.lock.ShpFiles) Charset(java.nio.charset.Charset) IndexedShapefileFeatureStore(org.geotoolkit.data.shapefile.indexed.IndexedShapefileFeatureStore) IndexedShapefileFeatureStore(org.geotoolkit.data.shapefile.indexed.IndexedShapefileFeatureStore) IndexType(org.geotoolkit.data.shapefile.indexed.IndexType) URI(java.net.URI)

Aggregations

MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 Charset (java.nio.charset.Charset)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 IndexType (org.geotoolkit.data.shapefile.indexed.IndexType)1 IndexedShapefileFeatureStore (org.geotoolkit.data.shapefile.indexed.IndexedShapefileFeatureStore)1 ShpFiles (org.geotoolkit.data.shapefile.lock.ShpFiles)1