How to use the rasterio.crs.CRS.from_string function in rasterio

To help you get started, we’ve selected a few rasterio examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mapbox / rasterio / tests / test_crs.py View on Github external
def test_is_geographic_from_string():
    wgs84_crs = CRS.from_string('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
    assert wgs84_crs.is_geographic is True

    nad27_crs = CRS.from_string('+proj=longlat +ellps=clrk66 +datum=NAD27 +no_defs')
    assert nad27_crs.is_geographic is True

    lcc_crs = CRS.from_string('+lon_0=-95 +ellps=GRS80 +y_0=0 +no_defs=True +proj=lcc +x_0=0 +units=m +lat_2=77 +lat_1=49 +lat_0=0')
    assert lcc_crs.is_geographic is False
github mapbox / rasterio / tests / test_crs.py View on Github external
def test_crs_OSR_equivalence():
    crs1 = CRS.from_string('+proj=longlat +datum=WGS84 +no_defs')
    crs2 = CRS.from_string('+proj=latlong +datum=WGS84 +no_defs')
    crs3 = CRS({'init': 'EPSG:4326'})
    assert crs1 == crs2
    assert crs1 == crs3
github mapbox / rasterio / tests / test_crs.py View on Github external
def test_from_string():
    wgs84_crs = CRS.from_string('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
    assert wgs84_crs.to_dict() == {'init': 'epsg:4326'}

    # Make sure this doesn't get handled using the from_epsg() even though 'epsg' is in the string
    epsg_init_crs = CRS.from_string('+units=m +init=epsg:26911 +no_defs=True')
    assert epsg_init_crs.to_dict() == {'init': 'epsg:26911'}
github mapbox / rasterio / tests / test_warpedvrt.py View on Github external
def test_warped_vrt_nondefault_nodata(path_rgb_byte_tif):
    """A VirtualVRT has expected nondefault nodata values."""
    with rasterio.open(path_rgb_byte_tif) as src:
        vrt = WarpedVRT(src, crs=DST_CRS, src_nodata=None, nodata=None)
        assert vrt.dst_crs == CRS.from_string(DST_CRS)
        assert vrt.src_nodata is None
        assert vrt.dst_nodata is None
        assert vrt.tolerance == 0.125
        assert vrt.resampling == Resampling.nearest
        assert vrt.warp_extras == {"init_dest": "NO_DATA"}
        assert vrt.mask_flag_enums == ([MaskFlags.all_valid],) * 3
github mapbox / rasterio / tests / test_crs.py View on Github external
def test_is_same_crs():
    crs1 = CRS({'init': 'EPSG:4326'})
    crs2 = CRS({'init': 'EPSG:3857'})

    assert crs1 == crs1
    assert crs1 != crs2

    wgs84_crs = CRS.from_string('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
    assert crs1 == wgs84_crs

    # Make sure that same projection with different parameter are not equal
    lcc_crs1 = CRS.from_string('+lon_0=-95 +ellps=GRS80 +y_0=0 +no_defs=True +proj=lcc +x_0=0 +units=m +lat_2=77 +lat_1=49 +lat_0=0')
    lcc_crs2 = CRS.from_string('+lon_0=-95 +ellps=GRS80 +y_0=0 +no_defs=True +proj=lcc +x_0=0 +units=m +lat_2=77 +lat_1=45 +lat_0=0')
    assert lcc_crs1 != lcc_crs2
github mapbox / rasterio / tests / test_crs.py View on Github external
def test_is_projected():
    assert CRS({'init': 'epsg:3857'}).is_projected is True

    lcc_crs = CRS.from_string('+lon_0=-95 +ellps=GRS80 +y_0=0 +no_defs=True +proj=lcc +x_0=0 +units=m +lat_2=77 +lat_1=49 +lat_0=0')
    assert CRS(lcc_crs).is_projected is True

    wgs84_crs = CRS.from_string('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
    assert CRS(wgs84_crs).is_projected is False
github ceholden / yatsm / yatsm / gis / tilespec.py View on Github external
""" Retrieve default tile specifications packaged within ``tilezilla``

    Args:
        filename (str): Optionally, override default data location

    Returns:
        dict: default tilespecs packaged within ``tilezilla`` as TileSpec
            objects
    """
    filename = filename or Path(__file__).parent.joinpath('tile_specs.json')

    with open(str(filename), 'r') as f:
        tilespecs = json.load(f)

    for key in tilespecs:
        tilespecs[key]['crs'] = CRS.from_string(tilespecs[key]['crs'])
        tilespecs[key] = TileSpec(desc=key, **tilespecs[key])
    return tilespecs
github ungarj / mapchete / mapchete / formats / __init__.py View on Github external
def read_output_metadata(metadata_json):
    params = read_json(metadata_json)
    grid = params["pyramid"]["grid"]
    if grid["type"] == "geodetic" and grid["shape"] == [2, 1]:  # pragma: no cover
        warnings.warn(
            DeprecationWarning(
                "Deprecated grid shape ordering found. "
                "Please change grid shape from [2, 1] to [1, 2] in %s."
                % metadata_json
            )
        )
        params["pyramid"]["grid"]["shape"] = [1, 2]
    if "crs" in grid and isinstance(grid["crs"], str):
        crs = CRS.from_string(grid["crs"])
        warnings.warn(
            DeprecationWarning(
                "Deprecated 'srs' found in %s: '%s'. "
                "Use WKT representation instead: %s" % (
                    metadata_json, grid["crs"], pformat(dict(wkt=crs.to_wkt()))
                )
            )
        )
        params["pyramid"]["grid"].update(srs=dict(wkt=crs.to_wkt()))
    params.update(
        pyramid=BufferedTilePyramid(
            params["pyramid"]["grid"],
            metatiling=params["pyramid"].get("metatiling", 1),
            tile_size=params["pyramid"].get("tile_size", 256),
            pixelbuffer=params["pyramid"].get("pixelbuffer", 0)
        )
github CosmiQ / cw-geodata / cw_geodata / geo_utils / core.py View on Github external
return Affine(obj)
        elif obj_type == 'Affine':
            return obj
        elif obj_type == 'GeoTIFF':
            return rasterio.open(obj).crs
        elif obj_type == 'GeoDataFrame':
            if isinstance(obj, str):  # if it's a path to a gdf
                return gpd.read_file(obj).crs
            else:  # assume it's a GeoDataFrame object
                return obj.crs
        elif obj_type == 'epsg string':
            if obj.startswith('{init'):
                return rasterio.crs.CRS.from_string(
                    obj.lstrip('{init: ').rstrip('}'))
            elif obj.lower().startswith('epsg'):
                return rasterio.crs.CRS.from_string(obj)
        elif obj_type == 'OGR Geometry':
            return get_crs_from_ogr(obj)
        elif obj_type == 'shapely Geometry':
            raise TypeError('Cannot extract a coordinate system from a ' +
                            'shapely.Geometry')
        else:
            raise TypeError('Cannot extract CRS from this object type.')
github GeoscienceAustralia / wagl / wagl / data.py View on Github external
:return:
        A NumPy array containing the reprojected result.
    """

    if not isinstance(dst_geobox, GriddedGeoBox):
        msg = 'dst_geobox must be an instance of a GriddedGeoBox! Type: {}'
        msg = msg.format(type(dst_geobox))
        raise TypeError(msg)

    if not isinstance(src_geobox, GriddedGeoBox):
        msg = 'src_geobox must be an instance of a GriddedGeoBox! Type: {}'
        msg = msg.format(type(src_geobox))
        raise TypeError(msg)

    # Get the source and destination projections in Proj4 styled dicts
    src_prj = CRS.from_string(src_geobox.crs.ExportToProj4())
    dst_prj = CRS.from_string(dst_geobox.crs.ExportToProj4())

    # Get the source and destination transforms
    src_trans = src_geobox.transform
    dst_trans = dst_geobox.transform

    # Define the output NumPy array
    dst_arr = np.zeros(dst_geobox.shape, dtype=src_img.dtype)

    reproject(src_img, dst_arr, src_transform=src_trans,
              src_crs=src_prj, dst_transform=dst_trans, dst_crs=dst_prj,
              resampling=resampling)

    return dst_arr