예제 #1
0
 def test_write_read_gpickle(self):
     for i, graph_type in enumerate(ALL_CLASSES):
         with self.subTest(graph_type=graph_type, SEED=gnx_tu.SEED):
             g = get_random_geograph_subclass(NB_VERTICES, graph_type)
             file_path = os.path.join(self.results_dir,
                                      "write_test_%d.gpickle" % i)
             gnx.write_gpickle(g, file_path)
             read_graph = gnx.read_gpickle(file_path)
             assert_is_instance(read_graph, graph_type)
             assert_graphs_have_same_edges_geometry(g, read_graph)
예제 #2
0
 def test_graph_to_undirected(self):
     graphs_undirected_match = {
         gnx.GeoGraph: gnx.GeoGraph,
         gnx.GeoMultiGraph: gnx.GeoMultiGraph,
         gnx.GeoDiGraph: gnx.GeoGraph,
         gnx.GeoMultiDiGraph: gnx.GeoMultiGraph
     }
     for graph_type in ALL_CLASSES:
         with self.subTest(graph_type=graph_type, SEED=gnx_tu.SEED):
             graph = get_random_geograph_subclass(NB_POINTS, graph_type)
             directed_graph = graph.to_undirected()
             assert_is_instance(directed_graph,
                                graphs_undirected_match[graph_type])
예제 #3
0
 def test_add_edges_gdf(self):
     for graph_type in ALL_CLASSES:
         with self.subTest(graph_type=graph_type, SEED=gnx_tu.SEED):
             g = get_random_geograph_subclass(NB_POINTS, graph_type)
             initial_edges = list(g.edges)
             nb_initial_edges = len(initial_edges)
             g2 = get_random_geograph_subclass(NB_POINTS, graph_type)
             edges_gdf = g2.edges_to_gdf()
             g.add_edges_from_gdf(edges_gdf,
                                  settings.EDGE_FIRST_NODE_COLUMN_NAME,
                                  gnx.settings.EDGE_SECOND_NODE_COLUMN_NAME)
             if g.is_multigraph():
                 g2_nb_edges = g2.number_of_edges()
                 assert_equal(
                     g.number_of_edges(), g2_nb_edges + nb_initial_edges,
                     "Resulting graph have not the right number of edges.")
             else:
                 g2_nb_distinct_edges = len(
                     [e for e in g2.edges if e not in initial_edges])
                 assert_equal(
                     g.number_of_edges(),
                     g2_nb_distinct_edges + nb_initial_edges,
                     "Resulting graph have not the right number of edges.")
예제 #4
0
 def test_spatial_keys_persistence(self):
     for graph_type in ALL_CLASSES:
         with self.subTest(graph_type=graph_type, SEED=gnx_tu.SEED):
             g = get_random_geograph_subclass(NB_POINTS, graph_type)
             gnx.rename_edges_attribute(g, g.edges_geometry_key, "abcd")
             g.edges_geometry_key = "abcd"
             gnx.rename_nodes_attribute(g, g.nodes_geometry_key, "efgh")
             g.nodes_geometry_key = "efgh"
             g.crs = "epsg:3945"
             g2 = g.copy(as_view=False)
             assert_graphs_have_same_spatial_keys(g, g2)
             g3 = g.to_undirected(as_view=False)
             assert_graphs_have_same_spatial_keys(g, g3)
             g4 = g.to_directed(as_view=False)
             assert_graphs_have_same_spatial_keys(g, g4)
예제 #5
0
    def test_write_read_geofile(self):
        for i, graph_type in enumerate(ALL_CLASSES):
            with self.subTest(graph_type=graph_type, SEED=gnx_tu.SEED):
                g_write = get_random_geograph_subclass(NB_VERTICES, graph_type)
                g_write.name = "test_%d" % i
                gnx.write_geofile(g_write, self.results_dir, driver="GPKG")
                nodes_file_path = os.path.join(self.results_dir,
                                               g_write.name + "_nodes.gpkg")
                edges_file_path = os.path.join(self.results_dir,
                                               g_write.name + "_edges.gpkg")
                g_read = gnx.read_geofiles(nodes_file_path,
                                           edges_file_path,
                                           directed=g_write.is_directed(),
                                           multigraph=g_write.is_multigraph())

                from geonetworkx.testing import assert_graphs_have_same_geonodes
                assert_graphs_have_same_geonodes(
                    g_write, g_read,
                    "Written and read graph have different nodes.")
                assert_graphs_have_same_edges_geometry(
                    g_write, g_read,
                    "Written and read graph have different edge geometries")
예제 #6
0
 def test_edges_to_gdf(self):
     for graph_type in ALL_CLASSES:
         with self.subTest(graph_type=graph_type, SEED=gnx_tu.SEED):
             g = get_random_geograph_subclass(NB_POINTS, graph_type)
             gdf = g.edges_to_gdf()
             assert_equal(g.number_of_edges(), len(gdf))
예제 #7
0
 def test_write_geofile(self):
     for graph_type in ALL_CLASSES:
         with self.subTest(graph_type=graph_type, SEED=gnx_tu.SEED):
             g = get_random_geograph_subclass(NB_VERTICES, graph_type)
             gnx.write_geofile(g, self.results_dir, driver="GPKG")