Example #1
0
    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
Example #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}")
Example #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
Example #4
0
def wkt_to_gpkg_spatial_ref_sys(wkt):
    """Given a WKT crs definition, generate a gpkg_spatial_ref_sys meta item."""
    spatial_ref = SpatialReference(wkt)
    organization = spatial_ref.GetAuthorityName(None) or "NONE"
    srs_id = crs_util.get_identifier_int(spatial_ref)
    return [{
        "srs_name": spatial_ref.GetName(),
        "definition": wkt,
        "organization": organization,
        "srs_id": srs_id,
        "organization_coordsys_id": srs_id,
        "description": None,
    }]
Example #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,
    }]
Example #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)}"
Example #7
0
 def generate_postgis_spatial_ref_sys(cls, v2_obj):
     """
     Generates the contents of the spatial_ref_sys table from the v2 object.
     The result is a list containing a dict per table row.
     Each dict has the format {column-name: value}.
     """
     result = []
     for crs_name, definition in v2_obj.crs_definitions().items():
         spatial_ref = SpatialReference(definition)
         auth_name = spatial_ref.GetAuthorityName(None) or "NONE"
         crs_id = crs_util.get_identifier_int(spatial_ref)
         result.append({
             "srid": crs_id,
             "auth_name": auth_name,
             "auth_srid": crs_id,
             "srtext": definition,
             "proj4text": spatial_ref.ExportToProj4(),
         })
     return result