.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.Topology
with additional methods conforming to theHighchartsMeta
interface.Class Inheritance
- classmethod from_dict(as_dict: dict, allow_snake_case: bool = True)[source]
Construct an instance of the class from a
dict
object.
- classmethod from_geodataframe(as_gdf, prequantize=False, **kwargs)[source]
Create a
MapData
instance from ageopandas.GeoDataFrame
.- Parameters:
as_gdf (
geopandas.GeoDataFrame
) – Thegeopandas.GeoDataFrame
containing the relevant map geometries.prequantize (
bool
) – IfTrue
, will perform the TopoJSON optimizations (“quantizing the topology”) before generating theTopology
instance. 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 astr
or as a filename which contains the JS object literal.allow_snake_case (
bool
) – IfTrue
, interpretssnake_case
keys as equivalent tocamelCase
keys. 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_case
keys as equivalent tocamelCase
keys. 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 (
str
orNone
) – 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 (
str
orint
) – 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 (
str
ordict
orNone
) – coordinate reference system to set on the resulting frame. Default tries to use crs from data-input, otherwise isNone
.validate (
bool
) – Set toTrue
to 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 (
str
orint
) – Name or index of the object. Defaults to0
to select the first object.
- Return type:
geopandas.GeoDataFrame
- to_geodataframe(object_name=None)[source]
Generate a
GeoPandas.GeoDataFrame
instance 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 (
str
orNone
) – If set, writes the object to a file on drive. Defaults toNone
.pretty (
bool
) – IfTrue
, the JSON object will be ‘pretty’, depending on theident
andmaxlinelength
options. IfFalse
, it will be compact, eliminating whitespace. Default isFalse
.indent (
int
) – Ifpretty
isTrue
, determines object indentation. Defaults to4
.maxlinelength (
int
) – Ifpretty
isTrue
, determines the maximum length of each line. Defaults to88
.validate (
bool
) – Set toTrue
to 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 (
int
orNone
) – Evenly round the coordinates to the given number of decimals. Default isNone
, which means no rounding is applied.object_name (
str
orint
) – Name or index of the object. Defaults to0
to select the first object.
- to_js_literal(filename=None, encoding='utf-8') str | None [source]
Return the object represented as a
str
containing the JavaScript object literal.
- 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
str
or abytes
object depending on the JSON serialization library you are using. For example, if your environment has orjson, the result will be abytes
representation 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
) – Adict
which contains the keys:min
,max
,step
,value
. Defaults to:{"min": 0, "max": 10, "step": 0.01, "value": 0.01}
slider_topoquantize (
dict
) – Adict
which 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.0001
to10
.- Parameters:
epsilon (
float
) – tolerance parametersimplify_algorithm (
str
orNone
) – Choose between'dp'
and'vw'
, for Douglas-Peucker or Visvalingam-Whyatt respectively.'vw'
will only be supported ifsimplify_with
is set to'simplification'
. Defaults toNone
, which behaves as'dp'
.simplify_with (
str
orNone
) – Sets the package to use for simplifying. Choose between'shapely'
or'simplification'
.shapely
only supports Douglas-Peucker andsimplification
supports both Douglas-Peucker and Visvalingam-Whyatt. Thesimplification
package is known to be quicker thanshapely
. Defaults toNone
, which behaves as'shapely'
.prevent_oversimplify (
bool
orNone
) – 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
inplace
isFalse
, otherwiseNone
- Return type: