faninsar.query.Polygons#
- class faninsar.query.Polygons(gdf: gpd.GeoDataFrame | gpd.GeoSeries, types: Literal['desired', 'undesired'] | Sequence[Literal['desired', 'undesired']] = 'desired', crs: CrsLike = None, all_touched: bool = True, pad: bool = False)[source]#
Bases:
objectPolygons object is used to store the desired and undesired regions.
desired regions will be retrieved from the dataset, while undesired regions will be removed from the dataset.
Tip
When a mixed-types Polygons, where both “desired” and “undesired” polygons, are provided:
the “undesired” polygons will only be useful when there are overlapping regions with the “desired” polygons. Otherwise, the “desired” polygons are enough.
you can use
to_desired()to get the desired polygons from a mixed-types Polygons object.
- __init__(gdf: gpd.GeoDataFrame | gpd.GeoSeries, types: Literal['desired', 'undesired'] | Sequence[Literal['desired', 'undesired']] = 'desired', crs: CrsLike = None, all_touched: bool = True, pad: bool = False) None[source]#
Initialize a Polygons object.
- Parameters:
gdf (GeoDataFrame | GeoSeries) – The GeoDataFrame or GeoSeries containing the “desired” or “undesired” regions/polygons.
types ('desired' | 'undesired' | Sequence['desired', 'undesired']) – The types of polygons. If ‘desired’, the polygons are the desired regions. If ‘undesired’, the polygons are the regions to be removed. Default is ‘desired’.
crs (Any, optional) – The CRS of the polygons. Can be any object that can be passed to
pyproj.crs.CRS.from_user_input(). If None, the CRS of the input geometry will be used. Default is None.all_touched (bool, optional) – Whether to include all pixels touched by the polygon. Default is True.
pad (bool, optional) – If True, the features will be padded in each direction by one half of a pixel prior to cropping raster. Defaults to False.
Methods
__init__(gdf[, types, crs, all_touched, pad])Initialize a Polygons object.
copy()Return a copy of the Polygons object.
from_file(filename[, types, crs])Initialize a Polygon object from a shapefile.
plot(**kwargs)Plot the polygons on a map.
set_crs(crs[, allow_override])Set the CRS of polygons.
Return a GeoDataFrame of the polygons.
to_bbox()Return a list of BoundingBox representing bounding boxes of polygons.
to_crs(crs)Return a new Polygons object with new CRS.
Return a desired polygons, with regions of undesired polygons being removed.
Attributes
Whether to include all pixels touched by the polygon.
The CRS of the polygons.
Desired part of polygons.
GeoDataFrame format of polygons.
The geometry column of the polygons.
Whether the polygons contain both desired and undesired polygons.
Whether to pad the features in each direction by one half of a pixel.
The types of polygons.
Undesired part of polygons.
- classmethod from_file(filename: str | Path, types: Literal['desired', 'undesired'] | Sequence[Literal['desired', 'undesired']] = 'desired', crs: CrsLike = None, **kwargs) Polygons[source]#
Initialize a Polygon object from a shapefile.
- Parameters:
filename (str | Path) – The path to the shapefile. file type can be any type that can be passed to
geopandas.read_file().types ('desired' | 'undesired' | Sequence['desired', 'undesired'], optional) – The types of polygons. If ‘desired’, the polygons are the desired polygons. If ‘undesired’, the polygons are the polygons to be removed. Default is ‘desired’.
crs (Any, optional) – The CRS of the polygons. Can be any object that can be passed to
pyproj.crs.CRS.from_user_input(). If None, the CRS of the input geometries will be used. Default is None.**kwargs (dict) – Other parameters passed to
geopandas.read_file().
- Returns:
The Polygons object.
- Return type:
- plot(**kwargs) Axes[source]#
Plot the polygons on a map.
- Parameters:
**kwargs (dict) – Other parameters passed to
geopandas.GeoDataFrame.plot().- Returns:
The matplotlib axes.
- Return type:
Axes
- set_crs(crs: CrsLike, allow_override: bool = False) None[source]#
Set the CRS of polygons.
Warning
This method will only set the crs attribute without converting the geometries to a new coordinate reference system. If you want to convert the geometries to a new coordinate, please use
to_crs()- Parameters:
crs (Any) – The new CRS. Can be any object that can be passed to
pyproj.crs.CRS.from_user_input().allow_override (bool, optional) – Whether to allow overriding the existing CRS. If False, a CRSError will be raised if the CRS has already been set. Default is False.
- Raises:
CRSError – If the CRS has already been set and allow_override is False.
- to_GeoDataFrame() GeoDataFrame[source]#
Return a GeoDataFrame of the polygons.
This method is an alias of
framefor API consistency withPointsandBoundingBox.
- to_bbox() list[BoundingBox][source]#
Return a list of BoundingBox representing bounding boxes of polygons.
Warning
This method will only return the bounding boxes of the desired polygons. If the Polygons object only contains “undesired” polygons, the returned list will be empty.
- to_crs(crs: CrsLike) Polygons[source]#
Return a new Polygons object with new CRS.
- Parameters:
crs (Any) – The new CRS. Can be any object that can be passed to
pyproj.crs.CRS.from_user_input().- Returns:
The new Polygons object.
- Return type:
- to_desired() Polygons[source]#
Return a desired polygons, with regions of undesired polygons being removed.
Warning
This method should only be used when the Polygons object contains both “desired” and “undesired” polygons. If the Polygons object only contains “undesired” polygons, the returned Polygons object will be empty.
- property frame: GeoDataFrame#
GeoDataFrame format of polygons.