Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
podcast
Filter by Categories
Galleries
Print Category 1
Print Category 2
Uncategorized

Polygons vs Multipolygons in GIS

Polygons vs Multipolygons: Understanding the Differences in GIS

In this blog post, we’ll dive into the world of polygons and multipolygons in GIS, and discuss the differences between them and when to use each one. We’ll also review how to convert multipolygons to polygons and in various software and programming languages such as QGIS, GDAL, python, and PostGIS.

What are polygons in GIS

In GIS, a polygon is a two or three dimensional shape that is defined by a set of ordered x,y coordinates. It is a closed shape that represents a single, continuous area. Examples of polygons include a city block, a lake, or a field.

What are multipolygons in GIS

A multipolygon, on the other hand, is a collection of multiple polygons. These polygons may be overlapping, disjoint, or nested. An example of a multipolygon might be a collection of land parcels, where each parcel is represented by a separate polygon, but all of the polygons together make up a larger area, such as a city or a country.

Both polygons and multipolygons can be used to represent real-world features in a GIS, but they are used in different ways depending on the type of data being represented.

You would use a multipolygon instead of a polygon when you need to represent a feature that is made up of multiple, non-contiguous areas or multiple individual polygons that together make up a larger area.

Here are a few examples of situations where you might use a multipolygon:

  • Representing an island group with multiple islands that are not connected.
  • Representing a lake or river that is made up of multiple, non-contiguous sections.
  • Representing a city that is made up of multiple neighborhoods or districts, each with its own boundaries.
  • Representing a country with multiple territories or enclaves that are not connected to the mainland.
  • Representing a building that is made up of multiple structures or wings that are not connected

In general, if the feature you’re trying to represent can be broken down into multiple, distinct polygons that together make up a larger area, a multipolygon would be the more appropriate choice.

Examples of test-based representations of polygons and multipolygons

A Well-Known Text (WKT) representation of a polygon might look like this:

POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))

This defines a single polygon with five coordinates that form the shape. The first and last coordinate are the same, indicating that the polygon is closed.

A WKT representation of a multipolygon might look like this:

