Example #1
0
    def test_reprs(self):
        from textwrap import dedent

        args = dict(nxny=(3, 3), dxdy=(1, 1), x0y0=(0, 0), proj=wgs84)
        g1 = Grid(**args)

        self.assertEqual(g1.__repr__(), g1.__str__())
Example #2
0
    def test_lookup_grid(self):

        data = np.arange(12).reshape((4, 3))
        args = dict(nxny=(3, 4), dxdy=(1, 1), x0y0=(0, 0), proj=wgs84)
        g = Grid(**args)
        lut = g.grid_lookup(g)
        for ji, l in lut.items():
            self.assertEqual(data[ji], data[l[:, 0], l[:, 1]])

        args = dict(nxny=(2, 3), dxdy=(1, 1), x0y0=(0, 0), proj=wgs84)
        g2 = Grid(**args)
        lut = g2.grid_lookup(g)
        for ji, l in lut.items():
            self.assertEqual(data[ji], data[l[:, 0], l[:, 1]])

        lut = g.grid_lookup(g2)
        for (j, i), l in lut.items():
            if j > 2 or i > 1:
                assert l is None
            else:
                self.assertEqual(data[j, i], data[l[:, 0], l[:, 1]])

        args = dict(nxny=(1, 1), dxdy=(10, 10), x0y0=(0, 0), proj=wgs84)
        g3 = Grid(**args)

        lut = g3.grid_lookup(g)
        od = data[lut[(0, 0)][:, 0], lut[(0, 0)][:, 1]]
        self.assertEqual(len(od), 12)
        assert_allclose(np.mean(od), np.mean(data))
Example #3
0
    def test_ij_to_crs(self):
        """Converting to projection"""

        # It should work exact same for any projection
        projs = [wgs84, gis.check_crs('+init=epsg:26915')]

        for proj in projs:

            args = dict(nxny=(3, 3), dxdy=(1, 1), x0y0=(0, 0), proj=proj)

            g = Grid(**args)
            exp_i, exp_j = np.meshgrid(np.arange(3), np.arange(3))
            r_i, r_j = g.ij_to_crs(exp_i, exp_j)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)
            proj_out = proj
            r_i, r_j = g.ij_to_crs(exp_i, exp_j, crs=proj_out)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)

            # The equivalents
            gc = g.corner_grid
            r_i, r_j = gc.ij_to_crs(exp_i + 0.5, exp_j + 0.5)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)
            gc = g.center_grid
            r_i, r_j = gc.ij_to_crs(exp_i, exp_j)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)

            args = dict(nxny=(3, 3), dxdy=(1, -1), x0y0=(0, 0), proj=proj)
            g = Grid(**args)
            exp_i, exp_j = np.meshgrid(np.arange(3), -np.arange(3))
            in_i, in_j = np.meshgrid(np.arange(3), np.arange(3))
            r_i, r_j = g.ij_to_crs(in_i, in_j)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)
            proj_out = proj
            r_i, r_j = g.ij_to_crs(in_i, in_j, crs=proj_out)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)

            # The equivalents
            gc = g.corner_grid
            r_i, r_j = gc.ij_to_crs(in_i, in_j)
            assert_allclose(exp_i - 0.5, r_i, atol=1e-03)
            assert_allclose(exp_j + 0.5, r_j, atol=1e-03)
            gc = g.center_grid
            r_i, r_j = gc.ij_to_crs(in_i, in_j)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)

            # if we take some random projection it wont work
            proj_out = pyproj.Proj(proj="utm", zone=10, datum='NAD27')
            r_i, r_j = g.ij_to_crs(exp_i, exp_j, crs=proj_out)
            self.assertFalse(np.allclose(exp_i, r_i))
            self.assertFalse(np.allclose(exp_j, r_j))

            # Raise
            self.assertRaises(ValueError, g.ij_to_crs, exp_i, exp_j, crs='ups')
Example #4
0
    def test_extent(self):

        # It should work exact same for any projection
        args = dict(nxny=(9, 9), dxdy=(1, 1), x0y0=(0, 0), proj=wgs84)
        g1 = Grid(**args)
        assert_allclose(g1.extent, g1.extent_in_crs(crs=g1.proj), atol=1e-3)

        args = dict(nxny=(9, 9),
                    dxdy=(30000, 30000),
                    x0y0=(0., 1577463),
                    proj=gis.check_crs('+init=epsg:26915'))
        g2 = Grid(**args)
        assert_allclose(g2.extent, g2.extent_in_crs(crs=g2.proj), atol=1e-3)

        exg = np.array(g2.extent_in_crs(crs=g1))
        exgx, exgy = g1.ij_to_crs(exg[[0, 1]], exg[[2, 3]], crs=wgs84)

        lon, lat = g2.corner_grid.ll_coordinates
        assert_allclose([np.min(lon), np.min(lat)], [exgx[0], exgy[0]],
                        rtol=0.1)

        p = g2.extent_as_polygon(crs=g2.proj)

        assert p.is_valid
        x, y = p.exterior.coords.xy
        assert_allclose([np.min(x), np.max(x),
                         np.min(y), np.max(y)], g2.extent)
Example #5
0
def test_contourf():
    a = np.zeros((4, 5))
    a[0, 0] = -1
    a[1, 1] = 1.1
    a[2, 2] = 2.2
    a[2, 4] = 1.9
    a[3, 3] = 9

    # UL Corner
    g = Grid(nxny=(5, 4),
             dxdy=(1, -1),
             x0y0=(-1, 3),
             proj=wgs84,
             pixel_ref='corner')
    c = Map(g, ny=400, countries=False)

    c.set_cmap(mpl.cm.get_cmap('viridis'))
    c.set_plot_params(levels=[0, 1, 2, 3])
    c.set_data(a)
    s = a * 0.
    s[2, 2] = 1
    c.set_contourf(s,
                   interp='linear',
                   hatches=['xxx'],
                   colors='none',
                   levels=[0.5, 1.5])

    s = a * 0.
    s[0:2, 3:] = 1
    s[0, 4] = 2
    c.set_contour(s,
                  interp='linear',
                  colors='k',
                  linewidths=6,
                  levels=[0.5, 1., 1.5])

    c.set_lonlat_contours(interval=0.5)

    # Add a geometry for fun
    gs = g.to_dict()
    gs['nxny'] = (1, 2)
    gs['x0y0'] = (0, 2)
    gs = Grid.from_dict(gs)
    c.set_geometry(gs.extent_as_polygon(), edgecolor='r', linewidth=2)

    fig, ax = plt.subplots(1)
    c.visualize(ax=ax)
    fig.tight_layout()

    # remove it
    c.set_contourf()
    c.set_contour()

    return fig
Example #6
0
    def test_geometry(self):
        projs = [wgs84, gis.check_crs('+init=epsg:26915')]
        from shapely.geometry import Point
        for proj in projs:
            g = Grid(nxny=(3, 3), dxdy=(1, 1), x0y0=(0.5, 0.5), proj=proj)
            gdf = g.to_geometry()
            self.assertEqual(len(gdf), 9)
            self.assertTrue(gdf.contains(Point(1.5, 1.5))[4])
            self.assertFalse(gdf.contains(Point(1.5, 1.5))[5])

        gdf = g.to_geometry(to_crs=wgs84)
        # This is now quite off
        self.assertFalse(gdf.contains(Point(1.5, 1.5))[4])
