Search in sources :

Example 1 with AccessManager

use of org.geotoolkit.data.shapefile.lock.AccessManager in project geotoolkit by Geomatys.

the class ShapefileFeatureStore method createFeatureType.

// //////////////////////////////////////////////////////////////////////////
// schema manipulation /////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////
/**
 * Set the FeatureType of this DataStore. This method will delete any
 * existing local resources or throw an IOException if the featurestore is
 * remote.
 *
 * @param featureType The desired FeatureType.
 * @throws DataStoreException If the featurestore is remote.
 *
 * @todo must synchronize this properly
 */
@Override
public void createFeatureType(final FeatureType featureType) throws DataStoreException {
    final GenericName typeName = featureType.getName();
    if (!isWritable(typeName.toString())) {
        throw new DataStoreException("Read-only acces prevent type creation.");
    }
    if (typeName == null) {
        throw new DataStoreException("Type name can not be null.");
    }
    if (!featureType.isSimple()) {
        throw new DataStoreException("Feature type must not be null and must be a simple feature type.");
    }
    if (!featureType.getName().equals(typeName)) {
        throw new DataStoreException("Shapefile featurestore can only hold typename same as feature type name.");
    }
    try {
        // delete the files
        shpFiles.delete();
    } catch (IOException ex) {
        throw new DataStoreException("Cannot reset datastore content", ex);
    }
    final AccessManager locker = shpFiles.createLocker();
    // update schema and name
    name = typeName;
    schema = featureType;
    AttributeType desc;
    try {
        desc = Features.toAttribute(FeatureExt.getDefaultGeometry(featureType)).orElse(null);
    } catch (PropertyNotFoundException e) {
        getLogger().log(Level.FINE, e, () -> String.format("No geometry can be found in given datatype%n%s", featureType));
        desc = null;
    }
    CoordinateReferenceSystem crs = null;
    final Class<?> geomType;
    final ShapeType shapeType;
    if (desc != null) {
        crs = FeatureExt.getCRS(desc);
        geomType = desc.getValueClass();
        shapeType = ShapeType.findBestGeometryType(geomType);
    } else {
        geomType = null;
        shapeType = ShapeType.NULL;
    }
    if (shapeType == ShapeType.UNDEFINED) {
        throw new DataStoreException("Cannot create a shapefile whose geometry type is " + geomType);
    }
    try (Closeable disposeLocker = locker::disposeReaderAndWriters) {
        final StorageFile shpStoragefile = locker.getStorageFile(SHP);
        final StorageFile shxStoragefile = locker.getStorageFile(SHX);
        final StorageFile dbfStoragefile = locker.getStorageFile(DBF);
        final StorageFile prjStoragefile = locker.getStorageFile(PRJ);
        final StorageFile cpgStoragefile = locker.getStorageFile(CPG);
        try (FileChannel shpChannel = shpStoragefile.getWriteChannel();
            FileChannel shxChannel = shxStoragefile.getWriteChannel()) {
            try (ShapefileWriter writer = new ShapefileWriter(shpChannel, shxChannel)) {
                // try to get the domain first
                final Envelope domain = CRS.getDomainOfValidity(crs);
                if (domain != null) {
                    writer.writeHeaders(new JTSEnvelope2D(domain), shapeType, 0, 100);
                } else {
                    // try to reproject the single overall envelope keeping poles out of the way
                    final JTSEnvelope2D env = new JTSEnvelope2D(-179, 179, -89, 89, CommonCRS.WGS84.normalizedGeographic());
                    JTSEnvelope2D transformedBounds;
                    if (crs != null) {
                        try {
                            transformedBounds = env.transform(crs);
                        } catch (Exception t) {
                            getLogger().log(Level.WARNING, t.getLocalizedMessage(), t);
                            // It can happen for local projections :
                            transformedBounds = new JTSEnvelope2D(crs);
                        }
                    } else {
                        transformedBounds = env;
                    }
                    writer.writeHeaders(transformedBounds, shapeType, 0, 100);
                }
            }
        }
        final DbaseFileHeader dbfheader = DbaseFileHeader.createDbaseHeader(schema);
        dbfheader.setNumRecords(0);
        try (WritableByteChannel dbfChannel = dbfStoragefile.getWriteChannel()) {
            dbfheader.writeHeader(dbfChannel);
        }
        if (crs != null) {
            // .prj files should have no carriage returns in them, this messes up
            // ESRI's ArcXXX software, so we'll be compatible
            final WKTFormat format = new WKTFormat(Locale.ENGLISH, null);
            format.setConvention(Convention.WKT1_COMMON_UNITS);
            format.setNameAuthority(Citations.ESRI);
            format.setIndentation(WKTFormat.SINGLE_LINE);
            final String s = format.format(crs);
            IOUtilities.writeString(s, prjStoragefile.getFile(), Charset.forName("ISO-8859-1"));
        } else {
            getLogger().warning("PRJ file not generated for null CoordinateReferenceSystem");
            Path prjFile = prjStoragefile.getFile();
            Files.deleteIfExists(prjFile);
        }
        // write dbf encoding .cpg
        CpgFiles.write(dbfCharset, cpgStoragefile.getFile());
    } catch (IOException ex) {
        throw new DataStoreException(ex);
    }
    // Once all writings have succeeded, we can commit them
    try {
        locker.replaceStorageFiles();
    } catch (IOException e) {
        throw new DataStoreException("Failed commiting file changes", e);
    }
    // force reading it again since the file type may be a little different
    name = null;
    schema = null;
    // we still preserve the original type name and attribute classes which may be more restricted
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder(getFeatureType());
    ftb.setName(typeName);
    final AttributeTypeBuilder gtb = (AttributeTypeBuilder) ftb.getProperty("the_geom");
    if (Geometry.class.equals(gtb.getValueClass())) {
        gtb.setValueClass(shapeType.bestJTSClass());
    }
    gtb.setName(desc.getName());
    for (PropertyType pt : featureType.getProperties(true)) {
        if (pt instanceof AttributeType) {
            final AttributeType at = (AttributeType) pt;
            if (!Geometry.class.isAssignableFrom(at.getValueClass())) {
                try {
                    ((AttributeTypeBuilder) ftb.getProperty(at.getName().toString())).setValueClass(at.getValueClass()).setName(at.getName());
                } catch (PropertyNotFoundException ex) {
                }
            }
        }
    }
    schema = ftb.build();
    name = schema.getName();
}
Also used : AccessManager(org.geotoolkit.data.shapefile.lock.AccessManager) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) Closeable(java.io.Closeable) PropertyType(org.opengis.feature.PropertyType) Envelope(org.opengis.geometry.Envelope) AttributeTypeBuilder(org.apache.sis.feature.builder.AttributeTypeBuilder) GenericName(org.opengis.util.GenericName) AttributeType(org.opengis.feature.AttributeType) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) WKTFormat(org.apache.sis.io.wkt.WKTFormat) Path(java.nio.file.Path) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) DataStoreException(org.apache.sis.storage.DataStoreException) FileChannel(java.nio.channels.FileChannel) ShapeType(org.geotoolkit.data.shapefile.shp.ShapeType) WritableByteChannel(java.nio.channels.WritableByteChannel) IOException(java.io.IOException) DataStoreException(org.apache.sis.storage.DataStoreException) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) UnsupportedQueryException(org.apache.sis.storage.UnsupportedQueryException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) DbaseFileHeader(org.geotoolkit.data.dbf.DbaseFileHeader) Geometry(org.locationtech.jts.geom.Geometry) JTSEnvelope2D(org.geotoolkit.geometry.jts.JTSEnvelope2D) StorageFile(org.geotoolkit.data.shapefile.lock.StorageFile) ShapefileWriter(org.geotoolkit.data.shapefile.shp.ShapefileWriter)

