def test_select_polygon(self): surface = self.small_surface() pointset = GeoPointset.fromSurface(surface) georegion = GeoRegion(pointset) self.assertEqual(0, len(georegion)) points = [(-0.1, 2.0), (1.9, 8.1), (6.1, 8.1), (9.1, 5), (7.1, 0.9)] polygon = CPolyline(name='test_polygon', init_points=points) picked = 52 # https://www.futilitycloset.com/2013/04/24/picks-theorem/ georegion.select_inside(polygon) self.assertEqual(picked, len(georegion)) georegion.deselect_inside(polygon) self.assertEqual(0, len(georegion)) georegion.select_outside(polygon) self.assertEqual(len(surface) - picked, len(georegion)) georegion.deselect_outside(polygon) self.assertEqual(0, len(georegion)) georegion.select_inside(polygon) georegion.select_outside(polygon) self.assertEqual(len(surface), len(georegion)) georegion.deselect_inside(polygon) georegion.deselect_outside(polygon) self.assertEqual(0, len(georegion)) georegion.select_inside(polygon) self.assertEqual(picked, len(georegion)) internal_square = [(2.5, 2.5), (2.5, 6.5), (6.5, 6.5), (6.5, 2.5)] georegion.deselect_inside(CPolyline(init_points=internal_square)) self.assertEqual(picked - 4 * 4, len(georegion)) # internal square is 4x4
def test_polyline_intersection(self): grid = EclGrid.createRectangular((100, 100, 10), (0.25, 0.25, 1)) # Fault1 Fault4 # | | # | | # | | # | ------- Fault2 | # | | # | | # (5 , 2.50) # -------- Fault3 # fault1 = Fault(grid, "Fault1") fault2 = Fault(grid, "Fault2") fault3 = Fault(grid, "Fault3") fault4 = Fault(grid, "Fault4") fault1.addRecord(1, 1, 10, grid.getNY() - 1, 0, 0, "X") fault2.addRecord(5, 10, 15, 15, 0, 0, "Y") fault3.addRecord(5, 10, 5, 5, 0, 0, "Y") fault4.addRecord(20, 20, 10, grid.getNY() - 1, 0, 0, "X") polyline = Polyline(init_points=[(4, 4), (8, 4)]) self.assertTrue(fault4.intersectsPolyline(polyline, 0)) cpolyline = CPolyline(init_points=[(4, 4), (8, 4)]) self.assertTrue(fault4.intersectsPolyline(cpolyline, 0)) polyline = Polyline(init_points=[(8, 4), (16, 4)]) self.assertFalse(fault4.intersectsPolyline(polyline, 0)) cpolyline = CPolyline(init_points=[(8, 4), (16, 4)]) self.assertFalse(fault4.intersectsPolyline(cpolyline, 0))
def test_extend_to_polyline(self): grid = EclGrid.create_rectangular( (3,3,1) , (1 , 1 , 1)) # o o o o # # o---o---o---o # # o===+ o o # | # o o o o fault1 = Fault(grid , "Fault") fault1.addRecord(0 , 0 , 0 , 0 , 0 , 0 , "X-") fault1.addRecord(0 , 0 , 0 , 0 , 0 , 0 , "Y") polyline = CPolyline( init_points = [(0,2) , (3,2)]) points = fault1.extendToPolyline( polyline , 0 ) self.assertEqual( points , [(1,1) , (2,2)]) end_join = fault1.endJoin( polyline , 0 ) self.assertEqual( end_join, [(1,1) , (0,2)] ) polyline2 = CPolyline( init_points = [(0.8,2) , (0.8,0.8)]) end_join = fault1.endJoin( polyline2 , 0 ) self.assertIsNone( end_join )
def test_extend_polyline_on(self): grid = EclGrid.create_rectangular( (3,3,1) , (1 , 1 , 1)) # o o o o # # o---o---o---o # # o===o===o===o # # o o o o fault1 = Fault(grid , "Fault") fault1.addRecord(0 , 2 , 0 , 0 , 0 , 0 , "Y") polyline0 = CPolyline( init_points = [(0,2)]) polyline1 = CPolyline( init_points = [(0,2) , (3,2)]) polyline2 = CPolyline( init_points = [(1,3) , (1,2)]) polyline3 = CPolyline( init_points = [(1,3) , (1,0)]) with self.assertRaises(ValueError): fault1.extendPolylineOnto( polyline0 , 0 ) points = fault1.extendPolylineOnto( polyline1 , 0 ) self.assertIsNone( points ) points = fault1.extendPolylineOnto( polyline2 , 0) self.assertEqual( points , [(1,2) , (1,1)]) points = fault1.extendPolylineOnto( polyline3 , 0) self.assertIsNone( points )
def test_equal(self): pl1 = CPolyline(name="Poly1", init_points=[(0, 0), (1, 1), (2, 2)]) pl2 = CPolyline(name="Poly2", init_points=[(0, 0), (1, 1), (2, 2)]) pl3 = CPolyline(init_points=[(0, 0), (1, 1), (2, 3)]) self.assertEqual(pl1, pl1) self.assertEqual(pl1, pl2) self.assertFalse(pl1 == pl3)
def test_intersects(self): polyline1 = CPolyline( init_points = [(0,0), (1,0) , (1,1)]) polyline2 = CPolyline( init_points = [(0.50,0.50) , (1.50,0.50)]) polyline3 = Polyline( init_points = [(0.50,0.50) , (1.50,0.50)]) polyline4 = CPolyline( init_points = [(0.50,1.50) , (1.50,1.50)]) self.assertTrue( polyline1.intersects( polyline2 )) self.assertTrue( polyline1.intersects( polyline3 )) self.assertFalse( polyline1.intersects( polyline4 ))
def test_front(self): polyline = CPolyline() polyline.addPoint( 1 , 1 ) polyline.addPoint( 0 , 0 , front = True ) self.assertEqual( len(polyline) , 2 ) x,y = polyline[0] self.assertEqual(x,0) self.assertEqual(y,0) x,y = polyline[1] self.assertEqual(x,1) self.assertEqual(y,1)
def test_extend_to_bbox(self): bbox = [(0, 0), (10, 0), (10, 10), (0, 10)] polyline = CPolyline(init_points=[(11, 11), (13, 13)]) with self.assertRaises(ValueError): polyline.extendToBBox(bbox, start=False) polyline = CPolyline(init_points=[(1, 1), (3, 3)]) line1 = polyline.extendToBBox(bbox, start=True) self.assertEqual(line1, CPolyline(init_points=[(1, 1), (0, 0)])) line1 = polyline.extendToBBox(bbox, start=False) self.assertEqual(line1, CPolyline(init_points=[(3, 3), (10, 10)]))
def select_outside_polygon(self, points, intersect=False): """ Will select all points outside polygon. See select_inside_polygon for more docuemntation. """ cfunc.select_outside_polygon(self, CPolyline(init_points=points))
def test_join__polylines(self): l1 = Polyline(init_points=[(0, 1), (1, 1)]) l2 = CPolyline(init_points=[(2, -1), (2, 0)]) l3 = CPolyline(init_points=[(2, 2), (2, 3)]) l4 = Polyline() l5 = CPolyline(init_points=[(0.5, 0), (0.5, 2)]) with self.assertRaises(ValueError): GeometryTools.joinPolylines(l1, l4) with self.assertRaises(ValueError): GeometryTools.joinPolylines(l4, l1) self.assertIsNone(GeometryTools.joinPolylines(l1, l5)) self.assertEqual(GeometryTools.joinPolylines(l1, l2), [(1, 1), (2, 0)])
def deselect_outside_polygon(self, points): """ Will select all points outside polygon. See select_inside_polygon for more docuemntation. """ self._deselect_outside_polygon(CPolyline(init_points=points))
def test_join_extend_polylines_onto(self): l1 = Polyline(init_points=[(0, 1), (1, 1)]) l2 = CPolyline(init_points=[(2, 0), (2, 2)]) l3 = CPolyline(init_points=[(0.5, 0), (0.5, 2)]) l4 = Polyline(init_points=[(0, 5), (1, 5)]) l5 = Polyline(init_points=[(0, 5)]) self.assertIsNone(GeometryTools.connectPolylines(l1, l3)) with self.assertRaises(ValueError): GeometryTools.connectPolylines(l1, l5) with self.assertRaises(ValueError): GeometryTools.connectPolylines(l1, l4) self.assertEqual(GeometryTools.connectPolylines(l1, l2), [(1, 1), (2, 1)])
def test_add_polyline_barrier(self): d = 10 layer = Layer(d,d) grid = EclGrid.createRectangular( (d,d,1) , (1,1,1) ) pl = CPolyline( init_points = [(0 , 0) , (d/2 , d/2) , (d,d)]) layer.addPolylineBarrier( pl , grid , 0) for i in range(d): self.assertTrue( layer.bottomBarrier(i,i) ) if i < (d - 1): self.assertTrue( layer.leftBarrier(i+1,i) )
def test_item(self): polyline = CPolyline() polyline.addPoint( 10 , 20 ) self.assertEqual( len(polyline) , 1 ) with self.assertRaises(TypeError): (x,y) = polyline["KEY"] with self.assertRaises(IndexError): (x,y) = polyline[10] (x,y) = polyline[0] self.assertEqual( x , 10 ) self.assertEqual( y , 20 ) polyline.addPoint(20,20) (x,y) = polyline[-1] self.assertEqual( x , 20 ) self.assertEqual( y , 20 )
def create_coll2(self): coll1 = self.create_collection() coll2 = coll1.shallowCopy() coll1.addPolyline( CPolyline( name = "POLY3" , init_points = [(1,1) , (2,2) , (1,3) , (1,1)])) self.assertEqual(len(coll1) , 3) self.assertTrue( "POLY3" in coll1 ) self.assertEqual(len(coll2) , 2) self.assertFalse( "POLY3" in coll2 ) return coll2
def test_add_polyline(self): pc = CPolylineCollection() pl = pc.createPolyline( name = "TestP" ) self.assertTrue( isinstance(pl , CPolyline)) self.assertEqual(len(pc) , 1) self.assertTrue( "TestP" in pc ) with self.assertRaises(IndexError): pl = pc[2] p0 = pc[0] self.assertTrue( p0 == pl ) with self.assertRaises(KeyError): pn = pc["missing"] pn = pc["TestP"] self.assertTrue( pn == pl ) px = CPolyline( name = "TestP") with self.assertRaises(KeyError): pc.addPolyline( px ) self.assertEqual(len(pc) , 1) p2 = CPolyline( name = "Poly2") pc.addPolyline( p2 ) self.assertEqual( len(pc) , 2 ) self.assertTrue( "Poly2" in pc ) l = [] for p in pc: l.append(p) self.assertEqual( len(pc) , 2 ) points = [(0,1) , (1,1)] pc.addPolyline( points , name = "XYZ") self.assertTrue( "XYZ" in pc )
def test_intersects(self): polyline1 = CPolyline(init_points=[(0, 0), (1, 0), (1, 1)]) polyline2 = CPolyline(init_points=[(0.50, 0.50), (1.50, 0.50)]) polyline3 = Polyline(init_points=[(0.50, 0.50), (1.50, 0.50)]) polyline4 = CPolyline(init_points=[(0.50, 1.50), (1.50, 1.50)]) self.assertTrue(polyline1.intersects(polyline2)) self.assertTrue(polyline1.intersects(polyline3)) self.assertFalse(polyline1.intersects(polyline4))
def test_cross_segment(self): polyline = CPolyline(init_points=[(0, 0), (1, 0), (1, 1)]) # # x # | # | # | # x-------x # self.assertTrue(polyline.segmentIntersects((0.5, 0.5), (0.5, -0.5))) self.assertTrue(polyline.segmentIntersects((0.5, 0.5), (1.5, 0.5))) self.assertFalse(polyline.segmentIntersects((0.5, 0.5), (0.5, 1.5))) self.assertFalse(polyline.segmentIntersects((0.5, 0.5), (-0.5, 0.5))) self.assertFalse(polyline.segmentIntersects((0.5, 1.5), (1.5, 1.5))) self.assertTrue(polyline.segmentIntersects((1.0, 1.0), (1.5, 1.5))) self.assertTrue(polyline.segmentIntersects((1.5, 1.5), (1.0, 1.0))) self.assertTrue(polyline.segmentIntersects((1, 0), (1.0, 1.0)))
def test_construction(self): polyline = CPolyline() self.assertEqual( len(polyline) , 0 ) with self.assertRaises(IOError): CPolyline.createFromXYZFile( "Does/not/exist" ) p1 = CPolyline.createFromXYZFile( self.polyline1 ) self.assertEqual( len(p1) , 13 ) x,y = p1[-1] self.assertEqual(x , 389789.263184) self.assertEqual(y , 6605784.945099) p2 = CPolyline.createFromXYZFile( self.polyline2 ) self.assertEqual( len(p2) , 20 ) x,y = p2[-1] self.assertEqual(x , 396056.314697) self.assertEqual(y , 6605835.119461) p3 = CPolyline.createFromXYZFile( self.polyline3 ) self.assertEqual( len(p3) , 20 ) x,y = p3[-1] self.assertEqual(x , 396056.314697) self.assertEqual(y , 6605835.119461)
def test_front(self): polyline = CPolyline() polyline.addPoint(1, 1) polyline.addPoint(0, 0, front=True) self.assertEqual(len(polyline), 2) x, y = polyline[0] self.assertEqual(x, 0) self.assertEqual(y, 0) x, y = polyline[1] self.assertEqual(x, 1) self.assertEqual(y, 1)
def select_inside_polygon(self, points, intersect=False): """ Will select all points inside polygon. Will select all points inside polygon specified by input variable @points. Points should be a list of two-element tuples (x,y). So to select all the points within the rectangle bounded by the lower left rectangle (0,0) and upper right (100,100) the @points list should be: points = [(0,0) , (0,100) , (100,100) , (100,0)] The elements in the points list should be (utm_x, utm_y) values. These values will be compared with the centerpoints of the cells in the grid. The selection is based the top k=0 layer, and then extending this selection to all k values; this implies that the selection polygon will effectively be translated if the pillars are not vertical. """ self._select_inside_polygon(CPolyline(init_points=points))
def test_extend_to_bbox(self): bbox = [(0,0) , (10,0) , (10,10) , (0,10)] polyline = CPolyline( init_points = [(11,11) , (13,13)]) with self.assertRaises(ValueError): polyline.extendToBBox( bbox , start = False ) polyline = CPolyline( init_points = [(1,1) , (3,3)]) line1 = polyline.extendToBBox( bbox , start = True ) self.assertEqual( line1 , CPolyline( init_points = [(1,1) , (0,0)])) line1 = polyline.extendToBBox( bbox , start = False ) self.assertEqual( line1 , CPolyline( init_points = [(3,3) , (10,10)]))
def extendToBBox(self , bbox , k , start = True): fault_polyline = self.getPolyline(k) if start: p0 = fault_polyline[1] p1 = fault_polyline[0] else: p0 = fault_polyline[-2] p1 = fault_polyline[-1] ray_dir = GeometryTools.lineToRay(p0,p1) intersections = GeometryTools.rayPolygonIntersections( p1 , ray_dir , bbox) if intersections: p2 = intersections[0][1] if self.getName(): name = "Extend:%s" % self.getName() else: name = None return CPolyline( name = name , init_points = [(p1[0] , p1[1]) , p2]) else: raise Exception("Logical error - must intersect with bounding box")
def addPolyline(self , polyline , name = None): if not isinstance(polyline , CPolyline): polyline = CPolyline( init_points = polyline , name = name) else: if not name is None: raise ValueError("The name keyword argument can only be supplied when add not CPOlyline object") name = polyline.getName() if name and name in self: raise KeyError("The polyline collection already has an object:%s" % name) if polyline.isReference(): self._add_polyline( polyline , False) else: polyline.convertToCReference( self ) self._add_polyline( polyline , True)
def create_collection(self): collection = CPolylineCollection() p1 = CPolyline( name = "POLY1" , init_points = [(0,10) , (1,11) , (2,12)]) p2 = CPolyline( name = "POLY2" , init_points = [(0,100) , (10,110) , (20,120)]) collection.addPolyline( p1 ) collection.addPolyline( p2 ) tail = p1[-1] self.assertEqual( tail , (2,12)) self.assertEqual(p1.getName() , "POLY1") tail = p2[-1] self.assertEqual( tail , (20,120)) self.assertEqual(p2.getName() , "POLY2") return collection
def test_cross_segment(self): polyline = CPolyline( init_points = [(0,0), (1,0) , (1,1)]) # # x # | # | # | # x-------x # self.assertTrue(polyline.segmentIntersects( (0.5 , 0.5) , (0.5 , -0.5))) self.assertTrue(polyline.segmentIntersects( (0.5 , 0.5) , (1.5 , 0.5))) self.assertFalse(polyline.segmentIntersects( (0.5 , 0.5) , ( 0.5 , 1.5))) self.assertFalse(polyline.segmentIntersects( (0.5 , 0.5) , (-0.5 , 0.5))) self.assertFalse(polyline.segmentIntersects( (0.5 , 1.5) , ( 1.5 , 1.5))) self.assertTrue( polyline.segmentIntersects( (1.0 , 1.0) , ( 1.5 , 1.5))) self.assertTrue( polyline.segmentIntersects( ( 1.5 , 1.5) , (1.0 , 1.0))) self.assertTrue( polyline.segmentIntersects( ( 1 , 0) , (1.0 , 1.0)))
def test_item(self): polyline = CPolyline() polyline.addPoint(10, 20) self.assertEqual(len(polyline), 1) with self.assertRaises(TypeError): (x, y) = polyline["KEY"] with self.assertRaises(IndexError): (x, y) = polyline[10] (x, y) = polyline[0] self.assertEqual(x, 10) self.assertEqual(y, 20) polyline.addPoint(20, 20) (x, y) = polyline[-1] self.assertEqual(x, 20) self.assertEqual(y, 20)
def test_construction(self): polyline = CPolyline() self.assertEqual(len(polyline), 0) with self.assertRaises(IOError): CPolyline.createFromXYZFile("Does/not/exist") p1 = CPolyline.createFromXYZFile(self.polyline1) self.assertEqual(len(p1), 13) x, y = p1[-1] self.assertEqual(x, 389789.263184) self.assertEqual(y, 6605784.945099) p2 = CPolyline.createFromXYZFile(self.polyline2) self.assertEqual(len(p2), 20) x, y = p2[-1] self.assertEqual(x, 396056.314697) self.assertEqual(y, 6605835.119461) p3 = CPolyline.createFromXYZFile(self.polyline3) self.assertEqual(len(p3), 20) x, y = p3[-1] self.assertEqual(x, 396056.314697) self.assertEqual(y, 6605835.119461)
def test_name(self): p1 = CPolyline() self.assertTrue(p1.getName() is None) p2 = CPolyline(name="Poly2") self.assertEqual(p2.getName(), "Poly2")
def __initPolyline(self): pl = CPolyline() for (i,j) in self.getIJPolyline(): x,y,z = self.__grid.getNodeXYZ(i,j,self.__k) pl.addPoint( x, y) self.__polyline = pl
def test_length(self): polyline = CPolyline(init_points=[(0, 1)]) self.assertEqual(polyline.segmentLength(), 0) polyline = CPolyline(init_points=[(0, 0), (1, 0), (1, 1), (2, 2)]) self.assertEqual(polyline.segmentLength(), 2 + math.sqrt(2))
def test_unzip(self): pl = CPolyline(init_points=[(0, 3), (1, 4), (2, 5)]) x, y = pl.unzip() self.assertEqual(x, [0, 1, 2]) self.assertEqual(y, [3, 4, 5])
def __initPolyline(self): pl = CPolyline() for (i, j) in self.getIJPolyline(): x, y, z = self.__grid.getNodeXYZ(i, j, self.__k) pl.addPoint(x, y) self.__polyline = pl
def getPolyline(self , name = None): polyline = CPolyline( name = name) for fl in self: polyline += fl.getPolyline() return polyline
def test_intersects2(self): polyline = CPolyline(init_points=[(2, 10), (2, 100)]) self.assertTrue(polyline.intersects(polyline))
def test_intersects2(self): polyline = CPolyline( init_points = [(2,10),(2,100)]) self.assertTrue( polyline.intersects( polyline ))
def test_length(self): polyline = CPolyline( init_points = [(0,1)]) self.assertEqual( polyline.segmentLength() , 0 ) polyline = CPolyline( init_points = [(0,0) , (1,0) , (1,1) , (2,2)]) self.assertEqual( polyline.segmentLength() , 2 + math.sqrt(2))
def test_name(self): p1 = CPolyline() self.assertTrue( p1.getName() is None ) p2 = CPolyline( name = "Poly2" ) self.assertEqual( p2.getName() , "Poly2")
def test_unzip(self): pl = CPolyline( init_points = [(0,3) , (1,4) , (2,5)] ) x,y = pl.unzip() self.assertEqual(x , [0,1,2]) self.assertEqual(y , [3,4,5])