Main Content

mapclip

Clip planar shape to xy-limits or polygon

Since R2022a

    Description

    clipped = mapclip(shape,xlimits,ylimits) clips the specified planar point, line, or polygon shapes to the specified xy-limits. Specify the shapes using shape, the x-limits using xlimits, and the y-limits using ylimits.

    To crop raster data that is represented by an array and a map raster reference object, use the mapcrop function instead.

    example

    clipped = mapclip(shape,clipperPolygon) clips the shapes to the specified clipper polygon. (since R2024b)

    example

    Examples

    collapse all

    Read hydrography data as a geospatial table. Extract the polygon shape for a pond.

    hydro = readgeotable("concord_hydro_area.shp");
    shape = hydro.Shape(14)
    shape = 
      mappolyshape with properties:
    
                  NumRegions: 1
                    NumHoles: 3
                    Geometry: "polygon"
        CoordinateSystemType: "planar"
                ProjectedCRS: [1×1 projcrs]
    
    

    Specify the xy-limits and clip the shape.

    xlimits = [207736  208212];
    ylimits = [912283  912636];
    clipped = mapclip(shape,xlimits,ylimits);

    Display the shape and the clipped shape on a map.

    figure
    geoplot(shape)
    hold on
    geoplot(clipped)

    Figure contains an axes object with type geoaxes. The geoaxes object contains 2 objects of type polygon.

    Since R2024b

    Read road data for an area in Boston as a geospatial table. The table represents the roads using line shapes in planar coordinates. Extract the line shapes.

    roads = readgeotable("boston_roads.shp");
    shape = roads.Shape
    shape=2795×1 maplineshape array with properties:
                    NumParts: [2795×1 double]
                    Geometry: "line"
        CoordinateSystemType: "planar"
                ProjectedCRS: [1×1 projcrs]
          ⋮
    
    

    Specify the clipper polygon. Create a geospatial table that contains a point shape object for a named location in Boston. Then, define an area of interest (AOI) by creating a circle with a radius of 1000 meters that is centered on the point shape.

    places = readgeotable("boston_placenames.shp");
    flagstaff = geocode("FLAGSTAFF HILL",places);
    
    clipperPolygon = aoicircle(flagstaff,1000)
    clipperPolygon = 
      mappolyshape with properties:
    
                  NumRegions: 1
                    NumHoles: 0
                    Geometry: "polygon"
        CoordinateSystemType: "planar"
                ProjectedCRS: [1×1 projcrs]
    
    

    Verify that the road data and the AOI use the same projected coordinate reference system (CRS).

    isequal(shape.ProjectedCRS,clipperPolygon.ProjectedCRS)
    ans = logical
       1
    
    

    Clip the road data to the AOI.

    clipped = mapclip(shape,clipperPolygon);

    Create a map that displays the clipped road data, the named location, and the AOI.

    figure
    geoplot(clipped,LineWidth=0.8)
    hold on
    geoplot(flagstaff,MarkerSize=20)
    geoplot(clipperPolygon,FaceColor="none")

    Figure contains an axes object with type geoaxes. The geoaxes object contains 3 objects of type line, point, polygon.

    Read the names and locations of places in Boston as a geospatial table. Extract the point shapes.

    places = readgeotable("boston_placenames.shp");
    shape = places.Shape
    shape = 
      13x1 mappointshape array with properties:
    
                   NumPoints: [13x1 double]
                           X: [13x1 double]
                           Y: [13x1 double]
                    Geometry: "point"
        CoordinateSystemType: "planar"
                ProjectedCRS: [1x1 projcrs]
    
    

    Specify the xy-limits and clip the shapes.

    xlimits = [235226 237174];
    ylimits = [900179 901059];
    clipped = mapclip(shape,xlimits,ylimits);

    When a point shape lies outside the specified limits, the clipped shape has no coordinate data and the NumPoints property is 0. Remove shapes with no coordinate data from the clipped shapes.

    idx = clipped.NumPoints ~= 0;
    clipped = clipped(idx)
    clipped = 
      5x1 mappointshape array with properties:
    
                   NumPoints: [5x1 double]
                           X: [5x1 double]
                           Y: [5x1 double]
                    Geometry: "point"
        CoordinateSystemType: "planar"
                ProjectedCRS: [1x1 projcrs]
    
    

    When a line or polygon shape has no coordinate data, its respective the NumParts or NumRegions property is 0.

    If your shape is in projected coordinates and your limits are in geographic coordinates, then you must project the geographic coordinates before clipping the shape. To use this approach:

    • You must know the projected coordinate reference system (CRS) for the shape. You can determine whether a shape object is associated with a projected CRS by querying the ProjectedCRS property.

    • The projection method for the CRS must result in x-coordinates and y-coordinates that run approximately west-to-east and south-to-north, respectively, such as transverse Mercator or Lambert Conic Conformal. This approach is not valid for projection methods such as polar stereographic. You can find the projection method for a projected CRS object by querying the ProjectionMethod property.

    Read road data for an area in Concord, MA as a geospatial table. Extract the line shapes.

    roads = readgeotable("concord_roads.shp");
    shape = roads.Shape
    shape=609×1 object
      16×1 maplineshape array with properties:
    
                    NumParts: [16×1 double]
                    Geometry: "line"
        CoordinateSystemType: "planar"
                ProjectedCRS: [1×1 projcrs]
          ⋮
    
    

    Verify that the shape has a projected CRS with an appropriate projection method.

    crs = shape.ProjectedCRS
    crs = 
      projcrs with properties:
    
                        Name: "NAD83 / Massachusetts Mainland"
               GeographicCRS: [1×1 geocrs]
            ProjectionMethod: "Lambert Conic Conformal (2SP)"
                  LengthUnit: "meter"
        ProjectionParameters: [1×1 map.crs.ProjectionParameters]
    
    

    Specify the latitude and longitude limits using geographic coordinates. Project the latitude and longitude limits to xy-limits by using the projected CRS and the projfwd function.

    latlim = [42.4657 42.4687];
    lonlim = [-71.3996 -71.3946];
    [xlimits,ylimits] = projfwd(crs,latlim,lonlim);

    Clip the shapes.

    clipped = mapclip(shape,xlimits,ylimits);

    Display the clipped shapes on a map. Compare the limits you specified with the limits of the plot by setting the tick format to decimal degrees.

    figure
    geoplot(clipped)
    geotickformat("dd")

    Figure contains an axes object with type geoaxes. The geoaxes object contains an object of type line.

    Input Arguments

    collapse all

    Shape, specified as a mappointshape, maplineshape, or mappolyshape object or as an array of mappointshape, maplineshape, or mappolyshape objects. When you specify an array, you can include a combination of point, line, and polygon shape objects.

    x-limits, specified as a two-element vector of the form [xmin xmax], where xmax is greater than xmin.

    The units of xlimits must match the units of shape. If the ProjectedCRS property of shape contains a projcrs object, you can find the units by querying the LengthUnit property of the projcrs object, for example shape.ProjectedCRS.LengthUnit.

    y-limits, specified as a two-element vector of the form [ymin ymax], where ymax is greater than ymin.

    The units of ylimits must match the units of shape. If the ProjectedCRS property of shape contains a projcrs object, you can find the units by querying the LengthUnit property of the projcrs object, for example shape.ProjectedCRS.LengthUnit.

    Since R2024b

    Clipper polygon, specified as a mappolyshape object.

    The ProjectedCRS properties of clipperPolygon and shape must be equal, unless one of the ProjectedCRS properties is empty.

    Output Arguments

    collapse all

    Clipped shape, returned as a mappointshape, maplineshape, or mappolyshape object or as an array of mappointshape, maplineshape, or mappolyshape objects.

    clipped has the same type and size as shape.

    If an element of shape lies completely outside the specified limits, then the corresponding element of clipped does not contain coordinate data. When a point, line, or polygon shape does not contain coordinate data, its respective NumPoints, NumParts, or NumRegions property is 0.

    Tips

    • If you clip a shape within a geospatial table, the function does not modify any attributes of the table.

    Version History

    Introduced in R2022a

    expand all