Example 2 with AccessManager

use of org.geotoolkit.data.shapefile.lock.AccessManager in project geotoolkit by Geomatys.

the class IndexedShapefileFeatureStore method getBBoxAttributesReader.

protected IndexedShapefileAttributeReader getBBoxAttributesReader(final List<AttributeType> properties, final Envelope bbox, final boolean loose, final Hints hints, final boolean read3D, final double[] res) throws DataStoreException {
    final AccessManager locker = shpFiles.createLocker();
    final double[] minRes = (double[]) hints.get(Hints.KEY_IGNORE_SMALL_FEATURES);
    CloseableCollection<ShpData> goodCollec = null;
    try {
        final QuadTree quadTree = openQuadTree();
        if (quadTree != null) {
            final ShxReader shx;
            try {
                shx = locker.getSHXReader(useMemoryMappedBuffer);
            } catch (IOException ex) {
                throw new DataStoreException("Error opening Shx file: " + ex.getMessage(), ex);
            }
            final DataReader<ShpData> dr = new IndexDataReader(shx);
            goodCollec = quadTree.search(dr, bbox, minRes);
        }
    } catch (Exception e) {
        throw new DataStoreException("Error querying index: " + e.getMessage());
    }
    final LazySearchCollection<ShpData> col = (LazySearchCollection) goodCollec;
    final LazyTyleSearchIterator.Buffered<ShpData> ite = (col != null) ? col.bboxIterator() : null;
    // check if we need to open the dbf reader, no need when only geometry
    final boolean readDBF = !(properties.size() == 1 && Geometry.class.isAssignableFrom(properties.get(0).getValueClass()));
    final AttributeType[] atts = properties.toArray(new AttributeType[properties.size()]);
    try {
        return new IndexedBBoxShapefileAttributeReader(locker, atts, read3D, useMemoryMappedBuffer, res, readDBF, dbfCharset, minRes, col, ite, bbox, loose, minRes);
    } catch (IOException ex) {
        throw new DataStoreException(ex);
    }
}
Also used : AccessManager(org.geotoolkit.data.shapefile.lock.AccessManager) ShpData(org.geotoolkit.data.shapefile.indexed.IndexDataReader.ShpData) DataStoreException(org.apache.sis.storage.DataStoreException) ShxReader(org.geotoolkit.data.shapefile.shx.ShxReader) IOException(java.io.IOException) MismatchedFeatureException(org.opengis.feature.MismatchedFeatureException) TreeException(org.geotoolkit.index.TreeException) DataStoreException(org.apache.sis.storage.DataStoreException) UnsupportedQueryException(org.apache.sis.storage.UnsupportedQueryException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Geometry(org.locationtech.jts.geom.Geometry) AttributeType(org.opengis.feature.AttributeType)

Example 3 with AccessManager

use of org.geotoolkit.data.shapefile.lock.AccessManager in project geotoolkit by Geomatys.

the class IndexedShapefileFeatureStore method getEnvelope.

@Override
public org.opengis.geometry.Envelope getEnvelope(final Query query) throws DataStoreException {
    if (!(query instanceof org.geotoolkit.storage.feature.query.Query))
        throw new UnsupportedQueryException();
    final org.geotoolkit.storage.feature.query.Query gquery = (org.geotoolkit.storage.feature.query.Query) query;
    final Filter filter = gquery.getSelection();
    if (filter == Filter.include() || QueryUtilities.queryAll(gquery)) {
        // use the generic envelope calculation
        return super.getEnvelope(gquery);
    }
    final Set<String> fids = new TreeSet<>();
    IdCollectorFilterVisitor.ID_COLLECTOR.visit(filter, fids);
    final Set records = new HashSet();
    if (!fids.isEmpty()) {
        Collection<ShpData> recordsFound = null;
        try {
            recordsFound = queryFidIndex(fids);
        } catch (IOException ex) {
            throw new DataStoreException(ex);
        }
        if (recordsFound != null) {
            records.addAll(recordsFound);
        }
    }
    if (records.isEmpty())
        return null;
    final AccessManager locker = shpFiles.createLocker();
    ShapefileReader reader = null;
    try {
        reader = locker.getSHPReader(false, false, false, null);
        final JTSEnvelope2D ret = new JTSEnvelope2D(FeatureExt.getCRS(getFeatureType(getNames().iterator().next().toString())));
        for (final Iterator iter = records.iterator(); iter.hasNext(); ) {
            final Data data = (Data) iter.next();
            reader.goTo(((Long) data.getValue(1)).intValue());
            final Record record = reader.nextRecord();
            ret.expandToInclude(record.minX, record.minY);
            ret.expandToInclude(record.maxX, record.maxY);
        }
        return ret;
    } catch (IOException ex) {
        throw new DataStoreException(ex);
    } finally {
        // todo replace by ARM in JDK 1.7
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ex) {
                throw new DataStoreException(ex);
            }
        }
    }
}
Also used : AccessManager(org.geotoolkit.data.shapefile.lock.AccessManager) ShpData(org.geotoolkit.data.shapefile.indexed.IndexDataReader.ShpData) DataStoreException(org.apache.sis.storage.DataStoreException) Query(org.apache.sis.storage.Query) UnsupportedQueryException(org.apache.sis.storage.UnsupportedQueryException) ShapefileReader(org.geotoolkit.data.shapefile.shp.ShapefileReader) Data(org.geotoolkit.index.Data) ShpData(org.geotoolkit.data.shapefile.indexed.IndexDataReader.ShpData) IOException(java.io.IOException) JTSEnvelope2D(org.geotoolkit.geometry.jts.JTSEnvelope2D) Filter(org.opengis.filter.Filter) Record(org.geotoolkit.data.shapefile.shp.ShapefileReader.Record)

