topojson.core.topology
Topology
Topology(self, data, topology=True, prequantize=True, topoquantize=False, presimplify=False, toposimplify=False, shared_coords=True, prevent_oversimplify=True, simplify_with='shapely', simplify_algorithm='dp', winding_order='CW_CCW')
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
data
: any geometric typeGeometric data that should be converted into TopoJSON
topology
: booleanSpecify if the topology should be computed for deriving the TopoJSON. Default is
True
.
prequantize
: boolean, intIf 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
1e4
,1e5
or1e5
. Default isTrue
(which correspond to a quantize factor of1e5
).
topoquantize
: boolean or intIf 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. See for more details the
prequantize
parameter. Default isFalse
.
presimplify
: boolean, floatApply presimplify to remove unnecessary points from linestrings before the topology is constructed. This will simplify the input geometries. Use with care. Default is
False
.
toposimplify
: boolean, floatApply 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
. Defaults toFalse
.
shared_coords
: booleanSets the strategy to detect junctions. When set to
True
a path is considered shared when all coordinates appear in both paths (coords-connected
). When set toFalse
a path is considered shared when coordinates are the same path (path-connected
). The path-connected strategy is more ‘correct’, but slower. Default isTrue
.
prevent_oversimplify
: booleanIf this setting is set to
True
, 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. Default isTrue
.
simplify_with
: strSets the package to use for simplifying (both pre- and toposimplify). Choose between
shapely
orsimplification
. Shapely adopts solely Douglas-Peucker and simplification both Douglas-Peucker and Visvalingam-Whyatt. The package simplification is known to be quicker than shapely. Default isshapely
.
simplify_algorithm
: strChoose between
dp
andvw
, for Douglas-Peucker or Visvalingam-Whyatt respectively.vw
will only be selected ifsimplify_with
is set tosimplification
. Default isdp
, since it “produced the most accurate generalization” (Shi, W. & Cheung, C., 2006).
winding_order
: strDetermines the winding order of the features in the output geometry. Choose between
CW_CCW
for clockwise orientation for outer rings and counter- clockwise for interior rings. OrCCW_CW
for counter-clockwise for outer rings and clockwise for interior rings. Default isCW_CCW
.
object_name
: strName to use as key for the objects in the topojson file. This name is used for writing and reading topojson file formats. Default is
data
.
to_dict
Topology.to_dict(self, options=False)
Convert the Topology to a dictionary.
Parameters
to_svg
Topology.to_svg(self, separate=False, include_junctions=False)
Display the arcs and junctions as SVG.
Parameters
to_json
Topology.to_json(self, fp=None, options=False, pretty=False, indent=4, maxlinelength=88)
Convert the Topology to a JSON object.
Parameters
fp
: strIf set, writes the object to a file on drive. Default is
None
options
: booleanIf
True
, the options also will be included. Default isFalse
pretty
: booleanIf
True
, the JSON object will be ‘pretty’, depending on theident
andmaxlinelength
options Default isFalse
indent
: intIf
pretty=True
, declares the indentation of the objects. Default is4
.
maxlinelength
: intIf
pretty=True
, declares the maximum length of each line. Default is88
.
to_geojson
Topology.to_geojson(self, fp=None, pretty=False, indent=4, maxlinelength=88, validate=False, objectname='data')
Convert the Topology to a GeoJSON object. Remember that this will destroy the computed Topology.
Parameters
fp
: strIf set, writes the object to a file on drive. Default is
None
pretty
: booleanIf
True
, the JSON object will be ‘pretty’, depending on theident
andmaxlinelength
options. Default isFalse
indent
: intIf
pretty=True
, declares the indentation of the objects. Default is4
maxlinelength
: intIf
pretty=True
, declares the maximum length of each line. Default is88
validate
: booleanSet to
True
to validate each feature before inclusion in the GeoJSON. Only features that are valid geometries objects will be included. Default isFalse
objectname
: strThe name of the object within the Topology to convert to GeoJSON. Default is
data
decimals
: int or NoneEvenly round the coordinates to the given number of decimals. Default is
None
, which means no rounding is applied.
to_gdf
Topology.to_gdf(self)
Convert the Topology to a GeoDataFrame. Remember that this will destroy the computed Topology.
Note: This function use the TopoJSON driver within Fiona to parse the Topology to a GeoDataFrame. If data is missing (eg. Fiona cannot parse nested geometry collections) you can trying using the .to_geojson()
function prior creating the GeoDataFrame.
to_alt
Topology.to_alt(self, mesh=True, color=None, tooltip=True, projection='identity', objectname='data')
Display as Altair visualization.
Parameters
mesh
: booleanIf
True
, render arcs only (mesh object). IfFalse
render as geoshape. Default isTrue
color
: strAssign an property attribute to be used for color encoding. Remember that most of the time the wanted attribute is nested within properties. Moreover, specific type declaration is required. Eg
color='properties.name:N'
. Default isNone
tooltip
: booleanOption to include or exclude tooltips on geoshape objects Default is
True
.
projection
: strDefines the projection of the visualization. Defaults to a non-geographic, Cartesian projection (known by Altair as
identity
).
objectname
: strThe name of the object within the Topology to display. Default is
data
to_widget
Topology.to_widget(self, slider_toposimplify={'min': 0, 'max': 10, 'step': 0.01, 'value': 0.01}, slider_topoquantize={'min': 1, 'max': 6, 'step': 1, 'value': 100000.0, '> + ###### `base': ` : 10})
Create an interactive widget based on Altair. The widget includes sliders to interactively change the toposimplify
and topoquantize
settings.
Parameters
topoquantize
Topology.topoquantize(self, quant_factor, inplace=False)
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
Returns
toposimplify
Topology.toposimplify(self, epsilon, inplace=False)
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
Returns