Reference
Graphs
unweaver.graphs.DiGraphGPKG
Bases: DiGraphGPKGView
Mutable directed graph backed by a GeoPackage. An extension of unweaver.graphs.DiGraphGPKGView.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Optional[str]
|
An optional path to database file (or :memory:-type string). |
None
|
network |
Optional[GeoPackageNetwork]
|
An optional path to a custom GeoPackageNetwork instance. |
None
|
**kwargs |
Any
|
Keyword arguments compatible with networkx.DiGraph. |
{}
|
Source code in unweaver/graphs/digraphgpkg/digraphgpkg.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
add_edges_from(ebunch, _batch_size=1000, counter=None, **attr)
Equivalent to add_edges_from in networkx but with batched SQL writes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ebunch |
Iterable[EdgeTuple]
|
edge bunch, identical to nx ebunch_to_add. |
required |
_batch_size |
int
|
Number of rows to commit to the database at a time. |
1000
|
**attr |
Any
|
Default attributes, identical to nx attr. |
{}
|
Source code in unweaver/graphs/digraphgpkg/digraphgpkg.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
create_graph(path=None, **kwargs)
classmethod
Create a new DiGraphGPKG (.gpkg) at a given path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The path of the new GeoPackage (.gpkg). |
None
|
**kwargs |
Any
|
Any other keyword arguments to pass to the new DiGraphGPKG instance. |
{}
|
Returns:
Type | Description |
---|---|
DiGraphGPKG
|
A new DiGraphGPKG instance. |
Source code in unweaver/graphs/digraphgpkg/digraphgpkg.py
60 61 62 63 64 65 66 67 68 69 70 71 |
|
update_edges(ebunch)
Update edges as a batch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ebunch |
Iterable[EdgeTuple]
|
Any iterable of edge tuples (u, v, d). |
required |
Source code in unweaver/graphs/digraphgpkg/digraphgpkg.py
101 102 103 104 105 106 107 108 109 |
|
unweaver.graphs.DiGraphGPKGView
Bases: nx.DiGraph
An immutable, networkx
-compatible directed graph view over a
routable GeoPackage. Either the path to a .gpkg file or an existing
instance of
unweaver.network_adapters.geopackagenetwork.GeoPackageNetwork must be
provided.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
incoming_graph_data |
Optional[nx.DiGraph]
|
Any class derived from |
None
|
path |
Optional[str]
|
A path to the GeoPackage file (.gpkg). If no file exists at this path, one will be created. |
None
|
network |
Optional[GeoPackageNetwork]
|
An existing GeoPackageNetwork instance. |
None
|
**attr |
Any
|
Any parameters to be attached as graph attributes. |
{}
|
Source code in unweaver/graphs/digraphgpkg/digraphgpkg_view.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
edges_dwithin(lon, lat, distance, sort=False)
Retrieve an iterable of edges within N meters of a latitude-longitude coordinate pair.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lon |
float
|
Longitude of the query point. |
required |
lat |
float
|
Latitude of the query point. |
required |
distance |
float
|
Search radius for the edge, can be thought of as defining the circle radius with which edges will be tested for intersection. Is in meters. |
required |
sort |
bool
|
Whether to sort the iterable by distance such that the nearest edges are iterated over first. |
False
|
Returns:
Type | Description |
---|---|
Iterable[EdgeTuple]
|
A generator of edge tuples, possibly sorted by distance (if the |
Source code in unweaver/graphs/digraphgpkg/digraphgpkg_view.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
iter_edges()
Roughly equivalent to the .edges interface, but much faster.
Returns:
Type | Description |
---|---|
Iterable[EdgeTuple]
|
generator of (u, v, d) similar to .edges, but where d is a dictionary, not an Edge that syncs to database. |
Source code in unweaver/graphs/digraphgpkg/digraphgpkg_view.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
size(weight=None)
The 'size' of the directed graph, with the same behavior as
networkx.DiGraph
: if the weight parameter is set to a string, it's
the sum of values for all edges that have that string as a key in their
data dictionaries. If the weight parameter is unset, then it's the
count of edges.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
weight |
Optional[str]
|
The string to use as an edge dictionary key to calculate a weighted sum over all edges. |
None
|
Returns:
Type | Description |
---|---|
int
|
Either the number of edges (weight=None) or the sum of edge properties for the weight string. |
Source code in unweaver/graphs/digraphgpkg/digraphgpkg_view.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
to_in_memory()
Copy the GeoPackage, itself an SQLite database, into an in-memory SQLite database. This may speed up queries and is useful if you want to create an ephemeral graph.
Returns:
Type | Description |
---|---|
DiGraphGPKGView
|
A new instance of this class, backed by an in-memory SQLite database. |
Source code in unweaver/graphs/digraphgpkg/digraphgpkg_view.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
unweaver.graphs.AugmentedDiGraphGPKGView
Bases: nx.DiGraph
A wrapper over DiGraphGPKGView that allows for overlaying an in-memory DiGraph but with a seamless interface. When querying the graph, such as asking for a particular edge based on a (u, d) pair (G[u][v]), an AugmentedDiGraphGPKGView will first attempt to retrieve this edge from the in-memory DiGraph, then check the DiGraphGPKGView.
This wrapper is particularly useful for adding temporary nodes and edges for the purposes of running a graph analysis algorithm. For example, Unweaver uses AugmentedDiGraphGPKGView when it's necessary to start "part-way along" an edge for a shortest-path query using Dijkstra's algorithm. There is often no on-graph node near the physical locationfrom which someone wants to begin searching for shortest paths, so Unweaver creates two new temporary edges starting from the nearest point on the nearest edge, connecting them to the on-graph nodes for that edge, and creates an AugmentedDiGraphGPKGView using those temporary edges.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G |
DiGraphGPKGView
|
A DiGraphGPKGView, usually the main graph data. |
required |
G_overlay |
nx.DiGraph
|
A dict-of-dict-of-dicts (or networkx.DiGraph) to overlay. |
required |
Source code in unweaver/graphs/augmented.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
prepare_augmented(G, candidate)
classmethod
Create an AugmentedDiGraphGPKGView based on a DiGraphGPKGView and a start point candidatee (a ProjectedNode class instance). This will embed an overlay node and two edges.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G |
DiGraphGPKGView
|
The base DiGraphGPKGView. |
required |
candidate |
ProjectedNode
|
The potential start point candidate. |
required |
Source code in unweaver/graphs/augmented.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|