Read and write

cast_for_fiona(gdf)[source]

Transform elements so that attributes can be writable by fiona.

Parameters

gdf (gpd.GeoDataFrame :) – GeoDataFrame to modify

get_graph_with_wkt_geometry(geograph)[source]

Modify the edges geometry attribute to a well-known text format to make the graph writable is some text formats. The returned graph is not as operational as the given one (edge geometries has been removed).

Parameters

geograph (GeoGraph :) – Geograph to transform

Returns

A networkx graph with WKT geometries instead of shapely objects.

Return type

nx.Graph

Return type

Graph

graph_edges_to_gdf(graph)[source]
Create and fill a GeoDataFrame (geopandas) from edges of a networkX graph. The 'geometry' attribute is used

for shapes.

Parameters

graph (nx.Graph) – Graph to parse

Returns

The resulting GeoDataFrame : one row is an edge

Return type

gpd.GeoDataFrame

Return type

GeoDataFrame

graph_nodes_to_gdf(graph)[source]

Create and fill a GeoDataFrame (geopandas) from nodes of a networkX graph. The 'geometry' attribute is used for shapes.

Parameters

graph (GeoGraph) – Graph to parse

Returns

The resulting GeoDataFrame : one row is a node

Return type

gpd.GeoDataFrame

Return type

GeoDataFrame

parse_bool_columns_as_int(gdf)[source]

Transform bool columns into integer columns.

Parameters

gdf (gpd.GeoDataFrame :) – GeoDataFrame to modify

parse_edges_attribute_as_wkt(graph, attribute_name)[source]

Transform a graph edges attribute from WKT to shapely objects. Attribute is replaced.

Parameters
  • graph (nx.Graph :) – Graph to modify and parse

  • attribute_name (str :) – Attribute to parse the edges geometries

parse_graph_as_geograph(graph, **attr)[source]

Parse a networkx.Graph as a geonetworkx.GeoGraph with the closest geonetworkx graph type.

Parameters
  • graph (nx.Graph, nx.DiGraph, nx.MultiGraph or nx.MultiDiGraph) –

  • **attr – Potential spatial keys.

Returns

Depending the orientation and multi edges properties.

Return type

GeoGraph, GeoDiGraph, GeoMultiGraph or GeoMultiDiGraph

parse_nodes_attribute_as_wkt(graph, attribute_name)[source]

Transform a graph nodes attribute from WKT to shapely objects. Attribute is replaced.

Parameters
  • graph (nx.Graph :) – Graph to modify and parse

  • attribute_name (str :) – Attribute to parse the nodes geometries

parse_numpy_types(gdf)[source]

Transform numpy types as scalar types.

Parameters

gdf (gpd.GeoDataFrame :) – GeoDataFrame to modify

read_geofiles(nodes_file_path, edges_file_path, directed=True, multigraph=False, node_index_attr='id', edge_first_node_attr='u', edge_second_node_attr='v')[source]

Read geofiles to create a GeoGraph. Geofiles are read with geopandas.read_file method.

Parameters
  • nodes_file_path (str) – File path of nodes.

  • edges_file_path (str) – File path of edges.

  • directed (bool) – If True, returns a directed graph. (Default value = True)

  • multigraph (bool) – If True, returns a multigraph. (Default value = False)

  • node_index_attr (str) – Node id attribute in the geofile for nodes labeling. (Default value = settings.NODE_ID_COLUMN_NAME)

  • edge_first_node_attr (str) – Edge first node attribute in the geofile. (Default value = settings.EDGE_FIRST_NODE_COLUMN_NAME)

  • edge_second_node_attr (str) – Edge second node attribute in the geofile. (Default value = settings.EDGE_SECOND_NODE_COLUMN_NAME)

Returns

A parsed Geograph.

Return type

GeoGraph, GeoDiGraph, GeoMultiGraph, GeoMultiDiGraph

See also

GeoGraph.add_nodes_from_gdf, GeoGraph.add_edges_from_gdf, geopandas.read_file

read_geograph_with_coordinates_attributes(graph, x_key='x', y_key='y', **attr)[source]

Parse a networkx graph which have node’s coordinates as attribute. This method can be useful to parse an output graph of the osmnx package.

