def test_buffer_plot_metrics(self): ba = pls.BufferAnalysis(self.landscape_fp, self.geom, self.buffer_dists, base_mask_crs=self.geom_crs) # test for `None` (landscape-level) and an existing class (class-level) for class_val in [None, ba.classes[0]]: # test that the x data of the line corresponds to `buffer_dists` self.assertTrue( np.all( ba.plot_metric('patch_density', class_val=class_val).lines[ 0].get_xdata() == self.buffer_dists))
def test_buffer_init(self): naive_gser = gpd.GeoSeries([self.geom]) gser = gpd.GeoSeries([self.geom], crs=self.geom_crs) # test that we cannot init from a shapely geometry without providing # its crs self.assertRaises(ValueError, pls.BufferAnalysis, self.landscape_fp, self.geom, self.buffer_dists) # test that we cannot init with a landscape that does not have crs and # transform information, even when providing the `base_mask` arguments # properly for base_mask in [self.geom, naive_gser, gser]: self.assertRaises(ValueError, pls.BufferAnalysis, self.landscape, base_mask, self.buffer_dists, {'base_mask_crs': self.geom_crs}) self.assertRaises( ValueError, pls.BufferAnalysis, self.landscape, base_mask, self.buffer_dists, { 'base_mask_crs': self.geom_crs, 'landscape_crs': self.landscape_crs }) self.assertRaises( ValueError, pls.BufferAnalysis, self.landscape, base_mask, self.buffer_dists, { 'base_mask_crs': self.geom_crs, 'landscape_transform': self.landscape_transform }) # test that we can properly instantiate it from: # 1. a landscape filepath, shapely geometry, and its crs # 2. a landscape filepath, naive geopandas GeoSeries (with no crs set) # and its crs # 3. a landscape filepath, geopandas GeoSeries with crs set # 4. a landscape filepath, geopandas GeoSeries with crs set and a crs ( # which will override the crs of the GeoSeries) # 5. any of the above but changing the landscape filepath for a # Landscape instance with its crs and transform for ba in [ pls.BufferAnalysis(self.landscape_fp, self.geom, self.buffer_dists, base_mask_crs=self.geom_crs), pls.BufferAnalysis(self.landscape_fp, naive_gser, self.buffer_dists, base_mask_crs=self.geom_crs), pls.BufferAnalysis(self.landscape_fp, gser, self.buffer_dists), pls.BufferAnalysis(self.landscape_fp, gser, self.buffer_dists, base_mask_crs=self.geom_crs), pls.BufferAnalysis( self.landscape, gser, self.buffer_dists, base_mask_crs=self.geom_crs, landscape_crs=self.landscape_crs, landscape_transform=self.landscape_transform) ]: self.assertEqual(ba.feature_name, 'buffer_dists') self.assertEqual(len(ba), len(ba.masks_arr)) self.assertEqual(len(ba), len(ba.buffer_dists)) # test that we cannot instantiate a `BufferAnalysis` with # `buffer_rings=True` if `base_mask` is a polygon or a GeoSeries # containing a polygon polygon = self.geom.buffer(1000) # this will return a polygon instance self.assertRaises(ValueError, pls.BufferAnalysis, self.landscape_fp, polygon, self.buffer_dists, { 'buffer_rings': True, 'base_mask_crs': self.geom_crs }) polygon_gser = gpd.GeoSeries([polygon], crs=self.geom_crs) self.assertRaises(ValueError, pls.BufferAnalysis, self.landscape_fp, polygon_gser, self.buffer_dists, { 'buffer_rings': True, }) # test that otherwise, buffer rings are properly instantiated ba_rings = pls.BufferAnalysis(self.landscape_fp, gser, self.buffer_dists, buffer_rings=True) # the `buffer_dists` attribute must be a string of the form '{r}-{R}' # where r and R respectively represent the smaller and larger radius # that compose each ring for buffer_ring_str in ba_rings.buffer_dists: self.assertIn('-', buffer_ring_str) # compare it with the default instance (with the argument # `buffer_rings=False`) that does not consider rings but cumulatively # considers the inner areas in each mask. The first mask will in fact # be the same in both cases (the region that goes from 0 to the first # item of `self.buffer_dists`), but the successive masks will be # always larger in the default instance (since they will have the # surface of the corresponding ring plus the surface of the inner # region that is excluded when `buffer_rings=True`) ba = pls.BufferAnalysis(self.landscape_fp, gser, self.buffer_dists) for mask_arr, ring_mask_arr in zip(ba.masks_arr, ba_rings.masks_arr): self.assertGreaterEqual(np.sum(mask_arr), np.sum(ring_mask_arr))