def test_small_polygon(): bboxes = vl.boundingboxes() shp_px = rd.to_pixels(bboxes)[0] grid = rd.to_geometry_grid(*shp_px.bounds) areas = {} for i, b in grid.iteritems(): diff = b.Intersection(to_geometry(shp, proj=rd.proj)) areas[i] = diff.GetArea() total_area = sum(areas.values()) index = areas.keys() if total_area > 0: weights = np.array([areas[k] / total_area for k in index]) else: weights = np.zeros(len(index)) assert abs(total_area - shp.GetArea()) < 1e-8 assert abs(1 - sum(weights)) < 1e-8 values = rd.get_values_for_pixels(np.array(index)) assert_array_almost_equal(values, np.array([121, 176, 176, 176], dtype=np.uint8)) classes = np.arange(0, 256) exp_stats = [0.114183, 0.885817] exp_classes = [121, 176] for r in rd.query(vl): stats = compute_stats(r.values, r.weights) assert_array_almost_equal(exp_classes, classes[stats > 0]) assert_array_almost_equal(exp_stats, stats[stats > 0])
def test_small_polygon(): bboxes = vl.boundingboxes() shp_px = rd.to_pixels(bboxes)[0] grid = rd.to_geometry_grid(*shp_px.bounds) areas = {} for i, b in grid.iteritems(): diff = b.Intersection(to_geometry(shp, proj=rd.proj)) areas[i] = diff.GetArea() total_area = sum(areas.values()) index = areas.keys() if total_area > 0: weights = np.array([areas[k]/total_area for k in index]) else: weights = np.zeros(len(index)) assert abs(total_area - shp.GetArea()) < 1e-8 assert abs(1 - sum(weights)) < 1e-8 values = rd.get_values_for_pixels(np.array(index)) assert_array_almost_equal(values, np.array([121, 176, 176, 176], dtype=np.uint8)) classes = np.arange(0, 256) exp_stats = [0.114183, 0.885817] exp_classes = [121, 176] for r in rd.query(vl): stats = compute_stats(r.values, r.weights) assert_array_almost_equal(exp_classes, classes[stats > 0]) assert_array_almost_equal(exp_stats, stats[stats > 0])
def _small_pixel_query(self, shp, shp_px): grid = self.to_geometry_grid(*shp_px.bounds) areas = {} for i, b in grid.items(): if b.Intersects(to_geometry(shp, proj=self.proj)): diff = b.Intersection(to_geometry(shp, proj=self.proj)) areas[i] = diff.GetArea() index = areas.keys() total_area = sum(areas.values()) if total_area > 0: weights = np.array([areas[k] / self.pixel_area for k in index]) else: weights = np.zeros(len(index)) values = self.get_values_for_pixels(np.array(index)) return values, weights
def _small_pixel_query(self, shp, shp_px): grid = self.to_geometry_grid(*shp_px.bounds) areas = {} for i, b in grid.items(): if b.Intersects(to_geometry(shp, proj=self.proj)): diff = b.Intersection(to_geometry(shp, proj=self.proj)) areas[i] = diff.GetArea() index = areas.keys() total_area = sum(areas.values()) if total_area > 0: weights = np.array([areas[k]/self.pixel_area for k in index]) else: weights = np.zeros(len(index)) values = self.get_values_for_pixels(np.array(index)) return values, weights
def bbox(self): """Returns bounding box of raster in raster coordinates. Returns ------- ogr.Geometry Bounding box in raster coordinates: (xmin : float minimum longitude (leftmost) ymin : float minimum latitude (bottom) xmax : float maximum longitude (rightmost) ymax : float maximum latitude (top)) """ (xmin, xmax, ymin, ymax) = self.get_extent() return to_geometry(box(xmin, ymin, xmax, ymax), proj=self.proj)