def __init__(self, uri):
        try:
            if uri.startswith("s3://"):
                url = urlparse(uri)
                obj = S3.get_object(Bucket=url.netloc, Key=url.path[1:])
                oin_meta = json.loads(obj["Body"].read().decode("utf-8"))
            elif uri.startswith(("http://", "https://")):
                oin_meta = requests.get(uri).json()
            else:
                raise NoCatalogAvailable()
        except Exception:
            raise NoCatalogAvailable()

        self._meta = oin_meta
        self._metadata_url = uri
        self._name = oin_meta.get("title")
        self._provider = oin_meta.get("provider")
        self._source = oin_meta.get("uuid")

        with get_source(self._source) as src:
            self._bounds = warp.transform_bounds(src.crs, WGS84_CRS,
                                                 *src.bounds)
            self._resolution = get_resolution_in_meters(
                Bounds(src.bounds, src.crs), (src.height, src.width))
            approximate_zoom = get_zoom(max(self._resolution), op=math.ceil)

            if src.meta["dtype"] != "uint8":
                global_min = src.get_tag_item("TIFFTAG_MINSAMPLEVALUE")
                global_max = src.get_tag_item("TIFFTAG_MAXSAMPLEVALUE")

                for band in range(0, src.count):
                    self._meta["values"] = self._meta.get("values", {})
                    self._meta["values"][band] = {}
                    min_val = src.get_tag_item("STATISTICS_MINIMUM",
                                               bidx=band + 1)
                    max_val = src.get_tag_item("STATISTICS_MAXIMUM",
                                               bidx=band + 1)
                    mean_val = src.get_tag_item("STATISTICS_MEAN",
                                                bidx=band + 1)

                    if min_val is not None:
                        self._meta["values"][band]["min"] = float(min_val)
                    elif global_min is not None:
                        self._meta["values"][band]["min"] = float(global_min)

                    if max_val is not None:
                        self._meta["values"][band]["max"] = float(max_val)
                    elif global_max is not None:
                        self._meta["values"][band]["max"] = float(global_max)

                    if mean_val is not None:
                        self._meta["values"][band]["mean"] = float(mean_val)

        self._center = [
            (self._bounds[0] + self.bounds[2]) / 2,
            (self._bounds[1] + self.bounds[3]) / 2,
            approximate_zoom - 3,
        ]
        self._maxzoom = approximate_zoom + 3
        self._minzoom = approximate_zoom - 10
Beispiel #2
0
    def __init__(self, uri):
        rsp = requests.get(uri)

        if not rsp.ok:
            raise NoDataAvailable()

        oin_meta = rsp.json()
        self._meta = oin_meta
        self._metadata_url = uri
        self._name = oin_meta.get('title')
        self._provider = oin_meta.get('provider')
        self._source = oin_meta.get('uuid')

        with get_source(self._source) as src:
            self._bounds = warp.transform_bounds(src.crs, WGS84_CRS,
                                                 *src.bounds)
            self._resolution = get_resolution_in_meters(
                Bounds(src.bounds, src.crs), (src.height, src.width))
            approximate_zoom = get_zoom(max(self._resolution), op=math.ceil)

        self._center = [(self._bounds[0] + self.bounds[2]) / 2,
                        (self._bounds[1] + self.bounds[3]) / 2,
                        approximate_zoom - 3]
        self._maxzoom = approximate_zoom + 3
        self._minzoom = approximate_zoom - 10
    def __init__(self, uri, rgb=None, nodata=None, linear_stretch=None, resample=None):
        self._uri = uri

        if rgb:
            self._rgb = rgb

        if nodata:
            self._nodata = nodata

        if linear_stretch:
            self._linear_stretch = linear_stretch

        try:
            # test whether provided resampling method is valid
            Resampling[resample]
            self._resample = resample
        except KeyError:
            self._resample = None

        self._meta = {}

        with get_source(self._uri) as src:
            self._bounds = warp.transform_bounds(src.crs, WGS84_CRS, *src.bounds)
            self._resolution = get_resolution_in_meters(
                Bounds(src.bounds, src.crs), (src.height, src.width)
            )
            approximate_zoom = get_zoom(max(self._resolution), op=math.ceil)

            global_min = src.get_tag_item("TIFFTAG_MINSAMPLEVALUE")
            global_max = src.get_tag_item("TIFFTAG_MAXSAMPLEVALUE")

            for band in range(0, src.count):
                self._meta["values"] = self._meta.get("values", {})
                self._meta["values"][band] = {}
                min_val = src.get_tag_item("STATISTICS_MINIMUM", bidx=band + 1)
                max_val = src.get_tag_item("STATISTICS_MAXIMUM", bidx=band + 1)
                mean_val = src.get_tag_item("STATISTICS_MEAN", bidx=band + 1)

                if min_val is not None:
                    self._meta["values"][band]["min"] = float(min_val)
                elif global_min is not None:
                    self._meta["values"][band]["min"] = float(global_min)

                if max_val is not None:
                    self._meta["values"][band]["max"] = float(max_val)
                elif global_max is not None:
                    self._meta["values"][band]["max"] = float(global_max)

                if mean_val is not None:
                    self._meta["values"][band]["mean"] = float(mean_val)

        self._center = [
            (self._bounds[0] + self.bounds[2]) / 2,
            (self._bounds[1] + self.bounds[3]) / 2,
            approximate_zoom - 3,
        ]
        self._maxzoom = approximate_zoom + 3
        self._minzoom = approximate_zoom - 10
