Exemple #1
0
def test_gmap_transformed():
    dem = GeoTiff(get_demo_file('hef_srtm.tif'))
    dem.set_subset(margin=-100)

    dem = mercator_grid(center_ll=(10.76, 46.798444),
                        extent=(10000, 7000))

    i, j = dem.ij_coordinates
    g = GoogleVisibleMap(x=i, y=j, crs=dem, size_x=500, size_y=400)
    img = g.get_vardata()

    m = Map(dem, countries=False)

    with pytest.raises(ValueError):
        m.set_data(img)

    m.set_lonlat_contours(interval=0.025)
    m.set_shapefile(get_demo_file('Hintereisferner.shp'),
                    linewidths=2, edgecolor='darkred')
    m.set_rgb(img, g.grid)

    fig, ax = plt.subplots(1, 1)
    m.visualize(ax=ax, addcbar=False)
    plt.tight_layout()
    return fig
Exemple #2
0
def loop(lst):

    g = GeoTiff(lst)

    ls = g.get_vardata()
    lon = g.grid.ll_coordinates[0]
    lat = g.grid.ll_coordinates[1]

    ls = np.array(ls, dtype=float)
    ls[ls == -9999] = np.nan
    year = lst[-11:-7]
    month = lst[-6:-4]

    date = pd.Timestamp(year + '-' + month)

    da = xr.DataArray(ls,
                      coords={
                          'time': date,
                          'lat': lat[:, 0],
                          'lon': lon[0, :]
                      },
                      dims=['lat', 'lon'])  # [np.newaxis, :])

    ds = xr.Dataset()
    ds['precip'] = da

    enc = {'precip': {'complevel': 5, 'zlib': True}}
    out = lst.replace('.tif', '.nc')
    ds.to_netcdf(path=out, mode='w', encoding=enc, format='NETCDF4')
Exemple #3
0
def test_gmap_transformed():
    dem = GeoTiff(get_demo_file('hef_srtm.tif'))
    dem.set_subset(margin=-100)

    dem = mercator_grid(center_ll=(10.76, 46.798444), extent=(10000, 7000))

    i, j = dem.ij_coordinates
    g = GoogleVisibleMap(x=i, y=j, crs=dem, size_x=500, size_y=400)
    img = g.get_vardata()

    m = Map(dem, countries=False)

    with pytest.raises(ValueError):
        m.set_data(img)

    m.set_lonlat_contours(interval=0.025)
    m.set_shapefile(get_demo_file('Hintereisferner.shp'),
                    linewidths=2,
                    edgecolor='darkred')
    m.set_rgb(img, g.grid)

    fig, ax = plt.subplots(1, 1)
    m.visualize(ax=ax, addcbar=False)
    plt.tight_layout()
    return fig
Exemple #4
0
def grid_tiff(file, grid):

    g = GeoTiff(file)
    # Spare memory
    ex = grid.extent_in_crs(crs=wgs84)  # l, r, b, t
    g.set_subset(corners=((ex[0], ex[2]), (ex[1], ex[3])),
                 crs=wgs84, margin=10)
    ls = g.get_vardata()
    ls_on_grid = grid.lookup_transform(ls, grid=grid)

    return ls_on_grid
Exemple #5
0
def grid_tiff(file, grid):

    g = GeoTiff(file)
    # Spare memory
    ex = grid.extent_in_crs(crs=wgs84)  # l, r, b, t
    g.set_subset(corners=((ex[0], ex[2]), (ex[1], ex[3])),
                 crs=wgs84,
                 margin=10)
    ls = g.get_vardata()
    ls_on_grid = grid.lookup_transform(ls, grid=grid)

    return ls_on_grid
Exemple #6
0
    def set_topography(self, topo=None, crs=None, relief_factor=0.7, **kwargs):
        """Add topographical shading to the map.

        Parameters
        ----------
        topo: path to a geotiff file containing the topography, OR
              2d data array
        relief_factor: how strong should the shading be?
        kwargs: any keyword accepted by salem.Grid.map_gridded_data (interp,ks)

        Returns
        -------
        the topography if needed (bonus)
        """

        if topo is None:
            self._shading_base()
            return

        kwargs.setdefault('interp', 'spline')

        if isinstance(topo, six.string_types):
            _, ext = os.path.splitext(topo)
            if ext.lower() == '.tif':
                g = GeoTiff(topo)
                # Spare memory
                ex = self.grid.extent_in_crs(crs=wgs84)  # l, r, b, t
                g.set_subset(corners=((ex[0], ex[2]), (ex[1], ex[3])),
                             crs=wgs84,
                             margin=10)
                z = g.get_vardata()
                z[z < -999] = 0
                z = self.grid.map_gridded_data(z, g.grid, **kwargs)
            else:
                raise ValueError(
                    'File extension not recognised: {}'.format(ext))
        else:
            z = self._check_data(topo, crs=crs, **kwargs)

        # Gradient in m m-1
        ddx = self.grid.dx
        ddy = self.grid.dy
        if self.grid.proj.is_latlong():
            # we make a coarse approx of the avg dx on a sphere
            _, lat = self.grid.ll_coordinates
            ddx = np.mean(ddx * 111200 * np.cos(lat * np.pi / 180))
            ddy *= 111200

        dy, dx = np.gradient(z, ddy, ddx)
        self._shading_base(dx - dy, relief_factor=relief_factor)
        return z
