cg.cg — Computational Geometry

The cg.cg module provides computational geometry methods.

New in version 0.1.0.

autocnet.cg.cg.alpha_shape(points, alpha)[source]

Compute a convex hull from a set of points.

credit: https://gist.github.com/dwyerk/10561690

Parameters
  • points (np.array) – points in the format [[x0,y0], [x1, y,1], … [xn, yn]]

  • alpha (float) – Higher alphas creater a tighter the boundery around input points, alphas approaching 0 create a convex hull. Best to keep in the range (0, 1]

Returns

convex_hull – Shapely polygon of the convex hull

Return type

shapely.geometry.Polygon

autocnet.cg.cg.compute_voronoi(keypoints, intersection=None, geometry=False, s=30)[source]

Creates a voronoi diagram for all edges in a graph, and assigns a given weight to each edge. This is based around voronoi polygons generated by scipy’s voronoi method, to determine if an image has significant coverage.

Parameters
  • graph (object) – A networkx graph object

  • clean_keys (list) – Of strings used to apply masks to omit correspondences

  • s (int) – Offset for the corners of the image

autocnet.cg.cg.convex_hull(points)[source]
Parameters

points (ndarray) – (n, 2) array of point coordinates

Returns

hull – Provides a convex hull that is used to determine coverage

Return type

2-D convex hull

autocnet.cg.cg.convex_hull_ratio(points, ideal_area)[source]
Parameters
  • points (ndarray) – (n, 2) array of point coordinates

  • ideal_area (float) – The total area that could be covered

Returns

ratio – The ratio convex hull volume / ideal_area

Return type

float

autocnet.cg.cg.create_points_along_line(p1, p2, npts)[source]

Compute a set of nodes equally spaced between two points, not including the end points.

Parameters
  • p1 (iterable) – in the form (x,y)

  • p2 (iterable) – in the form(x,y)

  • npts (int) – The number of nodes to be returned

Returns

(n,2) array of nodes

Return type

ndarray

autocnet.cg.cg.distribute_points_classic(geom, nspts, ewpts, use_mrr=True, **kwargs)[source]

This is a decision tree that attempts to perform a very simplistic approximation of the shape of the geometry and then place some number of north/south and east/west points into the geometry.

Parameters
  • geom (shapely.geom) – A shapely geometry object

  • nspts (int) – The number of points to attempt to place in the N/S (up/down) direction

  • ewpts (int) – The number of points to attempt to place in the E/W (right/left) direction

  • use_mrr (boolean) – If True (default) compute the minimum rotated rectangle bounding the geometry

Returns

valid – of point coordinates in the form [(x1,y1), (x2,y2), …, (xn, yn)]

Return type

list

autocnet.cg.cg.distribute_points_in_geom(geom, method='classic', nspts_func=<function <lambda>>, ewpts_func=<function <lambda>>, Session=None, **kwargs)[source]

Given a geometry, attempt a basic classification of the shape. RIght now, this simply attempts to determine if the bounding box is generally N/S or generally E/W trending. Once the determination is made, the algorithm places points in the geometry and returns a list of valid (intersecting) points.

The kwargs for this algorithm take a function that expects a number as an input and returns an integer number of points to place. The input number is the distance between the top/bottom or left/right sides of the geometry.

This algorithm does not know anything about the units being used so the caller is responsible for acocunting for units (if appropriate) in the passed funcs.

Parameters
  • geom (shapely.geom object) – The geometry object

  • nspts_func (obj) – Function taking a Number and returning an int

  • ewpts_func (obj) – Function taking a Number and returning an int

Returns

valid – of valid points in the form (x,y) or (lon,lat)

Return type

np.ndarray

autocnet.cg.cg.distribute_points_new(geom, nspts, ewpts, Session)[source]

This is a decision tree that attempts to perform a very simplistic approximation of the shape of the geometry and then place some number of north/south and east/west points into the geometry. This function works best on bulky geometries, such as a combination of all network image footprints.

Parameters
  • geom (shapely.geom) – A shapely geometry object which is a union of all of the image footprints in the network that is being grounded.

  • nspts (int) – The number of points to attempt to place in the N/S (up/down) direction

  • ewpts (int) – The number of points to attempt to place in the E/W (right/left) direction

Returns

valid – of point coordinates in the form [(x1,y1), (x2,y2), …, (xn, yn)]

Return type

list

autocnet.cg.cg.geom_mask(keypoints, geom)[source]

Masks any points that are outside of the bounds of the given geometry.

Parameters
  • keypoints (dataframe) – A pandas dataframe of points to mask

  • geom (object) – Shapely geometry object to use as a mask

autocnet.cg.cg.get_area(poly1, poly2)[source]
Parameters
  • poly1 (ogr polygon) – General ogr polygon

  • poly2 (ogr polygon) – General ogr polygon

Returns

intersection_area – returns the intersection area of two polygons

Return type

float

autocnet.cg.cg.nearest(pt, search)[source]

Fine the index of nearest (Euclidean) point in a list of points.

Parameters
  • pt (ndarray) – (2,1) array

  • search (ndarray) – (n,2) array of points to search within. The returned index is the closet point in this set to the search

Returns

The index to the nearest point.

Return type

int

autocnet.cg.cg.rasterize_polygon(shape, vertices, dtype=<class 'bool'>)[source]

Simple tool to convert poly into a boolean numpy array.

source: https://stackoverflow.com/questions/37117878/generating-a-filled-polygon-inside-a-numpy-array

Parameters
  • shape (tuple) – size of the array in (y,x) format

  • vertices (np.array, list) – array of vertices in [[x0, y0], [x1, y1]…] format

  • dtype (type) – datatype of output mask

Returns

mask – mask with filled polygon set to true

Return type

np.array

autocnet.cg.cg.single_centroid(geom)[source]

For a geom, return the centroid

Parameters

geom (shapely.geom object) –

Returns

in the form [(x,y)]

Return type

list

autocnet.cg.cg.two_point_extrapolate(x, xs, ys)[source]
Parameters
  • x (float) – point where you want corresponding y value

  • xs (ndarray) – (1, 2) array of point x coordinates

  • ys (ndarray) – (1, 2) array of point x coordinates

Returns

y – extrapolated value associated with x

Return type

float

autocnet.cg.cg.two_poly_overlap(poly1, poly2)[source]
Parameters
  • poly1 (ogr polygon) – Any polygon that shares some kind of overlap with poly2

  • poly2 (ogr polygon) – Any polygon that shares some kind of overlap with poly1

Returns

  • overlap_percn (float) – The percentage of image overlap

  • overlap_area (float) – The total area of overalap

autocnet.cg.cg.xy_in_polygon(x, y, geom)[source]

Returns true is an x,y pair is contained within the geom.

Parameters
  • x (Number) – The x coordinate

  • y (Number) – The y coordinate

Returns

True if the point is contained within the geom.

Return type

bool