Beispiel #4
0
 def add_pixels_to_asset(asset):
     try:
         url = asset['url']
         with Timer(f'reading pixels for {url}'):
             source = Source(url=url, name=url, resolution=None)
             with get_source(url) as src:
                 with Timer(f'reading window for {url}'):
                     asset['pixels'] = read_window(
                         src, canvas_bounds, shape, source)
     except Exception as e:
         LOG.error(e)
         raise e
Beispiel #5
0
    def __init__(self, uri, rgb=None, nodata=None, linear_stretch=None, resample=None, 
        dst_max=None, dst_min=None, force_cast=None, to_vis=None):
        self._uri = uri
        self._rgb = rgb
        self._nodata = nodata
        self._linear_stretch = linear_stretch
        self._dst_min = dst_min
        self._dst_max = dst_max
        self._force_cast = force_cast
        self._to_vis = to_vis
        try:
            # test whether provided resampling method is valid
            Resampling[resample]
            self._resample = resample
        except KeyError:
            self._resample = None
        self._meta = {}
        self.src_meta = {}

        with get_source(self._uri) as src:
            self.src_meta = snake_case_to_camel_case_keys_of_dict(src.tags())
            self.src_meta["bandCount"] = src.count
            self._bounds = warp.transform_bounds(src.crs, WGS84_CRS, *src.bounds)
            self._resolution = get_resolution_in_meters(
                Bounds(src.bounds, src.crs), (src.height, src.width)
            )
            approximate_zoom = get_zoom(max(self._resolution), op=math.ceil)
            
            global_min = src.get_tag_item("TIFFTAG_MINSAMPLEVALUE")
            global_max = src.get_tag_item("TIFFTAG_MAXSAMPLEVALUE")

            band_order = src.get_tag_item("BAND_ORDER")
            if band_order is not None:
                band_order = band_order.split(',')
            if str(self._rgb).lower() == "metadata":
                if band_order is not None:
                    def get_band_from_band_order(band_order, band_name, fallback):
                        if band_name in band_order:
                            return str(band_order.index(band_name) + 1)
                        else:
                            return fallback
                    red_band = get_band_from_band_order(band_order, "RED", "1")
                    green_band = get_band_from_band_order(band_order, "GRE", "2")
                    blue_band = get_band_from_band_order(band_order, "BLU", "3")
                    self._rgb = ",".join([red_band, green_band, blue_band])
                else:
                    # Fallback
                    if src.count >= 3:
                        self._rgb = "1,2,3"
                    else:
                        self._rgb = "1,1,1"
            
            self.src_meta["bandMetadata"] = {}
            # FarmLens specific
            band_assignments = band_order
            if band_order is None:
                band_assignments = range(0, src.count)
            for band in xrange(0, src.count):
                self.src_meta["bandMetadata"][band_assignments[band]] = snake_case_to_camel_case_keys_of_dict(src.tags(bidx=band+1))
                self._meta["values"] = self._meta.get("values", {})
                self._meta["values"][band] = {}
                min_val = src.get_tag_item("STATISTICS_MINIMUM", bidx=band + 1)
                max_val = src.get_tag_item("STATISTICS_MAXIMUM", bidx=band + 1)
                mean_val = src.get_tag_item("STATISTICS_MEAN", bidx=band + 1)
                stddev_val = src.get_tag_item("STATISTICS_STDDEV", bidx=band + 1)
                
                if min_val is not None:
                    self._meta["values"][band]["min"] = float(min_val)
                elif global_min is not None:
                    self._meta["values"][band]["min"] = float(global_min)

                if max_val is not None:
                    self._meta["values"][band]["max"] = float(max_val)
                elif global_max is not None:
                    self._meta["values"][band]["max"] = float(global_max)

                if mean_val is not None:
                    self._meta["values"][band]["mean"] = float(mean_val)

                if stddev_val is not None:
                    self._meta["values"][band]["stddev"] = float(stddev_val)

        self._center = [
            (self._bounds[0] + self.bounds[2]) / 2,
            (self._bounds[1] + self.bounds[3]) / 2,
            approximate_zoom - 3,
        ]
        self._maxzoom = approximate_zoom + 3
        self._minzoom = approximate_zoom - 10