Exemple #7
0
    def set_topography(self, topo=None, crs=None, relief_factor=0.7, **kwargs):
        """Add topographical shading to the map.

        Parameters
        ----------
        topo: path to a geotiff file containing the topography, OR
              2d data array
        relief_factor: how strong should the shading be?
        kwargs: any keyword accepted by salem.Grid.map_gridded_data (interp,ks)

        Returns
        -------
        the topography if needed (bonus)
        """

        if topo is None:
            self._shading_base()
            return

        kwargs.setdefault('interp', 'spline')

        if isinstance(topo, six.string_types):
            _, ext = os.path.splitext(topo)
            if ext.lower() == '.tif':
                g = GeoTiff(topo)
                # Spare memory
                ex = self.grid.extent_in_crs(crs=wgs84)  # l, r, b, t
                g.set_subset(corners=((ex[0], ex[2]), (ex[1], ex[3])),
                             crs=wgs84, margin=10)
                z = g.get_vardata()
                z[z < -999] = 0
                z = self.grid.map_gridded_data(z, g.grid, **kwargs)
            else:
                raise ValueError('File extension not recognised: {}'
                                 .format(ext))
        else:
            z = self._check_data(topo, crs=crs, **kwargs)

        # Gradient in m m-1
        ddx = self.grid.dx
        ddy = self.grid.dy
        if self.grid.proj.is_latlong():
            # we make a coarse approx of the avg dx on a sphere
            _, lat = self.grid.ll_coordinates
            ddx = np.mean(ddx * 111200 * np.cos(lat * np.pi / 180))
            ddy *= 111200

        dy, dx = np.gradient(z, ddy, ddx)
        self._shading_base(dx - dy, relief_factor=relief_factor)
        return z
Exemple #8
0
def test_hef_from_array():
    grid = mercator_grid(center_ll=(10.76, 46.798444), extent=(10000, 7000))
    c = Map(grid, countries=False)
    c.set_lonlat_contours(interval=0)
    c.set_shapefile(get_demo_file('Hintereisferner_UTM.shp'))

    dem = GeoTiff(get_demo_file('hef_srtm.tif'))
    mytopo = dem.get_vardata()
    c.set_topography(mytopo, crs=dem.grid, interp='spline')

    fig, ax = plt.subplots(1, 1)
    c.visualize(ax=ax, addcbar=False, title='From array')
    plt.tight_layout()
    return fig
Exemple #9
0
def open_xr_dataset(file):
    """Thin wrapper around xarray's open_dataset.

    This is needed because variables often have not enough georef attrs
    to be understood alone, and datasets tend to loose their attrs with
    operations...

    Returns:
    --------
    xr.Dataset
    """

    # if geotiff, use Salem
    p, ext = os.path.splitext(file)
    if (ext.lower() == '.tif') or (ext.lower() == '.tiff'):
        from salem import GeoTiff
        geo = GeoTiff(file)
        # TODO: currently everything is loaded in memory (baaad)
        da = xr.DataArray(geo.get_vardata(),
                          coords={'x': geo.grid.x_coord,
                                  'y': geo.grid.y_coord},
                          dims=['y', 'x'])
        ds = xr.Dataset()
        ds.attrs['pyproj_srs'] = geo.grid.proj.srs
        ds['data'] = da
        ds['data'].attrs['pyproj_srs'] = geo.grid.proj.srs
        return ds

    # otherwise rely on xarray
    ds = xr.open_dataset(file)

    # did we get it? If not no need to go further
    try:
        grid = ds.salem.grid
    except:
        warnings.warn('File not recognised as Salem grid. Fall back to xarray',
                      RuntimeWarning)
        return ds

    # add cartesian coords for WRF
    if 'west_east' in ds.variables:
        ds['west_east'] = ds.salem.grid.x_coord
        ds['south_north'] = ds.salem.grid.y_coord

    # add pyproj string everywhere
    ds.attrs['pyproj_srs'] = grid.proj.srs
    for v in ds.data_vars:
        ds[v].attrs['pyproj_srs'] = grid.proj.srs

    return ds
