topojson.core.topology¶
Topology ¶
Bases: Hashmap
Returns a TopoJSON topology for the specified geometric object. TopoJSON is an extension of GeoJSON providing multiple approaches to compress the geographical input data. These options include simplifying the linestrings or quantizing the coordinates but foremost the computation of a topology.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
_any_ geometric type
|
Geometric data that should be converted into TopoJSON, such as GeoJSON, shapely
geometries, a GeoDataFrame or a Polars DataFrame with a column of WKB
(polars-st). It is possible to provide a list of multiple GeoDataFrames as
separate objects. In this case it is required to provide an equal length list of
the names of the objects for parameter |
required |
topology
|
boolean
|
Specify if the topology should be computed for deriving the TopoJSON.
Default is |
True
|
prequantize
|
(boolean, int, dict)
|
If the prequantization parameter is specified, the input geometry is
quantized prior to computing the topology, the returned topology is
quantized, and its arcs are delta-encoded. 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 |
True
|
topoquantize
|
(boolean, int or dict)
|
If the topoquantization parameter is specified, the input geometry is quantized
after the topology is constructed. If the topology is already quantized this
will be resolved first before the topoquantization is applied. As for
|
False
|
presimplify
|
(boolean, float)
|
Apply presimplify to remove unnecessary points from linestrings before the
topology is constructed. This will simplify the input geometries; lines are
simplified one by one, so shared borders can drift apart, unless
|
False
|
toposimplify
|
(boolean, float)
|
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 |
False
|
shared_coords
|
boolean
|
Sets the strategy to detect junctions. When set to |
False
|
prevent_oversimplify
|
boolean
|
If this setting is set to |
True
|
simplify_with
|
str
|
Sets the package to use for simplifying (both pre- and toposimplify). Choose
between |
'shapely'
|
simplify_algorithm
|
str
|
Choose between |
'dp'
|
winding_order
|
str
|
Determines the winding order of the features in the output geometry. Choose
between |
'CW_CCW'
|
object_name
|
Union[str, list[str]]
|
Name to use as key for the objects in the topojson file. This name is used for
writing and reading topojson file formats.
It is possible to define multiple objects within the topojson file. In this
case it is required to provide a list of the referenced |
'data'
|
ignore_index
|
bool
|
If set to true existing ids/indexes of geojson FeatureCollections will be ignored and overwritten. Otherwise features with ids will use their existing one. If indexes are not ignored and a duplicate id exists an exception will be raised. Default is false. |
False
|
to_dict ¶
to_dict(options: bool = False, state: bool = False) -> dict[str, Any]
Convert the Topology to a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
options
|
boolean
|
If |
False
|
state
|
boolean
|
If |
False
|
to_svg ¶
to_svg(separate: bool = False) -> None
Display the arcs and junctions as SVG.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
separate
|
boolean
|
If |
False
|
to_json ¶
to_json(fp: str | PathLike[str] | None = None, options: bool = False, pretty: bool = False, indent: int = 4, maxlinelength: int = 88, state: bool = False) -> str | None
Convert the Topology to a JSON object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fp
|
str
|
If set, writes the object to a file on drive.
Default is |
None
|
options
|
boolean
|
If |
False
|
pretty
|
boolean
|
If |
False
|
indent
|
int
|
If |
4
|
maxlinelength
|
int
|
If |
88
|
state
|
boolean
|
If |
False
|
to_geojson ¶
to_geojson(fp: str | PathLike[str] | None = None, pretty: bool = False, indent: int = 4, maxlinelength: int = 88, validate: bool = False, winding_order: WindingOrder = 'CCW_CW', decimals: int | None = None, object_name: str | int = 0) -> str | None
Convert the Topology to a GeoJSON object. Remember that this will destroy the computed Topology.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fp
|
str
|
If set, writes the object to a file on drive.
Default is |
None
|
pretty
|
boolean
|
If |
False
|
indent
|
int
|
If |
4
|
maxlinelength
|
int
|
If |
88
|
validate
|
boolean
|
Set to |
False
|
winding_order
|
str
|
Determines the winding order of the features in the output geometry. Choose
between |
'CCW_CW'
|
decimals
|
int or None
|
Evenly round the coordinates to the given number of decimals. Default is None, which means no rounding is applied. |
None
|
object_name
|
(str, int)
|
The name or the index of the object within the Topology to display. Default is index 0. |
0
|
to_gdf ¶
to_gdf(crs: Any = None, validate: bool = False, winding_order: WindingOrder = 'CCW_CW', object_name: str | int = 0) -> geopandas.GeoDataFrame
Convert the Topology to a GeoDataFrame. Remember that this will destroy the computed Topology.
Note: This function use not the TopoJSON driver within Fiona, but a custom
implemented more robust variant. See for info the to_geojson() function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
crs
|
(str, dict)
|
coordinate reference system to set on the resulting frame.
Default tries to use crs from data-input, otherwise is |
None
|
validate
|
boolean
|
Set to |
False
|
winding_order
|
str
|
Determines the winding order of the features in the output geometry. Choose
between |
'CCW_CW'
|
object_name
|
(str, int)
|
Name or index of the object.
Default is index |
0
|
to_polars ¶
to_polars(crs: Any = None, validate: bool = False, winding_order: WindingOrder = 'CCW_CW', object_name: str | int = 0) -> polars.DataFrame
Convert the Topology to a Polars DataFrame: the id and the properties as
columns, and the geometry as WKB in the column geometry, as polars-st stores
it. With a CRS that has an EPSG code, the WKB is EWKB with this code as SRID,
so that polars-st knows the CRS. Remember that this will destroy the computed
Topology.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
crs
|
(str, int, CRS)
|
coordinate reference system of the geometries, such as |
None
|
validate
|
boolean
|
Set to |
False
|
winding_order
|
str
|
Determines the winding order of the features in the output geometry. Choose
between |
'CCW_CW'
|
object_name
|
(str, int)
|
Name or index of the object.
Default is index |
0
|
to_alt ¶
to_alt(color: str | None = None, tooltip: bool = True, projection: str = 'identity', object_name: str | int = 0) -> altair.Chart
Display as Altair visualization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color
|
str
|
Assign an 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. Eg |
None
|
tooltip
|
boolean
|
Option to include or exclude tooltips on geoshape objects
Default is |
True
|
projection
|
str
|
Defines the projection of the visualization. Defaults to a non-geographic,
Cartesian projection (known by Altair as |
'identity'
|
object_name
|
(str, int)
|
The name or the index of the object within the Topology to display. Default is index 0. |
0
|
to_widget ¶
to_widget(slider_toposimplify: Slider | None = None, slider_topoquantize: Slider | None = None, slider_keep: Slider | None = None) -> Any
Create an interactive widget based on Altair. The widget includes sliders to
interactively change the toposimplify and topoquantize settings. With
an algorithm "share of vertices", the keep slider sets the share of the
vertices to keep instead of the tolerance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slider_toposimplify
|
dict
|
The dict should contain the following keys: |
None
|
slider_topoquantize
|
dict
|
The dict should contain the following keys: |
None
|
slider_keep
|
dict
|
The dict should contain the following keys: |
None
|
topoquantize ¶
topoquantize(quant_factor: float | Transform, inplace: Literal[False] = False) -> Self
topoquantize(quant_factor: float | Transform, inplace: Literal[True]) -> None
topoquantize(quant_factor: float | Transform, inplace: bool = False) -> Self | None
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 or 1e6.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
quant_factor
|
float or dict
|
Quantization factor: the number of steps on each axis of the bounding
box. Or a fixed TopoJSON transform as a dict
( |
required |
inplace
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
object or None
|
Quantized coordinates and delta-encoded arcs if |
toposimplify ¶
toposimplify(epsilon: float | None = None, simplify_algorithm: Algorithm | None = None, simplify_with: Package | None = None, prevent_oversimplify: bool | None = None, inplace: Literal[False] = False, keep: float | None = None) -> Self
toposimplify(epsilon: float | None = None, simplify_algorithm: Algorithm | None = None, simplify_with: Package | None = None, prevent_oversimplify: bool | None = None, *, inplace: Literal[True], keep: float | None = None) -> None
toposimplify(epsilon: float | None = None, simplify_algorithm: Algorithm | None = None, simplify_with: Package | None = None, prevent_oversimplify: bool | None = None, inplace: bool = False, keep: float | None = None) -> Self | None
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 to 10.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epsilon
|
float
|
tolerance parameter. Give either |
None
|
simplify_algorithm
|
str
|
Choose between |
None
|
simplify_with
|
str
|
Sets the package to use for simplifying. Choose between |
None
|
prevent_oversimplify
|
boolean
|
If this setting is set to |
None
|
inplace
|
bool
|
If |
False
|
keep
|
float
|
Instead of |
None
|
Returns:
| Type | Description |
|---|---|
object or None
|
Topology object with simplified linestrings if |
read_json
classmethod
¶
read_json(fp: str | PathLike[str] | IO[str]) -> Self
Read a Topology from a TopoJSON file. If the file was written with
to_json(..., state=True), the options and source hashes are restored, so that
add, remove and sync can continue from it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fp
|
str, path or file-like object
|
TopoJSON file to read. |
required |
Returns:
| Type | Description |
|---|---|
Topology
|
|
add ¶
add(data: Any, object_name: str | None = None) -> Self
Add features to the Topology without recomputing it. Existing arcs are cut where the new features share a path with them; the result is the same as a full build on the same quantization grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
GeoDataFrame or GeoSeries
|
Features to add. The index is used as feature id and must not exist yet. |
required |
object_name
|
str
|
Object to add the features to. Only needed if the Topology has more than one object. |
None
|
Returns:
| Type | Description |
|---|---|
Topology
|
|
remove ¶
remove(ids: Iterable[Hashable], object_name: str | None = None) -> Self
Remove features from the Topology without recomputing it. Arcs that are no longer used are dropped and arcs are merged where a point is no longer a junction. Compared to a full build, a ring that is no longer cut can start at another vertex, and the bbox (recomputed from the quantized data) can differ by at most half a grid cell.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ids
|
iterable
|
Ids of the features to remove. |
required |
object_name
|
str
|
Object to remove the features from. Only needed if the Topology has more than one object. |
None
|
Returns:
| Type | Description |
|---|---|
Topology
|
|
sync ¶
sync(data: Any, object_name: str | None = None) -> Self
Make the Topology equal to data: features that are new are added, features
that are gone are removed and features with a changed geometry are replaced.
Unchanged features are left as they are. What happened is stored in
self.last_sync.
Comparing uses a hash of the input geometry of each feature. These hashes are
kept on the Topology and written with to_json(..., state=True).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
GeoDataFrame or GeoSeries
|
The complete set of features; the index is the feature id. |
required |
object_name
|
str
|
Object to synchronise. Only needed if the Topology has more than one object. |
None
|
Returns:
| Type | Description |
|---|---|
Topology
|
|