コード例 #1
0
    def process(self, metadata: FeatureCollection, params: dict):
        """
        Main wrapper method to facilitate snap processing per feature
        """
        polarisations: List = params.get("polarisations", ["VV"]) or ["VV"]

        self.assert_input_params()

        results: List[Feature] = []
        out_dict: dict = {}
        for in_feature in metadata.get("features"):
            coordinate = in_feature["bbox"]
            self.assert_dem(coordinate)
            try:
                processed_graphs = self.process_snap(in_feature, polarisations)
                LOGGER.info("SNAP processing is finished!")
                out_feature = copy.deepcopy(in_feature)
                processed_tif_uuid = out_feature.properties["up42.data_path"]
                out_path = "/tmp/output/%s/" % (processed_tif_uuid)
                if not os.path.exists(out_path):
                    os.mkdir(out_path)
                for out_polarisation in processed_graphs:
                    # Besides the path we only need to change the capabilities
                    shutil.move(
                        ("%s.tif" % out_polarisation),
                        ("%s%s.tif" %
                         (out_path, out_polarisation.split("_")[-1])),
                    )
                del out_feature["properties"]["up42.data_path"]
                set_data_path(out_feature, processed_tif_uuid + ".tif")
                results.append(out_feature)
                out_dict[processed_tif_uuid] = {
                    "id": processed_tif_uuid,
                    "z": [i.split("_")[-1] for i in processed_graphs],
                    "out_path": out_path,
                }
                Path(__file__).parent.joinpath(
                    "template/"
                    "snap_polarimetry_graph_%s.xml" % "copy").unlink()
            except WrongPolarizationError:
                LOGGER.error(
                    "%s: some or all of the polarisations (%r) don't exist "
                    "in this product (%s), skipping.",
                    "WrongPolarizationError",
                    polarisations,
                    self.safe_file_name(in_feature),
                )
                continue

        return FeatureCollection(results), out_dict
コード例 #2
0
    def process(self, metadata: FeatureCollection, params: dict):
        """
        Main wrapper method to facilitate snap processing per feature
        """
        polarisations: List = params.get("polarisations", ["VV"]) or ["VV"]

        results: List[Feature] = []
        out_path: str = ''
        processed_graphs: List = []
        for in_feature in metadata.get("features"):
            coordinate = in_feature['bbox']
            self.assert_dem(coordinate)
            try:
                processed_graphs = self.process_snap(in_feature, polarisations)
                LOGGER.info("SNAP processing is finished!")
                for out_polarisation in processed_graphs:
                    # Besides the path we only need to change the capabilities
                    out_feature = copy.deepcopy(in_feature)
                    processed_tif_uuid = str(uuid.uuid4())
                    out_path = "/tmp/output/%s/" % processed_tif_uuid
                    os.mkdir(out_path)
                    shutil.move(("%s.tif" % out_polarisation),
                                ("%s%s.tif" % (out_path, out_polarisation)))

                    del out_feature["properties"][SENTINEL1_L1C_GRD]

                    set_capability(out_feature,
                                   SNAP_POLARIMETRIC,
                                   processed_tif_uuid+".tif")

                    results.append(out_feature)

            except WrongPolarizationError:
                continue

        Path(__file__).parent.joinpath("template/snap_polarimetry_graph_%s.xml" % "copy").unlink()
        return FeatureCollection(results), out_path, processed_graphs
