shapewrite
Write geographic vector data structure to shapefile
Description
Examples
Write Feature Data to Shapefile
Import a shapefile, remove a subset of the data, and write the modified data to a new shapefile.
Get information about a shapefile as a structure. The shapefile contains a network of road segments in Concord, MA.
shapeinfo("concord_roads.shp")
ans = struct with fields:
Filename: [3x72 char]
ShapeType: 'PolyLine'
BoundingBox: [2x2 double]
NumFeatures: 609
Attributes: [5x1 struct]
CoordinateReferenceSystem: [1x1 projcrs]
The NumFeatures
field indicates that the shapefile contains 609
features.
Import the shapefile as a geospatial table.
GT = readgeotable("concord_roads.shp");
The CLASS
attribute of the shapefile includes information about road types. For example, a value of 2
indicates a multilane highway and a value of 6
indicates a minor road. Create a subtable containing major roads such that the value of CLASS
is less than 4
.
rows = GT.CLASS < 4; majorGT = GT(rows,:);
Export the geospatial table as a shapefile.
shapewrite(majorGT,"main_concord_roads.shp")
Get information about the new shapefile as a structure.
shapeinfo("main_concord_roads.shp")
ans = struct with fields:
Filename: [3x77 char]
ShapeType: 'PolyLine'
BoundingBox: [2x2 double]
NumFeatures: 107
Attributes: [5x1 struct]
CoordinateReferenceSystem: [1x1 projcrs]
The new shapefile has 107
features instead of 609
features.
Write Data Stored in Planar Point Vector to Shapefile
Read a shapefile containing a vector of world cities. Store the data as a planar point vector using the mappoint
function.
S = shaperead("worldcities.shp");
p = mappoint(S)
p = 318x1 mappoint vector with properties: Collection properties: Geometry: 'point' Metadata: [1x1 struct] Feature properties: X: [-3.9509 54.7589 -0.2121 35.3894 38.7575 138.8528 44.5408 72.2474 30.4098 3.0397 77.0010 35.9214 75.0498 4.8892 -149.1074 32.9496 122.8003 47.5258 -71.4434 40.6098 58.6638 38.8593 -57.5113 23.5183 -84.3489 174.7379 44.4258 ... ] (1x318 double) Y: [5.2985 24.6525 5.6106 37.0613 9.0235 -34.6645 12.8767 22.7778 31.3044 36.7870 43.3620 31.7444 31.7744 52.3699 61.6040 39.7831 41.4673 -18.9141 -16.3867 64.7383 38.0316 15.3042 -25.1162 38.0164 33.7657 -36.9126 33.3449 -38.7281 ... ] (1x318 double) Name: {1x318 cell}
Append Paderborn, Germany to the point vector.
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: [-3.9509 54.7589 -0.2121 35.3894 38.7575 138.8528 44.5408 72.2474 30.4098 3.0397 77.0010 35.9214 75.0498 4.8892 -149.1074 32.9496 122.8003 47.5258 -71.4434 40.6098 58.6638 38.8593 -57.5113 23.5183 -84.3489 174.7379 44.4258 ... ] (1x319 double) Y: [5.2985 24.6525 5.6106 37.0613 9.0235 -34.6645 12.8767 22.7778 31.3044 36.7870 43.3620 31.7444 31.7744 52.3699 61.6040 39.7831 41.4673 -18.9141 -16.3867 64.7383 38.0316 15.3042 -25.1162 38.0164 33.7657 -36.9126 33.3449 -38.7281 ... ] (1x319 double) Name: {1x319 cell}
The size of p
has increased by 1.
Write the updated point vector to a shapefile.
shapewrite(p,"worldcities_updated.shp")
Input Arguments
S
— Vector geographic features
geospatial table | mappoint
vector | mapshape
vector | mapstruct vector | geopoint
vector | geoshape
vector | geostruct vector
Vector geographic features, specified as one of the following:
A geospatial table. The
shapewrite
function does not support geospatial tables containing more than one type of shape object. For more information about geospatial tables, see Create Geospatial Tables.A
mappoint
vector.A
mapshape
vector.A map structure array (
mapstruct
) withX
andY
coordinate fields.A
geopoint
vector.A
geoshape
vector.A geographic structure array (
geostruct
) withLat
andLon
fields.
S
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 geospatial table containinggeopointshape
,geolineshape
, orgeopolyshape
objects, ageopoint
vector, ageoshape
vector, or a geographic structure array, theshapewrite
function 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
— Filename
string scalar | character vector
Filename and location at which to create the shapefile, specified as a
string scalar or character vector. If you include a file extension, it must
be .shp
or .SHP
.
The shapewrite
function creates a main file
(.shp
), an index file (.shx
), and
an attribute file (.dbf
).
If you specify S
as a geospatial table that contains
a coordinate reference system (CRS) object, then the function also creates a
projection file (.prj
) containing the well-known text
(WKT) string representation of the CRS. To create a projection file from a
geospatial table containing planar shape objects, the
ProjectedCRS
properties of the shapes must be
nonempty. To create a projection file from a geospatial table containing
geographic shape objects, the GeographicCRS
properties
of the shapes must be nonempty. You can convert structure arrays to
geospatial tables by using the struct2geotable
function.
All of the files have the same base name. For example, if you specify
filename
as "myfile.shp"
, then
the function creates myfile.shp
,
myfile.shx
, myfile.dbf
, and
myfile.prj
.
dbfspec
— Feature attributes to include in shapefile
scalar structure
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 fileFieldType
— The field type to be used in the file:'N'
(numeric) or'C'
(character)FieldLength
— The field length in the file, in bytesFieldDecimalCount
— 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
.
More About
Shapefile format
Data files in the shapefile format store vector data using multiple files: a main
file (.shp
), an index file (.shx
), and an
attribute file (.dbf
). Files in the shapefile format can also
include optional files, such as projection files (.prj
). All the
files have the same base name, for example, myfile.shp
,
myfile.shx
, and myfile.dbf
.
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 callingshapewrite
. 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.
Version History
Introduced before R2006aR2023b: Write projection file containing WKT string representation of CRS
When you specify S
as a geospatial table that contains a
coordinate reference system (CRS) object, then the shapewrite
function creates a projection file (.prj
) containing the
well-known text (WKT) string representation of the CRS.
R2022b: shapewrite
truncates text when length exceeds 254 characters or value of FieldLength
When a text attribute contains more than 254 characters, the
shapewrite
function issues a warning and truncates the text
to 254 characters. In previous releases, the shapewrite
function did not truncate the text and, as a result, created a file that does not
conform to shapefile specifications.
When a text attribute contains more characters than the value stored in the
FieldLength
field of the dbfspec
argument, the shapewrite
function issues a warning and
truncates the text to the number of characters equal to the value of
FieldLength
. In previous releases, the
shapewrite
function issued an error.
R2021b: Write geospatial table to shapefile format
The shapewrite
function accepts geospatial tables as
input.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)