Exemple #10
0
def test_hef_from_array():
    grid = mercator_grid(center_ll=(10.76, 46.798444),
                         extent=(10000, 7000))
    c = Map(grid, countries=False)
    c.set_lonlat_contours(interval=0)
    c.set_shapefile(get_demo_file('Hintereisferner_UTM.shp'))

    dem = GeoTiff(get_demo_file('hef_srtm.tif'))
    mytopo = dem.get_vardata()
    c.set_topography(mytopo, crs=dem.grid, interp='spline')

    fig, ax = plt.subplots(1, 1)
    c.visualize(ax=ax, addcbar=False, title='From array')
    plt.tight_layout()
    return fig
Exemple #11
0
    def test_read_to_grid(self):

        g = GeoTiff(utils.get_demo_file('hef_srtm.tif'))
        sf = utils.get_demo_file('Hintereisferner_UTM.shp')

        df1 = read_shapefile_to_grid(sf, g.grid)

        df2 = transform_geopandas(read_shapefile(sf), to_crs=g.grid)
        assert_allclose(df1.geometry[0].exterior.coords,
                        df2.geometry[0].exterior.coords)

        # test for caching
        d = g.grid.to_dict()
        # change key ordering by chance
        d2 = dict((k, v) for k, v in d.items())

        from salem.sio import _memory_shapefile_to_grid, cached_shapefile_path
        shape_cpath = cached_shapefile_path(sf)
        res = _memory_shapefile_to_grid.call_and_shelve(shape_cpath,
                                                        grid=g.grid,
                                                        **d)
        h1 = res.argument_hash
        res = _memory_shapefile_to_grid.call_and_shelve(shape_cpath,
                                                        grid=g.grid,
                                                        **d2)
        h2 = res.argument_hash
        self.assertEqual(h1, h2)
Exemple #12
0
    def test_read_to_grid(self):

        g = GeoTiff(utils.get_demo_file('hef_srtm.tif'))
        sf = utils.get_demo_file('Hintereisferner_UTM.shp')

        df1 = read_shapefile_to_grid(sf, g.grid)

        df2 = transform_geopandas(read_shapefile(sf), to_crs=g.grid)
        assert_allclose(df1.geometry[0].exterior.coords,
                        df2.geometry[0].exterior.coords)
Exemple #13
0
def test_hef_topo_withnan():
    grid = mercator_grid(center_ll=(10.76, 46.798444), extent=(10000, 7000))
    c = Map(grid, countries=False)
    c.set_lonlat_contours(interval=10)
    c.set_shapefile(get_demo_file('Hintereisferner_UTM.shp'))

    dem = GeoTiff(get_demo_file('hef_srtm.tif'))
    mytopo = dem.get_vardata()
    h = c.set_topography(mytopo, crs=dem.grid, interp='spline')

    c.set_lonlat_contours()
    c.set_cmap(get_cmap('topo'))
    c.set_plot_params(nlevels=256)
    # Try with nan data
    h[-100:, -100:] = np.NaN
    c.set_data(h)
    fig, ax = plt.subplots(1, 1)
    c.visualize(ax=ax, title='color with NaN')
    plt.tight_layout()
    return fig
Exemple #14
0
def test_hef_topo_withnan():
    grid = mercator_grid(center_ll=(10.76, 46.798444),
                         extent=(10000, 7000))
    c = Map(grid, countries=False)
    c.set_lonlat_contours(interval=10)
    c.set_shapefile(get_demo_file('Hintereisferner_UTM.shp'))

    dem = GeoTiff(get_demo_file('hef_srtm.tif'))
    mytopo = dem.get_vardata()
    h = c.set_topography(mytopo, crs=dem.grid, interp='spline')

    c.set_lonlat_contours()
    c.set_cmap(get_cmap('topo'))
    c.set_plot_params(nlevels=256)
    # Try with nan data
    h[-100:, -100:] = np.NaN
    c.set_data(h)
    fig, ax = plt.subplots(1, 1)
    c.visualize(ax=ax, title='color with NaN')
    plt.tight_layout()
    return fig