Example 4 with AccessManager

use of org.geotoolkit.data.shapefile.lock.AccessManager in project geotoolkit by Geomatys.

the class ShapeFileIndexer method index.

/**
 * Index the shapefile denoted by setShapeFileName(String fileName) If when
 * a thread starts, another thread is indexing the same file, this thread
 * will wait that the first thread ends indexing; in this case <b>zero</b>
 * is reurned as result of the indexing process.
 *
 * @param verbose
 *                enable/disable printing of dots every 500 indexed records
 * @param listener
 *                DOCUMENT ME!
 *
 * @return The number of indexed records (or zero)
 *
 * @throws MalformedURLException
 * @throws IOException
 * @throws TreeException
 * @throws StoreException
 *                 DOCUMENT ME!
 * @throws LockTimeoutException
 */
public int index(final boolean verbose, final ProgressController listener) throws MalformedURLException, IOException, TreeException, StoreException {
    if (this.shpFiles == null) {
        throw new IOException("You have to set a shape file name!");
    }
    int cnt = 0;
    final AccessManager locker = shpFiles.createLocker();
    try (Closeable disposeLocker = locker::disposeReaderAndWriters) {
        // Temporary file for building...
        final StorageFile storage = locker.getStorageFile(this.idxType.shpFileType);
        final Path treeFile = storage.getFile();
        try (ShapefileReader reader = locker.getSHPReader(true, false, false, null)) {
            switch(idxType) {
                case QIX:
                    cnt = this.buildQuadTree(locker, reader, treeFile, verbose);
                    break;
                default:
                    throw new IllegalArgumentException("NONE is not a legal index choice");
            }
        } catch (DataStoreException ex) {
            if (ex.getCause() instanceof IOException)
                throw (IOException) ex.getCause();
            else
                throw new IOException(ex);
        }
    }
    locker.replaceStorageFiles();
    return cnt;
}
Also used : AccessManager(org.geotoolkit.data.shapefile.lock.AccessManager) Path(java.nio.file.Path) DataStoreException(org.apache.sis.storage.DataStoreException) Closeable(java.io.Closeable) StorageFile(org.geotoolkit.data.shapefile.lock.StorageFile) ShapefileReader(org.geotoolkit.data.shapefile.shp.ShapefileReader) IOException(java.io.IOException)

