intersect
Description
calculates the geometric intersection of the specified point, line, or polygon
shapes.shapeout = intersect(shape1)
When you specify point shapes, the intersection is a point shape that contains the points in
shape1that overlap.When you specify line shapes, the intersection is a line shape that contains the line parts in
shape1that overlap.When you specify polygon shapes, the intersection is a polygon shape that contains the polygon regions in
shape1that overlap.
Examples
Read a shapefile containing the coordinates of locations in Boston as a geospatial table. The table represents the locations using point shapes in planar coordinates.
GT = readgeotable("boston_placenames.shp");Create a subtable that contains two table rows. Then, create a vector of polygon shapes by buffering the point shapes.
subGT = geocode(["BEACON HILL","COPPS HILL"],GT); shape1 = buffer(subGT.Shape,1000)
shape1=2×1 mappolyshape array with properties:
NumRegions: [2×1 double]
NumHoles: [2×1 double]
Geometry: "polygon"
CoordinateSystemType: "planar"
ProjectedCRS: [1×1 projcrs]
Calculate the intersection of the polygon shapes. The result is a scalar polygon shape in planar coordinates.
shapeout = intersect(shape1)
shapeout =
mappolyshape with properties:
NumRegions: 1
NumHoles: 0
Geometry: "polygon"
CoordinateSystemType: "planar"
ProjectedCRS: [1×1 projcrs]
Display the original polygon shapes and the intersection on a map.
figure
geoplot(shape1)
hold on
geoplot(shapeout)
Create two vectors of polygon shapes in geographic coordinates.
The first vector contains polygon shapes that buffer the cities of Adelaide and Brisbane.
The second vector contains polygon shapes that buffer the cities of Melbourne and Sydney.
GT1 = geocode(["Adelaide","Brisbane"]); shape1 = buffer(GT1.Shape,6); GT2 = geocode(["Melbourne","Sydney"]); shape2 = buffer(GT2.Shape,3.5);
Calculate the element-wise intersections of the polygon shapes. The result is a two-element vector of polygon shapes.
The first polygon shape is the intersection of the polygon shapes for Adelaide and Melbourne.
The second polygon shape is the intersection of the polygon shapes for Brisbane and Sydney.
shapeout = intersect(shape1,shape2)
shapeout=2×1 geopolyshape array with properties:
NumRegions: [2×1 double]
NumHoles: [2×1 double]
Geometry: "polygon"
CoordinateSystemType: "geographic"
GeographicCRS: [1×1 geocrs]
Display the original polygon shapes and the intersections on a map. Note that the intersect function did not calculate the intersection of the polygon shapes for Melbourne and Sydney.
figure
geoplot([shape1; shape2])
hold on
geoplot(shapeout)
Input Arguments
Input shapes, specified as one of these options:
Array of
geopointshapeobjects — Point shapes in geographic coordinatesArray of
geolineshapeobjects — Line shapes in geographic coordinatesArray of
geopolyshapeobjects — Polygon shapes in geographic coordinatesArray of
mappointshapeobjects — Point shapes in planar coordinatesArray of
maplineshapeobjects — Line shapes in planar coordinatesArray of
mappolyshapeobjects — Polygon shapes in planar coordinates
If you specify both shape1 and shape2 as
input to the intersect function, then shape1
and shape2 must share these characteristics:
The geometric types of
shape1andshape2must match. For example, ifshape1is a polygon, thenshape2must also be a polygon. You can find the geometric type of a shape object by querying theGeometryproperty.The coordinate system types of
shape1andshape2must match. For example, ifshape1is in geographic coordinates, thenshape2must also be in geographic coordinates. You can find the coordinate system type of a shape object by querying theCoordinateSystemTypeproperty.The coordinate reference systems (CRSs) of
shape1andshape2must match, unless one ofshape1orshape2is not associated with a CRS. For a shape object in geographic coordinates, you can find the CRS by querying theGeographicCRSproperty. For a shape object in planar coordinates, you can find the CRS by querying theProjectedCRSproperty.The sizes of
shape1andshape2must be compatible. For more information about compatible array sizes, see Compatible Array Sizes for Basic Operations.
Output Arguments
Intersection of the shapes, returned as an array of geopointshape,
geolineshape,
geopolyshape,
mappointshape,
maplineshape, or
mappolyshape
objects.
When you specify only shape1, the shapeout
argument is a scalar. When you specify both shape1 and
shape2, the size of shapeout depends on the
sizes of shape1 and shape2. For more
information about array sizes, see Compatible Array Sizes for Basic Operations.
The geometric type and coordinate system of shapeout matches
the geometric type and coordinate system of the input shape or shapes. For example, if
shape1 and shape2 contain
maplineshape objects, then shapeout also
contains maplineshape objects.
Version History
Introduced in R2025aThe intersect function shows improved performance for element-wise
intersections.
For example, in this code, the call to intersect is about 7.8x
faster than in the previous release. The code performs these operations:
Reads 51 polygons into the workspace.
Creates 51 areas of interest (AOIs) by buffering a point in each polygon.
Measures the time required to calculate the element-wise intersections of the polygons and the AOIs.
function t = timingTest % Read polygons. states = readgeotable("usastatehi.shp"); shape1 = states.Shape; % Create AOIs. points = geopointshape(states.LabelLat,states.LabelLon); shape2 = buffer(points,3); % Measure time to calculate element-wise intersections. shapeout = @() intersect(shape1,shape2'); t = timeit(shapeout); end
The approximate execution times are:
R2025b: 0.39 s
R2026a: 0.05 s
This code was timed on a Windows® 11, AMD EPYC™ 74F3 24-Core Processor @ 3.19 GHz test system by calling the function
timingTest.
See Also
Functions
Objects
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.
Seleziona un sito web
Seleziona un sito web per visualizzare contenuto tradotto dove disponibile e vedere eventi e offerte locali. In base alla tua area geografica, ti consigliamo di selezionare: .
Puoi anche selezionare un sito web dal seguente elenco:
Come ottenere le migliori prestazioni del sito
Per ottenere le migliori prestazioni del sito, seleziona il sito cinese (in cinese o in inglese). I siti MathWorks per gli altri paesi non sono ottimizzati per essere visitati dalla tua area geografica.
Americhe
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)