Exemple #15
0
    def test_to_cartopy(self):

        import cartopy.crs as ccrs
        from salem import GeoNetcdf, GeoTiff

        grid = gis.mercator_grid(center_ll=(11.38, 47.26),
                                 extent=(2000000, 2000000))
        p = gis.proj_to_cartopy(grid.proj)
        assert isinstance(p, ccrs.TransverseMercator)
        fuzzy_proj_tester(grid.proj, pyproj.Proj(p.proj4_params))

        ds = GeoNetcdf(get_demo_file('wrfout_d01.nc'))
        p = gis.proj_to_cartopy(ds.grid.proj)
        assert isinstance(p, ccrs.LambertConformal)
        fuzzy_proj_tester(ds.grid.proj, pyproj.Proj(p.proj4_params))

        ds = GeoNetcdf(get_demo_file('wrf_mercator.nc'))
        p = gis.proj_to_cartopy(ds.grid.proj)
        assert isinstance(p, ccrs.Mercator)
        fuzzy_proj_tester(ds.grid.proj, pyproj.Proj(p.proj4_params))

        ds = GeoTiff(get_demo_file('himalaya.tif'))
        p = gis.proj_to_cartopy(ds.grid.proj)
        assert isinstance(p, ccrs.PlateCarree)

        ds = GeoTiff(get_demo_file('hef_roi.tif'))
        p = gis.proj_to_cartopy(ds.grid.proj)
        assert isinstance(p, ccrs.PlateCarree)
        fuzzy_proj_tester(ds.grid.proj, pyproj.Proj(p.proj4_params))

        p = gis.proj_to_cartopy(wgs84)
        assert isinstance(p, ccrs.PlateCarree)

        p = gis.proj_to_cartopy(pyproj.Proj('+proj=utm +zone=15'))
        assert isinstance(p, ccrs.UTM)

        # this needs gdal
        if gis.has_gdal:
            p = gis.proj_to_cartopy(pyproj.Proj(init='epsg:26915'))
            assert isinstance(p, ccrs.UTM)
Exemple #16
0
def read_tiff(file):

    g = GeoTiff(file)
    ls = g.get_vardata()

    return ls
# read the ocean shapefile (data from http://www.naturalearthdata.com)
oceans = salem.read_shapefile(salem.get_demo_file('ne_50m_ocean.shp'),
                              cached=True)

river = salem.read_shapefile('/users/global/cornkle/data/pythonWorkspace/proj_CEH/shapes/rivers/ne_10m_rivers_lake_centerlines.shp', cached=True)
lakes = salem.read_shapefile('/users/global/cornkle/data/pythonWorkspace/proj_CEH/shapes/lakes/ne_10m_lakes.shp', cached=True)
map.set_shapefile(lakes, edgecolor='k', facecolor='none', linewidth=2,)


srtm_on_ds = ds18_present.salem.lookup_transform(top)
t_on_ds = ds18_present.salem.transform(t)
t2_on_ds = ds18_present.salem.transform(t2)

grid = ds18_present.salem.grid
#deforestation
g = GeoTiff(lst)
ex = grid.extent_in_crs(crs=wgs84)  # l, r, b, t
g.set_subset(corners=((ex[0], ex[2]), (ex[1], ex[3])),
             crs=wgs84, margin=10)
ls = g.get_vardata()
ls = np.array(ls, dtype=float)
lst_on_ds = ds18_present.salem.lookup_transform(ls, grid=g.grid)

#evergreen forest
g = GeoTiff(vegfra)

ex = grid.extent_in_crs(crs=wgs84)  # l, r, b, t
g.set_subset(corners=((ex[0], ex[2]), (ex[1], ex[3])),
             crs=wgs84, margin=10)
ls = g.get_vardata()
ls = np.array(ls, dtype=float)
Exemple #18
0
    cached=True)
map.set_shapefile(
    lakes,
    edgecolor='k',
    facecolor='none',
    linewidth=2,
)

srtm = open_xr_dataset(get_demo_file('hef_srtm.tif'))
srtm_on_ds = ds.salem.lookup_transform(srtm)

grid = ds.salem.grid
srtm_on_ds = ds.salem.lookup_transform(top)
#t_on_ds = ds.salem.lookup_transform(t, grid=grid)
#
g = GeoTiff(lst)
# Spare memory
ex = grid.extent_in_crs(crs=wgs84)  # l, r, b, t
g.set_subset(corners=((ex[0], ex[2]), (ex[1], ex[3])), crs=wgs84, margin=10)
ls = g.get_vardata()
ls = np.array(ls, dtype=float)
ls[ls > 200] = np.nan
ls = grid.map_gridded_data(ls, g.grid)
lst_on_ds = ds.salem.lookup_transform(ls, grid=grid)

g = GeoTiff(vegfra)
# Spare memory
ex = grid.extent_in_crs(crs=wgs84)  # l, r, b, t
g.set_subset(corners=((ex[0], ex[2]), (ex[1], ex[3])), crs=wgs84, margin=10)
ls = g.get_vardata()
ls = np.array(ls, dtype=float)
Exemple #19
0
def read_tiff(file):

    g = GeoTiff(file)
    ls = g.get_vardata()

    return ls