def _inspect_data(self):
        """
        Extract coordinate reference system and min/max values from a
        GDAL supported raster data file.
        """

        if self._data is None:
            # TODO: This method is really ugly, but is nice for testing
            self._data = {}
            df = gdal.Open(self.filename)
            # TODO: df may be None?
            crs = df.GetProjection()
            if crs:
                spref = SpatialReference(wkt=crs)
                # default to epsg:4326
                auth = spref.GetAuthorityName(None) or 'epsg'
                code = spref.GetAuthorityCode(None) or '4326'
                self._data['crs'] = "%s:%s" % (auth.lower(), code)
                # LOG.info('Detected CRS: %s', self._data['crs'])
            else:
                self._data['crs'] = 'epsg:4326'  # use epsg:4326 as default
            band = df.GetRasterBand(1)
            self._data['min'], self._data['max'], _, _ = band.GetStatistics(
                True, False)
            self._data['nodata'] = band.GetNoDataValue()
            self._data['datatype'] = band.DataType
Exemple #2
0
def get_identifier(crs):
    """
    Given a CRS, generate a unique idenfier for it. Eg: "EPSG:2193"
    """
    if isinstance(crs, str):
        crs = SpatialReference(crs)
    if isinstance(crs, SpatialReference):
        auth_name = crs.GetAuthorityName(None)
        auth_code = crs.GetAuthorityCode(None)
        return f"{auth_name}:{auth_code}"
    raise RuntimeError(f"Unrecognised CRS: {crs}")
Exemple #3
0
def test_parse():
    assert crs_util.parse_name(TEST_WKT) == "WGS 84"
    assert crs_util.parse_authority(TEST_WKT) == ("EPSG", "4326")

    assert crs_util.parse_name(AXIS_LAST_WKT) == "WGS 84"
    assert crs_util.parse_authority(AXIS_LAST_WKT) == ("EPSG", "4326")

    # Strangely this doesn't entirely work using osgeo:
    spatial_ref = SpatialReference(TEST_WKT)
    assert spatial_ref.GetName() == "WGS 84"
    assert spatial_ref.GetAuthorityName(None) is None
    assert spatial_ref.GetAuthorityCode(None) is None
Exemple #4
0
def get_identifier_int(crs):
    """
    Given a CRS, generate a stable, unique identifer for it of type 'int'. Eg: 2193
    """
    if isinstance(crs, str):
        crs = SpatialReference(crs)
    if not isinstance(crs, SpatialReference):
        raise RuntimeError(f"Unrecognised CRS: {crs}")
    auth_code = crs.GetAuthorityCode(None)
    if auth_code and auth_code.isdigit() and int(auth_code) > 0:
        return int(auth_code)
    # Stable code that fits easily in an int32 and won't collide with EPSG codes.
    return (hash(crs.ExportToWkt()) & 0xFFFFFFF) + 1000000
Exemple #5
0
def wkt_to_gpkg_spatial_ref_sys(wkt):
    """Given a WKT crs definition, generate a gpkg_spatial_ref_sys meta item."""
    # TODO: Better support for custom WKT. https://github.com/koordinates/sno/issues/148
    spatial_ref = SpatialReference(wkt)
    spatial_ref.AutoIdentifyEPSG()
    organization = spatial_ref.GetAuthorityName(None) or "NONE"
    srs_id = spatial_ref.GetAuthorityCode(None) or 0
    return [{
        "srs_name": spatial_ref.GetName(),
        "definition": wkt,
        "organization": organization,
        "srs_id": srs_id,
        "organization_coordsys_id": srs_id,
        "description": None,
    }]
Exemple #6
0
def get_identifier_str(crs):
    """
    Given a CRS, generate a stable, unique identifier for it of type 'str'. Eg: "EPSG:2193"
    """
    if isinstance(crs, str):
        crs = SpatialReference(crs)
    if not isinstance(crs, SpatialReference):
        raise RuntimeError(f"Unrecognised CRS: {crs}")
    auth_name = crs.GetAuthorityName(None)
    auth_code = crs.GetAuthorityCode(None)
    if auth_name and auth_code:
        return f"{auth_name}:{auth_code}"
    code = auth_name or auth_code
    if code and code.strip() not in ("0", "EPSG"):
        return code
    return f"CUSTOM:{get_identifier_int(crs)}"