Ejemplo n.º 1
0
    def __init__(self, service_cfg, refresh=False):
        if not self.initialised or refresh:
            self.initialised = True

            self.wms = service_cfg.get("wms", True)
            self.wcs = service_cfg.get("wcs", False)

            self.title = service_cfg["title"]
            self.url = service_cfg["url"]
            if not self.url.startswith("http"):
                raise Exception(
                    "URL in service_cfg does not start with http or https.")
            self.published_CRSs = {}
            for crs_str, crsdef in service_cfg["published_CRSs"].items():
                self.published_CRSs[crs_str] = {
                    "geographic":
                    crsdef["geographic"],
                    "horizontal_coord":
                    crsdef.get("horizontal_coord", "longitude"),
                    "vertical_coord":
                    crsdef.get("vertical_coord", "latitude"),
                    "vertical_coord_first":
                    crsdef.get("vertical_coord_first", False),
                }
                if self.published_CRSs[crs_str]["geographic"]:
                    if self.published_CRSs[crs_str][
                            "horizontal_coord"] != "longitude":
                        raise Exception(
                            "Published CRS {} is geographic but has a horizontal coordinate that is not 'longitude'"
                            .format(crs_str))
                    if self.published_CRSs[crs_str][
                            "vertical_coord"] != "latitude":
                        raise Exception(
                            "Published CRS {} is geographic but has a vertical coordinate that is not 'latitude'"
                            .format(crs_str))

            if self.wcs:
                self.default_geographic_CRS = service_cfg[
                    "default_geographic_CRS"]
                if self.default_geographic_CRS not in self.published_CRSs:
                    raise Exception(
                        "Configured default geographic CRS not listed in published CRSs."
                    )
                if not self.published_CRSs[
                        self.default_geographic_CRS]["geographic"]:
                    raise Exception(
                        "Configured default geographic CRS not listed in published CRSs as geographic."
                    )
                self.default_geographic_CRS_def = self.published_CRSs[
                    self.default_geographic_CRS]
                self.wcs_formats = {}
                for fmt_name, fmt in service_cfg["wcs_formats"].items():
                    self.wcs_formats[fmt_name] = {
                        "mime": fmt["mime"],
                        "extension": fmt["extension"],
                        "multi-time": fmt["multi-time"],
                        "name": fmt_name,
                    }
                    rpath = fmt["renderer"]
                    mod, func = rpath.rsplit(".", 1)
                    _tmp = __import__(mod, globals(), locals(), [func], 0)
                    self.wcs_formats[fmt_name]["renderer"] = getattr(
                        _tmp, func)
                if not self.wcs_formats:
                    raise Exception(
                        "Must configure at least one wcs format to support WCS."
                    )

                self.native_wcs_format = service_cfg["native_wcs_format"]
                if self.native_wcs_format not in self.wcs_formats:
                    raise Exception(
                        "Configured native WCS format not a supported format.")
            else:
                self.default_geographic_CRS = None
                self.default_geographic_CRS_def = {}
                self.wcs_formats = {}
                self.native_wcs_format = None

            # WMS specific config
            self.layer_limit = 1
            self.max_width = service_cfg.get("max_width", 256)
            self.max_height = service_cfg.get("max_height", 256)

            self.abstract = service_cfg.get("abstract")
            self.keywords = service_cfg.get("keywords", [])
            self.contact_info = service_cfg.get("contact_info", {})
            self.fees = service_cfg.get("fees", "")
            self.access_constraints = service_cfg.get("access_constraints", "")
Ejemplo n.º 2
0
def get_gdal_opts():
    gdal_opts = service_cfg.get("gdal_opts", dict())
    return {**DEFAULT_GDAL_OPTS, **gdal_opts}
