.topojson
class: Topology
- class Topology(data, topology=True, prequantize=True, topoquantize=False, presimplify=False, toposimplify=False, shared_coords=False, prevent_oversimplify=True, simplify_with='shapely', simplify_algorithm='dp', winding_order='CW_CCW', object_name='data')[source]
Object representation of a topology.
Note
Inherits from
topojson.Topologywith additional methods conforming to theHighchartsMetainterface.Class Inheritance

- classmethod from_dict(as_dict: dict, allow_snake_case: bool = True)[source]
Construct an instance of the class from a
dictobject.
- classmethod from_geodataframe(as_gdf, prequantize=False, **kwargs)[source]
Create a
MapDatainstance from ageopandas.GeoDataFrame.- Parameters:
as_gdf (
geopandas.GeoDataFrame) – Thegeopandas.GeoDataFramecontaining the relevant map geometries.prequantize (
bool) – IfTrue, will perform the TopoJSON optimizations (“quantizing the topology”) before generating theTopologyinstance. Defaults toFalse.
- Return type:
- classmethod from_geojson(as_geojson_or_file: str | bytes, allow_snake_case: bool = True, **kwargs)[source]
Construct an instance of the class from a JSON string.
- Parameters:
- Returns:
A Python objcet representation of
as_geojson_or_file.- Return type:
MapData
- classmethod from_js_literal(as_str_or_file, allow_snake_case: bool = True, _break_loop_on_failure: bool = False)[source]
Return a Python object representation of a Highcharts JavaScript object literal.
- Parameters:
as_str_or_file (
str) – The JavaScript object literal, represented either as astror as a filename which contains the JS object literal.allow_snake_case (
bool) – IfTrue, interpretssnake_casekeys as equivalent tocamelCasekeys. Defaults toTrue._break_loop_on_failure (
bool) – IfTrue, will break any looping operations in the event of a failure. Otherwise, will attempt to repair the failure. Defaults toFalse.
- Returns:
A Python object representation of the Highcharts JavaScript object literal.
- Return type:
HighchartsMeta
- classmethod from_json(as_json_or_file: str | bytes, allow_snake_case: bool = True)[source]
Construct an instance of the class from a JSON string.
- Parameters:
as_json_or_file – The JSON string for the object or the filename of a file that contains the JSON string.
allow_snake_case (
bool) – IfTrue, interpretssnake_casekeys as equivalent tocamelCasekeys. Defaults toTrue.
- Returns:
A Python objcet representation of
as_json.- Return type:
MapData
- classmethod from_topojson(as_topojson_or_file: str | bytes, allow_snake_case: bool = True, **kwargs)[source]
Construct an instance of the class from a TopoJSON string.
- Parameters:
- Returns:
A Python objcet representation of
as_topojson_or_file.- Return type:
MapData
- to_alt(color=None, tooltip=True, projection='identity', object_name=0)[source]
Display as Altair visualization.
- Parameters:
color (
strorNone) – Assign a property attribute to be used for color encoding and renders the Altair visualization as geoshape. Remember that most of the time the wanted attribute is nested within properties. Moreover, specific type declaration is required. Egcolor='properties.name:N'. Defaults toNone, which renders as a mesh.tooltip (
bool) – Option to include or exclude tooltips on geoshape objects. Defaults toTrue.projection (
str) – Defines the projection of the visualization. Defaults to a non-geographic, Cartesian projection (known by Altair as'identity').object_name (
strorint) – The name or the index of the object within the Topology to display. Defaults to index0.
- to_gdf(crs=None, validate=False, winding_order='CCW_CW', object_name=0)[source]
Convert the Topology to a GeoDataFrame. Remember that this will destroy the computed Topology.
Note
This function does not use the TopoJSON driver within Fiona, but a custom implemented more robust variant. See for info the to_geojson() function.
- Parameters:
crs (
strordictorNone) – coordinate reference system to set on the resulting frame. Default tries to use crs from data-input, otherwise isNone.validate (
bool) – Set toTrueto validate each feature before inclusion in the GeoJSON. Only features that are valid geometries objects will be included. Defaults toFalse.winding_order (
str) –Determines the winding order of the features in the output geometry. Accepts:
'CW_CCW'for clockwise orientation for outer rings and counter-clockwise for interior rings'CCW_CW'for counter-clockwise for outer rings and clockwise for interior rings.
Default is CCW_CW for GeoJSON.
object_name (
strorint) – Name or index of the object. Defaults to0to select the first object.
- Return type:
geopandas.GeoDataFrame
- to_geodataframe(object_name=None)[source]
Generate a
GeoPandas.GeoDataFrameinstance of the topology.
- to_geojson(fp=None, pretty=False, indent=4, maxlinelength=88, validate=False, winding_order='CCW_CW', decimals=None, object_name=0)[source]
Convert the Topology to a GeoJSON object. Remember that this will destroy the computed Topology.
- Parameters:
fp (
strorNone) – If set, writes the object to a file on drive. Defaults toNone.pretty (
bool) – IfTrue, the JSON object will be ‘pretty’, depending on theidentandmaxlinelengthoptions. IfFalse, it will be compact, eliminating whitespace. Default isFalse.indent (
int) – IfprettyisTrue, determines object indentation. Defaults to4.maxlinelength (
int) – IfprettyisTrue, determines the maximum length of each line. Defaults to88.validate (
bool) – Set toTrueto validate each feature before inclusion in the GeoJSON. Only features that are valid geometries objects will be included. Default isFalse.winding_order (
str) –Determines the winding order of the features in the output geometry. Accepts:
'CW_CCW'for clockwise orientation for outer rings and counter-clockwise for interior rings'CCW_CW'for counter-clockwise for outer rings and clockwise for interior rings.
Default is CCW_CW for GeoJSON.
decimals (
intorNone) – Evenly round the coordinates to the given number of decimals. Default isNone, which means no rounding is applied.object_name (
strorint) – Name or index of the object. Defaults to0to select the first object.
- to_js_literal(filename=None, encoding='utf-8', careful_validation=False) str | None[source]
Return the object represented as a
strcontaining the JavaScript object literal.- Parameters:
along the way using the esprima-python library. Defaults to
False.Warning
Setting this value to
Truewill significantly degrade serialization performance, though it may prove useful for debugging purposes.
- to_json(filename=None, encoding='utf-8')[source]
Generate a JSON string/byte string representation of the object compatible with the Highcharts JavaScript library.
Note
This method will either return a standard
stror abytesobject depending on the JSON serialization library you are using. For example, if your environment has orjson, the result will be abytesrepresentation of the string.- Parameters:
- Returns:
A JSON representation of the object compatible with the Highcharts library.
- Return type:
- to_svg(separate=False)[source]
Display the arcs and junctions as SVG.
- Parameters:
separate (
bool) – IfTrue, each of the arcs will be displayed separately. Defaults toFalse.
- to_topojson(filename=None, encoding='utf-8')[source]
Generate a TopoJSON string/byte string representation of the object.
- to_widget(slider_toposimplify=None, slider_topoquantize=None)[source]
Create an interactive widget based on Altair. The widget includes sliders to interactively change the toposimplify and topoquantize settings.
- Parameters:
slider_toposimplify (
dict) – Adictwhich contains the keys:min,max,step,value. Defaults to:{"min": 0, "max": 10, "step": 0.01, "value": 0.01}slider_topoquantize (
dict) – Adictwhich contains the keys:min,max,value,base. Defaults to:{"min": 1, "max": 6, "step": 1, "value": 1e5, "base": 10}
- topoquantize(quant_factor, inplace=False)[source]
Quantization is recommended to improve the quality of the topology if the input geometry is messy (i.e., small floating point error means that adjacent boundaries do not have identical values); typical values are powers of ten, such as
1e4,1e5, or1e6.
- toposimplify(epsilon, simplify_algorithm=None, simplify_with=None, prevent_oversimplify=None, inplace=False)[source]
Apply toposimplify to remove unnecessary points from arcs after the topology is constructed. This will simplify the constructed arcs without altering the topological relations. Sensible values for coordinates stored in degrees are in the range of
0.0001to10.- Parameters:
epsilon (
float) – tolerance parametersimplify_algorithm (
strorNone) – Choose between'dp'and'vw', for Douglas-Peucker or Visvalingam-Whyatt respectively.'vw'will only be supported ifsimplify_withis set to'simplification'. Defaults toNone, which behaves as'dp'.simplify_with (
strorNone) – Sets the package to use for simplifying. Choose between'shapely'or'simplification'.shapelyonly supports Douglas-Peucker andsimplificationsupports both Douglas-Peucker and Visvalingam-Whyatt. Thesimplificationpackage is known to be quicker thanshapely. Defaults toNone, which behaves as'shapely'.prevent_oversimplify (
boolorNone) – If this setting is set toTrue, the simplification is slower, but the likelihood of producing valid geometries is higher as it prevents oversimplification. Simplification happens on paths separately, so this setting is especially relevant for rings with no partial shared paths. This is also known as a topology-preserving variant of simplification. Defaults toNone, which behaves asTrue.inplace (
bool) – IfTrue, do operation in place and returnNone. Defaults toFalse.
- Returns:
Topology object with simplified linestrings if
inplaceisFalse, otherwiseNone- Return type: