Convert latitude-longitude vectors to regular data grid
[Z,R] = vec2mtx(lat,lon,density)
[Z,R] = vec2mtx(lat,lon,density,latlim,lonlim)
[Z,R] = vec2mtx(lat,lon,Z1,R1)
[Z,R] = vec2mtx(...,'filled')
[Z,R] = vec2mtx(lat,lon,density) creates a regular data grid
Z from vector data, placing ones in grid cells intersected by a
vector and zeroes elsewhere.
R is the raster reference object for the
lon are vectors of equal
length containing geographic locations in units of degrees.
indicates the number of grid cells per unit of latitude and longitude (a value of 10
indicates 10 cells per degree, for example), and must be scalar-valued. Whenever there
is space, a buffer of two grid cells is included on each of the four sides of the grid.
The buffer is reduced as needed to keep the latitudinal limits within [-90 90] and to
keep the difference in longitude limits from exceeding 360 degrees.
[Z,R] = vec2mtx(lat,lon,density,latlim,lonlim) uses
the two-element vectors
define the latitude and longitude limits of the grid.
[Z,R] = vec2mtx(lat,lon,Z1,R1) uses a pre-existing
Z1, georeferenced by
R1, to define the
limits and density of the output grid. Specify
R1 as a
GeographicCellsReference object. The
R1 must be consistent with
With this syntax, the output
R is equal to
[Z,R] = vec2mtx(...,'filled'), where
lon form one or more closed polygons
NaN-separators), fills the area outside the polygons with the
value two instead of the value zero.
lat,lon vertex arrays will result in
an error unless the grid limits are explicitly provided (via
In the case of explicit limits,
Z will be filled
entirely with 0s if the
'filled' parameter is omitted,
and 2s if it is included.
It's possible to apply
vec2mtx to sets of
polygons that tile without overlap to cover an area, as in Example
1 below, but using
'filled' with polygons that
actually overlap may lead to confusion as to which areas are inside
and which are outside.
Convert latitude-longitude polygons to a regular data grid and display as a map.
states = shaperead('usastatelo', 'UseGeoCoords', true); lat = [states.Lat]; lon = [states.Lon]; [Z,R] = vec2mtx(lat,lon,5,'filled'); figure worldmap(Z,R) geoshow(Z,R,'DisplayType','texturemap') colormap(flag(3))
Combine two separate calls to
vec2mtx to create a
4-color raster map showing interior land areas, coastlines, oceans, and
load coastlines [Z,R] = vec2mtx(coastlat,coastlon,1, ... [-90 90],[-90 270],'filled'); rivers = shaperead('worldrivers.shp','UseGeoCoords',true); A = vec2mtx([rivers.Lat],[rivers.Lon],Z,R); Z(A == 1) = 3; figure worldmap(Z,R) geoshow(Z,R,'DisplayType','texturemap') colormap([.45 .60 .30; 0 0 0; 0 0.5 1; 0 0 1])
Define Limits and Density of Output Grid Using Spatial Referencing Object
Import US state polygons as a geospatial table. Extract the coordinates of the polygons by converting the geospatial table to a table.
states = readgeotable("usastatelo.shp"); T = geotable2table(states,["Latitude" "Longitude"]); [lat,lon] = polyjoin(T.Latitude',T.Longitude');
Choose geographic limits.
latlim = [ 15 75]; lonlim = [-190 -65];
Specify a grid with 5 cells per degree.
density = 5;
Compute raster size. (M and N both work out to be integers.)
M = density * diff(latlim); N = density * diff(lonlim);
Create a geographic raster reference object.
R = georasterref("RasterSize", [M N], ... "ColumnsStartFrom","north","Latlim",latlim, ... "Lonlim",lonlim);
Create a blank grid that is consistent with
R in size.
vec2mtx requires a data grid as input.
Z = zeros(R.RasterSize);
Z with a new grid including state outlines and interiors.
Z = vec2mtx(lat,lon,Z,R,"filled");
Plot the georeferenced grid.
figure; worldmap(Z,R); geoshow(Z,R,"DisplayType", "texturemap") colormap(flag(3))
Version HistoryIntroduced before R2006a
vec2mtx will not accept referencing vectors or referencing matrices as input
vec2mtx function will not accept referencing vectors or
referencing matrices as input in a future release. Use a geographic raster reference
object, specified as a
GeographicCellsReference object, as
input instead. Reference objects have several advantages over referencing vectors
and referencing matrices.
Unlike referencing vectors and referencing matrices, reference objects have properties that document the size of the associated raster, its geographic limits, and the direction of its rows and columns.
You can manipulate the limits of geographic rasters associated with reference objects using the
You can manipulate the size and resolution of geographic rasters associated with reference objects using the
To update your code, create a geographic reference object.
Create a geographic reference object for a raster of cells by using the
Convert from a referencing vector to a geographic reference object by using the
Convert from a referencing matrix to a geographic reference object by using the
Once you have created a reference object, replace uses of the referencing vector or referencing matrix in your code with the reference object.
vec2mtx returns reference objects instead of referencing vectors
Starting in R2021a, most syntaxes of the
return a raster reference object instead of a referencing vector. This change is
unlikely to affect your existing code because most Mapping Toolbox™ functions that accept referencing vectors as input also accept raster
If you specify a referencing vector or matrix as input, then the
vec2mtx function still returns a referencing vector or