Main Content

Rasterize Polygons Interactively

This example shows how to use the encodem function with seed points found using the getseeds function to fill multiple polygons after they are gridded. The example extracts data for Indiana and its surrounding states, and then deletes unwanted areas of these polygons using maptrimp .

Extract data for Indiana and its neighbors by passing their names in a cell array to shaperead.

pcs = {'Indiana', 'Michigan', 'Ohio', 'Kentucky', 'Illinois'};

centralUS = shaperead('usastatelo.shp',...
    'UseGeoCoords', true,...
    'Selector',{@(name)any(strcmpi(name,pcs),2), 'Name'});

meLat = [centralUS.Lat];
meLon = [centralUS.Lon];

Rasterize the trimmed polygons at a 1-arc-minute resolution (60 cells per degree), also producing a raster reference object.

[meGrid,meR] = vec2mtx(meLat,meLon,60);

Set up a map figure and display the binary grid you just created.

axesm eqdcyl

Use the getseeds function to select five seed points, one in each of the outlines of Indiana, Michigan, Ohio, Kentucky, and Illinois. The getseeds function changes the cursor to a cross-hairs. You pick seed points by positioning the cursor within a state boundary and clicking the mouse. The getseeds function returns control to the command prompt after you pick five locations in the figure window.

[row,col,val] = getseeds(meGrid,meR,5,[3 4 5 6 7]);

Fill each state with a unique value, producing a new grid, using the encodem function.

meGrid5 = encodem(meGrid,[row col val],1);

Display meGrid5 to see the result.