pycontrails.core.polygon

Algorithm support for grid to polygon conversion.

Functions

buffer_and_clean(contour, min_area, ...)

Buffer and clean a contour.

determine_buffer(longitude, latitude)

Determine the proper buffer size to use when converting to polygons.

find_multipolygon(arr, threshold, min_area, ...)

Compute a multipolygon from a 2d array.

multipolygon_to_geojson(multipolygon, altitude)

Convert a shapely multipolygon to a GeoJSON feature.

pycontrails.core.polygon.buffer_and_clean(contour, min_area, convex_hull, epsilon, precision, buffer, is_exterior)

Buffer and clean a contour.

Parameters:
  • contour (npt.NDArray[np.float64]) – Contour to buffer and clean. A 2d array of shape (n, 2) where n is the number of vertices in the contour.

  • min_area (float) – Minimum area of the polygon. If the area of the buffered contour is less than this, return None.

  • convex_hull (bool) – Whether to take the convex hull of the buffered contour.

  • epsilon (float) – Epsilon value for polygon simplification. If 0, no simplification is performed.

  • precision (int | None) – Precision of the output polygon. If None, no rounding is performed.

  • buffer (float) – Buffer distance.

  • is_exterior (bool, optional) – Whether the contour is an exterior contour. If True, the contour is buffered with a larger buffer distance. The polygon orientation is CCW iff this is True.

Returns:

shapely.Polygon | None – Buffered and cleaned polygon. If the area of the buffered contour is less than min_area, return None.

pycontrails.core.polygon.determine_buffer(longitude, latitude)

Determine the proper buffer size to use when converting to polygons.

pycontrails.core.polygon.find_multipolygon(arr, threshold, min_area, epsilon, interiors=True, convex_hull=False, longitude=None, latitude=None, precision=None)

Compute a multipolygon from a 2d array.

Parameters:
  • arr (npt.NDArray[np.float64]) – Array to convert to a multipolygon. The array will be converted to a binary array by comparing each element to threshold. This binary array is then passed into cv2.findContours() to find the contours.

  • threshold (float) – Threshold to use when converting arr to a binary array.

  • min_area (float) – Minimum area of a polygon to be included in the output.

  • epsilon (float) – Epsilon value to use when simplifying the polygons. Passed into shapely’s shapely.geometry.Polygon.simplify() method.

  • interiors (bool, optional) – Whether to include interior polygons.

  • convex_hull (bool, optional) – Experimental. Whether to take the convex hull of each polygon.

  • longitude (npt.NDArray[np.float64] | None, optional) – If provided, the coordinates values corresponding to the longitude dimensions of arr. The contour coordinates will be converted to longitude-latitude values by indexing into this array. Defaults to None.

  • latitude (npt.NDArray[np.float64] | None, optional) – If provided, the coordinates values corresponding to the latitude dimensions of arr.

  • precision (int | None, optional) – If provided, the precision to use when rounding the coordinates. Defaults to None.

Returns:

shapely.MultiPolygon – A multipolygon of the contours.

Raises:

ValueError – If arr is not 2d.

pycontrails.core.polygon.multipolygon_to_geojson(multipolygon, altitude, properties=None)

Convert a shapely multipolygon to a GeoJSON feature.

Parameters:
  • multipolygon (shapely.MultiPolygon) – Multipolygon to convert.

  • altitude (float | None) – Altitude of the multipolygon. If provided, the multipolygon coordinates will be given a z-coordinate.

  • properties (dict[str, Any] | None, optional) – Properties to add to the GeoJSON feature.

Returns:

dict[str, Any] – GeoJSON feature with geometry type “MultiPolygon”.