Exemplo n.º 1
0
    def upload_ndarray(self,
                       ndarray,
                       product_id,
                       image_id,
                       proj4=None,
                       wkt_srs=None,
                       geotrans=None,
                       raster_meta=None,
                       overviews=None,
                       overview_resampler=None,
                       add_namespace=False,
                       **kwargs):
        """Upload an ndarray with georeferencing information.

        This is an asynchronous operation and you can query for the status
        using `Catalog.upload_result()` with the upload_id returned by this
        method.  The upload id is the image_id.

        :param ndarray ndarray: (Required) A numpy ndarray with image data. If you are providing a multi-band image
            it should have 3 dimensions and the 3rd dimension of the array should index the bands. The dtype of the
            ndarray must also be one of the following:
            ['uint8', 'int8', 'uint16', 'int16', 'uint32', 'int32', 'float32', 'float64']
        :param str product_id: (Required) The id of the product this image belongs to.
        :param str image_id: (Required) Resulting image's id = <product_id>:<image_id>.
        :param str proj4: (One of proj4 or wkt_srs is required) A proj4 formatted string representing the
            spatial reference system used by the image.
        :param str wkt_srs: (One of proj4 or wkt_srs is required) A well known text string representing the
            spatial reference system used by the image.
        :param list(float) geotrans: (Required) The 6 number geographic transform of the image. Maps pixel coordinates
            to coordinates in the specified spatial reference system.
        :param dict raster_meta: Metadata returned from the :meth:`descarteslabs.client.services.raster.Raster.ndarray`
            request which generated the initial data for the :param ndarray: being uploaded. Passing :param geotrans:
            and :param wkt_srs: is unnecessary in this case.
        :param list(int) overviews: a list of overview resolution magnification factors i.e [2, 4] would make two
            overviews at 2x and 4x the native resolution. Maximum number of overviews allowed is 16.
        :param str overview_resampler: Resampler algorithm to use when building overviews. Controls how pixels are
            combined to make lower res pixels in overviews. Allowed resampler algorithms are:
            ['nearest', 'average', 'gauss', 'cubic', 'cubicspline', 'lanczos', 'average_mp',
            'average_magphase', 'mode'].
        :param bool add_namespace: Add your user namespace to the product_id. *Deprecated*

        :return: The upload id.
        :rtype: str

        .. note::
            - See :meth:`Catalog.add_image` for additional kwargs.
            - Only one of `proj4` or `wkt_srs` can be provided.
        """
        if ndarray.dtype.name not in self.UPLOAD_NDARRAY_SUPPORTED_DTYPES:
            raise TypeError("{} is not in supported dtypes {}".format(
                ndarray.dtype.name, self.UPLOAD_NDARRAY_SUPPORTED_DTYPES))

        metadata = kwargs
        metadata.setdefault("process_controls",
                            {}).update({"upload_type": "ndarray"})
        if raster_meta is not None:
            geotrans = raster_meta.get("geoTransform")
            wkt_srs = raster_meta.get("coordinateSystem", {}).get("wkt")
        for arg in ["image_id", "proj4", "wkt_srs", "geotrans"]:
            if locals()[arg] is not None:
                kwargs[arg] = locals()[arg]
        for arg in ["overviews", "overview_resampler"]:
            if locals()[arg] is not None:
                metadata["process_controls"][arg] = locals()[arg]
        with NamedTemporaryFile(delete=False) as tmp:
            try:
                np.save(tmp, ndarray, allow_pickle=False)
                # From tempfile docs:
                # Whether the name can be used to open the file a second time,
                # while the named temporary file is still open, varies across
                # platforms (it can be so used on Unix; it cannot on Windows
                # NT or later)
                #
                # We close the underlying file object so _do_upload can open
                # the path again in a cross platform compatible way.
                # Cleanup is manual in the finally block.
                tmp.close()
                failed, upload_id, error = self._do_upload(
                    tmp.name,
                    product_id,
                    metadata=metadata,
                    add_namespace=add_namespace)

                if failed:
                    raise error

                return upload_id
            finally:
                os.unlink(tmp.name)
