makedbfspec

DBF specification from geographic data structure

Syntax

dbfspec = makedbfspec(S)

Description

dbfspec = makedbfspec(S) analyzes a geographic data structure, S, and constructs a DBF specification suitable for use with shapewrite. You can modify dbfspec, then pass it to shapewrite to exert control over which geostruct attribute fields are written to the DBF component of the shapefile, the field-widths, and the precision used for numerical values.

dbfspec is a scalar MATLAB® structure with two levels. The top level consists of a field for each attribute in S. Each of these fields, in turn, contains a scalar structure with a fixed set of four fields:

dbfspec field

Contents

FieldName

The field name to be used within the DBF file. This name will be identical to the name of the corresponding attribute, but may be modified prior to calling shapewrite. This modification might be necessary, for example, because you want to use spaces in your DBF field names but know that you cannot use spaces for MATLAB variable names.

FieldType

The field type to be used in the file, either 'N' (numeric) or 'C' (character).

FieldLength

The number of bytes that each instance of the field will occupy in the file.

FieldDecimalCount

The number of digits to the right of the decimal place that are kept in a numeric field. Zero for integer-valued fields and character fields. The default value for noninteger numeric fields is 6.

Examples

Import a shapefile representing a small network of road segments, and construct a DBF specification.

s = shaperead('concord_roads')

s = 
609x1 struct array with fields:
    Geometry
    BoundingBox
    X
    Y
    STREETNAME
    RT_NUMBER
    CLASS
    ADMIN_TYPE
    LENGTH

dbfspec = makedbfspec(s)

dbfspec = 
    STREETNAME: [1x1 struct]
     RT_NUMBER: [1x1 struct]
         CLASS: [1x1 struct]
    ADMIN_TYPE: [1x1 struct]
        LENGTH: [1x1 struct]

Modify the DBF spec to (a) eliminate the 'ADMIN_TYPE' attribute, (b) rename the 'STREETNAME' field to 'Street Name', and (c) reduce the number of decimal places used to store road lengths.

dbfspec = rmfield(dbfspec,'ADMIN_TYPE')

dbfspec = 
    STREETNAME: [1x1 struct]
     RT_NUMBER: [1x1 struct]
         CLASS: [1x1 struct]
        LENGTH: [1x1 struct]

dbfspec.STREETNAME.FieldName = 'Street Name';
dbfspec.LENGTH.FieldDecimalCount = 1;

Export the road network back to a modified shapefile. (Actually, only the DBF component will be different.)

shapewrite(s, 'concord_roads_modified', 'DbfSpec', dbfspec)

Verify the changes you made. Notice the appearance of 'Street Name' in the field names reported by shapeinfo, the absence of the 'ADMIN_TYPE' field, and the reduction in the precision of the road lengths.

info = shapeinfo('concord_roads_modified')
info = 
       Filename: [3x28 char]
      ShapeType: 'PolyLine'
    BoundingBox: [2x2 double]
    NumFeatures: 609
     Attributes: [4x1 struct]

{info.Attributes.Name}

ans = 
    'Street Name'    'RT_NUMBER'    'CLASS'    'LENGTH'

r = shaperead('concord_roads_modified')

r = 
609x1 struct array with fields:
    Geometry
    BoundingBox
    X
    Y
    StreetName
    RT_NUMBER
    CLASS
    LENGTH

s(33).LENGTH

ans =
    3.492817400000000e+002

r(33).LENGTH

ans =
    3.493000000000000e+002
Introduced before R2006a