def testContains(self): rect1 = QgsRectangle(0.0, 0.0, 5.0, 5.0) rect2 = QgsRectangle(2.0, 2.0, 7.0, 7.0) pnt1 = QgsPoint(4.0, 4.0) pnt2 = QgsPoint(6.0, 2.0) rect3 = rect1.intersect(rect2) myMessage = "Expected: %s\nGot: %s\n" % (True, rect1.contains(rect3)) assert rect1.contains(rect3), myMessage myMessage = "Expected: %s\nGot: %s\n" % (True, rect2.contains(rect3)) assert rect2.contains(rect3), myMessage # test for point myMessage = "Expected: %s\nGot: %s\n" % (True, rect1.contains(pnt1)) assert rect1.contains(pnt1), myMessage myMessage = "Expected: %s\nGot: %s\n" % (True, rect2.contains(pnt1)) assert rect2.contains(pnt1), myMessage myMessage = "Expected: %s\nGot: %s\n" % (True, rect3.contains(pnt1)) assert rect3.contains(pnt1), myMessage myMessage = "Expected: %s\nGot: %s\n" % (False, rect1.contains(pnt2)) self.assertFalse(rect1.contains(pnt2), myMessage) myMessage = "Expected: %s\nGot: %s\n" % (True, rect2.contains(pnt2)) assert rect2.contains(pnt2), myMessage myMessage = "Expected: %s\nGot: %s\n" % (False, rect3.contains(pnt2)) self.assertFalse(rect3.contains(pnt2), myMessage) myMessage = "Expected: %s\nGot: %s\n" % (True, rect3.contains(pnt1)) self.assertTrue(rect3.contains(pnt1), myMessage)
def testContains(self): rect1 = QgsRectangle(0.0, 0.0, 5.0, 5.0) rect2 = QgsRectangle(2.0, 2.0, 7.0, 7.0) pnt1 = QgsPointXY(4.0, 4.0) pnt2 = QgsPointXY(6.0, 2.0) rect3 = rect1.intersect(rect2) myMessage = ('Expected: %s\nGot: %s\n' % (True, rect1.contains(rect3))) assert rect1.contains(rect3), myMessage myMessage = ('Expected: %s\nGot: %s\n' % (True, rect2.contains(rect3))) assert rect2.contains(rect3), myMessage # test for point myMessage = ('Expected: %s\nGot: %s\n' % (True, rect1.contains(pnt1))) assert rect1.contains(pnt1), myMessage myMessage = ('Expected: %s\nGot: %s\n' % (True, rect2.contains(pnt1))) assert rect2.contains(pnt1), myMessage myMessage = ('Expected: %s\nGot: %s\n' % (True, rect3.contains(pnt1))) assert rect3.contains(pnt1), myMessage myMessage = ('Expected: %s\nGot: %s\n' % (False, rect1.contains(pnt2))) self.assertFalse(rect1.contains(pnt2), myMessage) myMessage = ('Expected: %s\nGot: %s\n' % (True, rect2.contains(pnt2))) assert rect2.contains(pnt2), myMessage myMessage = ('Expected: %s\nGot: %s\n' % (False, rect3.contains(pnt2))) self.assertFalse(rect3.contains(pnt2), myMessage) myMessage = ('Expected: %s\nGot: %s\n' % (True, rect3.contains(pnt1))) self.assertTrue(rect3.contains(pnt1), myMessage)
def testContains(self): rect1 = QgsRectangle(0.0, 0.0, 5.0, 5.0) rect2 = QgsRectangle(2.0, 2.0, 7.0, 7.0) pnt1 = QgsPointXY(4.0, 4.0) pnt2 = QgsPointXY(6.0, 2.0) rect3 = rect1.intersect(rect2) self.assertTrue(rect1.contains(rect3)) self.assertTrue(rect2.contains(rect3)) # test for point self.assertTrue(rect1.contains(pnt1)) self.assertTrue(rect1.contains(pnt1.x(), pnt1.y())) self.assertTrue(rect2.contains(pnt1)) self.assertTrue(rect2.contains(pnt1.x(), pnt1.y())) self.assertTrue(rect3.contains(pnt1)) self.assertTrue(rect3.contains(pnt1.x(), pnt1.y())) self.assertFalse(rect1.contains(pnt2)) self.assertFalse(rect1.contains(pnt2.x(), pnt2.y())) self.assertTrue(rect2.contains(pnt2)) self.assertTrue(rect2.contains(pnt2.x(), pnt2.y())) self.assertFalse(rect3.contains(pnt2)) self.assertFalse(rect3.contains(pnt2.x(), pnt2.y())) self.assertTrue(rect3.contains(pnt1)) self.assertTrue(rect3.contains(pnt1.x(), pnt1.y()))
def testIntersection(self): rect1 = QgsRectangle(0.0, 0.0, 5.0, 5.0) rect2 = QgsRectangle(2.0, 2.0, 7.0, 7.0) self.assertTrue(rect1.intersects(rect2)) rect3 = rect1.intersect(rect2) self.assertFalse(rect3.isEmpty(), "Empty rectangle returned") self.assertEqual(rect3.width(), 3.0) self.assertEqual(rect3.height(), 3.0)
def testIntersection(self): rect1 = QgsRectangle(0.0, 0.0, 5.0, 5.0) rect2 = QgsRectangle(2.0, 2.0, 7.0, 7.0) myMessage = "Expected: %s\nGot: %s\n" % (True, rect1.intersects(rect2)) assert rect1.intersects(rect2), myMessage rect3 = rect1.intersect(rect2) self.assertFalse(rect3.isEmpty(), "Empty rectangle returned") myMessage = "Expected: %s\nGot: %s\n" % (3.0, rect3.width()) assert rect3.width() == 3.0, myMessage myMessage = "Expected: %s\nGot: %s\n" % (3.0, rect3.height()) assert rect3.height() == 3.0, myMessage
def testIntersection(self): rect1 = QgsRectangle(0.0, 0.0, 5.0, 5.0) rect2 = QgsRectangle(2.0, 2.0, 7.0, 7.0) myMessage = ('Expected: %s\nGot: %s\n' % (True, rect1.intersects(rect2))) assert rect1.intersects(rect2), myMessage rect3 = rect1.intersect(rect2) self.assertFalse(rect3.isEmpty(), "Empty rectangle returned") myMessage = ('Expected: %s\nGot: %s\n' % (3.0, rect3.width())) assert rect3.width() == 3.0, myMessage myMessage = ('Expected: %s\nGot: %s\n' % (3.0, rect3.height())) assert rect3.height() == 3.0, myMessage
def build_navigation(self, tile_size, nav_mode, polygons=None, points=None): # define type of navigation if polygons: self.nav_type = "polygons" self.polygons = polygons elif points: self.nav_type = "points" self.points = points else: self.nav_type = "whole" # clear before build self.delete() # compute the tiles in whole image or covering the polygons if self.nav_type in ["whole", "polygons"]: # define the extent to compute the tiles if self.nav_type == "whole": rectangle_nav = self.layer_to_edit.qgs_layer.extent() if self.nav_type == "polygons": # compute the wrapper extent of all polygons rectangle_nav = QgsRectangle() for polygon in self.polygons: rectangle_nav.combineExtentWith(polygon.boundingBox()) # intersect with the layer to edit rectangle_nav = rectangle_nav.intersect(self.layer_to_edit.extent()) # number of tiles nx_tiles = ceil((rectangle_nav.xMaximum() - rectangle_nav.xMinimum()) / tile_size) ny_tiles = ceil((rectangle_nav.yMaximum() - rectangle_nav.yMinimum()) / tile_size) idx_tile = 1 # left-right and top-bottom if nav_mode == "horizontal": x_left_right = [] for y_tile in range(ny_tiles): for x_tile in range(nx_tiles): if y_tile % 2 == 0: # left to right xmin = rectangle_nav.xMinimum() + x_tile * tile_size xmax = xmin + tile_size x_left_right.append((xmin, xmax)) else: # right to left xmin, xmax = x_left_right.pop() ymax = rectangle_nav.yMaximum() - y_tile * tile_size ymin = ymax - tile_size # fix/adjust right and bottom tiles borders if xmax > rectangle_nav.xMaximum(): xmax = rectangle_nav.xMaximum() if ymin < rectangle_nav.yMinimum(): ymin = rectangle_nav.yMinimum() tile = Tile(idx_tile, xmin, xmax, ymin, ymax, self.tiles_color) if tile.is_valid(self): self.tiles.append(tile) idx_tile += 1 # top-bottom and left-right if nav_mode == "vertical": y_top_bottom = [] for x_tile in range(nx_tiles): for y_tile in range(ny_tiles): if x_tile % 2 == 0: # top to bottom ymax = rectangle_nav.yMaximum() - y_tile * tile_size ymin = ymax - tile_size y_top_bottom.append((ymin, ymax)) else: # bottom to top ymin, ymax = y_top_bottom.pop() xmin = rectangle_nav.xMinimum() + x_tile * tile_size xmax = xmin + tile_size # fix/adjust right and bottom tiles borders if xmax > rectangle_nav.xMaximum(): xmax = rectangle_nav.xMaximum() if ymin < rectangle_nav.yMinimum(): ymin = rectangle_nav.yMinimum() tile = Tile(idx_tile, xmin, xmax, ymin, ymax, self.tiles_color) if tile.is_valid(self): self.tiles.append(tile) idx_tile += 1 # compute the tiles using the points as center of the tiles if self.nav_type == "points": idx_tile = 1 # left-right and top-bottom if nav_mode == "horizontal": points_sorted = sorted(self.points, key=lambda p: (-p.y(), p.x())) # top-bottom and left-right if nav_mode == "vertical": points_sorted = sorted(self.points, key=lambda p: (p.x(), -p.y())) for point in points_sorted: # check if point is inside layer to edit if not self.layer_to_edit.extent().contains(point): continue xmin = point.x() - tile_size/2 xmax = point.x() + tile_size/2 ymin = point.y() - tile_size/2 ymax = point.y() + tile_size/2 tile = Tile(idx_tile, xmin, xmax, ymin, ymax, self.tiles_color) if tile.is_valid(self): self.tiles.append(tile) idx_tile += 1 # show all tiles in build navigation canvas dialog from ThRasE.core.edition import LayerToEdit [tile.create(LayerToEdit.current.navigation_dialog.render_widget.canvas, line_width=1, rbs_in="nav_dialog") for tile in self.tiles] # init self.current_tile = next((tile for tile in self.tiles if tile.idx == 1), None) return True if self.current_tile is not None else False