Cartogrammetry

create

Module for creating square or circle style cartograms.

class cartogrammetry.create.CircleCartogram(gdf: geopandas.geodataframe.GeoDataFrame, size_column: str = None, mode: int = 1, time_limit: int = 300)

Extends the Cartogram class to create a cartogram where every geometry is represented as a circle.

calculate() → None
class cartogrammetry.create.SquareCartogram(gdf: geopandas.geodataframe.GeoDataFrame, size_column: str = None, mode: int = 1, time_limit: int = 300)

Extends the Cartogram class to create a cartogram where every geometry is represented as a square.

calculate() → None
cartogrammetry.create.create_square(row: pandas.core.series.Series) → shapely.geometry.polygon.Polygon

Function to create a square polygon feature from a centerpoint and width.

Returns

shapely.geometry.Polygon

cartogram

Module for creating block or circle style cartograms.

class cartogrammetry.cartogram.Cartogram(gdf: geopandas.geodataframe.GeoDataFrame, size_column: str = None, mode: int = 1, time_limit: int = 300)

Class to create a cartogram from a geopandas geodataframe.

Parameters
  • gdf – GeoDataFrame with polygon features to create a cartogram from.

  • size_column – Numeric dataframe column to use for scaling the cartogram.

  • time_limit – The maximum time allowed to find a solution. If exceeded the best solution at that time is returned.

  • mode – Mode 1, 2 or 3. Corresponds to 1: quick method but suboptimal, 2: quick, optimal, but with overlapping geometries, 3: optimal, no overlapping geometries, but slow.

calculate_neighbors() → None

Calculate which geometries are adjacent to each other.

calculate_size() → None

Calculate the size for each geometry based on a numeric column.

offset() → <module ‘geopandas.geoseries’ from ‘c:\users\ivodeliefdetensing\miniconda3\envs\cartogrammetry\lib\site-packages\geopandas\geoseries.py’>

Offset geometries (required for ILP)

solve_cartogram() → None

Method for running all the steps required to make a cartogram.

solve

Module for solving the location of a blocks or circles in cartogram using linear programming.

class cartogrammetry.solve.Solver(gdf: geopandas.geodataframe.GeoDataFrame, gap_size: float = 0.0, time_limit: int = 300, mode: int = 1, solve_msg: bool = True)

Linear Programming Solver for creating cartograms.

This class solves the location of geometries in a GeoDataFrame, in order to create a cartogram. The methodology is derived from the article ‘Computing stable Demers cartograms’ by Nickel et al. (2019).

Methodology

For each combination of neighboring geometries we create 2 additional variables:

  • h(i,j) = The non-negative horizontal distance between two squares

  • v(i,j) = The non-negative vertical distance between two squares

The distance between two squares is calculated as the sum of the width of both squares divided by 2.

The objective is to minimize the sum of the variables h(i,j) en v(i,j). These are all the horizontal and vertical distances between geometries.

The following constraints are added:

  1. The horizontal distance is greater than or equal to the smallest possible width between the two centerpoints (p_i and p_j) plus a gap if they are not adjacent.

    x_j - x_i >= (w_j + w_i) / 2 + gap

    where gap = 0 if p_i and p_j are adjacent along the x axis

  2. The vertical distance is greater than or equal to the smallest possible height between the two centerpoints (p_i and p_j) plus a gap if they are not adjacent.

    y_j - y_i >= (w_j + w_i) / 2 + gap

    where gap = 0 if i and j are adjacent along the y axis

  3. The horizontal distance between p_i and p_j is greater than or equal to the distance between x_i and x_j minus the width of both geometries.

    h(i,j) >= max(x_j - x_i - w(i,j), x_i - x_j - w(i,j))

  4. The vertical distance between p_i and p_j is greater than or equal to the distance between y_i and y_j minus the width of both geometries.

    v(i,j) >= max(y_j - y_i - w(i,j), y_i - y_j - w(i,j))

There are 4 modes implemented:

  • Mode 1: For any combination of p_i and p_j either constraint 1 or 2 must apply. Which one is used depends on which axis has the greatest range. E.g. if x_j - x_i > y_j - y_i, then constraints 1 applies and p_i and p_j are considered horizontally adjacent.

  • Mode 2: A variable S is created with a value between 0 and 1 to determine the most optimal horizontal or vertical position for adjacent geometries. The result is allowed to have overlapping geometries.

  • Mode 3: Similar to mode 2, but user an integer variable (MIP) instead of a float. This reduces overlap but takes more time to solve.

  • Mode 4: Similar to mode 2, but after the initial solved result the variable S is rounded and added to the problem as a constant.

param gdf

Geodataframe to optimize.

param gap_size

The minimum length in meters between non-adjacent geometries.

param time_limit

The maximum time allowed to find a solution. If exceeded the best solution at that time is returned.

param mode

Mode 1, 2 or 3. Corresponds to 1: quick method but suboptimal, 2: quick, optimal, but with overlapping geometries, 3: optimal, no overlapping geometries, but slow.

param solve_msg

Boolean value for displaying solver logging.

add_adjacent_variables_constraints_m1() → None

Method to create LP variables for each pair of adjacent geometries (using mode 1).

add_adjacent_variables_constraints_m23(mip: bool = False) → None

Method to create LP variables for each pair of adjacent geometries (using mode 2 or mode 3).

add_adjacent_variables_constraints_m4(s_dict) → None

Method to create LP variables for each pair of adjacent geometries (using mode 4).

add_coord_variables(original_location=True) → None

Method to create LP variables for each coordinate.

For each geometry in the GeoDataFrame we create 2 variables:

  • X coordinate of centerpoint

  • Y coordinate of centerpoint

run() → None

Methode to run all required steps to find the optimal centerpoint locations for each geometry in a cartogram.

solve() → dict

Method to solve for the optimal location of geometries given an input Geodataframe with a size column.

Returns

Dictionary that contains all the values for all S variables.