Points, Lines, and Polygons

In the context of geodata, vector data means points, lines, and polygons that represent geographic objects. Vector geospatial data is used to represent point features, such as cities and landmarks; linear features, such as rivers and highways; and areal features, such as bodies of water and voting districts.

Displaying a Point

In the MATLAB® workspace, vector data is expressed as pairs of variables that represent the geographic or plane coordinates for a set of points of interest. For example, a single point, the location of the Eiffel Tower can be mapped as a vector.

  1. Display a map of France.

    h = worldmap('France');
    landareas = shaperead('landareas.shp','UseGeoCoords', true);
    geoshow (landareas, 'FaceColor', [1 1 .5]);
  2. Save the location of the Eiffel Tower in vector form.

    TowerLat = 48.85;
    TowerLon = 2.28;
  3. Place a red dot on the map to indicate the tower and label it.

    geoshow(TowerLat, TowerLon, 'Marker','.','MarkerEdgeColor','red')
    textm(TowerLat,TowerLon + 0.5, 'Eiffel Tower');

Displaying a Line

This simple example demonstrates how to use the function linem to display vector data for three short lines branching from one common endpoint.

axesm sinusoid; framem;
linem([15; 0; -45; -25],[-100; 0; 70; 110],'r-')
linem([15; -30; -60; -65],[-100; -20; 100; 150],'b-')
linem([15; 20; 40; 20],[-100; -20; 40; 50], 'g-')

Rivers are examples of lines. Enter the following code to add rivers to a map of France:

h = worldmap('France');
landareas = shaperead('landareas.shp','UseGeoCoords', true);
geoshow (landareas, 'FaceColor', [1 1 .5]);
rivers = shaperead('worldrivers', 'UseGeoCoords', true);
geoshow(rivers, 'Color', 'blue')

Find out more about the rivers structure array:

rivers = 

128x1 struct array with fields:

rivers contains 128 world rivers. Type the following at the command line to view the structure for the eighth river:

ans = 

       Geometry: 'Line'
    BoundingBox: [2x2 double]
            Lon: [129.6929 128.9659 128.7473 NaN]
            Lat: [63.3965 63.4980 63.5220 NaN]
           Name: 'Lena'

The rivers are stored as shapes of type 'Line'. Data for the eighth river, Lena, is stored in Lat and Lon vectors. Note that each vector ends with a NaN.

Displaying a Polygon

Many common map objects, such as state boundaries, islands, and continents, are polygons. Some polygon objects in the real world can have many parts: for example, the islands that make up the state of Hawaii. When encoding as vector variables the shapes of such compound entities, you must separate successive entities. To indicate that such a discontinuity exists, the toolbox uses the convention of placing NaNs in identical positions in both vector variables. A NaN separator serves as a "pen-up" command, a command to stop drawing one polygon and start drawing another. The example below demonstrates how NaN separators divide the data for simple polygons.

  1. Copy and paste the following vector variables at the command line:

    x = [40  55  33  10  0  5  10  40  NaN  10  25  30  25  10...
         10  NaN  90  80  65  80  90  NaN];
    y = [50  20  0  0  15  25  55  50  NaN  20  10  10  20  30...
         20  NaN  10  0  20  25  10  NaN];

    These vector variables appear as rows in the following table, along with a row listing the indices.

    Notice that the NaNs appear in the same locations in both x and y vectors. Columns 9, 16, and 22 of the table have NaNs. These mark the division between separate polygons. Also, notice that the x and y data for vertices 1 and 8 are the same. This is the point where segments join together to close the polygon.

  2. Use the mapshow function to display the polygon.


  3. In this example, the vector variables contain data that displays as a compact, multipart polygon with a hole. Compare the data from the table to the illustration. Note that the vertices in the illustration have been labeled to correspond to the indices. (Your output will not contain these labels.)

    Individual contours in x and y are assumed to be external contours if their vertices are arranged in clockwise order; otherwise they are assumed to be internal contours. You can see that the "hole" has vertices that appear in counter-clockwise order.

Now consider an example of polygons on a map of the United States. Enter the following code to display a map of the U.S. (excluding Alaska and Hawaii):

figure; ax = usamap('conus');
set(ax, 'Visible', 'off')
states = shaperead('usastatelo', 'UseGeoCoords', true);
names = {states.Name};
indexConus = 1:numel(states);
stateColor = [0.5 1 0.5];
geoshow(ax, states(indexConus),  'FaceColor', stateColor)
setm(ax, 'Frame', 'off', 'Grid', 'off',...
     'ParallelLabel', 'off', 'MeridianLabel', 'off')

Examine the structure for one of the states:

ans = 

       Geometry: 'Polygon'
    BoundingBox: [2x2 double]
            Lon: [1x183 double]
            Lat: [1x183 double]
           Name: 'Arkansas'
       LabelLat: 34.8350
       LabelLon: -91.8861

You can see that Arkansas is of shape type 'Polygon'. View the last entry in the Lat vector:

ans =


The NaN serves as a separator between polygons.

Compare the first and next-to-last entries:

ans =


ans =


The first and next-to-last entries are the same to close the polygon.