Example 5 with AccessManager

use of org.geotoolkit.data.shapefile.lock.AccessManager in project geotoolkit by Geomatys.

the class DbaseFileTest method testRowVsEntry.

@Test
public void testRowVsEntry() throws Exception {
    Object[] attrs = new Object[dbf.getHeader().getNumFields()];
    final AccessManager locker = shpFiles.createLocker();
    DbaseFileReader dbf2 = locker.getDBFReader(false, ShapefileFeatureStore.DEFAULT_STRING_CHARSET);
    while (dbf.hasNext()) {
        final DbaseFileReader.Row r1 = dbf.next();
        final DbaseFileReader.Row r2 = dbf2.next();
        r1.readAll(attrs);
        for (int i = 0, ii = attrs.length; i < ii; i++) {
            assertNotNull(attrs[i]);
            assertNotNull(r2.read(i));
            assertEquals(attrs[i], r2.read(i));
        }
    }
    dbf2.close();
}
Also used : AccessManager(org.geotoolkit.data.shapefile.lock.AccessManager) DbaseFileReader(org.geotoolkit.data.dbf.DbaseFileReader) Test(org.junit.Test)

Aggregations

AccessManager (org.geotoolkit.data.shapefile.lock.AccessManager)26 ShpFiles (org.geotoolkit.data.shapefile.lock.ShpFiles)13 Test (org.junit.Test)11 ShapefileReader (org.geotoolkit.data.shapefile.shp.ShapefileReader)10 IOException (java.io.IOException)9 DataStoreException (org.apache.sis.storage.DataStoreException)8 StorageFile (org.geotoolkit.data.shapefile.lock.StorageFile)6 Geometry (org.locationtech.jts.geom.Geometry)5 AttributeType (org.opengis.feature.AttributeType)5 URL (java.net.URL)4 UnsupportedQueryException (org.apache.sis.storage.UnsupportedQueryException)4 ShpData (org.geotoolkit.data.shapefile.indexed.IndexDataReader.ShpData)4 ShxReader (org.geotoolkit.data.shapefile.shx.ShxReader)4 Closeable (java.io.Closeable)3 File (java.io.File)3 MalformedURLException (java.net.MalformedURLException)3 DbaseFileHeader (org.geotoolkit.data.dbf.DbaseFileHeader)3 DbaseFileReader (org.geotoolkit.data.dbf.DbaseFileReader)3 IndexedFidReader (org.geotoolkit.data.shapefile.fix.IndexedFidReader)3 JTSEnvelope2D (org.geotoolkit.geometry.jts.JTSEnvelope2D)3