shapewrite

Write geographic vector data structure to shapefile

Description

example

shapewrite(S,filename) writes the vector geographic features stored in shapefile 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 dbfspec, a DBF specification.

Examples

collapse all

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]

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')

Input Arguments

collapse all

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). A has the following restrictions on its attribute fields:

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

  • 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.

File name and location of the shapefile to create, specified as a string scalar or character vector. 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.

Feature attributes to include 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

To create a DBF spec, call makedbfspec and then 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.

Tips

  • 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.

Introduced before R2006a