Example #7
0
    def test_errors(self):
        """Check that errors are occurring"""

        # It should work exact same for any projection
        projs = [wgs84, pyproj.Proj(init='epsg:26915')]

        for proj in projs:
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                args = dict(nxny=(3, 3), dxdy=(1, -1), ll_corner=(0, 0), proj=proj)
                self.assertRaises(ValueError, Grid, **args)
                args = dict(nxny=(3, 3), dxdy=(-1, 0), ul_corner=(0, 0), proj=proj)
                self.assertRaises(ValueError, Grid, **args)
                args = dict(nxny=(3, 3), dxdy=(1, 1), proj=proj)
                self.assertRaises(ValueError, Grid, **args)
                args = dict(nxny=(3, -3), dxdy=(1, 1), ll_corner=(0, 0), proj=proj)
                self.assertRaises(ValueError, Grid, **args)
                args = dict(nxny=(3, 3), dxdy=(1, 1), ll_corner=(0, 0),
                            proj=proj, pixel_ref='areyoudumb')
                self.assertRaises(ValueError, Grid, **args)

                args = dict(nxny=(3, 3), dxdy=(1, 1), ll_corner=(0, 0), proj=proj)
                g = Grid(**args)
                self.assertRaises(ValueError, g.transform, 0, 0, crs=None)
                self.assertRaises(ValueError, g.transform, 0, 0, crs='areyou?')
                self.assertRaises(ValueError, g.map_gridded_data,
                                  np.zeros((3, 3)), 'areyou?')
                self.assertRaises(ValueError, g.map_gridded_data,
                                  np.zeros(3), g)
                self.assertRaises(ValueError, g.map_gridded_data,
                                  np.zeros((3, 4)), g)
                self.assertRaises(ValueError, g.map_gridded_data,
                                  np.zeros((3, 3)), g, interp='youare')

        # deprecation warnings
        for proj in projs:
            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter("always")
                args = dict(nxny=(3, 3), dxdy=(1, -1), corner=(0, 0),
                            proj=proj)
                Grid(**args)
                args = dict(nxny=(3, 3), dxdy=(1, -1), ul_corner=(0, 0),
                            proj=proj)
                Grid(**args)
                args = dict(nxny=(3, 3), dxdy=(1, 1), ll_corner=(0, 0),
                            proj=proj)
                Grid(**args)
                if python_version == 'py3':
                    self.assertEqual(len(w), 3)
Example #8
0
    def __init__(self, file):
        """Open the file.

        Parameters
        ----------
        file: path to the file
        """
        if rasterio is None:
            raise ImportError('This feature needs rasterio to be insalled')

        # brutally efficient
        with rasterio.Env():
            with rasterio.open(file) as src:
                nxny = (src.width, src.height)
                ul_corner = (src.bounds.left, src.bounds.top)
                proj = pyproj.Proj(src.crs)
                dxdy = (src.res[0], -src.res[1])
                grid = Grid(x0y0=ul_corner,
                            nxny=nxny,
                            dxdy=dxdy,
                            pixel_ref='corner',
                            proj=proj)
        # done
        self.file = file
        GeoDataset.__init__(self, grid)
Example #9
0
    def __init__(self, file):
        """Open the file.

        Parameters
        ----------
        file: path to the file
        """

        bname = os.path.basename(file).split('.')[0]
        pok = bname.find('UTM')
        if pok == -1:
            raise ValueError(file + ' does not seem to be an ITMIX file.')
        zone = int(bname[pok+3:])
        south = False
        if zone < 0:
            south = True
            zone = -zone
        proj = pyproj.Proj(proj='utm', zone=zone, ellps='WGS84',
                           south=south)

        # brutally efficient
        with rasterio.Env():
            with rasterio.open(file) as src:
                nxny = (src.width, src.height)
                ul_corner = (src.bounds.left, src.bounds.top)
                dxdy = (src.res[0], -src.res[1])
                grid = Grid(ul_corner=ul_corner, nxny=nxny, dxdy=dxdy,
                            pixel_ref='corner', proj=proj)
        # done
        self.file = file
        GeoDataset.__init__(self, grid)
Example #10
0
    def test_geometry(self):

        import shapely.geometry as shpg

        g = Grid(nxny=(3, 3),
                 dxdy=(1, 1),
                 x0y0=(0, 0),
                 proj=wgs84,
                 pixel_ref='corner')
        p = shpg.Polygon([(1.5, 1.), (2., 1.5), (1.5, 2.), (1., 1.5)])
        o = gis.transform_geometry(p, to_crs=g)
        assert_allclose(p.exterior.coords, o.exterior.coords)

        q = gis.transform_geometry(o, crs=g)
        assert_allclose(p.exterior.coords, q.exterior.coords)

        o = gis.transform_geometry(p, to_crs=g.center_grid)
        totest = np.array(o.exterior.coords) + 0.5
        assert_allclose(p.exterior.coords, totest)

        x, y = g.corner_grid.xy_coordinates
        p = shpg.MultiPoint(
            [shpg.Point(i, j) for i, j in zip(x.flatten(), y.flatten())])
        o = gis.transform_geometry(p, to_crs=g.proj)
        assert_allclose([_p.coords for _p in o], [_p.coords for _p in p])
Example #11
0
    def test_shape(self):
        """Is the transformation doing well?"""

        from salem import read_shapefile

        so = read_shapefile(get_demo_file('Hintereisferner.shp'))
        sref = read_shapefile(get_demo_file('Hintereisferner_UTM.shp'))
        st = gis.transform_geopandas(so, to_crs=sref.crs)
        self.assertFalse(st is so)
        assert_allclose(st.geometry[0].exterior.coords,
                        sref.geometry[0].exterior.coords)

        sti = gis.transform_geopandas(so, to_crs=sref.crs, inplace=True)
        self.assertTrue(sti is so)
        assert_allclose(so.geometry[0].exterior.coords,
                        sref.geometry[0].exterior.coords)
        assert_allclose(sti.geometry[0].exterior.coords,
                        sref.geometry[0].exterior.coords)

        g = Grid(nxny=(1, 1), dxdy=(1, 1), x0y0=(10., 46.), proj=wgs84)
        so = read_shapefile(get_demo_file('Hintereisferner.shp'))
        st = gis.transform_geopandas(so, to_crs=g)

        ref = np.array(so.geometry[0].exterior.coords)
        ref = ref - np.floor(ref)
        assert_allclose(ref, st.geometry[0].exterior.coords)

        # round trip
        so_back = gis.transform_geopandas(st, to_crs=so.crs)
        assert_allclose(so_back.geometry[0].exterior.coords,
                        so.geometry[0].exterior.coords)
Example #12
0
    def test_ij_to_crs(self):
        """Converting to projection"""

        # It should work exact same for any projection
        projs = [wgs84, pyproj.Proj(init='epsg:26915')]

        for proj in projs:

            args = dict(nxny=(3, 3), dxdy=(1, 1), ll_corner=(0, 0), proj=proj)

            g = Grid(**args)
            exp_i, exp_j = np.meshgrid(np.arange(3), np.arange(3))
            r_i, r_j = g.ij_to_crs(exp_i, exp_j)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)
            proj_out = proj
            r_i, r_j = g.ij_to_crs(exp_i, exp_j, crs=proj_out)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)

            # The equivalents
            gc = g.corner_grid
            r_i, r_j = gc.ij_to_crs(exp_i+0.5, exp_j+0.5)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)
            gc = g.center_grid
            r_i, r_j = gc.ij_to_crs(exp_i, exp_j)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)

            args = dict(nxny=(3, 3), dxdy=(1, -1), ul_corner=(0, 0), proj=proj)
            g = Grid(**args)
            exp_i, exp_j = np.meshgrid(np.arange(3), -np.arange(3))
            in_i, in_j = np.meshgrid(np.arange(3), np.arange(3))
            r_i, r_j = g.ij_to_crs(in_i, in_j)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)
            proj_out = proj
            r_i, r_j = g.ij_to_crs(in_i, in_j, crs=proj_out)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)

            # The equivalents
            gc = g.corner_grid
            r_i, r_j = gc.ij_to_crs(in_i, in_j)
            assert_allclose(exp_i-0.5, r_i, atol=1e-03)
            assert_allclose(exp_j+0.5, r_j, atol=1e-03)
            gc = g.center_grid
            r_i, r_j = gc.ij_to_crs(in_i, in_j)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)

            # if we take some random projection it wont work
            proj_out = pyproj.Proj(proj="utm", zone=10, datum='NAD27')
            r_i, r_j = g.ij_to_crs(exp_i, exp_j, crs=proj_out)
            self.assertFalse(np.allclose(exp_i, r_i))
            self.assertFalse(np.allclose(exp_j, r_j))

            # Raise
            self.assertRaises(ValueError, g.ij_to_crs, exp_i, exp_j, crs='ups')
