Source code for

from typing import TYPE_CHECKING, Optional

from rastervision.pipeline.file_system import json_to_file
from import ChipClassificationLabels
from import LabelStore
from import boxes_to_geojson
from import (
from import (GeoJSONVectorSourceConfig)

    from import Box
    from import ClassConfig, CRSTransformer

[docs]class ChipClassificationGeoJSONStore(LabelStore): """Storage for chip classification predictions."""
[docs] def __init__(self, uri: str, class_config: 'ClassConfig', crs_transformer: 'CRSTransformer', bbox: Optional['Box'] = None): """Constructor. Args: uri: uri of GeoJSON file containing labels class_config: ClassConfig crs_transformer: CRSTransformer to convert from map coords in label in GeoJSON file to pixel coords. bbox (Optional[Box], optional): User-specified crop of the extent. If provided, only labels falling inside it are returned by :meth:`.ChipClassificationGeoJSONStore.get_labels`. Must be provided if the corresponding RasterSource has bbox != extent. """ self.uri = uri self.class_config = class_config self._crs_transformer = crs_transformer self._bbox = bbox
[docs] def save(self, labels: ChipClassificationLabels) -> None: """Save labels to URI if writable. Note that if the grid is inferred from polygons, only the grid will be written, not the original polygons. """ boxes = labels.get_cells() class_ids = labels.get_class_ids() scores = list(labels.get_scores()) geojson = boxes_to_geojson( boxes, class_ids, self.crs_transformer, self.class_config, scores=scores, bbox=self.bbox) json_to_file(geojson, self.uri)
[docs] def get_labels(self) -> ChipClassificationLabels: vs = GeoJSONVectorSourceConfig(uris=self.uri) ls = ChipClassificationLabelSourceConfig(vector_source=vs).build( class_config=self.class_config, crs_transformer=self.crs_transformer, bbox=self.bbox) return ls.get_labels()
@property def bbox(self) -> 'Box': return self._bbox @property def crs_transformer(self) -> 'CRSTransformer': return self._crs_transformer
[docs] def set_bbox(self, bbox: 'Box') -> None: self._bbox = bbox