MULTIPOLYGON(((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))

This defines two separate polygons, each with its own set of coordinates. The polygons are enclosed within their own set of parentheses and the entire multipolygon is enclosed in an additional set of parentheses.

It’s worth noting that a polygon can also be represented as a MultiPolygon with one polygon in it.

MULTIPOLYGON(((30 10, 40 40, 20 40, 10 20, 30 10)))

This also defines a single polygon, but it’s enclosed in an additional set of parentheses, indicating that it is part of a multipolygon.

A GeoJSON representation of a polygon might look like this:

{
    "type": "Polygon",
    "coordinates": [
        [
            [30, 10],
            [40, 40],
            [20, 40],
            [10, 20],
            [30, 10]
        ]
    ]
}

This defines a single polygon with five coordinates that form the shape.

A GeoJSON representation of a multipolygon might look like this:

{
    "type": "MultiPolygon",
    "coordinates": [
        [
            [
                [30, 20],
                [45, 40],
                [10, 40],
                [30, 20]
            ]
        ],
        [
            [
                [15, 5],
                [40, 10],
                [10, 20],
                [5, 10],
                [15, 5]
            ]
        ]
    ]
}

This defines two separate polygons, each with their own set of coordinates. The polygons are enclosed within their own set of brackets and the entire multipolygon is enclosed in an additional set of brackets.

It’s worth noting that a polygon can also be represented as a MultiPolygon with one polygon in it.

{
    "type": "MultiPolygon",
    "coordinates": [
        [
            [
                [30, 10],
                [40, 40],
                [20, 40],
                [10, 20],
                [30, 10]
            ]
        ]
    ]
}

This also defines a single polygon, but it’s enclosed in an additional set of brackets, indicating that it is part of a multipolygon.

How to convert mulitpolygons to polygons

There are several ways to convert multipolygons to polygons, depending on the software or programming language you are using. Here are a few examples:

  1. In QGIS, you can use the “Singleparts to multipart” tool from the “Vector geometry” toolbox to convert a multipolygon shapefile to a polygon shapefile. This tool separates the polygons in a multipolygon into individual polygons.
  2. In GDAL, you can use the ogr2ogr command line tool to convert a multipolygon shapefile to a polygon shapefile. The following command can be used:
ogr2ogr -f "ESRI Shapefile" output.shp input.shp -explodecollections

This will explode the multipolygon into individual polygons and save the result in a new shapefile.

  1. In Python, you can use the ogr or fiona library to convert a multipolygon shapefile to a polygon shapefile. Here’s an example of how you can use ogr library:
from osgeo import ogr

input_shapefile = ogr.Open("input.shp")
layer = input_shapefile.GetLayer(0)
output_driver = ogr.GetDriverByName("ESRI Shapefile")
output_shapefile = output_driver.CreateDataSource("output.shp")
output_layer = output_shapefile.CreateLayer("output", layer.GetSpatialRef(), ogr.wkbPolygon)

for feature in layer:
    geom = feature.GetGeometryRef()
    if geom.GetGeometryName() == "MULTIPOLYGON":
        for i in range(geom.GetGeometryCount()):
            poly = geom.GetGeometryRef(i)
            output_feature = ogr.Feature(output_layer.GetLayerDefn())
            output_feature.SetGeometry(poly)
            output_layer.CreateFeature(output_feature)
            output_feature = None
    else:
        output_feature = ogr.Feature(output_layer.GetLayerDefn())
        output_feature.SetGeometry(geom)
        output_layer.CreateFeature(output_feature)
        output_feature = None

input_shapefile = None
output_shapefile = None

This script opens the input shapefile reads the features, and checks if the feature is multipolygon, if so it will loop over the polygons inside the multi polygon and create new features for each polygon, if not it will just copy the feature to the output shapefile.

  1. You can also use Python’s geopandas library to convert a multipolygon to a polygon. Here’s an example:
import geopandas as gpd

input_gdf = gpd.read_file("input.shp")
output_gdf = input_gdf.explode()
output_gdf.to_file("output.shp", driver='ESRI Shapefile')

This script will read the input shapefile using geopandas, explode the multipolygons into individual polygons, and save the result as a new shapefile.

Finding and converting Multipolygons to Polygons in PostGIS

Selecting multipolygons in PostGIS

In PostGIS, you can use the ST_GeometryType function to check the geometry type of a feature. If the feature is a multipolygon, the function will return “ST_MultiPolygon”. Here’s an example of how you can use this function in a SQL query:

SELECT ST_GeometryType(geom) as geom_type
FROM your_table
WHERE ST_GeometryType(geom) = 'ST_MultiPolygon';

This query will return the geometry type of all features in the “your_table” table that have a geometry type of “ST_MultiPolygon”.

You can also use the ST_NumGeometries function to check if the feature is a multipolygon. This function returns the number of geometries in a geometry collection. If the feature is a multipolygon, the function will return a number greater than 1. Here’s an example:

SELECT ST_NumGeometries(geom) as num_geom
FROM your_table
WHERE ST_NumGeometries(geom) > 1;

This query will return the number of geometries in all features in the “your_table” table that has more than one geometry, which means its a multipolygon.

You can also use the ST_GeometryType function in combination with ST_NumGeometries function as in the following query:

SELECT ST_GeometryType(geom) as geom_type, ST_NumGeometries(geom) as num_geom
FROM your_table
WHERE (ST_GeometryType(geom) = 'ST_MultiPolygon' OR ST_NumGeometries(geom) > 1);

This query will return the geometry type and the number of geometries in all features in the “your_table” table that are multipolygons.

Converting multipolygons to polygons in PostGIS

In PostGIS, you can use the ST_Dump function to convert multipolygons to polygons. The ST_Dump function returns a set of geometries that make up a geometry collection, such as a multipolygon. Here’s an example of how you can use this function in a SQL query:

WITH multipolygons_table AS (
    SELECT id, (ST_Dump(geom)).geom as geom
    FROM your_table
    WHERE ST_GeometryType(geom) = 'ST_MultiPolygon'
)
SELECT id, geom
FROM multipolygons_table;

This query will create a new table “multipolygons_table” that contains all the polygons that make up the original multipolygons in “your_table” table.

You can also use the ST_Dump function to insert the polygons into a new table, for example:

CREATE TABLE polygons_table(id SERIAL PRIMARY KEY, geom geometry(Polygon, 4326));
INSERT INTO polygons_table(geom)
SELECT (ST_Dump(geom)).geom
FROM your_table
WHERE ST_GeometryType(geom) = 'ST_MultiPolygon';

This query will create a new table “polygons_table” and insert all the polygons that make up the original multipolygons in “your_table” table.

You can also use the ST_Dump function to update the existing table, for example:

WITH multipolygons_table AS (
    SELECT id, (ST_Dump(geom)).geom as geom
    FROM your_table
    WHERE ST_GeometryType(geom) = 'ST_

Summary

In this article, we discussed the differences between polygons and multipolygons in GIS and when to use each one. We also went over how to identify polygons and multipolygons in various software and programming languages such as QGIS, GDAL, python, and PostGIS. We provided examples and tips on how to convert multipolygons to polygons using different methods in different software. The article was intended to provide a comprehensive guide on how to work with polygons and multipolygons in GIS, and give readers the tools and knowledge they need to effectively manipulate and analyze their data.