def check1c(hucs,rivers): assert(len(hucs) is 1) poly0 = hucs.polygon(0) print(poly0.boundary.coords[:]) assert(workflow.utils.close(poly0, shapely.geometry.Polygon([(0,-5), (10,-5), (10,0), (10,5), (0,5)]))) gon0b, gon0i = hucs.gons[0] assert(len(gon0b) is 1) assert(0 in gon0b.keys()) assert(len(gon0i) is 0) assert(gon0b[0] == 0) assert(0 in hucs.boundaries.keys()) assert(len(hucs.boundaries) == 1) assert(len(hucs.intersections) == 0) segs0 = hucs.boundaries[0] assert(len(segs0) is 1) assert(0 in segs0.keys()) assert(segs0[0] == 0) assert(len(hucs.segments) is 1) assert(0 in hucs.segments.keys()) riverlist = list(rivers[0].dfs()) assert(len(riverlist) is 1) print(riverlist[0].coords[:]) assert(workflow.utils.close(riverlist[0], shapely.geometry.LineString([(5,0), (10,0), (15,0.001)]))) for tree in rivers: assert(workflow.tree.is_consistent(tree))
def test_snap3(): # move a river point onto the huc boundary tb = two_boxes() rs = [shapely.geometry.LineString([(5.,0.), (10.001,0), (15,0)]),] hucs, rivers = data(tb, rs) rivers = workflow.hydrography.snap(hucs, rivers, 0.1, cut_intersections=True) poly0 = hucs.polygon(0) check2(hucs,rivers)
def check2(hucs,rivers): assert(len(hucs) is 2) poly0 = hucs.polygon(0) print(poly0.boundary.coords[:]) assert(workflow.utils.close(poly0, shapely.geometry.Polygon([(0,-5), (10,-5), (10,0), (10,5), (0,5)]))) poly1 = hucs.polygon(1) print(poly1.boundary.coords[:]) assert(workflow.utils.close(poly1, shapely.geometry.Polygon([(10,-5), (20,-5), (20,5), (10,5), (10,0)]))) riverlist = list(rivers[0].dfs()) # dfs, preOrdered assert(len(riverlist) is 2) print(riverlist[0].coords[:]) assert(workflow.utils.close(riverlist[0], shapely.geometry.LineString([(10,0), (15,0)]))) print(riverlist[1].coords[:]) assert(workflow.utils.close(riverlist[1], shapely.geometry.LineString([(5,0), (10,0)]))) for tree in rivers: assert(workflow.tree.is_consistent(tree))
def check3(hucs,rivers): assert(len(hucs) is 3) poly0 = hucs.polygon(0) assert(workflow.utils.close(poly0, shapely.geometry.Polygon([(0,-5),(10,-5),(10,5),(0,5)]))) poly1 = hucs.polygon(1) assert(workflow.utils.close(poly1, shapely.geometry.Polygon([(10,-5),(20,-5),(20,5),(10,5)]))) poly2 = hucs.polygon(2) print(list(poly2.boundary.coords)) print(list(shapely.geometry.Polygon([(0,5),(10,5),(20,5),(20,10),(10,10),(0,10)]).boundary.coords)) assert(workflow.utils.close(poly2, shapely.geometry.Polygon([(0,5),(10,5),(20,5),(20,10),(10,10),(0,10)]))) assert(len(rivers[0]) is 3) riverlist = list(rivers[0].dfs()) print(riverlist[0].coords[:]) assert(workflow.utils.close(riverlist[0], shapely.geometry.LineString([(10.,5.), (10.,10.)]))) print(riverlist[1].coords[:]) assert(workflow.utils.close(riverlist[1], shapely.geometry.LineString([(5.,0.), (10.,5.)]))) print(riverlist[2].coords[:]) assert(workflow.utils.close(riverlist[2], shapely.geometry.LineString([(15.,0.), (10.,5.)]))) for tree in rivers: assert(workflow.tree.is_consistent(tree))
def snap(hucs, rivers, tol=0.1, tol_triples=None, cut_intersections=False): """Snap HUCs to rivers.""" assert (type(hucs) is workflow.hucs.HUCs) assert (type(rivers) is list) assert (all(workflow.tree.is_consistent(river) for river in rivers)) list(hucs.polygons()) if len(rivers) is 0: return True if tol_triples is None: tol_triples = tol # snap boundary triple junctions to river endpoints logging.info(" Snapping polygon segment boundaries to river endpoints") snap_polygon_endpoints(hucs, rivers, tol_triples) if not all(workflow.tree.is_consistent(river) for river in rivers): logging.info(" ...resulted in inconsistent rivers!") return False try: list(hucs.polygons()) except AssertionError: logging.info(" ...resulted in inconsistent HUCs") return False logging.debug('snap part 1') logging.debug(list(rivers[0].segment.coords)) logging.debug(list(hucs.polygon(0).boundary.coords)) # snap endpoints of all rivers to the boundary if close # note this is a null-op on cases dealt with above logging.info(" Snapping river endpoints to the polygon") for tree in rivers: snap_endpoints(tree, hucs, tol) if not all(workflow.tree.is_consistent(river) for river in rivers): logging.info(" ...resulted in inconsistent rivers!") return False try: list(hucs.polygons()) except AssertionError: logging.info(" ...resulted in inconsistent HUCs") return False logging.debug('snap part 2') logging.debug(list(rivers[0].segment.coords)) logging.debug(list(hucs.polygon(0).boundary.coords)) # deal with intersections if cut_intersections: logging.info(" Cutting at crossings") snap_crossings(hucs, rivers, tol) consistent = all( workflow.tree.is_consistent(river) for river in rivers) if not consistent: logging.info(" ...resulted in inconsistent rivers!") return False try: list(hucs.polygons()) except AssertionError: logging.info(" ...resulted in inconsistent HUCs") return False logging.debug('snap part 3') logging.debug(list(rivers[0].segment.coords)) logging.debug(list(hucs.polygon(0).boundary.coords)) # dealing with crossings might have generated river segments # outside of my space. remove these. Note the use of negative tol rivers = filter_rivers_to_huc(hucs, rivers, -tol) return rivers