greedy API reference

greedy

greedy.greedy(gdf, strategy='balanced', balance='count', min_colors=4, sw='queen', min_distance=None, silence_warnings=True, interchange=False)

Color GeoDataFrame using various strategies of greedy (topological) colouring.

Attempts to color a GeoDataFrame using as few colors as possible, where no neighbours can have same color as the feature itself. Offers various strategies ported from QGIS or implemented within networkX for greedy graph coloring.

greedy will return pandas.Series representing assinged color codes.

Parameters
gdfGeoDataFrame

GeoDataFrame

strategystr (default ‘balanced’)

Determine coloring strategy. Options are 'balanced' for algorithm based on QGIS Topological coloring. It is aiming for a visual balance, defined by the balance parameter.

Other options are those supported by networkx.greedy_color:

  • 'largest_first'

  • 'random_sequential'

  • 'smallest_last'

  • 'independent_set'

  • 'connected_sequential_bfs'

  • 'connected_sequential_dfs'

  • 'connected_sequential' (alias for the previous strategy)

  • 'saturation_largest_first'

  • 'DSATUR' (alias for the previous strategy)

For details see https://networkx.github.io/documentation/stable/reference/algorithms/generated/networkx.algorithms.coloring.greedy_color.html

balancestr (default ‘count’)

If strategy is 'balanced', determine the method of color balancing.

  • 'count' attempts to balance the number of features per each color.

  • 'area' attempts to balance the area covered by each color.

  • 'centroid' attempts to balance the distance between colors based on the distance between centroids.

  • 'distance' attempts to balance the distance between colors based on the distance between geometries. Slower than 'centroid', but more precise.

'centroid' and 'distance' are significantly slower than other especially for larger GeoDataFrames.

Apart from 'count', all require CRS to be projected (not in degrees) to ensure metric values are correct.

min_colors: int (default 4)

If strategy is 'balanced', define the minimal number of colors to be used.

sw‘queen’, ‘rook’ or libpysal.weights.W (default ‘queen’)

If min_distance is None, one can pass 'libpysal.weights.W' object denoting neighbors or let greedy to generate one based on 'queen' or 'rook' contiguity.

min_distancefloat

Set minimal distance between colors.

If min_distance is not None, slower algorithm for generating spatial weghts is used based on intersection between geometries. Min_distance is then used as a tolerance of intersection.

silence_warningsbool (default True)

Silence libpysal warnings when creating spatial weights.

interchangebool (defaul False)

Use the color interchange algorithm (applicable for networkx strategies)

For details see https://networkx.github.io/documentation/stable/reference/algorithms/generated/networkx.algorithms.coloring.greedy_color.html

Returns
colorpd.Series

pandas.Series representing assinged color codes

Examples

Default:

>>> gdf['greedy_colors'] = greedy(gdf)

Balanced by area:

>>> gdf['balanced_area'] = greedy(gdf, strategy='balanced',
>>>                               balance='area')

Using rook adjacency:

>>> gdf['rook_adjacency'] = greedy(gdf, sw='rook')

Adding minimal distance between colors:

>>> gdf['min_distance'] = greedy(gdf, min_distance=100)

Using different coloring strategy:

>>> gdf['smallest_last'] = greedy(gdf, strategy='smallest_last')

Helper functions

greedy.greedy._balanced(features, sw, balance='count', min_colors=4)

Strategy to color features in a way which is visually balanced.

Algorithm ported from QGIS to be used with GeoDataFrames and libpysal weights objects.

Original algorithm: Date : February 2017 Copyright : (C) 2017 by Nyall Dawson Email : nyall dot dawson at gmail dot com

Parameters
featuresGeoDataFrame

GeoDataFrame

swlibpysal.weights.W

spatial weights object denoting adjacency of features

balancestr

the method of color balancing

min_colorsint

the minimal number of colors to be used

Returns
feature_colorsdict

dictionary with assigned color codes

greedy.greedy._geos_sw(features, tolerance=0, silence_warnings=False)

Generate libpysal spatial weights object based on intersections of features.

Intersecting features are denoted as neighbours. If tolerance > 0, all features within the set tolerance are denoted as neighbours.

Parameters
featuresGeoDataFrame

GeoDataFrame

tolerancefloat (default 0)

minimal distance between colors

silence_warningsbool (default True)

silence lilbpysal warnings (if min_distance is set)

Returns
Wlibpysal.weights.W

spatial weights object