Parameters
  • graph (nx.Graph) – Given graph to parse. All nodes must have the x_key and y_key attributes.

  • x_key – x-coordinates attribute to parse (Default value = ‘x’)

  • y_key – y-coordinates attribute to parse (Default value = ‘y’)

  • **attr – Optional geograph spatial keys.

Returns

The parsed geograph (shallow copy of the input graph).

Return type

GeoGraph, GeoDiGraph, GeoMultiGraph, GeoMultiDiGraph

Return type

GeoGraph

read_gpickle(path, **attr)[source]

Read geograph object in Python pickle format.

Parameters
  • path (str) – Path where to read a graph object.

  • **attr – Potential spatial keys.

Returns

The parsed geograph.

Return type

GeoGraph, GeoDiGraph, GeoMultiGraph or GeoMultiDiGraph

See also

write_gpickle, nx.read_gpickle, nx.write_gpickle

read_graphml(path, node_type=<class 'str'>, edge_key_type=<class 'int'>, **attr)[source]

Read graph in GraphML format from path.

Parameters
  • path – File path to the graphml file.

  • node_type – See nx.read_graphml (Default value = str)

  • edge_key_type – See nx.read_graphml (Default value = int)

  • **attr – Potential spatial keys

Returns

Parsed Geograph

Return type

GeoGraph, GeoDiGraph, GeoMultiGraph, GeoMultiDiGraph

See also

write_graphml, nx.read_graphml, nx.write_graphml

Return type

GeoGraph

stringify_crs(graph)[source]

Write the CRS attribute as a string.

stringify_unwritable_columns(gdf)[source]

Transform elements which have type bool or list to string

Parameters

gdf (gpd.GeoDataFrame :) – GeoDataFrame to modify

write_edges_to_geofile(graph, file_name, driver='GPKG', fiona_cast=True)[source]

Writes the edges of a geograph as a geographic file.

Parameters
  • graph (GeoGraph, GeoDiGraph, GeoMultiGraph, GeoMultiDiGraph) – Graph to export

  • file_name – File name (with path)

  • driver – driver for export file format (GPKG, geojson, etc: can be found from fiona.supported_drivers) (Default value = “GPKG”)

  • fiona_cast – If true, methods for casting types to writable fiona types are used (Default value = True)

write_geofile(graph, path='./', nodes=True, edges=True, driver='GPKG', fiona_cast=True)[source]

Export a networkx graph as a geographic files. Two files are generated: one for the nodes and one for the edges. The files names will be prefixed by the graph name and suffixed by “_edges” or “_nodes”.

Parameters
  • graph – Graph to export

  • path – export directory (Default value = ‘./’)

  • nodes – boolean to indicate whether export nodes or not. (Default value = True)

  • edges – boolean to indicate whether export edges or not. (Default value = True)

  • driver

    driver for export file format (GPKG, geojson, etc: can be found from fiona.supported_drivers)

    (Default value = “GPKG”)

  • fiona_cast – If true, methods for casting types to writable fiona types are used (Default value = True)

write_gpickle(geograph, path, protocol=4)[source]

Write geograph object in Python pickle format.

Parameters

See also

read_gpickle, nx.read_gpickle, nx.write_gpickle

write_graphml(geograph, path, encoding='utf-8', prettyprint=True, infer_numeric_types=False)[source]

Generate GraphML file for the given geograph.

Parameters
  • geograph (GeoGraph, GeoDiGraph, GeoMultiGraph, GeoMultiDiGraph) – Geograph to write

  • path (str) – Writing file path

  • encoding – See nx.write_graphml (Default value = ‘utf-8’)

  • prettyprint – See nx.write_graphml (Default value = True)

  • infer_numeric_types – See nx.write_graphml (Default value = False)

See also

read_graphml, nx.read_graphml, nx.write_graphml

write_nodes_to_geofile(graph, file_name, driver='GPKG', fiona_cast=True)[source]

Writes the nodes of a geograph as a geographic file.

Parameters
  • graph (GeoGraph, GeoDiGraph, GeoMultiGraph, GeoMultiDiGraph) – Graph to export

  • file_name – File name (with path)

  • driver – driver for export file format (GPKG, geojson, etc: can be found from fiona.supported_drivers) (Default value = “GPKG”)

  • fiona_cast – If true, methods for casting types to writable fiona types are used (Default value = True)