Example #13
0
    def test_reprs(self):
        from textwrap import dedent

        args = dict(nxny=(3, 3), dxdy=(1, 1), x0y0=(0, 0), proj=wgs84)
        g1 = Grid(**args)

        self.assertEqual(g1.__repr__(), g1.__str__())

        expected = dedent("""\
        <salem.Grid>
          proj: +datum=WGS84 +proj=latlong +units=m
          pixel_ref: center
          origin: lower-left
          (nx, ny): (3, 3)
          (dx, dy): (1.0, 1.0)
          (x0, y0): (0.0, 0.0)
        """)
        self.assertEqual(g1.__repr__(), expected)
Example #14
0
def test_text():
    # UL Corner
    g = Grid(nxny=(5, 4),
             dxdy=(10, 10),
             x0y0=(-20, -15),
             proj=wgs84,
             pixel_ref='corner')
    c = Map(g, ny=4, countries=False)
    c.set_lonlat_contours(interval=5., colors='crimson', linewidths=1)

    c.set_text(-5, -5, 'Less Middle', color='green', style='italic', size=25)
    c.set_geometry(shpg.Point(-10, -10),
                   s=500,
                   marker='o',
                   text='My point',
                   text_delta=[0, 0])

    shape = read_shapefile_to_grid(shapefiles['world_borders'], c.grid)
    had_c = set()
    for index, row in shape.iloc[::-1].iterrows():
        if row.CNTRY_NAME in had_c:
            c.set_geometry(row.geometry, crs=c.grid)
        else:
            c.set_geometry(row.geometry,
                           text=row.CNTRY_NAME,
                           crs=c.grid,
                           text_kwargs=dict(horizontalalignment='center',
                                            verticalalignment='center',
                                            clip_on=True,
                                            color='gray'),
                           text_delta=[0, 0])
        had_c.add(row.CNTRY_NAME)

    c.set_points([20, 20, 10], [10, 20, 20],
                 s=250,
                 marker='s',
                 c='purple',
                 hatch='||||',
                 text='baaaaad',
                 text_delta=[0, 0],
                 text_kwargs=dict(horizontalalignment='center',
                                  verticalalignment='center',
                                  color='red'))

    fig, ax = plt.subplots(1, 1)
    c.visualize(ax=ax, addcbar=False)
    plt.tight_layout()

    c.set_text()
    assert len(c._text) == 0

    c.set_geometry()
    assert len(c._geometries) == 0

    return fig
Example #15
0
    def test_regrid(self):
        """New grids"""

        # It should work exact same for any projection
        projs = [wgs84, pyproj.Proj(init='epsg:26915')]

        for proj in projs:

            kargs = [dict(nxny=(3, 2), dxdy=(1, 1), ll_corner=(0, 0),
                         proj=proj),
                     dict(nxny=(3, 2), dxdy=(1, -1), ul_corner=(0, 0),
                         proj=proj),
                     dict(nxny=(3, 2), dxdy=(1, 1), ll_corner=(0, 0),
                         proj=proj, pixel_ref='corner'),
                     dict(nxny=(3, 2), dxdy=(1, -1), ul_corner=(0, 0),
                         proj=proj, pixel_ref='corner')]

            for ka in kargs:
                g = Grid(**ka)

                rg = g.regrid()
                self.assertTrue(g == rg)

                rg = g.regrid(factor=3)
                assert_array_equal(g.extent, rg.extent)
                assert_array_equal(g.extent, rg.extent)

                gx, gy = g.center_grid.xy_coordinates
                rgx, rgy = rg.center_grid.xy_coordinates
                assert_allclose(gx, rgx[1::3, 1::3], atol=1e-7)
                assert_allclose(gy, rgy[1::3, 1::3], atol=1e-7)

                gx, gy = g.center_grid.ll_coordinates
                rgx, rgy = rg.center_grid.ll_coordinates
                assert_allclose(gx, rgx[1::3, 1::3], atol=1e-7)
                assert_allclose(gy, rgy[1::3, 1::3], atol=1e-7)

                nrg = g.regrid(nx=9)
                self.assertTrue(nrg == rg)

                nrg = g.regrid(ny=6)
                self.assertTrue(nrg == rg)
Example #16
0
    def test_comparisons(self):
        """See if the grids can compare themselves"""

        args = dict(nxny=(3, 3), dxdy=(1, 1), ll_corner=(0, 0), proj=wgs84)
        g1 = Grid(**args)
        self.assertEqual(g1.center_grid, g1.corner_grid)

        g2 = Grid(**args)
        self.assertEqual(g1, g2)

        args['dxdy'] = (1. + 1e-6, 1. + 1e-6)
        g2 = Grid(**args)
        self.assertNotEqual(g1, g2)

        args['proj'] = pyproj.Proj(init='epsg:26915')
        g2 = Grid(**args)
        self.assertNotEqual(g1, g2)

        # New instance, same proj
        args['proj'] = pyproj.Proj(init='epsg:26915')
        g1 = Grid(**args)
        self.assertEqual(g1, g2)
Example #17
0
 def __init__(self, file):
     self.nc = netCDF4.Dataset(file)
     proj = gis.check_crs(str(self.nc.proj4_str))
     x = self.nc.variables['x']
     y = self.nc.variables['y']
     dxdy = (x[1] - x[0], y[1] - y[0])
     nxny = (len(x), len(y))
     x0y0 = None
     if dxdy[1] > 0:
         x0y0 = (x[0], y[0])
     if dxdy[1] < 0:
         x0y0 = (x[0], y[0])
     self.grid = Grid(nxny=nxny, dxdy=dxdy, proj=proj, x0y0=x0y0)
Example #18
0
    def test_xarray_support(self):
        # what happens if we use salem's funcs with xarray?
        import xarray as xr

        projs = [wgs84, gis.check_crs('+init=epsg:26915')]

        for proj in projs:
            args = dict(nxny=(3, 3), dxdy=(1, 1), x0y0=(0, 0), proj=proj)
            g = Grid(**args)
            exp_i, exp_j = np.meshgrid(np.arange(3), np.arange(3))
            exp_i, exp_j = (xr.DataArray(exp_i, dims=['y', 'x']),
                            xr.DataArray(exp_j, dims=['y', 'x']))
            r_i, r_j = g.ij_to_crs(exp_i, exp_j)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)
            self.assertTrue(r_i.dims == exp_i.dims)

            # transform
            r_i, r_j = g.transform(exp_i, exp_j, crs=proj)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)
            # TODO: this doesn't work:
            # self.assertTrue(r_i.dims == exp_i.dims)

            # map
            nx, ny = (3, 4)
            data = np.arange(nx * ny).reshape((ny, nx))
            data = xr.DataArray(data,
                                coords={
                                    'y': np.arange(ny),
                                    'x': np.arange(nx)
                                },
                                dims=['y', 'x'])
            data.attrs = {'test': 'attr'}

            # Nearest Neighbor
            args = dict(nxny=(nx, ny), dxdy=(1, 1), x0y0=(0, 0), proj=proj)
            g = Grid(**args)
            odata = g.map_gridded_data(data, g)
            self.assertTrue(odata.shape == data.shape)
            assert_allclose(data, odata, atol=1e-03)

            # Transform can understand a grid
            data.attrs['pyproj_srs'] = g.proj.srs
            odata = g.map_gridded_data(data)
            self.assertTrue(odata.shape == data.shape)
            assert_allclose(data, odata, atol=1e-03)
Example #19
0
def test_contourf():
    a = np.zeros((4, 5))
    a[0, 0] = -1
    a[1, 1] = 1.1
    a[2, 2] = 2.2
    a[2, 4] = 1.9
    a[3, 3] = 9

    # UL Corner
    g = Grid(nxny=(5, 4),
             dxdy=(1, -1),
             ul_corner=(-1, 3),
             proj=wgs84,
             pixel_ref='corner')
    c = Map(g, ny=400, countries=False)

    c.set_cmap(mpl.cm.get_cmap('viridis'))
    c.set_plot_params(levels=[0, 1, 2, 3])
    c.set_data(a)
    s = a * 0.
    s[2, 2] = 1
    c.set_contourf(s,
                   interp='linear',
                   hatches=['xxx'],
                   colors='none',
                   levels=[0.5, 1.5])

    s = a * 0.
    s[0:2, 3:] = 1
    s[0, 4] = 2
    c.set_contour(s,
                  interp='linear',
                  colors='k',
                  linewidths=6,
                  levels=[0.5, 1., 1.5])

    c.set_lonlat_contours(interval=0.5)

    fig, ax = plt.subplots(1)
    c.visualize(ax=ax)
    fig.tight_layout()

    # remove it
    c.set_contourf()
    c.set_contour()

    return fig
