Geometry operations

class Extremity(shape_id, position, coords)[source]

Bases: object

Represents an extremity of a line. It’s useful to parse and deal with lines given as input.

almost_equally_located(p1, p2, tolerance=1e-08)[source]

Test if two point are loacated at the same place within a tolerance.

Parameters
  • p1 (Point) – First point to compare

  • p2 (Point) – Second point to compare

  • tolerance – Comparison tolerance (Default value = 1e-8)

Returns

True if the two points have the same coordinates.

Return type

bool

Return type

bool

convert_multilinestring_to_linestring(gdf)[source]
Convert all geometry attribute being a ‘MultiLineString’ to a ‘LineString’. The created line is a merge of all

sublines.

Parameters

gdf (gpd.GeoDataFrame) – A GeoDataFrame with a ‘geometry’ column to modify

Returns

The number of converted ‘MultiLineString’

Return type

int

Raises

RuntimeError – If an input shape is not a LineString or a MultiLineString

Return type

int

coordinates_almost_equal(c1, c2, tolerance=1e-08)[source]

Returns true if the two given list of coordinates equals within a given tolerance.

Parameters
  • c1 (Iterable) – First point coordinates

  • c2 (Iterable) – Second point coordinates

  • tolerance (float) – Tolerance comparison (Default value = 1e-8)

Returns

True if the coordinates almost equal, false otherwise.

Return type

bool

Return type

bool

discretize_line(line, discretization_tol)[source]

Takes a shapely LineString and discretize it into a list of shapely Points. Each point is at most at the discretization tolerance distance of the following point.

Parameters
  • line (LineString) – Line to discretize

  • discretization_tol (float) – Maximum distance between two points on the line.

Returns

An ordered list of shapely Point

Return type

list

See also

discretize_lines

Return type

list

discretize_lines(lines, discretization_tol)[source]

Discretize some line into points.

Parameters
  • lines (Iterable[LineString] :) – Lines to discretize

  • discretization_tol (float) – Maximum distance between two points on the line.

Returns

Return all the discretized points as a shapely MultiPoint and a dictionary to map the discretized points for each line.

Return type

MultiPoint and defaultdict

See also

discretize_line

get_closest_line_from_point(point_from, lines_to=None, discretization_tol=None, kd_tree=None, points_line_association=None)[source]

Find the closest line from a given point.

Parameters
  • point_from (PointCoordinatesLike) – Point coordinate to find the closest line.

  • lines_to (list) – Group of lines among which the closest has to be found (optional if kdtree and points_line_association are given). (Default value = None)

  • discretization_tol (float) – Maximum distance between discretized points (optional if kdtree and points_line_association are given). (Default value = None)

  • kd_tree (cKDTree) – An optional pre-computed kd_tree of discretized lines. (Default value = None)

  • points_line_association (dict) – An optional pre-computed dictionary matching lines and discretized points. (Default value = None)

Returns

  • float – distance

  • int – index of the closest line

get_closest_line_from_points(points_from, lines_to, discretization_tol)[source]

Find the closest line for each given points.

Parameters
  • points_from (list) – Points coordinates.

  • lines_to (list) – Group of lines among which the closest has to be found.

  • discretization_tol (float) – Maximum distance between discretized points

Returns

A list of closest lines indexes.

Return type

list

get_closest_point_from_line(line_from, discretization_tol, points_to=None, kd_tree=None)[source]

Return the closest point from a given line and its distance.

Parameters
  • line_from (LineString) – A shapely LineString (Default value = None)

  • discretization_tol (float) – Maximum distance between two discretized points on the line.

  • points_to (list) – A list of points among which the closest to the line has to be found (optional is kdtree is given)

  • kd_tree (cKDTree) – A kd-tree representing the points among which the closest to the line has to be found (optional if points_to is given) (Default value = None)

Returns

  • float – closest distance

  • int – index of the closest point

get_closest_point_from_multi_shape(multi_shape, points_to=None, kd_tree=None, discretization_tol=None)[source]

Computes the closest point to the multi shape (i.e. the point that has the smallest projection distance on the entire multi shape object.

Parameters
  • multi_shape (MultiPoint or MultiLineString) – The multi shape object can be any shapely object among: MultiPoint, MultiLineString

  • points_to (list) – A list of points among which to find the closest to the multi shape (Default value = None)

  • kd_tree (cKDTree) – A kdtree representing the points among which the closest to the multishape has to be found (optional if ‘points_to’ is given) (Default value = None)

  • discretization_tol (float) – A discretization tolerance if the multishape is a MultiLineString

Returns

  • float – distance

  • int – index of the closest point

get_closest_point_from_points(points_from, points_to=None, kd_tree=None)[source]

Compute the closest point among the points_from list for each point in the points_to list.

Parameters
  • points_from (PointsCoordinatesLike) – Iterable of points coordinates

  • points_to (list or None) – Iterable of points coordinates (Default value = None)

  • kd_tree (cKDTree) – a constructed kd tree representing points_from (Default value = None)

Returns

  • array of floats – distances

  • ndarray of ints – indexes

get_closest_point_from_shape(shape, points_to=None, kd_tree=None)[source]

Compute the closest point to the given shape.

Parameters
  • shape (Point, MultiPoint, LineString or MultiLineString) – Any shapely shape

  • points_to (list) – A list of points among which to find the closest to the multi shape (Default value = None)

  • kd_tree (cKDTree) – A kdtree representing the points among which the closest to the shape has to be found (optional if ‘points_to’ is given) (Default value = None)

Returns

  • float – distance

  • int – index of the closest point

get_closest_point_from_shapes(shapes_from, points_to)[source]

Compute the closest point for each given shape.

Parameters
  • shapes_from – An iterable of shapes (Point, MultiPoint, LineString, MultiLineString)

  • points_to (list) – A list of points among which to find the closest to the multi shape

Returns

  • float – distance

  • int – index of the closest point

get_default_discretization_tolerance(crs)[source]

Return a discretization tolerance with the right order of magnitude for the given crs.

Examples

>>> import geonetworkx as gnx
>>> print(gnx.get_default_discretization_tolerance("epsg:3857"))
3.0
get_polygons_neighborhood(polygons)[source]

Returns for each polygon a set of intersecting polygons.

get_shape_extremities(shape, shape_id)[source]

Return the extremities of a shape in the network_shapes_gdf.

insert_point_in_line(line, point_coords, position)[source]

Insert a new point in a line given its coordinates.

Return type

LineString

merge_two_lines_with_closest_extremities(first_line, second_line)[source]

Merge two lines with their closest extremities. Euclidian distance is used here.

Return type

LineString

merge_two_shape(e1, e2, line1, line2)[source]

Merge two lines (line1 and line2) with the given extremities (e1 and e2).

Return type

LineString

split_line(line, distance)[source]

Cuts a line in two at a distance from its starting point.