Ejemplo n.º 3
0
def determine_product_ranges(dc, product_name, time_offset, extractor):
    # pylint: disable=too-many-locals, too-many-branches, too-many-statements, protected-access
    start = datetime.now()
    product = dc.index.products.get_by_name(product_name)
    print("Product: ", product_name)
    if product is None:
        raise Exception("Product does not exist")
    r = {
        "product_id": product.id,
        "lat": {
            "min": None,
            "max": None
        },
        "lon": {
            "min": None,
            "max": None
        },
    }
    sub_r = {}
    time_set = set()

    crsids = service_cfg["published_CRSs"]
    calculate_extent = not service_cfg.get("use_default_extent", False)
    extents = {crsid: None for crsid in crsids}
    crses = {crsid: datacube.utils.geometry.CRS(crsid) for crsid in crsids}
    ds_count = 0
    for ds in dc.find_datasets(product=product_name):
        dt = ds.center_time + timedelta(hours=time_offset)
        time_set.add(dt.date())
        if calculate_extent or extractor is not None:
            if extractor is not None:
                path = extractor(ds)
                if path not in sub_r:
                    sub_r[path] = {
                        "product_id": product.id,
                        "sub_id": path,
                        "lat": {
                            "min": None,
                            "max": None,
                        },
                        "lon": {
                            "min": None,
                            "max": None,
                        },
                        "time_set": set(),
                        "extents": {crsid: None
                                    for crsid in crsids}
                    }
                sub_r[path]["lat"]["min"] = accum_min(
                    sub_r[path]["lat"]["min"], ds.metadata.lat.begin)
                sub_r[path]["lat"]["max"] = accum_max(
                    sub_r[path]["lat"]["max"], ds.metadata.lat.end)
                sub_r[path]["lon"]["min"] = accum_min(
                    sub_r[path]["lon"]["min"], ds.metadata.lon.begin)
                sub_r[path]["lon"]["max"] = accum_max(
                    sub_r[path]["lon"]["max"], ds.metadata.lon.end)
            else:
                path = None

            r["lat"]["min"] = accum_min(r["lat"]["min"], ds.metadata.lat.begin)
            r["lat"]["max"] = accum_max(r["lat"]["max"], ds.metadata.lat.end)
            r["lon"]["min"] = accum_min(r["lon"]["min"], ds.metadata.lon.begin)
            r["lon"]["max"] = accum_max(r["lon"]["max"], ds.metadata.lon.end)

            if path is not None:
                sub_r[path]["time_set"].add(dt.date())

            for crsid in crsids:
                crs = crses[crsid]
                ext = ds.extent
                if ext.crs != crs:
                    ext = ext.to_crs(crs)
                cvx_ext = ext.convex_hull
                if cvx_ext != ext:
                    print("INFO: Dataset", ds.id, "CRS", crsid,
                          "extent is not convex.")
                if extents[crsid] is None:
                    extents[crsid] = cvx_ext
                else:
                    if not extents[crsid].is_valid:
                        print("WARNING: Extent Union for", ds.id, "CRS", crsid,
                              "is not valid")
                    if not cvx_ext.is_valid:
                        print("WARNING: Extent for CRS", crsid, "is not valid")
                    union = extents[crsid].union(cvx_ext)
                    if union._geom is not None:
                        extents[crsid] = union
                    else:
                        print("WARNING: Dataset", ds.id, "CRS", crsid,
                              "union topology exception, ignoring union")
                if path is not None:
                    if sub_r[path]["extents"][crsid] is None:
                        sub_r[path]["extents"][crsid] = cvx_ext
                    else:
                        sub_r[path]["extents"][crsid] = sub_r[path]["extents"][
                            crsid].union(cvx_ext)
        ds_count += 1

    # Default extent usage
    if not calculate_extent and ds_count > 0:
        for crsid in crsids:
            crs = crses[crsid]
            default = datacube.utils.geometry.Geometry(DEFAULT_GEOJSON,
                                                       crs=DEFAULT_GEOJSON_CRS)
            extents[crsid] = default.to_crs(crs)

    r["times"] = sorted(time_set)
    r["time_set"] = time_set
    r["bboxes"] = {crsid: extents[crsid].boundingbox for crsid in crsids}
    if extractor is not None:
        for path in sub_r.keys():
            sub_r[path]["times"] = sorted(sub_r[path]["time_set"])
            sub_r[path]["bboxes"] = {
                crsid: sub_r[path]["extents"][crsid].boundingbox
                for crsid in crsids
            }
            del sub_r[path]["extents"]
        r["sub_products"] = sub_r
    end = datetime.now()
    print("Scanned %d datasets in %d seconds" % (ds_count,
                                                 (end - start).seconds))
    return r
Ejemplo n.º 4
0
def preauthenticate_s3():
    return service_cfg.get("preauthenticate_s3", False)
Ejemplo n.º 5
0
def get_gdal_opts():
    gdal_opts_cfg = service_cfg.get("gdal_opts", dict())
    return s3_gdal_opts(max_header_sz_kb=32,
                        GDAL_GEOREF_SOURCES='INTERNAL',
                        **gdal_opts_cfg)