Example #20
0
    def test_to_dataset(self):
        projs = [wgs84, pyproj.Proj(init='epsg:26915')]

        for proj in projs:
            g = Grid(nxny=(3, 3), dxdy=(1, 1), x0y0=(0, 0), proj=proj)
            ds = g.to_dataset()
            self.assertTrue(g == ds.salem.grid)

            g = Grid(nxny=(3, 3), dxdy=(1, 1), x0y0=(0, 0), proj=proj,
                     pixel_ref='corner')
            ds = g.to_dataset()
            self.assertTrue(g == ds.salem.grid)
Example #21
0
    def test_map_gridded_data(self):
        """Ok now the serious stuff starts with some fake data"""

        # It should work exact same for any projection
        projs = [wgs84, pyproj.Proj(init='epsg:26915')]

        for proj in projs:

            nx, ny = (3, 4)
            data = np.arange(nx*ny).reshape((ny, nx))

            # Nearest Neighbor
            args = dict(nxny=(nx, ny), dxdy=(1, 1), ll_corner=(0, 0), proj=proj)
            g = Grid(**args)
            odata = g.map_gridded_data(data, g)
            self.assertTrue(odata.shape == data.shape)
            assert_allclose(data, odata, atol=1e-03)

            # Out of the grid
            go = Grid(nxny=(nx, ny), dxdy=(1, 1), ll_corner=(9, 9), proj=proj)
            odata = g.map_gridded_data(data, go)
            odata.set_fill_value(-999)
            self.assertTrue(odata.shape == data.shape)
            self.assertTrue(np.all(odata.mask))

            args = dict(nxny=(nx-1, ny-1), dxdy=(1, 1), ll_corner=(0, 0), proj=proj)
            ig = Grid(**args)
            odata = g.map_gridded_data(data[0:ny-1, 0:nx-1], ig)
            self.assertTrue(odata.shape == (ny, nx))
            assert_allclose(data[0:ny-1, 0:nx-1], odata[0:ny-1, 0:nx-1], atol=1e-03)
            assert_array_equal([True]*3, odata.mask[ny-1, :])

            data = np.arange(nx*ny).reshape((ny, nx)) * 1.2
            odata = g.map_gridded_data(data[0:ny-1, 0:nx-1], ig)
            self.assertTrue(odata.shape == (ny, nx))
            assert_allclose(data[0:ny-1, 0:nx-1], odata[0:ny-1, 0:nx-1], atol=1e-03)
            self.assertTrue(np.sum(np.isfinite(odata)) == ((ny-1)*(nx-1)))

            # Bilinear
            data = np.arange(nx*ny).reshape((ny, nx))
            exp_data = np.array([ 2.,  3.,  5.,  6.,  8.,  9.]).reshape((ny-1, nx-1))
            args = dict(nxny=(nx, ny), dxdy=(1, 1), ll_corner=(0, 0), proj=proj)
            gfrom = Grid(**args)
            args = dict(nxny=(nx-1, ny-1), dxdy=(1, 1), ll_corner=(0.5, 0.5), proj=proj)
            gto = Grid(**args)
            odata = gto.map_gridded_data(data, gfrom, interp='linear')
            self.assertTrue(odata.shape == (ny-1, nx-1))
            assert_allclose(exp_data, odata, atol=1e-03)
Example #22
0
    def test_stagg(self):
        """Staggered grids."""

        # It should work exact same for any projection
        projs = [wgs84, pyproj.Proj(init='epsg:26915')]

        for proj in projs:
            args = dict(nxny=(3, 2), dxdy=(1, 1), x0y0=(0, 0),
                        proj=proj, pixel_ref='corner')
            g = Grid(**args)
            x, y = g.xstagg_xy_coordinates
            assert_array_equal(x, np.array([[0,1,2,3], [0,1,2,3]]))
            assert_array_equal(y, np.array([[0.5,  0.5,  0.5,  0.5],
                                            [1.5,  1.5,  1.5,  1.5]]))
            xx, yy = g.corner_grid.xstagg_xy_coordinates
            assert_array_equal(x, xx)
            assert_array_equal(y, yy)
            xt, yt = x, y

            x, y = g.ystagg_xy_coordinates
            assert_array_equal(x, np.array([[0.5,  1.5,  2.5],
                                            [0.5,  1.5,  2.5],
                                            [0.5,  1.5,  2.5]]))
            assert_array_equal(y, np.array([[0,  0,  0],
                                            [1,  1,  1],
                                            [2,  2,  2]]))
            xx, yy = g.corner_grid.ystagg_xy_coordinates
            assert_array_equal(x, xx)
            assert_array_equal(y, yy)

            if proj is wgs84:
                xx, yy = g.corner_grid.ystagg_ll_coordinates
                assert_allclose(x, xx)
                assert_allclose(y, yy)
                xx, yy = g.corner_grid.xstagg_ll_coordinates
                assert_allclose(xt, xx)
                assert_allclose(yt, yy)

                x, y = g.pixcorner_ll_coordinates
                assert_allclose(x, np.array([[0, 1, 2, 3],
                                             [0, 1, 2, 3],
                                             [0, 1, 2, 3]]))
                assert_allclose(y, np.array([[0, 0, 0, 0],
                                             [1, 1, 1, 1],
                                             [2, 2, 2, 2]]))
Example #23
0
    def test_extent(self):

        # It should work exact same for any projection
        args = dict(nxny=(9, 9), dxdy=(1, 1), ll_corner=(0, 0), proj=wgs84)
        g1 = Grid(**args)
        assert_allclose(g1.extent, g1.extent_in_crs(crs=g1.proj), atol=1e-3)

        args = dict(nxny=(9, 9),
                    dxdy=(30000, 30000),
                    ll_corner=(0., 1577463),
                    proj=pyproj.Proj(init='epsg:26915'))
        g2 = Grid(**args)
        assert_allclose(g2.extent, g2.extent_in_crs(crs=g2.proj), atol=1e-3)

        exg = np.array(g2.extent_in_crs(crs=g1))
        exgx, exgy = g1.ij_to_crs(exg[[0, 1]], exg[[2, 3]], crs=wgs84)

        lon, lat = g2.corner_grid.ll_coordinates
        assert_allclose([np.min(lon), np.min(lat)], [exgx[0], exgy[0]],
                        rtol=0.1)
Example #24
0
def test_geometries():

    # UL Corner
    g = Grid(nxny=(5, 4),
             dxdy=(10, 10),
             x0y0=(-20, -15),
             proj=wgs84,
             pixel_ref='corner')
    c = Map(g, ny=4)
    c.set_lonlat_contours(interval=10., colors='crimson', linewidths=1)

    c.set_geometry(shpg.Point(10, 10), color='darkred', markersize=60)
    c.set_geometry(shpg.Point(5, 5),
                   s=500,
                   marker='s',
                   facecolor='green',
                   hatch='||||')

    s = np.array([(-5, -10), (0., -5), (-5, 0.), (-10, -5)])
    l1 = shpg.LineString(s)
    l2 = shpg.LinearRing(s + 3)
    c.set_geometry(l1)
    c.set_geometry(l2, color='pink', linewidth=3)

    s += 20
    p = shpg.Polygon(shpg.LineString(s), [shpg.LineString(s / 4 + 10)])
    c.set_geometry(p, facecolor='red', edgecolor='k', linewidth=3, alpha=0.5)

    p1 = shpg.Point(20, 10)
    p2 = shpg.Point(20, 20)
    p3 = shpg.Point(10, 20)
    mpoints = shpg.MultiPoint([p1, p2, p3])
    c.set_geometry(mpoints, s=250, marker='s', c='purple', hatch='||||')

    c.set_scale_bar(color='blue')

    fig, ax = plt.subplots(1, 1)
    c.visualize(ax=ax, addcbar=False)
    plt.tight_layout()

    c.set_geometry()
    assert len(c._geometries) == 0

    return fig
