util

Convenience functions for the preparation and conversion of networkX graphs to and from cityseer data structures. Note that the cityseer network data structures can be created and manipulated directly, if so desired.

validate_cityseer_networkx_graph

validate_cityseer_networkx_graph
(
nx_multigraph : networkx.classes.multigraph.MultiGraph
validate_edges : bool = False
)->[ MultiGraph ]

Validates a networkX MultiGraph for use with cityseer.

Parameters

nx_multigraph
MultiGraph

A networkX MultiGraph with a crs attribute denoting a projected coordinate system, containing x and y node attributes, and geom edge attributes containing LineString geoms.

validate_edges
bool

Returns

MultiGraph

A networkX MultiGraph with nodes of degree=2 removed. Adjacent edges will be combined into a unified new edge with associated geom attributes spliced together.

measure_bearing

measure_bearing
(
xy_1
xy_2
)->[ float ]

Measures the angular bearing between two coordinate pairs.

measure_coords_angle

measure_coords_angle
(
coords_1
coords_2
coords_3
)->[ float ]

Measures angle between three coordinate pairs. Angular change is from one line segment to the next, across the intermediary coord.

measure_angle_diff_betw_linestrings

measure_angle_diff_betw_linestrings
(
linestring_coords_a
linestring_coords_b
)

Measures the angular difference between the bearings of two sets of linestring coords.

measure_cumulative_angle

measure_cumulative_angle
(
linestring_coords
)->[ float ]

Measures the cumulative angle along a LineString geom’s coords.

measure_max_angle

measure_max_angle
(
linestring_coords
)->[ float ]

Measures the maximum angle along a LineString geom’s coords.

snap_linestring_startpoint

snap_linestring_startpoint
(
linestring_coords
x_y
)->[ list[tuple[float float] | tuple[float float ] CoordinateSequence ]

Snaps a LineString’s start-point coordinate to a specified x_y coordinate.

Parameters

linestring_coords
tuple | list | np.ndarray

A list, tuple, or numpy array of x, y coordinate tuples.

x_y
tuple[float, float]

A tuple of floats representing the target x, y coordinates against which to align the linestring start point.

Returns

linestring_coords

A list of linestring coords aligned to the specified starting point.

snap_linestring_endpoint

snap_linestring_endpoint
(
linestring_coords
x_y
)->[ list[tuple[float float] | tuple[float float ] CoordinateSequence ]

Snaps a LineString’s end-point coordinate to a specified x_y coordinate.

Parameters

linestring_coords
tuple | list | np.ndarray

A list, tuple, or numpy array of x, y coordinate tuples.

x_y
tuple[float, float]

A tuple of floats representing the target x, y coordinates against which to align the linestring end point.

Returns

linestring_coords

A list of linestring coords aligned to the specified ending point.

align_linestring_coords

align_linestring_coords
(
linestring_coords
x_y
reverse : bool = False
tolerance : float = 0.5
)->[ list[tuple[float float] | tuple[float float ] CoordinateSequence ]

Align a LineString’s coordinate order to either start or end at a specified x_y coordinate within a given tolerance.

Parameters

linestring_coords
tuple | list | np.ndarray

A list, tuple, or numpy array of x, y coordinate tuples.

x_y
tuple[float, float]

A tuple of floats representing the target x, y coordinates against which to align the linestring coords.

reverse
bool

If reverse=False the coordinate order will be aligned to start from the given x_y coordinate. If reverse=True the coordinate order will be aligned to end at the given x_y coordinate.

tolerance
float

Distance tolerance in metres for matching the x_y coordinate to the linestring_coords.

Returns

linestring_coords

A list of linestring coords aligned to the specified endpoint.

snap_linestring_endpoints

snap_linestring_endpoints
(
nx_multigraph : networkx.classes.multigraph.MultiGraph
start_nd_key : str
end_nd_key : str
linestring_coords
tolerance : float = 0.5
)->[ list[tuple[float float] | tuple[float float ] CoordinateSequence ]

Snaps edge geom coordinate sequence to the nodes on either side.

Parameters

nx_multigraph
MultiGraph

A networkX MultiGraph with x and y node attributes and edge geom attributes.

start_nd_key
NodeKey

A node key corresponding to the edge’s start node.

end_nd_key
NodeKey

A node key corresponding to the edge’s end node.

linestring_coords
tuple | list | np.ndarray

A list, tuple, or numpy array of x, y coordinate tuples.

tolerance
float

Distance tolerance in metres for matching the x_y coordinate to the linestring_coords.

Returns

linestring_coords

A list of linestring coords aligned to the specified ending point.

weld_linestring_coords

weld_linestring_coords
(
linestring_coords_a
linestring_coords_b
force_xy
tolerance : float = 0.01
)->[ list[tuple[float float] | tuple[float float ] CoordinateSequence ]

Welds two linestrings. Finds a matching start / end point combination and merges the coordinates accordingly. If the optional force_xy is provided then the weld will be performed at the x_y end of the LineStrings. The force_xy parameter is useful for looping geometries or overlapping geometries where it can happen that welding works from either of the two ends, thus potentially mis-aligning the start point unless explicit.

EdgeInfo

Encapsulates EdgeInfo logic.

EdgeInfo

EdgeInfo
( )

Initialises a network information structure.

names

routes

highways

levels

gather_edge_info

gather_edge_info
(
self
edge_data
)

Gather edge data from provided edge_data.

set_edge_info

set_edge_info
(
self
nx_multigraph : networkx.classes.multigraph.MultiGraph
start_node_key : str
end_node_key : str
edge_idx : int
)

Set accumulated edge data to specified graph and edge.

add_node

add_node
(
nx_multigraph : networkx.classes.multigraph.MultiGraph
nodes_names : list[str]
x : float
y : float
live : bool | None = None
z : float | None = None
)->[ str bool ]

Add a node to a networkX MultiGraph. Assembles a new name from source node names. Checks for duplicates. Returns new name and is_dupe

create_nodes_strtree

create_nodes_strtree
(
nx_multigraph : networkx.classes.multigraph.MultiGraph
)->[ STRtree list[dict[str Any]] ]

Create a nodes-based STRtree spatial index.

create_edges_strtree

create_edges_strtree
(
nx_multigraph : networkx.classes.multigraph.MultiGraph
)->[ STRtree list[dict[str Any]] ]

Create an edges-based STRtree spatial index.

project_geom

project_geom
(
geom
from_crs_code : int | str
to_crs_code : int | str
)

Projects an input shapely geometry.

Parameters

geom
shapely.geometry

A GeoDataFrame containing building polygons.

from_crs_code
int | str

The EPSG code from which to convert the projection.

to_crs_code
int | str

The EPSG code into which to convert the projection.

Returns

shapely.geometry

A shapely geometry in the specified to_crs_code projection.

extract_utm_epsg_code

extract_utm_epsg_code
(
lng : float
lat : float
)->[ int ]

Finds the UTM coordinate reference system for a given longitude and latitude.

Parameters

lng
float

The longitude for which to find the appropriate UTM EPSG code.

lat
float

The latitude for which to find the appropriate UTM EPSG code.

Returns

int

The EPSG coordinate reference code for the UTM projection.