コード例 #1
0
def test_crs_infer_pass():
    assert infer_crs(TEST_CRS_RAW) == "epsg:4283"
コード例 #2
0
def get_dataset_srid_alchemy_expression(md: MetadataType,
                                        default_crs: str = None):
    doc = md.dataset_fields["metadata_doc"].alchemy_expression

    if "grid_spatial" not in md.definition["dataset"]:
        # Non-spatial product
        return None

    projection_offset = md.definition["dataset"]["grid_spatial"]

    if expects_eo3_metadata_type(md):
        spatial_ref = doc[["crs"]].astext
    else:
        # Most have a spatial_reference field we can use directly.
        spatial_ref = doc[projection_offset + ["spatial_reference"]].astext

    # When datasets have no CRS, optionally use this as default.
    default_crs_expression = None
    if default_crs:
        if not default_crs.lower().startswith(
                "epsg:") and not default_crs.lower().startswith("esri:"):
            # HACK: Change default CRS with inference
            inferred_crs = infer_crs(default_crs)
            if inferred_crs is None:
                raise UnsupportedWKTProductCRS(
                    f"WKT Product CRSes are not currently well supported, and "
                    f"we can't infer this product's one. "
                    f"(Ideally use an auth-name format for CRS, such as 'EPSG:1234') "
                    f"Got: {default_crs!r}")
            default_crs = inferred_crs

        auth_name, auth_srid = default_crs.split(":")
        default_crs_expression = (select([
            SPATIAL_REF_SYS.c.srid
        ]).where(func.lower(SPATIAL_REF_SYS.c.auth_name) == auth_name.lower(
        )).where(SPATIAL_REF_SYS.c.auth_srid == int(auth_srid)).as_scalar())

    expression = func.coalesce(
        case(
            [(
                # If matches shorthand code: eg. "epsg:1234"
                spatial_ref.op("~")(r"^[A-Za-z0-9]+:[0-9]+$"),
                select([SPATIAL_REF_SYS.c.srid]).where(
                    func.lower(SPATIAL_REF_SYS.c.auth_name) ==
                    func.lower(func.split_part(spatial_ref, ":", 1))).where(
                        SPATIAL_REF_SYS.c.auth_srid == func.split_part(
                            spatial_ref, ":", 2).cast(Integer)).as_scalar(),
            )],
            else_=None,
        ),
        case(
            [(
                # Plain WKT that ends in an authority code.
                # Extract the authority name and code using regexp. Yuck!
                # Eg: ".... AUTHORITY["EPSG","32756"]]"
                spatial_ref.op("~")
                (r'AUTHORITY\["[a-zA-Z0-9]+", *"[0-9]+"\]\]$'),
                select([SPATIAL_REF_SYS.c.srid]).where(
                    func.lower(SPATIAL_REF_SYS.c.auth_name) == func.lower(
                        func.substring(
                            spatial_ref,
                            r'AUTHORITY\["([a-zA-Z0-9]+)", *"[0-9]+"\]\]$',
                        ))
                ).where(SPATIAL_REF_SYS.c.auth_srid == func.substring(
                    spatial_ref, r'AUTHORITY\["[a-zA-Z0-9]+", *"([0-9]+)"\]\]$'
                ).cast(Integer)).as_scalar(),
            )],
            else_=None,
        ),
        # Some older datasets have datum/zone fields instead.
        # The only remaining ones in DEA are 'GDA94'.
        case(
            [(
                doc[(projection_offset + ["datum"])].astext == "GDA94",
                select([SPATIAL_REF_SYS.c.srid]).where(
                    func.lower(SPATIAL_REF_SYS.c.auth_name) == "epsg").where(
                        SPATIAL_REF_SYS.c.auth_srid == (
                            "283" +
                            func.abs(doc[(projection_offset +
                                          ["zone"])].astext.cast(Integer))
                        ).cast(Integer)).as_scalar(),
            )],
            else_=None,
        ),
        default_crs_expression,
        # TODO: Handle arbitrary WKT strings (?)
        # 'GEOGCS[\\"GEOCENTRIC DATUM of AUSTRALIA\\",DATUM[\\"GDA94\\",SPHEROID[
        #    \\"GRS80\\",6378137,298.257222101]],PRIMEM[\\"Greenwich\\",0],UNIT[\\
        # "degree\\",0.0174532925199433]]'
    )
    # print(as_sql(expression))
    return expression
コード例 #3
0
def test_crs_infer_fail():
    assert infer_crs("") is None