Example #25
0
    def test_regrid(self):
        """New grids"""

        # It should work exact same for any projection
        projs = [wgs84, gis.check_crs('+init=epsg:26915')]

        for proj in projs:

            kargs = [
                dict(nxny=(3, 2), dxdy=(1, 1), x0y0=(0, 0), proj=proj),
                dict(nxny=(3, 2), dxdy=(1, -1), x0y0=(0, 0), proj=proj),
                dict(nxny=(3, 2),
                     dxdy=(1, 1),
                     x0y0=(0, 0),
                     proj=proj,
                     pixel_ref='corner'),
                dict(nxny=(3, 2),
                     dxdy=(1, -1),
                     x0y0=(0, 0),
                     proj=proj,
                     pixel_ref='corner')
            ]

            for ka in kargs:
                g = Grid(**ka)

                rg = g.regrid()
                self.assertTrue(g == rg)

                rg = g.regrid(factor=3)
                assert_array_equal(g.extent, rg.extent)
                assert_array_equal(g.extent, rg.extent)

                bg = rg.regrid(factor=1 / 3)
                self.assertEqual(g, bg)

                gx, gy = g.center_grid.xy_coordinates
                rgx, rgy = rg.center_grid.xy_coordinates
                assert_allclose(gx, rgx[1::3, 1::3], atol=1e-7)
                assert_allclose(gy, rgy[1::3, 1::3], atol=1e-7)

                gx, gy = g.center_grid.ll_coordinates
                rgx, rgy = rg.center_grid.ll_coordinates
                assert_allclose(gx, rgx[1::3, 1::3], atol=1e-7)
                assert_allclose(gy, rgy[1::3, 1::3], atol=1e-7)

                nrg = g.regrid(nx=9)
                self.assertTrue(nrg == rg)

                nrg = g.regrid(ny=6)
                self.assertTrue(nrg == rg)
Example #26
0
    def set_rgb(self,
                img=None,
                crs=None,
                interp='nearest',
                natural_earth=None):
        """Manually force to a rgb img

        Parameters
        ----------
        img : array
            the image to plot
        crs : Grid
            the image reference system
        interp : str, default 'nearest'
            'nearest', 'linear', or 'spline'
        natural_earth : str
           'lr', 'mr' or 'hr' (low res, medium or high res)
           natural earth background img
        """

        if natural_earth is not None:
            from matplotlib.image import imread
            with warnings.catch_warnings():
                # DecompressionBombWarning
                warnings.simplefilter("ignore")
                img = imread(utils.get_natural_earth_file(natural_earth))
            ny, nx = img.shape[0], img.shape[1]
            dx, dy = 360. / nx, 180. / ny
            grid = Grid(nxny=(nx, ny),
                        dxdy=(dx, -dy),
                        x0y0=(-180., 90.),
                        pixel_ref='corner').center_grid
            return self.set_rgb(img, grid, interp='linear')

        if (len(img.shape) != 3) or (img.shape[-1] not in [3, 4]):
            raise ValueError('img should be of shape (y, x, 3) or (y, x, 4)')

        # Unefficient but by far easiest right now
        out = []
        for i in range(img.shape[-1]):
            out.append(self._check_data(img[..., i], crs=crs, interp=interp))
        self._rgb = np.dstack(out)
Example #27
0
    def __init__(self, file):
        """Open the file.

        Parameters
        ----------
        file: path to the file
        """

        # brutally efficient
        with rasterio.Env():
            with rasterio.open(file) as src:
                nxny = (src.width, src.height)
                ul_corner = (src.bounds.left, src.bounds.top)
                proj = pyproj.Proj(src.crs)
                dxdy = (src.res[0], -src.res[1])
                grid = Grid(ul_corner=ul_corner, nxny=nxny, dxdy=dxdy,
                            pixel_ref='corner', proj=proj)
        # done
        self.file = file
        GeoDataset.__init__(self, grid)
Example #28
0
    def test_period(self):
        """See if simple operations work well"""

        g = Grid(nxny=(3, 3), dxdy=(1, 1), x0y0=(0, 0), proj=wgs84)
        d = GeoDataset(g)
        self.assertTrue(d.time is None)
        self.assertTrue(d.sub_t is None)
        self.assertTrue(d.t0 is None)
        self.assertTrue(d.t1 is None)

        t = pd.date_range('1/1/2011', periods=72, freq='D')
        d = GeoDataset(g, time=t)
        assert_array_equal(d.time, t)
        assert_array_equal(d.sub_t, [0, 71])
        assert_array_equal(d.t0, t[0])
        assert_array_equal(d.t1, t[-1])
        d.set_period(t0='2011-01-03')
        assert_array_equal(d.sub_t, [2, 71])
        assert_array_equal(d.t0, t[2])
        assert_array_equal(d.t1, t[-1])
        d.set_period(t0='2011-01-03', t1=datetime(2011, 1, 5))
        assert_array_equal(d.sub_t, [2, 4])
        assert_array_equal(d.t0, t[2])
        assert_array_equal(d.t1, t[4])
        d.set_period(t1=datetime(2011, 1, 5))
        assert_array_equal(d.sub_t, [0, 4])
        assert_array_equal(d.t0, t[0])
        assert_array_equal(d.t1, t[4])
        d = GeoDataset(g, time=pd.Series(t, index=t))
        assert_array_equal(d.time, t)
        d.set_period(t0='2011-01-03', t1=datetime(2011, 1, 5))
        assert_array_equal(d.sub_t, [2, 4])
        assert_array_equal(d.t0, t[2])
        assert_array_equal(d.t1, t[4])
        d.set_period()
        assert_array_equal(d.time, t)
        assert_array_equal(d.sub_t, [0, 71])
        assert_array_equal(d.t0, t[0])
        assert_array_equal(d.t1, t[-1])

        self.assertRaises(NotImplementedError, d.get_vardata)
Example #29
0
    def test_extent(self):

        # It should work exact same for any projection
        args = dict(nxny=(9, 9), dxdy=(1, 1), ll_corner=(0, 0), proj=wgs84)
        g1 = Grid(**args)
        assert_allclose(g1.extent, g1.extent_in_crs(crs=g1.proj), atol=1e-3)

        args = dict(nxny=(9, 9), dxdy=(30000, 30000), ll_corner=(0., 1577463),
                    proj=pyproj.Proj(init='epsg:26915'))
        g2 = Grid(**args)
        assert_allclose(g2.extent, g2.extent_in_crs(crs=g2.proj), atol=1e-3)

        exg = np.array(g2.extent_in_crs(crs=g1))
        exgx, exgy = g1.ij_to_crs(exg[[0, 1]], exg[[2, 3]], crs=wgs84)

        lon, lat = g2.corner_grid.ll_coordinates
        assert_allclose([np.min(lon), np.min(lat)], [exgx[0], exgy[0]],
                        rtol=0.1)
Example #30
0
    def test_errors(self):
        """Check that errors are occurring"""

        # It should work exact same for any projection
        projs = [wgs84, pyproj.Proj(init='epsg:26915')]

        for proj in projs:
            args = dict(nxny=(3, 3), dxdy=(1, -1), ll_corner=(0, 0), proj=proj)
            self.assertRaises(ValueError, Grid, **args)
            args = dict(nxny=(3, 3), dxdy=(-1, 0), ul_corner=(0, 0), proj=proj)
            self.assertRaises(ValueError, Grid, **args)
            args = dict(nxny=(3, 3), dxdy=(1, 1), proj=proj)
            self.assertRaises(ValueError, Grid, **args)
            args = dict(nxny=(3, -3), dxdy=(1, 1), ll_corner=(0, 0), proj=proj)
            self.assertRaises(ValueError, Grid, **args)
            args = dict(nxny=(3, 3),
                        dxdy=(1, 1),
                        ll_corner=(0, 0),
                        proj=proj,
                        pixel_ref='areyoudumb')
            self.assertRaises(ValueError, Grid, **args)

            args = dict(nxny=(3, 3), dxdy=(1, 1), ll_corner=(0, 0), proj=proj)
            g = Grid(**args)
            self.assertRaises(ValueError, g.transform, 0, 0, crs=None)
            self.assertRaises(ValueError, g.transform, 0, 0, crs='areyou?')
            self.assertRaises(ValueError, g.map_gridded_data, np.zeros((3, 3)),
                              'areyou?')
            self.assertRaises(ValueError, g.map_gridded_data, np.zeros(3), g)
            self.assertRaises(ValueError, g.map_gridded_data, np.zeros((3, 4)),
                              g)
            self.assertRaises(ValueError,
                              g.map_gridded_data,
                              np.zeros((3, 3)),
                              g,
                              interp='youare')
