linelength
Syntax
Description
Examples
Calculate Length of Line in Geographic Coordinates
Read world rivers into the workspace as a geospatial table. Extract the line shapes.
rivers = readgeotable("worldrivers.shp");
shape = rivers.Shape
shape=128×1 geolineshape array with properties:
NumParts: [128x1 double]
Geometry: "line"
CoordinateSystemType: "geographic"
GeographicCRS: [1x1 geocrs]
⋮
Calculate the lengths of the line shapes.
len = linelength(shape);
The units depend on the geographic coordinate reference system (CRS) for the shape. Find the length unit for the geographic CRS. The result indicates that the line lengths are in meters.
shape.GeographicCRS.Spheroid.LengthUnit
ans = 'meter'
Include the line lengths in the geospatial table by creating a new table variable. Sort the rows of the table in descending order by Length
. Then, view the first eight rows of the table by using the head
function. Note that, because the worldrivers.shp
file stores information about some rivers using multiple line shapes, the line lengths reported in the first eight rows of the table might not reflect the total lengths of the named rivers.
rivers.Length = len; rivers = sortrows(rivers,"Length","descend"); head(rivers)
Shape Name Length ____________ ___________________ __________ geolineshape "Nile - White Nile" 4.2573e+06 geolineshape "Mekong" 3.9981e+06 geolineshape "Yangtze" 3.3539e+06 geolineshape "Yellow" 3.1722e+06 geolineshape "Niger" 3.0812e+06 geolineshape "Missouri" 2.9331e+06 geolineshape "Indus" 2.5962e+06 geolineshape "Brahmaputra" 2.5607e+06
Calculate Length of Line in Planar Coordinates
Read hydrographic data for Concord, MA into the workspace as a geospatial table. Extract the line shapes.
hydro = readgeotable("concord_hydro_line.shp");
shape = hydro.Shape
shape=237×1 maplineshape array with properties:
NumParts: [237x1 double]
Geometry: "line"
CoordinateSystemType: "planar"
ProjectedCRS: [1x1 projcrs]
⋮
Calculate the lengths of the line shapes.
len = linelength(shape);
The units depend on the projected coordinate reference system (CRS) for the shape. Find the length unit for the projected CRS. The result indicates that the line lengths are in meters.
shape.ProjectedCRS.LengthUnit
ans = "meter"
Create a new geospatial table from the shape objects and the line lengths. View the first eight rows of the table by using the head
function.
hydro_length = table(shape,len,VariableNames=["Shape","Length"]); head(hydro_length)
Shape Length ____________ ______ maplineshape 86.53 maplineshape 137.36 maplineshape 151.86 maplineshape 32.875 maplineshape 70.196 maplineshape 109.87 maplineshape 117.55 maplineshape 59.693
Input Arguments
shape
— Line shape
geolineshape
object |
maplineshape
object | array of geolineshape
objects | array of maplineshape
objects
Line shape, specified as a geolineshape
object, a maplineshape
object, an array of geolineshape
objects, or an array of
maplineshape
objects.
Output Arguments
len
— Line length
array
Line length, returned as an array. The size of len
matches the
size of shape
.
The units of len
depend on the type of line shape.
When
shape
containsgeolineshape
objects, the length unit of the reference ellipsoid for the shape determines the units. To find the length unit, get thegeocrs
object for the shape by querying theGeographicCRS
property of the shape object. Then, get the ellipsoid by querying theSpheroid
property of thegeocrs
object. If the ellipsoid is areferenceEllipsoid
orreferenceSphere
object, then query theLengthUnit
property of the ellipsoid. For a shapeshp
, the length unit isshp.GeographicCRS.Spheroid.LengthUnit
. If theGeographicCRS
property of the object is empty, the function calculates the line length using the WGS84 reference ellipsoid and a length unit of meters.When
shape
containsmaplineshape
objects, the length unit of the projected coordinate reference system for the shape determines the units. To find the length unit, get theprojcrs
object for the shape by querying theProjectedCRS
property of the shape object. Then, query theLengthUnit
property of theprojcrs
object. For a shapeshp
, the length unit isshp.ProjectedCRS.LengthUnit
.
When shape
contains geolineshape
objects, the
linelength
function calculates the line lengths using geodesics.
Data Types: double
Tips
The accuracy of the linelength
function depends on
the resolution of the data used to create the lines. As a result, the function can return
different results when you use line shapes from different data sets as inputs.
Version History
Introduced in R2024a
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)