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;
}
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();
}
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();
}
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();
}
Aggregations