Example #31
0
    def test_xarray_support(self):
        # what happens if we use salem's funcs with xarray?
        import xarray as xr

        projs = [wgs84, pyproj.Proj(init='epsg:26915')]

        for proj in projs:
            args = dict(nxny=(3, 3), dxdy=(1, 1), ll_corner=(0, 0), proj=proj)
            g = Grid(**args)
            exp_i, exp_j = np.meshgrid(np.arange(3), np.arange(3))
            exp_i, exp_j = (xr.DataArray(exp_i, dims=['y', 'x']),
                            xr.DataArray(exp_j, dims=['y', 'x']))
            r_i, r_j = g.ij_to_crs(exp_i, exp_j)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)
            self.assertTrue(r_i.dims == exp_i.dims)

            # transform
            r_i, r_j = g.transform(exp_i, exp_j, crs=proj)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)
            # TODO: this doesn't work:
            # self.assertTrue(r_i.dims == exp_i.dims)

            # map
            nx, ny = (3, 4)
            data = np.arange(nx * ny).reshape((ny, nx))
            data = xr.DataArray(data).rename({'dim_0': 'y', 'dim_1': 'x'})
            data.attrs = {'test': 'attr'}

            # Nearest Neighbor
            args = dict(nxny=(nx, ny), dxdy=(1, 1), ll_corner=(0, 0),
                        proj=proj)
            g = Grid(**args)
            odata = g.map_gridded_data(data, g)
            self.assertTrue(odata.shape == data.shape)
            assert_allclose(data, odata, atol=1e-03)

            # Transform can understand a grid
            data.attrs['pyproj_srs'] = g.proj.srs
            odata = g.map_gridded_data(data)
            self.assertTrue(odata.shape == data.shape)
            assert_allclose(data, odata, atol=1e-03)
Example #32
0
    def test_map_gridded_data_over(self):

        # It should work exact same for any projection
        projs = [wgs84, gis.check_crs('epsg:26915')]

        for proj in projs:
            nx, ny = (4, 5)
            data = np.arange(nx * ny).reshape((ny, nx)).astype(np.float)

            in_data = data * np.NaN
            in_data[0, :] = 78

            # Nearest Neighbor
            args = dict(nxny=(nx, ny), dxdy=(1, 1), x0y0=(0, 0), proj=proj)
            g = Grid(**args)
            odata = g.map_gridded_data(data, g, out=data.copy())
            self.assertTrue(odata.shape == data.shape)
            assert_allclose(data, odata, atol=1e-03)

            odata = g.map_gridded_data(in_data, g, out=data.copy())
            self.assertTrue(odata.shape == data.shape)
            assert_allclose(data[1:, :], odata[1:, :], atol=1e-03)
            assert_allclose(odata[0, :], 78, atol=1e-03)

            # Bilinear
            odata = g.map_gridded_data(data,
                                       g,
                                       interp='linear',
                                       out=data.copy())
            self.assertTrue(odata.shape == data.shape)
            assert_allclose(data, odata, atol=1e-03)

            # Spline
            odata = g.map_gridded_data(data,
                                       g,
                                       interp='spline',
                                       out=data.copy())
            self.assertTrue(odata.shape == data.shape)
            assert_allclose(data, odata, atol=1e-03)
Example #33
0
    def test_transform(self):
        """Converting to the grid"""

        # It should work exact same for any projection
        projs = [wgs84, pyproj.Proj(init='epsg:26915')]

        for proj in projs:

            args = dict(nxny=(3, 3), dxdy=(1, 1), ll_corner=(0, 0), proj=proj)

            g = Grid(**args)
            exp_i, exp_j = np.meshgrid(np.arange(3), np.arange(3))
            r_i, r_j = g.transform(exp_i, exp_j, crs=proj)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)
            r_i, r_j = g.transform(exp_i, exp_j, crs=g)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)
            r_i, r_j = g.corner_grid.transform(exp_i, exp_j, crs=proj)
            assert_allclose(exp_i+0.5, r_i, atol=1e-03)
            assert_allclose(exp_j+0.5, r_j, atol=1e-03)
            r_i, r_j = g.corner_grid.transform(exp_i, exp_j, crs=g)
            assert_allclose(exp_i+0.5, r_i, atol=1e-03)
            assert_allclose(exp_j+0.5, r_j, atol=1e-03)

            args['pixel_ref'] = 'corner'
            g = Grid(**args)
            exp_i, exp_j = np.meshgrid(np.arange(3), np.arange(3))
            r_i, r_j = g.transform(exp_i, exp_j, crs=proj)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)
            r_i, r_j = g.transform(exp_i, exp_j, crs=g)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)
            r_i, r_j = g.corner_grid.transform(exp_i, exp_j, crs=proj)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)
            r_i, r_j = g.corner_grid.transform(exp_i, exp_j, crs=g)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)
            r_i, r_j = g.center_grid.transform(exp_i, exp_j, crs=proj)
            assert_allclose(exp_i-0.5, r_i, atol=1e-03)
            assert_allclose(exp_j-0.5, r_j, atol=1e-03)
            r_i, r_j = g.center_grid.transform(exp_i, exp_j, crs=g)
            assert_allclose(exp_i-0.5, r_i, atol=1e-03)
            assert_allclose(exp_j-0.5, r_j, atol=1e-03)
            ex = g.corner_grid.extent
            assert_allclose([0, 3, 0, 3], ex, atol=1e-03)
            assert_allclose(g.center_grid.extent,
                                       g.corner_grid.extent,
                                       atol=1e-03)


            # Masked
            xi = [-0.6, 0.5, 1.2, 2.9, 3.1, 3.6]
            yi = xi
            ex = [-1, 0, 1, 2, 3, 3]
            ey = ex
            r_i, r_j = g.corner_grid.transform(xi, yi, crs=proj)
            assert_allclose(xi, r_i, atol=1e-03)
            assert_allclose(yi, r_j, atol=1e-03)
            r_i, r_j = g.corner_grid.transform(xi, yi, crs=proj, nearest=True)
            assert_array_equal(ex, r_i)
            assert_array_equal(ey, r_j)
            r_i, r_j = g.center_grid.transform(xi, yi, crs=proj, nearest=True)
            assert_array_equal(ex, r_i)
            assert_array_equal(ey, r_j)
            ex = np.ma.masked_array(ex, mask=[1, 0, 0, 0, 1, 1])
            ey = ex
            r_i, r_j = g.center_grid.transform(xi, yi, crs=proj,
                                               nearest=True, maskout=True)
            assert_array_equal(ex, r_i)
            assert_array_equal(ey, r_j)
            assert_array_equal(ex.mask, r_i.mask)
            assert_array_equal(ey.mask, r_j.mask)
            r_i, r_j = g.corner_grid.transform(xi, yi, crs=proj,
                                               nearest=True, maskout=True)
            assert_array_equal(ex, r_i)
            assert_array_equal(ey, r_j)
            assert_array_equal(ex.mask, r_i.mask)
            assert_array_equal(ey.mask, r_j.mask)

            del args['pixel_ref']
            del args['ll_corner']
            args['ul_corner'] = (0, 0)
            args['dxdy'] = (1, -1)
            g = Grid(**args)
            in_i, in_j = np.meshgrid(np.arange(3), -np.arange(3))
            exp_i, exp_j = np.meshgrid(np.arange(3), np.arange(3))
            r_i, r_j = g.transform(in_i, in_j, crs=proj)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)
            in_i, in_j = np.meshgrid(np.arange(3), np.arange(3))
            r_i, r_j = g.transform(in_i, in_j, crs=g)
            assert_allclose(exp_i, r_i, atol=1e-03)
            assert_allclose(exp_j, r_j, atol=1e-03)
