Skip to content

TopoJSON

topojson.TopoJSON

TopoJSON is a JSON format for encoding geographic data structures into a shared topology.

Notes

See topojson-specification

arcs instance-attribute

arcs: list[list[list[int]]]

bbox instance-attribute

bbox: list[float]

objects instance-attribute

objects: dict[str, Geometry]

transform instance-attribute

transform: Optional[Transform]

compute_bbox

compute_bbox() -> list[float]

Returns the computed bounding box of the specified topology \([x_0, y_0, x_1, y_1]\) where \(x_0\) is the minimum x-value, \(y_0\) is the minimum y-value, x_1 is the maximum x-value, and \(y_1\) is the maximum y-value. If the topology has no points and no arcs, the returned bounding box is \([\infty, \infty, -\infty, -\infty]\).

(This method ignores the existing topology.bbox, if any.)

Returns:

Type Description
list[float]

Computed bounding box

feature

feature(key: str) -> GeoJSON

Returns the GeoJSON Feature or FeatureCollection for the specified object in the given topology. If the object is a Geometry_GeometryCollection, a GeoJSON_FeatureCollection is returned, and each geometry in the collection is mapped to a Feature. Otherwise, a Feature is returned. The returned feature is a shallow copy of the source object: they may share identifiers, bounding boxes, properties and coordinates.

Parameters:

Name Type Description Default

key

str

Key to access the object by doing topology.objects[key]

required

Returns:

Type Description
GeoJSON

GeoJSON Feature or FeatureCollection

Raises:

Type Description
KeyError

When key is not found in objects

Examples:

  • A point is mapped to a feature with a geometry object of type "Point".
  • Likewise for line strings, polygons, and other simple geometries.
  • A null geometry object (of type null in TopoJSON) is mapped to a feature with a null geometry object.
  • A geometry collection of points is mapped to a feature collection of features, each with a point geometry.
  • A geometry collection of geometry collections is mapped to a feature collection of features, each with a geometry collection.

merge

Returns the GeoJSON MultiPolygon geometry object representing the union for the specified array of Polygon and MultiPolygon objects in the given topology. Interior borders shared by adjacent polygons are removed. See Merging States for an example. The returned geometry is a shallow copy of the source object: they may share coordinates.

Parameters:

Name Type Description Default

key

str

Key to access the object by doing topology.objects[key]. The object must be a Geometry_GeometryCollection.

required

Returns:

Type Description
FeatureGeometryType_MultiLineString

GeoJSON MultiPolygon geometry object

Raises:

Type Description
KeyError

When key is not found in objects

TypeError

Selected object is not a Geometry_GeometryCollection.

mesh

mesh(key: Optional[str] = None, filter: Optional[Callable[[Geometry, Geometry], bool]] = None) -> FeatureGeometryType_MultiLineString

Returns the GeoJSON MultiLineString geometry object representing the mesh for the specified object in the given topology. This is useful for rendering strokes in complicated objects efficiently, as edges that are shared by multiple features are only stroked once. If object is not specified, a mesh of the entire topology is returned. The returned geometry is a shallow copy of the source object: they may share coordinates.

Parameters:

Name Type Description Default

key

str

Key to access the object by doing topology.objects[key].

None

filter

Optional[Callable[[Geometry, Geometry], bool]]

The filter function is called once for each candidate arc and takes two arguments, a and b, two geometry objects that share that arc. Each arc is only included in the resulting mesh if the filter function returns true. For typical map topologies the geometries a and b are adjacent polygons and the candidate arc is their boundary. If an arc is only used by a single geometry then a and b are identical.

None

Returns:

Type Description
FeatureGeometryType_MultiLineString

GeoJSON MultiLineString geometry object

Warnings

Currently, filter argument does not change the result because it is avoided due to performance issues.

Raises:

Type Description
KeyError

When key is not found in objects

neighbors

neighbors(keys: list[str]) -> list[list[int]]

Returns an array representing the set of neighboring objects for each object in the specified objects array. The returned array has the same number of elements as the input array; each element i in the returned array is the array of indexes for neighbors of object i in the input array. For example, if the specified objects array contains the features foo and bar, and these features are neighbors, the returned array will be [[1], [0]], indicating that foo is a neighbor of bar and vice versa. Each array of neighbor indexes for each object is guaranteed to be sorted in ascending order.

Parameters:

Name Type Description Default

keys

list[str]

List of keys for accessing the object by doing topology.objects[key].

required

Returns:

Type Description
list[list[int]]

Neighboring objects

Raises:

Type Description
KeyError

When key is not found in objects

quantize

quantize(transform: float) -> TopoJSON

Returns a shallow copy of the specified topology with quantized and delta-encoded arcs according to the specified transform object. If the topology is already quantized, an error is thrown. See also topoquantize.

The corresponding transform object is first computed using the bounding box of the topology. The quantization number transform must be a positive integer greater than one which determines the maximum number of expressible values per dimension in the resulting quantized coordinates; typically, a power of ten is chosen such as 1e4, 1e5 or 1e6. If the topology does not already have a topology.bbox, one is computed using topojson.bbox.

Parameters:

Name Type Description Default

transform

float

The quantization number transform.

required

Returns:

Type Description
TopoJSON

Shallow copy with quantized and delta-encoded arcs.

Raises:

Type Description
RuntimeError

If topology is already quantized or transform is smaller than 2.

write

write(file: str)

Write expression to json.

Parameters:

Name Type Description Default

file

str

Path to a file

required

Raises:

Type Description
RuntimeError

When serialization fails

OsError

When the file cannot be written