Main Content

geoclip

Clip geographic shape to latitude-longitude limits

Since R2022a

Description

example

clipped = geoclip(shape,latlim,lonlim) clips the geographic point, line, or polygon shapes in shape to the latitude and longitude limits in latlim and lonlim, respectively.

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

Examples

collapse all

Read worldwide land areas as a geospatial table. Extract the polygon shapes.

land = readgeotable("landareas.shp");
shape = land.Shape
shape=537×1 object
  16×1 geopolyshape array with properties:

              NumRegions: [16×1 double]
                NumHoles: [16×1 double]
                Geometry: "polygon"
    CoordinateSystemType: "geographic"
           GeographicCRS: [1×1 geocrs]
      ⋮

Specify the latitude and longitude limits for an area containing part of Asia. Then, clip the shapes.

latlim = [4 42];
lonlim = [65 130];
clipped = geoclip(shape,latlim,lonlim);

Display the clipped shapes on a map with no basemap. Display the bounding box as a rectangle.

figure
geobasemap none
hold on
geoplot(clipped)
geoplot(latlim([1 2 2 1 1]),lonlim([1 1 2 2 1]),"k")

Read the names and locations of world cities as a geospatial table. Extract the point shapes.

cities = readgeotable("worldcities.shp");
shape = cities.Shape
shape = 
  318x1 geopointshape array with properties:

               NumPoints: [318x1 double]
                Latitude: [318x1 double]
               Longitude: [318x1 double]
                Geometry: "point"
    CoordinateSystemType: "geographic"
           GeographicCRS: [1x1 geocrs]

Specify the latitude and longitude limits for an area containing part of Asia. Then, clip the shapes.

latlim = [4 42];
lonlim = [65 130];
clipped = geoclip(shape,latlim,lonlim);

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 = 
  57x1 geopointshape array with properties:

               NumPoints: [57x1 double]
                Latitude: [57x1 double]
               Longitude: [57x1 double]
                Geometry: "point"
    CoordinateSystemType: "geographic"
           GeographicCRS: [1x1 geocrs]

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

Create a line shape in geographic coordinates from global coastline data. The longitude coordinates are in the range of –180 to 180 degrees.

load coastlines
coast = geolineshape(coastlat,coastlon)
coast = 
  geolineshape with properties:

                NumParts: 241
                Geometry: "line"
    CoordinateSystemType: "geographic"
           GeographicCRS: []

Shift the longitude coordinates so they are in the range of 0 to 360 degrees.

coastShift = geoclip(coast,[-90 90],[0 360]);

Display the shifted line shape on a map.

figure
geobasemap none
hold on
geoplot(coastShift)

Input Arguments

collapse all

Shape, specified as a geopointshape, geolineshape, or geopolyshape object or as an array of geopointshape, geolineshape, or geopolyshape objects. When you specify an array, you can include a combination of point, line, and polygon shape objects.

Latitude limits, specified as a two-element vector of the form [slat nlat], where slat is the southern limit in degrees and nlat is the northern limit in degrees.

Longitude limits, specified as a two-element vector of the form [wlon elon], where wlon is the western limit in degrees and elon is the eastern limit in degrees.

Output Arguments

collapse all

Clipped shape, returned as a geopointshape, geolineshape, or geopolyshape object or as an array of geopointshape, geolineshape, or geopolyshape 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.

  • If you do not know the latitude and longitude limits you want, you can open and explore a geographic axes by using the geoaxes function. Return the limits of the axes by using the geolimits function or interactively select the southwest and northeast corners of a bounding box by using the ginput function.

Version History

Introduced in R2022a

See Also

Functions