Class ShapefileDbfReader
- All Implemented Interfaces:
Closeable,AutoCloseable
.dbf) attribute tables as
used in ESRI Shapefiles.
File format overview
A .dbf file consists of:
- A 32-byte file header containing the record count, header size, and record size.
- One 32-byte field descriptor per column, terminated by a
0x0Dbyte. - A sequence of fixed-width data records, each preceded by a one-byte
deletion flag (
0x20= valid,0x2A= deleted).
All integers in the header are little-endian. Field values are stored as fixed-width, space-padded ASCII (or the file's declared charset).
Usage
try (ShapefileDbfReader dbf = new ShapefileDbfReader(path)) {
List<Map<String, String>> records = dbf.readAllRecords();
for (Map<String, String> record : records) {
String name = record.get("ADMIN");
String iso = record.get("ISO_A3");
}
}
Character encoding
Natural Earth shapefiles use UTF-8 or ISO-8859-1. By default this reader
uses UTF-8 (StandardCharsets.UTF_8); callers can override this with
ShapefileDbfReader(Path, Charset). The dBASE language-driver byte
at offset 29 is read but not acted upon — explicit charset specification is
more reliable in practice.
Thread safety
Instances are not thread-safe. Each calling thread should open its own reader.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordImmutable description of a single.dbffield (column). -
Constructor Summary
ConstructorsConstructorDescriptionShapefileDbfReader(Path path) Opens the.dbffile at the given path using UTF-8 encoding for field values.ShapefileDbfReader(Path path, Charset charset) Opens the.dbffile at the given path using the specified character encoding for field values. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Returns the names of all fields in column order.Returns an unmodifiable view of the field descriptors in column order.intReturns the number of data records declared in the file header.Reads and returns all records as an ordered list ofString-valued maps.readRecord(int index) Reads a single record by its zero-based index.
-
Constructor Details
-
ShapefileDbfReader
Opens the.dbffile at the given path using UTF-8 encoding for field values.- Parameters:
path- path to the.dbffile; must not benull- Throws:
IOException- if the file cannot be opened or the header is malformed
-
ShapefileDbfReader
Opens the.dbffile at the given path using the specified character encoding for field values.- Parameters:
path- path to the.dbffile; must not benullcharset- character encoding to use when decoding field values; must not benull- Throws:
IOException- if the file cannot be opened or the header is malformed
-
-
Method Details
-
getRecordCount
public int getRecordCount()Returns the number of data records declared in the file header.This includes records marked as deleted.
readAllRecords()preserves this positional record count by representing deleted records as empty maps.- Returns:
- declared record count (≥ 0)
-
getFields
Returns an unmodifiable view of the field descriptors in column order.- Returns:
- list of
ShapefileDbfReader.FieldDescriptorinstances
-
getFieldNames
Returns the names of all fields in column order.- Returns:
- ordered list of field name strings
-
readAllRecords
Reads and returns all records as an ordered list ofString-valued maps.Each map key is the field name (trimmed, upper-cased as stored in the dBASE header). Each map value is the decoded, whitespace-trimmed field value, or an empty string if the field contained only spaces.
This is a sequential full-scan; the channel is rewound to the first data record before reading begins. Deleted records (deletion flag
0x2A) are represented by empty maps so that DBF record indexes remain aligned with the corresponding shapefile geometry records. Any other flag besides the valid-record marker0x20causes anIOException.- Returns:
- unmodifiable list of records, each a field-name → value map
- Throws:
IOException- if a read error occurs
-
readRecord
Reads a single record by its zero-based index.This performs a seek to the record's position rather than a sequential scan, making it efficient for random access. Returns
nullif the record is marked deleted.- Parameters:
index- zero-based record index; must be in [0, recordCount)- Returns:
- field-name → value map, or
nullif the record is deleted - Throws:
IOException- if a read error occursIndexOutOfBoundsException- ifindexis out of range
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-