Search in sources :

Example 1 with ShapefileReader

use of org.geotoolkit.data.shapefile.shp.ShapefileReader 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 2 with ShapefileReader

use of org.geotoolkit.data.shapefile.shp.ShapefileReader 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 3 with ShapefileReader

use of org.geotoolkit.data.shapefile.shp.ShapefileReader in project geotoolkit by Geomatys.

the class AccessManager method getSHPReader.

public ShapefileReader getSHPReader(final boolean strict, final boolean memoryMapped, final boolean read3D, final double[] resample) throws IOException, DataStoreException {
    final URI shpUrl = files.getURI(ShpFileType.SHP);
    final ReadableByteChannel shpChannel = toClosingChannel(files.getReadChannel(shpUrl), false);
    final URI shxUrl = files.getURI(ShpFileType.SHX);
    final ReadableByteChannel shxChannel;
    if (shxUrl == null || (files.isWritable() && !files.exists(shxUrl))) {
        // shx does not exist
        shxChannel = null;
    } else {
        shxChannel = toClosingChannel(files.getReadChannel(shxUrl), false);
    }
    final ShapefileReader shpReader = new ShapefileReader(shpChannel, shxChannel, strict, memoryMapped, read3D, resample);
    readEntries.add(new AccessEntry(ShpFileType.SHP, shpUrl, shpReader));
    readEntries.add(new AccessEntry(ShpFileType.SHX, shxUrl, shpReader));
    return shpReader;
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ShapefileReader(org.geotoolkit.data.shapefile.shp.ShapefileReader) URI(java.net.URI)

Example 4 with ShapefileReader

use of org.geotoolkit.data.shapefile.shp.ShapefileReader in project geotoolkit by Geomatys.

the class ShapefileTest method testShapefileReaderRecord.

@Test
public void testShapefileReaderRecord() throws Exception {
    final URL c1 = ShapeTestData.url(STATEPOP);
    final ShpFiles shpFiles = new ShpFiles(c1);
    final AccessManager locker = shpFiles.createLocker();
    ShapefileReader reader = locker.getSHPReader(false, false, true, null);
    URL c2;
    try {
        ArrayList offsets = new ArrayList();
        while (reader.hasNext()) {
            ShapefileReader.Record record = reader.nextRecord();
            offsets.add(new Integer(record.offset()));
            Geometry geom = (Geometry) record.shape();
            assertEquals(new Envelope(record.minX, record.maxX, record.minY, record.maxY), geom.getEnvelopeInternal());
            assertNotNull(record.toString());
        }
        copyShapefiles(STATEPOP);
        reader.close();
        c2 = TestData.url(AbstractTestCaseSupport.class, STATEPOP);
        final ShpFiles shpFiles2 = new ShpFiles(c2);
        final AccessManager locker2 = shpFiles.createLocker();
        reader = locker.getSHPReader(false, false, true, null);
        for (int i = 0, ii = offsets.size(); i < ii; i++) {
            reader.shapeAt(((Integer) offsets.get(i)).intValue());
        }
    } finally {
        reader.close();
    }
}
Also used : AccessManager(org.geotoolkit.data.shapefile.lock.AccessManager) Geometry(org.locationtech.jts.geom.Geometry) ShpFiles(org.geotoolkit.data.shapefile.lock.ShpFiles) ShapefileReader(org.geotoolkit.data.shapefile.shp.ShapefileReader) ArrayList(java.util.ArrayList) Envelope(org.locationtech.jts.geom.Envelope) URL(java.net.URL) Test(org.junit.Test)

Example 5 with ShapefileReader

use of org.geotoolkit.data.shapefile.shp.ShapefileReader in project geotoolkit by Geomatys.

the class ShapefileTest method testShapefileReaderRecord.

@Override
@Test
public void testShapefileReaderRecord() throws Exception {
    final File file = copyShapefiles(STATEPOP);
    final ShpFiles shpFiles = new ShpFiles(file.toURI().toURL());
    final AccessManager locker = shpFiles.createLocker();
    ShapefileReader reader = locker.getSHPReader(false, false, true, null);
    ArrayList offsets = new ArrayList();
    while (reader.hasNext()) {
        ShapefileReader.Record record = reader.nextRecord();
        offsets.add(new Integer(record.offset()));
        Geometry geom = (Geometry) record.shape();
        assertEquals(new Envelope(record.minX, record.maxX, record.minY, record.maxY), geom.getEnvelopeInternal());
        assertNotNull(record.toString());
    }
    reader.close();
    copyShapefiles(STATEPOP);
    reader = locker.getSHPReader(false, false, true, null);
    for (int i = 0, ii = offsets.size(); i < ii; i++) {
        reader.shapeAt(((Integer) offsets.get(i)).intValue());
    }
    reader.close();
}
Also used : AccessManager(org.geotoolkit.data.shapefile.lock.AccessManager) Geometry(org.locationtech.jts.geom.Geometry) ShpFiles(org.geotoolkit.data.shapefile.lock.ShpFiles) ShapefileReader(org.geotoolkit.data.shapefile.shp.ShapefileReader) ArrayList(java.util.ArrayList) Envelope(org.locationtech.jts.geom.Envelope) File(java.io.File) Test(org.junit.Test)

Aggregations

ShapefileReader (org.geotoolkit.data.shapefile.shp.ShapefileReader)12 AccessManager (org.geotoolkit.data.shapefile.lock.AccessManager)10 ShpFiles (org.geotoolkit.data.shapefile.lock.ShpFiles)8 URL (java.net.URL)5 Test (org.junit.Test)4 IOException (java.io.IOException)3 DataStoreException (org.apache.sis.storage.DataStoreException)3 Geometry (org.locationtech.jts.geom.Geometry)3 Closeable (java.io.Closeable)2 ReadableByteChannel (java.nio.channels.ReadableByteChannel)2 ArrayList (java.util.ArrayList)2 Envelope (org.locationtech.jts.geom.Envelope)2 File (java.io.File)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)1 Query (org.apache.sis.storage.Query)1 UnsupportedQueryException (org.apache.sis.storage.UnsupportedQueryException)1 DbaseFileHeader (org.geotoolkit.data.dbf.DbaseFileHeader)1 DbaseFileReader (org.geotoolkit.data.dbf.DbaseFileReader)1