Example #34
0
    def test_subset(self):
        """See if simple operations work well"""

        g = Grid(nxny=(3, 3), dxdy=(1, 1), ll_corner=(0, 0), proj=wgs84)
        d = GeoDataset(g)
        self.assertTrue(isinstance(d, GeoDataset))
        self.assertEqual(g, d.grid)

        d.set_subset(corners=([0, 0], [2, 2]), crs=wgs84)
        self.assertEqual(g, d.grid)

        d.set_subset()
        self.assertEqual(g, d.grid)

        d.set_subset(margin=-1)
        lon, lat = d.grid.ll_coordinates
        self.assertEqual(lon, 1)
        self.assertEqual(lat, 1)

        d.set_subset(corners=([0.1, 0.1], [1.9, 1.9]), crs=wgs84)
        self.assertEqual(g, d.grid)

        d.set_subset(corners=([0.51, 0.51], [1.9, 1.9]), crs=wgs84)
        self.assertNotEqual(g, d.grid)

        gm = Grid(nxny=(1, 1), dxdy=(1, 1), ll_corner=(1, 1), proj=wgs84)
        d.set_subset(corners=([1, 1], [1, 1]), crs=wgs84)
        self.assertEqual(gm, d.grid)

        d.set_subset()
        d.set_roi()
        d.set_roi(corners=([1, 1], [1, 1]), crs=wgs84)
        d.set_subset(toroi=True)
        self.assertEqual(gm, d.grid)

        gm = Grid(nxny=(1, 1), dxdy=(1, 1), ll_corner=(2, 2), proj=wgs84)
        d.set_subset(corners=([2, 2], [2, 2]), crs=wgs84)
        self.assertEqual(gm, d.grid)

        with warnings.catch_warnings(record=True) as w:
            # Cause all warnings to always be triggered.
            warnings.simplefilter("always")
            # Trigger a warning.
            d.set_subset(corners=([-4, -4], [5, 5]), crs=wgs84)
            self.assertEqual(g, d.grid)
            # Verify some things
            self.assertEqual(len(w), 2)
            self.assertTrue(issubclass(w[0].category, RuntimeWarning))
            self.assertTrue(issubclass(w[1].category, RuntimeWarning))
            self.assertTrue("x0 out of bounds" in str(w[0].message))
            self.assertTrue("y0 out of bounds" in str(w[1].message))

        self.assertRaises(RuntimeError, d.set_subset, corners=([-1, -1], [-1, -1]))
        self.assertRaises(RuntimeError, d.set_subset, corners=([5, 5], [5, 5]))

        shpf = get_demo_file("Hintereisferner.shp")
        reff = get_demo_file("hef_roi.tif")
        d = GeoTiff(reff)
        d.set_roi(shape=shpf)
        ref = d.get_vardata()
        # same errors as IDL: ENVI is just wrong
        self.assertTrue(np.sum(ref != d.roi) < 9)

        g = Grid(nxny=(3, 3), dxdy=(1, 1), ll_corner=(0, 0), proj=wgs84, pixel_ref="corner")
        p = shpg.Polygon([(1.5, 1.0), (2.0, 1.5), (1.5, 2.0), (1.0, 1.5)])
        roi = g.region_of_interest(geometry=p)
        np.testing.assert_array_equal([[0, 0, 0], [0, 1, 0], [0, 0, 0]], roi)

        d = GeoDataset(g)
        d.set_roi(corners=([1.1, 1.1], [1.9, 1.9]))
        d.set_subset(toroi=True)
        np.testing.assert_array_equal([[1]], d.roi)
        d.set_subset()
        np.testing.assert_array_equal([[0, 0, 0], [0, 1, 0], [0, 0, 0]], d.roi)
        d.set_roi()
        np.testing.assert_array_equal([[0, 0, 0], [0, 0, 0], [0, 0, 0]], d.roi)

        # Raises
        self.assertRaises(RuntimeError, d.set_subset, toroi=True)
Example #35
0
    def set_subset(self, corners=None, crs=wgs84, toroi=False, margin=0):
        """Set a subset for the dataset.
         This will be remembered at later calls to GeoDataset's
         getvardata implementations.
         Parameters
         ----------
         corners: a ((x0, y0), (x1, y1)) tuple of the corners of the square
         to subset the dataset to. The coordinates are not expressed in
         wgs84, set the crs keyword
         crs: the coordinates of the corner coordinates
         toroi: set to true to generate the smallest possible subset arond
         the region of interest set with set_roi()
         margin: when doing the subset, add a margin (can be negative!). Can
         be used alone: set_subset(margin=-5) will remove five pixels from
         each boundary of the dataset.
         TODO: shouldnt we make the toroi stuff easier to use?
         """

        # Useful variables
        mx = self._ogrid.nx-1
        my = self._ogrid.ny-1
        cgrid = self._ogrid.center_grid

        # Three possible cases
        if toroi:
            if self.roi is None or np.max(self.roi) == 0:
                raise RuntimeError('roi is empty.')
            ids = np.nonzero(self.roi)
            sub_x = [np.min(ids[1])-margin, np.max(ids[1])+margin]
            sub_y = [np.min(ids[0])-margin, np.max(ids[0])+margin]
        elif corners is not None:
            xy0, xy1 = corners
            x0, y0 = cgrid.transform(*xy0, crs=crs, nearest=True)
            x1, y1 = cgrid.transform(*xy1, crs=crs, nearest=True)
            sub_x = [np.min([x0, x1])-margin, np.max([x0, x1])+margin]
            sub_y = [np.min([y0, y1])-margin, np.max([y0, y1])+margin]
        else:
            # Reset
            sub_x = [0-margin, mx+margin]
            sub_y = [0-margin, my+margin]

        # Some necessary checks
        if (np.max(sub_x) < 0) or (np.min(sub_x) > mx) or \
           (np.max(sub_y) < 0) or (np.min(sub_y) > my):
            raise RuntimeError('subset not valid')

        if (sub_x[0] < 0) or (sub_x[1] > mx):
            warnings.warn('x0 out of bounds', RuntimeWarning)
        if (sub_y[0] < 0) or (sub_y[1] > my):
            warnings.warn('y0 out of bounds', RuntimeWarning)

        # Make the new grid
        sub_x = np.clip(sub_x, 0, mx)
        sub_y = np.clip(sub_y, 0, my)
        nxny = (sub_x[1] - sub_x[0] + 1, sub_y[1] - sub_y[0] + 1)
        dxdy = (self._ogrid.dx, self._ogrid.dy)
        xy0 = (self._ogrid.x0 + sub_x[0] * self._ogrid.dx,
               self._ogrid.y0 + sub_y[0] * self._ogrid.dy)
        self.grid = Grid(proj=self._ogrid.proj, nxny=nxny, dxdy=dxdy,
                         corner=xy0)
        # If we arrived here, we can safely set the subset
        self.sub_x = sub_x
        self.sub_y = sub_y
Example #36
0
def test_simple_map():
    a = np.zeros((4, 5))
    a[0, 0] = -1
    a[1, 1] = 1.1
    a[2, 2] = 2.2
    a[2, 4] = 1.9
    a[3, 3] = 9
    a_inv = a[::-1, :]
    fs = _create_dummy_shp('fs.shp')

    # UL Corner
    g1 = Grid(nxny=(5, 4),
              dxdy=(1, -1),
              x0y0=(-1, 3),
              proj=wgs84,
              pixel_ref='corner')
    c1 = Map(g1, ny=4, countries=False)

    # LL Corner
    g2 = Grid(nxny=(5, 4),
              dxdy=(1, 1),
              x0y0=(-1, -1),
              proj=wgs84,
              pixel_ref='corner')
    c2 = Map(g2, ny=4, countries=False)

    # Settings
    for c, data in zip([c1, c2], [a_inv, a]):
        c.set_cmap(mpl.cm.get_cmap('jet'))
        c.set_plot_params(levels=[0, 1, 2, 3])
        c.set_data(data)
        c.set_shapefile(fs)
        c.set_lonlat_contours(interval=0.5)

    fig = plt.figure(figsize=(9, 8))
    ax1 = fig.add_subplot(321)
    ax2 = fig.add_subplot(322)
    c1.visualize(ax1)
    c2.visualize(ax2)

    # UL Corner
    c1 = Map(g1, ny=400, countries=False)
    c2 = Map(g2, ny=400, countries=False)
    # Settings
    for c, data, g in zip([c1, c2], [a_inv, a], [g1, g2]):
        c.set_cmap(mpl.cm.get_cmap('jet'))
        c.set_data(data, crs=g)
        c.set_shapefile(fs)
        c.set_plot_params(nlevels=256)
        c.set_lonlat_contours(interval=2)
    ax1 = fig.add_subplot(323)
    ax2 = fig.add_subplot(324)
    c1.visualize(ax1)
    c2.visualize(ax2)

    # Settings
    for c, data in zip([c1, c2], [a_inv, a]):
        c.set_plot_params(nlevels=256, vmax=3)
        c.set_lonlat_contours(interval=1)
        c.set_data(data, interp='linear')
    ax1 = fig.add_subplot(325)
    ax2 = fig.add_subplot(326)
    c1.visualize(ax1)
    c2.visualize(ax2)

    fig.tight_layout()
    if os.path.exists(testdir):
        shutil.rmtree(testdir)
    return fig
