Quantcast

Documentation Center

  • Trial Software
  • Product Updates

shapewrite

Write geographic vector data structure to shapefile

Syntax

shapewrite(S,filename)
shapewrite(S,filename,'DbfSpec',dbfspec)

Description

shapewrite(S,filename) writes the vector geographic features stored in S to the file specified by filename in shapefile format.

shapewrite(S,filename,'DbfSpec',dbfspec) writes a shapefile in which the content and layout of the DBF file is controlled by a DBF specification, indicated here by the parameter value dbfspec.

Input Arguments

S

Vector geographic features, specified as a mappoint vector, mapshape vector, mapstruct (with X and Y coordinate fields), geopoint vector, geoshape vector, or a geostruct (with 'Lat' and 'Lon' fields) with the following restrictions on its attribute fields:

  • Each attribute field value must be either a real, finite, scalar double or a character string.

  • The type of a given attribute must be consistent across all features.

  • If S is a geopoint vector, geoshape vector, or a geostruct, shapewrite writes the latitude and longitude values as Y and X coordinates, respectively.

  • If a given attribute is integer-valued for all features, shapewrite writes it to the [basename '.dbf'] file as an integer. If an attribute is non-integer-valued for any feature,shapewrite writes it as a fixed point decimal value with six digits to the right of the decimal place.

filename

Name and location of the shapefile to create, specified as a character string. If the file name includes a file extension, it must be '.shp' or '.SHP'. shapewrite creates three output files: [basename '.shp'], [basename '.shx'], and [basename '.dbf'], where basename is filename without its extension.

'DbfSpec'

Feature attributes to be included in the shapefile, specified as a scalar MATLAB® structure containing one field for each feature attribute. Assign to that field a scalar structure with the following four fields:

  • FieldName — The field name to be used in the file

  • FieldType — The field type to be used in the file: 'N' (numeric) or 'C' (character)

  • FieldLength — The field length in the file, in bytes

  • FieldDecimalCount — For numeric fields, the number of digits to the right of the decimal place

Call makedbfspec to construct a DBF spec. Modify the output to remove attributes or change the FieldName, FieldLength, or FieldDecimalCount for one or more attributes.

To include an attribute in the output file, specify a field in dbfspec with the same name as the attribute is specified in S.

Examples

expand all

Write Feature Data to Shapefile

Derive a shapefile from concord_roads.shp in which roads of CLASS 5 and greater are omitted.

Get information about the contents of a shapefile. Note that it contains 609 features (NumFeatures).

shapeinfo('concord_roads')  
ans = 
       Filename: [3x67 char]
      ShapeType: 'PolyLine'
    BoundingBox: [2x2 double]
    NumFeatures: 609
     Attributes: [5x1 struct]

Read a selection of the data in the file into a mapstruct. Note the use of the 'Selector' option in shaperead, together with an anonymous function, to read only the main roads from the original shapefile.

S = shaperead('concord_roads', 'Selector', ...
              {@(roadclass) roadclass < 4, 'CLASS'});

Write the data to a new shapefile.

shapewrite(S, 'main_concord_roads.shp')

Get information about the contents of the new shapefile.

shapeinfo('main_concord_roads')  % 107 features
ans = 
       Filename: [3x24 char]
      ShapeType: 'PolyLine'
    BoundingBox: [2x2 double]
    NumFeatures: 107
     Attributes: [5x1 struct]

Write Data Stored in mappoint to Shapefile

Read a shapefile containing a vector of world cities and store the data in a mappoint vector.

p = mappoint(shaperead('worldcities.shp'))
p = 

 318x1 mappoint vector with properties:

 Collection properties:
    Geometry: 'point'
    Metadata: [1x1 struct]
 Feature properties:
           X: [1x318 double]
           Y: [1x318 double]
        Name: {1x318 cell}

Append Paderborn Germany to the mappoint vector. Note that the size of p has increased by 1.

x = 51.715254;
y = 8.75213;
p = append(p, x, y, 'Name', 'Paderborn')
p = 

 319x1 mappoint vector with properties:

 Collection properties:
    Geometry: 'point'
    Metadata: [1x1 struct]
 Feature properties:
           X: [1x319 double]
           Y: [1x319 double]
        Name: {1x319 cell}

Write the updated mappoint vector to a shapefile.

shapewrite(p,'worldcities_updated')

More About

expand all

Tips

  • The easiest way to create a DBF spec is to call makedbfspec and then modify the output to remove attributes or change the FieldName, FieldLength, or FieldDecimalCount for one or more attributes. See the help for makedbfspec for more details and an example.

  • The xBASE (.dbf) file specifications require that geostruct and mapstruct attribute names are truncated to 11 characters when copied as DBF field names. Consider shortening long field names before calling shapewrite. By doing this, you make field names in the DBF file more readable and avoid introducing duplicate names as a result of truncation.

  • Remember to set your character encoding scheme to match that of the geographic data structure you are exporting. For instance, if you are exporting a map that displays Japanese text, configure your machine to support Shift-JIS character encoding.

See Also

| |

Was this topic helpful?