コード例 #3
0
    def process(self, input_fc: FeatureCollection):
        """
        Main wrapper method to facilitate snap processing per feature
        """
        polarisations: List = self.params.polarisations or ["VV"]

        self.assert_input_params()

        results: List[Feature] = []
        out_dict: dict = {}
        for in_feature in input_fc.get("features"):
            coordinate = in_feature["bbox"]
            self.assert_dem(coordinate)
            try:
                processed_graphs = self.process_snap(in_feature, polarisations)
                LOGGER.info("SNAP processing is finished!")
                if not processed_graphs:
                    LOGGER.debug("No processed images returned, will continue")
                    continue
                out_feature = copy.deepcopy(in_feature)
                processed_tif_uuid = out_feature.properties["up42.data_path"]
                out_path = f"/tmp/output/{processed_tif_uuid}/"
                if not os.path.exists(out_path):
                    os.mkdir(out_path)
                for out_polarisation in processed_graphs:
                    # Besides the path we only need to change the capabilities
                    shutil.move(
                        ("%s.tif" % out_polarisation),
                        ("%s%s.tif" %
                         (out_path, out_polarisation.split("_")[-1])),
                    )
                del out_feature["properties"]["up42.data_path"]
                set_data_path(out_feature, processed_tif_uuid + ".tif")
                results.append(out_feature)
                out_dict[processed_tif_uuid] = {
                    "id": processed_tif_uuid,
                    "z": [i.split("_")[-1] for i in processed_graphs],
                    "out_path": out_path,
                }
                Path(__file__).parent.joinpath(
                    "template/"
                    "snap_polarimetry_graph_%s.xml" % "copy").unlink()
            except WrongPolarizationError:
                LOGGER.error(
                    f"WrongPolarizationError: some or all of the polarisations "
                    f"({polarisations}) don't exist in this product "
                    f"({self.safe_file_name(in_feature),}), skipping.", )
                continue

        if not results:
            raise UP42Error(
                SupportedErrors.NO_OUTPUT_ERROR,
                "The used input parameters don't result in any output "
                "when applied to the provided input images.",
            )

        for out_id in out_dict:  # pylint: disable=consider-using-dict-items
            my_out_path = out_dict[out_id]["out_path"]
            out_id_z = out_dict[out_id]["z"]
            if self.params.mask is not None:
                self.post_process(my_out_path, out_id_z)
            self.rename_final_stack(my_out_path, out_id_z)

        result_fc = FeatureCollection(results)

        if self.params.clip_to_aoi:
            result_fc = update_extents(result_fc)

        return result_fc
コード例 #4
0
    def run():
        query: STACQuery = load_query()
        ensure_data_directories_exist()
        query.set_param_if_not_exists(
            "zoom_level", AWSAspectum.DEFAULT_ZOOM_LEVEL
        )
        output_features: List[Feature] = []

        feature_id: str = str(uuid.uuid4())
        out_path = f'/tmp/output/{feature_id}.tif'

        logger.debug(f"File output will be {out_path}")

        poly = Polygon([
            [query.bbox[0], query.bbox[1]],
            [query.bbox[2], query.bbox[1]],
            [query.bbox[2], query.bbox[3]],
            [query.bbox[0], query.bbox[3]]
        ])
        geom = json.loads(json.dumps(mapping(poly)))

        feature = Feature(
            id=feature_id,
            bbox=query.bbox,
            geometry=geom,
            properties={
                'up42.data.aoiclipped': f'{feature_id}.tif'
            }
        )

        s3 = boto3.client(
            's3',
            aws_access_key_id=AWSAspectum.AWS_ACCESS_KEY,
            aws_secret_access_key=AWSAspectum.AWS_SECRET_ACCESS_KEY,
            region_name=AWSAspectum.AWS_REGION,

        )

        try:
            response = s3.head_object(
                Bucket=AWSAspectum.BUCKET_NAME,
                Key=query.file_path
            )
            logger.debug(
                f'[FILE SIZE ON S3] - '
                f'{AWSAspectum.__convert_size(response["ContentLength"])}'
            )
            with open(out_path, 'wb') as f:
                s3.download_fileobj(
                    AWSAspectum.BUCKET_NAME, query.file_path, f)
            output_features.append(feature)
        except botocore.exceptions.ClientError as e:
            if e.response['Error']['Code'] == "404":
                logger.error("The object does not exist.")
            else:
                raise
        logger.debug(
            f'[FILE SIZE AFTER DOWNLOAD] - '
            f'{AWSAspectum.__convert_size(os.path.getsize(out_path))}'
        )
        result = FeatureCollection(list(output_features))

        logger.debug("Saving %s result features", len(result.get("features")))
        save_metadata(result)