Example #37
0
    def test_subset(self):
        """See if simple operations work well"""

        import shapely.geometry as shpg

        g = Grid(nxny=(3, 3), dxdy=(1, 1), x0y0=(0, 0), proj=wgs84)
        d = GeoDataset(g)
        self.assertTrue(isinstance(d, GeoDataset))
        self.assertEqual(g, d.grid)

        d.set_subset(corners=([0, 0], [2, 2]), crs=wgs84)
        self.assertEqual(g, d.grid)

        d.set_subset()
        self.assertEqual(g, d.grid)

        d.set_subset(margin=-1)
        lon, lat = d.grid.ll_coordinates
        self.assertEqual(lon, 1)
        self.assertEqual(lat, 1)

        d.set_subset(corners=([0.1, 0.1], [1.9, 1.9]), crs=wgs84)
        self.assertEqual(g, d.grid)

        d.set_subset(corners=([0.51, 0.51], [1.9, 1.9]), crs=wgs84)
        self.assertNotEqual(g, d.grid)

        gm = Grid(nxny=(1, 1), dxdy=(1, 1), x0y0=(1, 1), proj=wgs84)
        d.set_subset(corners=([1, 1], [1, 1]), crs=wgs84)
        self.assertEqual(gm, d.grid)

        d.set_subset()
        d.set_roi()
        d.set_roi(corners=([1, 1], [1, 1]), crs=wgs84)
        d.set_subset(toroi=True)
        self.assertEqual(gm, d.grid)

        gm = Grid(nxny=(1, 1), dxdy=(1, 1), x0y0=(2, 2), proj=wgs84)
        d.set_subset(corners=([2, 2], [2, 2]), crs=wgs84)
        self.assertEqual(gm, d.grid)

        with warnings.catch_warnings(record=True) as w:
            # Cause all warnings to always be triggered.
            warnings.simplefilter("always")
            # Trigger a warning.
            d.set_subset(corners=([-4, -4], [5, 5]), crs=wgs84)
            self.assertEqual(g, d.grid)
            # Verify some things
            assert len(w) >= 2

        self.assertRaises(RuntimeError,
                          d.set_subset,
                          corners=([-1, -1], [-1, -1]))
        self.assertRaises(RuntimeError, d.set_subset, corners=([5, 5], [5, 5]))

        shpf = get_demo_file('Hintereisferner.shp')
        reff = get_demo_file('hef_roi.tif')
        d = GeoTiff(reff)
        d.set_roi(shape=shpf)
        ref = d.get_vardata()
        # same errors as IDL: ENVI is just wrong
        self.assertTrue(np.sum(ref != d.roi) < 9)

        g = Grid(nxny=(3, 3),
                 dxdy=(1, 1),
                 x0y0=(0, 0),
                 proj=wgs84,
                 pixel_ref='corner')
        p = shpg.Polygon([(1.5, 1.), (2., 1.5), (1.5, 2.), (1., 1.5)])
        roi = g.region_of_interest(geometry=p)
        np.testing.assert_array_equal([[0, 0, 0], [0, 1, 0], [0, 0, 0]], roi)

        d = GeoDataset(g)
        d.set_roi(corners=([1.1, 1.1], [1.9, 1.9]))
        d.set_subset(toroi=True)
        np.testing.assert_array_equal([[1]], d.roi)
        d.set_subset()
        np.testing.assert_array_equal([[0, 0, 0], [0, 1, 0], [0, 0, 0]], d.roi)
        d.set_roi()
        np.testing.assert_array_equal([[0, 0, 0], [0, 0, 0], [0, 0, 0]], d.roi)

        # Raises
        self.assertRaises(RuntimeError, d.set_subset, toroi=True)
Example #38
0
    def test_roi(self):

        import shapely.geometry as shpg

        g = Grid(nxny=(3, 3), dxdy=(1, 1), ll_corner=(0, 0), proj=wgs84,
                 pixel_ref='corner')
        p = shpg.Polygon([(1.5, 1.), (2., 1.5), (1.5, 2.), (1., 1.5)])
        roi = g.region_of_interest(geometry=p)
        np.testing.assert_array_equal([[0,0,0],[0,1,0],[0,0,0]], roi)

        roi = g.region_of_interest(corners=([0, 0], [2, 2]), crs=wgs84)
        np.testing.assert_array_equal([[1, 1, 1], [1, 1, 1], [1, 1, 1]], roi)

        roi = g.region_of_interest(corners=([1.3, 1.3], [1.7, 1.7]), crs=wgs84)
        np.testing.assert_array_equal([[0,0,0],[0,1,0],[0,0,0]], roi)

        roi = g.region_of_interest()
        np.testing.assert_array_equal([[0,0,0],[0,0,0],[0,0,0]], roi)

        mask = [[0,0,0],[0,1,0],[0,0,0]]
        roi = g.region_of_interest(roi=mask)
        np.testing.assert_array_equal([[0,0,0],[0,1,0],[0,0,0]], roi)

        nc = np.array(p.exterior.coords) + 0.1
        p = shpg.Polygon(nc)
        roi = g.region_of_interest(geometry=p, roi=roi)
        np.testing.assert_array_equal([[0,0,0],[0,1,0],[0,0,0]], roi)

        nc = np.array(p.exterior.coords) + 0.5
        p = shpg.Polygon(nc)
        roi = g.region_of_interest(geometry=p, roi=roi)
        np.testing.assert_array_equal([[0,0,0],[0,1,0],[0,0,0]], roi)

        nc = np.array(p.exterior.coords) + 0.5
        p = shpg.Polygon(nc)
        roi = g.region_of_interest(geometry=p, roi=roi)
        np.testing.assert_array_equal([[0,0,0],[0,1,0],[0,0,1]], roi)

        g = Grid(nxny=(4, 2), dxdy=(1, 1), ll_corner=(0, 0), proj=wgs84,
                 pixel_ref='corner')
        p = shpg.Polygon([(1.5, 1.), (2., 1.5), (1.5, 2.), (1., 1.5)])
        roi = g.region_of_interest(geometry=p)
        np.testing.assert_array_equal([[0,0,0,0],[0,1,0,0]], roi)

        g = Grid(nxny=(2, 4), dxdy=(1, 1), ll_corner=(0, 0), proj=wgs84,
                 pixel_ref='corner')
        p = shpg.Polygon([(1.5, 1.), (2., 1.5), (1.5, 2.), (1., 1.5)])
        roi = g.region_of_interest(geometry=p)
        np.testing.assert_array_equal([[0,0], [0,1], [0,0], [0,0]], roi)

        g = Grid(nxny=(3, 3), dxdy=(1, 1), ll_corner=(0, 0), proj=wgs84,
                 pixel_ref='corner')
        g2 = Grid(nxny=(1, 1), dxdy=(0.2, 0.2), ll_corner=(1.4, 1.4),
                  proj=wgs84, pixel_ref='corner')
        roi = g.region_of_interest(grid=g2)
        np.testing.assert_array_equal([[0,0,0],[0,1,0],[0,0,0]], roi)