Exemplo n.º 2
0
    def upload_ndarray(self,
                       ndarray,
                       product_id,
                       image_id,
                       proj4=None,
                       wkt_srs=None,
                       geotrans=None,
                       raster_meta=None,
                       overviews=None,
                       overview_resampler=None,
                       **kwargs):
        """Upload an ndarray with georeferencing information.

        :param ndarray ndarray: (Required) A numpy ndarray with image data. If you are providing a multi-band image
            it should have 3 dimensions and the 3rd dimension of the array should index the bands. The dtype of the
            ndarray must also be one of the following:
            ['uint8', 'int8', 'uint16', 'int16', 'uint32', 'int32', 'float32', 'float64']
        :param str product_id: (Required) The id of the product this image belongs to.
        :param str image_id: (Required) Resulting image's id = <product_id>:<image_id>.
        :param str proj4: (One of proj4 or wkt_srs is required) A proj4 formatted string representing the
            spatial reference system used by the image.
        :param str wkt_srs: (One of proj4 or wkt_srs is required) A well known text string representing the
            spatial reference system used by the image.
        :param list(float) geotrans: (Required) The 6 number geographic transform of the image. Maps pixel coordinates
            to coordinates in the specified spatial reference system.
        :param dict raster_meta: Metadata returned from the :meth:`descarteslabs.client.services.raster.Raster.ndarray`
            request which generated the initial data for the :param ndarray: being uploaded. Passing :param geotrans:
            and :param wkt_srs: is unnecessary in this case.
        :param list(int) overviews: a list of overview resolution magnification factors i.e [2, 4] would make two
            overviews at 2x and 4x the native resolution. Maximum number of overviews allowed is 16.
        :param str overview_resampler: Resampler algorithm to use when building overviews. Controls how pixels are
            combined to make lower res pixels in overviews. Allowed resampler algorithms are:
            ['nearest', 'average', 'gauss', 'cubic', 'cubicspline', 'lanczos', 'average_mp',
            'average_magphase', 'mode'].

        .. note::
            - See :meth:`Catalog.add_image` for additional kwargs.
            - Only one of `proj4` or `wkt_srs` can be provided.
        """
        if ndarray.dtype.name not in self.UPLOAD_NDARRAY_SUPPORTED_DTYPES:
            raise TypeError("{} is not in supported dtypes {}".format(
                ndarray.dtype.name, self.UPLOAD_NDARRAY_SUPPORTED_DTYPES))

        metadata = kwargs
        metadata.setdefault('process_controls',
                            {}).update({'upload_type': 'ndarray'})
        if raster_meta is not None:
            geotrans = raster_meta.get('geoTransform')
            wkt_srs = raster_meta.get('coordinateSystem', {}).get('wkt')
        for arg in ['image_id', 'proj4', 'wkt_srs', 'geotrans']:
            if locals()[arg] is not None:
                kwargs[arg] = locals()[arg]
        for arg in ['overviews', 'overview_resampler']:
            if locals()[arg] is not None:
                metadata['process_controls'][arg] = locals()[arg]
        with NamedTemporaryFile() as tmp:
            np.save(tmp, ndarray, allow_pickle=False)
            # From tempfile docs:
            # Whether the name can be used to open the file a second time, while
            # the named temporary file is still open, varies across platforms
            # (it can be so used on Unix; it cannot on Windows NT or later)
            #
            # We close the underlying file object so _do_upload can open the path again
            # in a cross platform compatible way.
            # When leaving the context manager the tempfile wrapper will still cleanup
            # and unlink the file descriptor.
            tmp.file.close()
            upload = self._do_upload(tmp.name, product_id, metadata=metadata)
            if upload[0]:
                raise upload[2]