Search in sources :

Example 1 with DbaseFileReader

use of org.geotoolkit.data.dbf.DbaseFileReader in project geotoolkit by Geomatys.

the class AccessManager method getDBFReader.

public DbaseFileReader getDBFReader(final boolean memoryMapped, final Charset set) throws IOException {
    final URI uri = files.getURI(ShpFileType.DBF);
    if (uri == null) {
        return null;
    }
    if (files.isWritable() && !files.exists(ShpFileType.DBF)) {
        return null;
    }
    final ReadableByteChannel rbc = toClosingChannel(files.getReadChannel(uri), false);
    final DbaseFileReader reader = new DbaseFileReader(rbc, memoryMapped, set);
    readEntries.add(new AccessEntry(ShpFileType.DBF, uri, reader));
    return reader;
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) DbaseFileReader(org.geotoolkit.data.dbf.DbaseFileReader) URI(java.net.URI)

Example 2 with DbaseFileReader

use of org.geotoolkit.data.dbf.DbaseFileReader 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)

Example 3 with DbaseFileReader

use of org.geotoolkit.data.dbf.DbaseFileReader in project geotoolkit by Geomatys.

the class DbaseFileTest method testEmptyFields.

@Test
public void testEmptyFields() throws Exception {
    DbaseFileHeader header = new DbaseFileHeader();
    header.addColumn("emptyString", 'C', 20, 0);
    header.addColumn("emptyInt", 'N', 20, 0);
    header.addColumn("emptyDouble", 'N', 20, 5);
    header.addColumn("emptyFloat", 'F', 20, 5);
    header.addColumn("emptyLogical", 'L', 1, 0);
    header.addColumn("emptyDate", 'D', 20, 0);
    header.setNumRecords(20);
    File f = new File(System.getProperty("java.io.tmpdir"), "scratchDBF.dbf");
    f.deleteOnExit();
    FileOutputStream fout = new FileOutputStream(f);
    DbaseFileWriter dbf = new DbaseFileWriter(header, fout.getChannel(), Charset.defaultCharset());
    for (int i = 0; i < header.getNumRecords(); i++) {
        dbf.write(new Object[] { String.valueOf(900 + i), null, null, null, null, null });
    }
    dbf.close();
    final ShpFiles tempShpFiles = new ShpFiles(f);
    final AccessManager locker = tempShpFiles.createLocker();
    DbaseFileReader r = locker.getDBFReader(false, ShapefileFeatureStore.DEFAULT_STRING_CHARSET);
    int cnt = 0;
    while (r.hasNext()) {
        cnt++;
        Object[] o = r.next().readAll(null);
        assertTrue(o.length == r.getHeader().getNumFields());
    }
    assertEquals("Bad number of records", cnt, 20);
    // make sure the channel is closed
    r.close();
    f.delete();
}
Also used : AccessManager(org.geotoolkit.data.shapefile.lock.AccessManager) DbaseFileWriter(org.geotoolkit.data.dbf.DbaseFileWriter) DbaseFileReader(org.geotoolkit.data.dbf.DbaseFileReader) FileOutputStream(java.io.FileOutputStream) ShpFiles(org.geotoolkit.data.shapefile.lock.ShpFiles) File(java.io.File) DbaseFileHeader(org.geotoolkit.data.dbf.DbaseFileHeader) Test(org.junit.Test)

Example 4 with DbaseFileReader

use of org.geotoolkit.data.dbf.DbaseFileReader in project geotoolkit by Geomatys.

the class ShapefileFeatureStore method buildSchema.

// //////////////////////////////////////////////////////////////////////////
// utils ///////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////
/**
 * Obtain the FeatureType of the given name. ShapefileFeatureStore contains
 * only one FeatureType.
 *
 * @return The FeatureType that this featurestore contains.
 * @throws IOException If a type by the requested name is not present.
 */
private synchronized FeatureType buildSchema() throws DataStoreException {
    // add an identifier field
    final FeatureTypeBuilder builder = new FeatureTypeBuilder();
    final String name = shpFiles.getTypeName();
    builder.setName(name);
    builder.addAttribute(String.class).setName(AttributeConvention.IDENTIFIER_PROPERTY);
    // read all attributes///////////////////////////////////////////////////
    final AccessManager locker = shpFiles.createLocker();
    try (Closeable disposeLocker = locker::disposeReaderAndWriters) {
        final ShapefileReader shp = locker.getSHPReader(true, useMemoryMappedBuffer, true, null);
        final DbaseFileReader dbf = locker.getDBFReader(useMemoryMappedBuffer, dbfCharset);
        CoordinateReferenceSystem crs = null;
        // read the projection
        final boolean qpjExists = shpFiles.exists(QPJ);
        final boolean prjExists = shpFiles.exists(PRJ);
        if (qpjExists || prjExists) {
            try (final ReadableByteChannel channel = qpjExists ? shpFiles.getReadChannel(QPJ) : shpFiles.getReadChannel(PRJ)) {
                crs = PrjFiles.read(channel, true);
            } catch (IOException ex) {
                getLogger().log(Level.WARNING, ex.getMessage(), ex);
                crs = null;
            }
        }
        final AttributeType geomDescriptor;
        // get the descriptor from shp
        geomDescriptor = shp.getHeader().createDescriptor(crs);
        builder.addAttribute(geomDescriptor).addRole(AttributeRole.DEFAULT_GEOMETRY);
        // get dbf attributes if exist
        if (dbf != null) {
            final DbaseFileHeader header = dbf.getHeader();
            for (AttributeType at : header.createDescriptors()) {
                builder.addAttribute(at);
            }
        }
    } catch (IOException ex) {
        throw new DataStoreException(ex);
    }
    return builder.build();
}
Also used : AccessManager(org.geotoolkit.data.shapefile.lock.AccessManager) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) ReadableByteChannel(java.nio.channels.ReadableByteChannel) DataStoreException(org.apache.sis.storage.DataStoreException) Closeable(java.io.Closeable) ShapefileReader(org.geotoolkit.data.shapefile.shp.ShapefileReader) IOException(java.io.IOException) DbaseFileHeader(org.geotoolkit.data.dbf.DbaseFileHeader) DbaseFileReader(org.geotoolkit.data.dbf.DbaseFileReader) AttributeType(org.opengis.feature.AttributeType) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Aggregations

DbaseFileReader (org.geotoolkit.data.dbf.DbaseFileReader)4 AccessManager (org.geotoolkit.data.shapefile.lock.AccessManager)3 ReadableByteChannel (java.nio.channels.ReadableByteChannel)2 DbaseFileHeader (org.geotoolkit.data.dbf.DbaseFileHeader)2 Test (org.junit.Test)2 Closeable (java.io.Closeable)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 URI (java.net.URI)1 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 DbaseFileWriter (org.geotoolkit.data.dbf.DbaseFileWriter)1 ShpFiles (org.geotoolkit.data.shapefile.lock.ShpFiles)1 ShapefileReader (org.geotoolkit.data.shapefile.shp.ShapefileReader)1 AttributeType (org.opengis.feature.AttributeType)1 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)1