def test_stream_poi_02(self): files = ["small25", "morocco", "tunez", "jebja30"] for file in files: net_path = infolder + "/{0}_network.net".format(file) net = Network(net_path) for kind in ["heads", "confluences", "outlets"]: poi = net.get_stream_poi(kind, "CELL") x, y = net.cell_2_xy(poi[:, 0], poi[:, 1]) poi = np.array((x, y)).T spoi = np.loadtxt(infolder + "/{0}_{1}.txt".format(file, kind), delimiter=";", skiprows=1) compare = np.array_equal(poi, spoi) self.assertEqual(compare, True)
layer.CreateField(ogr.FieldDefn("sid", ogr.OFTInteger)) layer.CreateField(ogr.FieldDefn("to", ogr.OFTInteger)) layer.CreateField(ogr.FieldDefn("order", ogr.OFTInteger)) # Iterate by segments id and take channel cells for sid in np.unique(seg_array): # Get segment network cells seg_pos = np.where(seg_array == sid) seg_ind = net._ix[seg_pos] mouth = net._ixc[ixcix[seg_ind[-1]]] seg_ind = np.append(seg_ind, mouth) head = seg_ind[0] flow_to = seg_array[ixcix[mouth]] order = ord_array[ixcix[head]] row, col = net.ind_2_cell(seg_ind) x, y = net.cell_2_xy(row, col) # Create and add new feature to layer feat = ogr.Feature(layer.GetLayerDefn()) geom = ogr.Geometry(ogr.wkbLineString) for n in range(x.size): geom.AddPoint(x[n], y[n]) feat.SetGeometry(geom) feat.SetField("sid", int(sid)) feat.SetField("to", int(flow_to)) feat.SetField("order", int(order)) layer.CreateFeature(feat) layer = None dataset = None
def test_empty_network(self): net = Network() # Test PRaster properties self.assertEqual(net.get_cellsize(), (1.0, -1.0)) self.assertEqual(net.get_dims(), (1, 1)) self.assertEqual(net.get_size(), (1, 1)) self.assertEqual(net.get_extent(), (0.0, 1.0, 0.0, 1.0)) self.assertEqual(net.get_geotransform(), (0.0, 1.0, 0.0, 1.0, 0.0, -1.0)) self.assertEqual(net.get_ncells(), 1) self.assertEqual(net.get_projection(), "") # Test PRaster functions self.assertEqual(net.cell_2_ind(0, 0), .0) self.assertEqual(net.cell_2_xy(0, 0), (0.5, 0.5)) self.assertEqual(net.xy_2_cell(1, 1), (0, 1)) self.assertEqual(net.ind_2_cell(0), (0, 0)) # Test saving functions path = outfolder + "/net_delete.dat" net.save(path) self.assertEqual(os.path.exists(path), True) if os.path.exists(path): os.remove(path) path = outfolder + "/points_delete.txt" net.export_to_points(path) self.assertEqual(os.path.exists(path), True) if os.path.exists(path): os.remove(path) path = outfolder + "/shp_delete.shp" net.export_to_shp(path) self.assertEqual(os.path.exists(path), True) if os.path.exists(path): os.remove(path) path = outfolder + "/chi_delete.shp" net.get_chi_shapefile(path, 0) self.assertEqual(os.path.exists(path), True) if os.path.exists(path): os.remove(path) # Test other functions self.assertEqual( np.array_equal(net.get_streams(False), np.array([1]).reshape(1, 1)), True) self.assertEqual( np.array_equal(net.get_stream_segments(False), np.array([0]).reshape(1, 1)), True) self.assertEqual( np.array_equal(net.get_stream_orders("strahler", False), np.array([1]).reshape(1, 1)), True) self.assertEqual( np.array_equal(net.get_stream_orders('shreeve', False), np.array([1]).reshape(1, 1)), True) self.assertEqual( np.array_equal(net.get_stream_orders('dinosaur', False), np.array([1]).reshape(1, 1)), True) self.assertEqual( np.array_equal(net.get_stream_poi("heads", "XY"), np.array([]).reshape(0, 2)), True) self.assertEqual( np.array_equal(net.get_stream_poi("heads", "CELL"), np.array([]).reshape(0, 2)), True) self.assertEqual( np.array_equal(net.get_stream_poi("heads", "IND"), np.array([])), True) self.assertEqual( np.array_equal(net.get_stream_poi("confluences", "XY"), np.array([]).reshape(0, 2)), True) self.assertEqual( np.array_equal(net.get_stream_poi("confluences", "CELL"), np.array([]).reshape(0, 2)), True) self.assertEqual( np.array_equal(net.get_stream_poi("confluences", "IND"), np.array([])), True) self.assertEqual( np.array_equal(net.get_stream_poi("outlets", "XY"), np.array([]).reshape(0, 2)), True) self.assertEqual( np.array_equal(net.get_stream_poi("outlets", "CELL"), np.array([]).reshape(0, 2)), True) self.assertEqual( np.array_equal(net.get_stream_poi("outlets", "IND"), np.array([])), True) self.assertEqual( np.array_equal(net.get_stream_poi("dinosaur", "dinosaur"), np.array([]).reshape(0, 2)), True) self.assertEqual( np.array_equal(net.snap_points(np.array((5, 5)).reshape(1, 2)), np.array([[0.5, 0.5]])), True) self.assertEqual(net.is_inside(0, 0), False) net.calculate_gradients(0)