spaghetti.
Network
(in_data=None, vertex_sig=11, unique_arcs=True, extractgraph=True, w_components=True, weightings=False, weights_kws={}, vertex_atol=None)[source]¶Spatiallyconstrained network representation and analytical functionality. Naming conventions are as follows, (1) arcs and vertices for the full network object, and (2) edges and nodes for the simplified graphtheoretic object. The term ‘link’ is used to refer to a network arc or a graph edge.
str
, list
, tuple
, numpy.ndarray
, libpysal.cg.Chain
, geopandas.GeoDataFrame
}The input geographic data. Either (1) a path to a shapefile
(str); (2) an iterable containing libpysal.cg.Chain
objects; (3) a single libpysal.cg.Chain
; or
(4) a geopandas.GeoDataFrame
.
int
Round the x and y coordinates of all vertices to vertex_sig
significant digits (combined significant digits on the left and
right of the decimal place). Default is 11. Set to None
for
no rounding.
If True
(default), keep only unique arcs (i.e., prune
out any duplicated arcs). If False
keep all segments.
If True
, extract a graphtheoretic object with no degree 2
nodes. Default is True
.
Set to False
to not record connected components from a
libpysal.weights.W
object. Default is True
.
dict
, bool}If dict, lists of weightings for each arc. If bool,
True
flags self.arc_lengths
as the weightings,
False
sets no weightings. Default is False
.
dict
Keyword arguments for libpysal.weights.W
.
int
, None
}Precision for vertex absolute tolerance. Round vertex coordinates to
vertex_atol
decimal places. Default is None
. ONLY change
the default when there are known issues with digitization.
Notes
See [CH81], [TF83], [AMO93], [LabbePT95], [KRUT09], [Barthelemy11], [Das13], [OS12d], [DB14], [Web16], for more indepth discussion on spatial networks, graph theory, and location along networks. For related networkcentric software see Snkit [RK19], SANET [OOS06], NetworkX [HSS08], Pandana [FWL12], and OSMnx [Boe17].
Examples
Create an instance of a network.
>>> import spaghetti
>>> from libpysal import examples
>>> streets_file = examples.get_path("streets.shp")
>>> ntw = spaghetti.Network(in_data=streets_file)
Fetch the number connected components in the network.
>>> ntw.network_n_components
1
Unique component labels in the network.
>>> import numpy
>>> list(numpy.unique(ntw.network_component_labels))
[0]
Show whether each component of the network is an isolated ring (or not).
>>> ntw.network_component_is_ring
{0: False}
Show how many network arcs are associated with the component.
>>> arcs = len(ntw.network_component2arc[ntw.network_component_labels[0]])
>>> arcs
303
Do the same as above, but for the graphtheoretic representation of the network object.
>>> ntw.graph_n_components
1
>>> list(numpy.unique(ntw.graph_component_labels))
[0]
>>> ntw.graph_component_is_ring
{0: False}
>>> edges = len(ntw.graph_component2edge[ntw.graph_component_labels[0]])
>>> edges
179
The number of arcs in the network is always greater than or equal to the number of edges in the graphtheoretic representation.
>>> arcs >= edges
True
Snap point observations to the network with attribute information.
>>> crimes_file = examples.get_path("crimes.shp")
>>> ntw.snapobservations(crimes_file, "crimes", attribute=True)
And without attribute information.
>>> schools_file = examples.get_path("schools.shp")
>>> ntw.snapobservations(schools_file, "schools", attribute=False)
Show the point patterns associated with the network.
>>> ntw.pointpatterns.keys()
dict_keys(['crimes', 'schools'])
list
List of lists storing vertex adjacency.
dict
Keys are vertex IDs and values are (x,y) coordinates of the vertices.
list
List of vertex IDs.
dict
Keys are tuples of vertex coords and values are the vertex ID.
list
List of arcs, where each arc is a sorted tuple of vertex IDs.
dict
Keys are tuples of sorted vertex IDs representing an arc and values are the length.
dict
Keys are a string name of the pattern and values are
PointPattern
class instances.
numpy.ndarray
All network vertices (nonobservations) distance matrix. Distances
between vertices in disparate components are recorded as inf
by default.
dict
Keys are the vertex IDs (int
). Values are dictionaries
with the keys being the IDs of the destination vertex
and values being lists of vertices along the shortest path.
If the destination vertex is a) the origin or b)
unreachable (disparate component) it is listed as itself being the
neighbor.
list
Tuples of graph edge IDs.
dict
Keys are the graph edge IDs (tuple
). Values are the
graph edge length (float
).
list
All vertices with degree 2 that are not in an isolated island ring (loop) component.
libpysal.weights.W
Weights object created from the network arcs.
int
Count of connected components in the network.
True
if the network representation is a single connected
component, otherwise False
.
numpy.ndarray
Component labels for network arcs.
dict
Lookup in the form {int: list} for arcs comprising network connected components keyed by component labels with arcs in a list as values.
dict
Length of each network component (keyed by component label).
int
The ID of the longest component in the network. This is not
necessarily equal to network_largest_component
.
dict
Lookup in the form {int: list} for vertices comprising network connected components keyed by component labels with vertices in a list as values.
dict
The number of vertices in each network component (keyed by component label).
int
The ID of the largest component in the network. Within spaghetti
the largest component is the one with the most vertices. This is not
necessarily equal to network_longest_component
.
dict
Lookup in the form {int: bool} keyed by component labels with values
as True
if the component is a closed ring, otherwise False
.
libpysal.weights.W
Weights object created from the graph edges.
int
Count of connected components in the network.
True
if the graph representation is a single connected
component, otherwise False
.
numpy.ndarray
Component labels for graph edges.
dict
Lookup in the form {int: list} for edges comprising graph connected components keyed by component labels with edges in a list as values.
dict
Length of each graph component (keyed by component label).
int
The ID of the longest component in the graph. This is not
necessarily equal to graph_largest_component
.
dict
Lookup in the form {int: list} for vertices comprising graph connected components keyed by component labels with vertices in a list as values.
dict
The number of vertices in each graph component (keyed by component label).
int
The ID of the largest component in the graph. Within spaghetti
the largest component is the one with the most vertices. This is not
necessarily equal to graph_longest_component
.
dict
Lookup in the form {int: bool} keyed by component labels with values as
True
if the component is a closed ring, otherwise False
.
__init__
(self, in_data=None, vertex_sig=11, unique_arcs=True, extractgraph=True, w_components=True, weightings=False, weights_kws={}, vertex_atol=None)[source]¶Initialize self. See help(type(self)) for accurate signature.
Methods

Compute a network constrained Ffunction, which is a cumulative frequency distribution of random distances within a set of observations associated with a network. 

Compute a network constrained Gfunction, which is a cumulative frequency distribution of nearest neighbor distances within a set of observations associated with a network. 

Compute a network constrained Kfunction. 

Initialize self. 

Compute either all distances between 

Given an observation on a network arc, return the distance to the two vertices that bound that end. 

Given an observation snapped to a network arc, calculate the distance from the original location to the snapped location. 

Create a contiguitybased 

Compute the counts per arc or edge (link). 

Create distancebased weights. 

Returns the arcs (links) adjacent to vertices. 

Using the existing network representation, create a graphtheoretic representation by removing all vertices with a neighbor incidence of two (nonarticulation points). 

All vertextovertex distances on a network. 

Identify connected component information from a 

Load a network from a binary file saved on disk. 

Compute the interpattern nearest neighbor distances or the intrapattern nearest neighbor distances between a source pattern and a destination pattern. 

Save a network to disk as a binary file. 

Return the shortest paths between observation points as 

Generate a simulated point pattern on the network. 

Snap a point pattern shapefile to a network object. 

Split all of the arcs in the network at a fixed distance. 
NetworkF
(self, pointpattern, nsteps=10, permutations=99, threshold=0.2, distribution='uniform', lowerbound=None, upperbound=None)[source]¶Compute a network constrained Ffunction, which is a cumulative frequency distribution of random distances within a set of observations associated with a network.
spaghetti.PointPattern
A spaghetti
point pattern object.
int
The number of steps at which the count of the nearest neighbors is computed.
int
The number of permutations to perform. Default 99.
float
The level at which significance is computed. (0.5 would be 97.5% and 2.5%).
str
The distribution from which random points are sampled.
Either "uniform"
or "poisson"
.
float
The lower bound at which the Ffunction is computed. Default 0.
float
The upper bound at which the Ffunction is computed. Defaults to the maximum observed nearest neighbor distance.
spaghetti.analysis.NetworkF
A network F class instance.
Notes
This is a networkconstrained version based on the Euclidean formulation found in [OSullivanU10] and mentioned in [OS12c]. It is formulated in [OSullivanU10] as:
where $p_i$ are randomly selected observations totaling $m$ in $S$ and $d$ is each step of distance along the network.
Examples
Create a network instance.
>>> import spaghetti
>>> from libpysal import examples
>>> ntw = spaghetti.Network(in_data=examples.get_path("streets.shp"))
Snap observation points onto the network.
>>> pt_str = "schools"
>>> in_data = examples.get_path(pt_str+".shp")
>>> ntw.snapobservations(in_data, pt_str, attribute=True)
Simulate observations along the network.
>>> schools = ntw.pointpatterns[pt_str]
>>> sim = ntw.simulate_observations(schools.npoints)
Compute a network constrained Ffunction of schools
with 5
permutations
and 10
nsteps
.
>>> fres = ntw.NetworkF(schools, permutations=5, nsteps=10)
>>> fres.lowerenvelope.shape[0]
10
NetworkG
(self, pointpattern, nsteps=10, permutations=99, threshold=0.5, distribution='uniform', lowerbound=None, upperbound=None)[source]¶Compute a network constrained Gfunction, which is a cumulative frequency distribution of nearest neighbor distances within a set of observations associated with a network.
spaghetti.PointPattern
A spaghetti
point pattern object.
int
The number of steps at which the count of the nearest neighbors is computed.
int
The number of permutations to perform. Default 99.
float
The level at which significance is computed. (0.5 would be 97.5% and 2.5%).
str
The distribution from which random points are sampled
Either "uniform"
or "poisson"
.
float
The lower bound at which the Gfunction is computed. Default 0.
float
The upper bound at which the Gfunction is computed. Defaults to the maximum observed nearest neighbor distance.
spaghetti.analysis.NetworkG
A network G class instance.
Notes
This is a networkconstrained version based on the Euclidean formulation found in [OSullivanU10] and mentioned in [OS12c]. It is formulated in [OSullivanU10] as:
where $s_i$ are observations totaling $n$ in $S$ and $d$ is each step of distance along the network.
[note from `jlaura`] Both the G and K functions generate a full distance matrix. This is because, I know that the full generation is correct and I believe that the truncated generated, e.g. nearest neighbor, has a bug.
Examples
Create a network instance.
>>> import spaghetti
>>> from libpysal import examples
>>> ntw = spaghetti.Network(in_data=examples.get_path("streets.shp"))
Snap observation points onto the network.
>>> pt_str = "schools"
>>> in_data = examples.get_path(pt_str+".shp")
>>> ntw.snapobservations(in_data, pt_str, attribute=True)
Simulate observations along the network.
>>> schools = ntw.pointpatterns[pt_str]
>>> sim = ntw.simulate_observations(schools.npoints)
Compute a network constrained Gfunction of schools
with 5
permutations
and 10
nsteps
.
>>> gres = ntw.NetworkG(schools, permutations=5, nsteps=10)
>>> gres.lowerenvelope.shape[0]
10
NetworkK
(self, pointpattern, nsteps=10, permutations=99, threshold=0.5, distribution='uniform', lowerbound=None, upperbound=None)[source]¶Compute a network constrained Kfunction.
spaghetti.PointPattern
A spaghetti
point pattern object.
int
The number of steps at which the count of the nearest neighbors is computed.
int
The number of permutations to perform. Default is 99.
float
The level at which significance is computed. (0.5 would be 97.5% and 2.5%).
str
The distribution from which random points are sampled
Either "uniform"
or "poisson"
.
float
The lower bound at which the Kfunction is computed. Default is 0.
float
The upper bound at which the Kfunction is computed. Defaults to the maximum observed nearest neighbor distance.
spaghetti.analysis.NetworkK
A network K class instance.
Notes
Based on [Rip77], [OSullivanU10], [OY01], and [OS12b].
[note from `jlaura`] Both the G and K functions generate a full distance matrix. This is because, I know that the full generation is correct and I believe that the truncated generated, e.g. nearest neighbor, has a bug.
Examples
Create a network instance.
>>> import spaghetti
>>> from libpysal import examples
>>> ntw = spaghetti.Network(in_data=examples.get_path("streets.shp"))
Snap observation points onto the network.
>>> pt_str = "schools"
>>> in_data = examples.get_path(pt_str+".shp")
>>> ntw.snapobservations(in_data, pt_str, attribute=True)
Simulate observations along the network.
>>> schools = ntw.pointpatterns[pt_str]
>>> sim = ntw.simulate_observations(schools.npoints)
Compute a network constrained Kfunction of schools
with 5
permutations
and 10
nsteps
.
>>> kres = ntw.NetworkK(schools, permutations=5, nsteps=10)
>>> kres.lowerenvelope.shape[0]
10
allneighbordistances
(self, sourcepattern, destpattern=None, fill_diagonal=None, n_processes=1, gen_tree=False, snap_dist=False)[source]¶Compute either all distances between i
and j
in a
single point pattern or all distances between each i
from a
source pattern and all j
from a destination pattern.
str
, spaghetti.PointPattern
}The key of a point pattern snapped to the network or
the full spaghetti.PointPattern
object.
str
(Optional) The key of a point pattern snapped to the network
or the full spaghetti.PointPattern
object.
float
, int
}(Optional) Fill the diagonal of the cost matrix. Default is
None
and will populate the diagonal with numpy.nan
.
Do not declare a destpattern
for a custom
fill_diagonal
.
int
, str
}Specify the number of cores to utilize. Default is 1 core.
Use "all"
to request all available cores.
Specify the exact number of cores with an integer.
Rebuild shortest path True
, or skip False
.
Default is False
.
Flag as True
to include the distance from the original
location to the snapped location along the network. Default
is False
.
numpy.ndarray
An array of shape (n,m) storing distances between all source and destination points.
dict
Nearest network node to point pattern vertex shortest
path lookup. The values of the dictionary are a tuple
of the nearest source vertex and the nearest destination
vertex to query the lookup tree. If two observations are
snapped to the same network arc a flag of .1 is set for
both the source and destination network vertex
indicating the same arc is used while also raising an
IndexError
when rebuilding the path.
Examples
Create a network instance.
>>> import spaghetti
>>> from libpysal import examples
>>> import numpy
>>> ntw = spaghetti.Network(examples.get_path("streets.shp"))
Snap observations to the network.
>>> ntw.snapobservations(
... examples.get_path("crimes.shp"), "crimes", attribute=True
... )
Calculate all distances between observations in the crimes
dataset.
>>> s2s_dist = ntw.allneighbordistances("crimes")
If calculating a typea
to typea
distance matrix
the distance between an observation and itself is nan
and
the distance between one observation and another will be positive value.
>>> s2s_dist[0,0], s2s_dist[1,0]
(nan, 3105.189475447081)
If calculating a typea
to typeb
distance matrix
the distance between all observations will likely be positive
values, may be zero (or approximately zero), but will never be negative.
>>> ntw.snapobservations(
... examples.get_path("schools.shp"), "schools", attribute=False
... )
>>> s2d_dist = ntw.allneighbordistances("crimes", destpattern="schools")
>>> numpy.round((s2d_dist[0,0], s2d_dist[1,0]), 5)
array([4520.72354, 6340.42297])
Shortest paths can also be reconstructed when desired by
setting the gen_tree
keyword argument to True
. Here
it is shown that the shortest path between school 6
and
school 7
flows along network arcs through network
vertices 173
and 64
. The ntw.network_trees
attribute
may then be queried for the network elements comprising that path.
>>> d2d_dist, tree = ntw.allneighbordistances("schools", gen_tree=True)
>>> tree[(6, 7)]
(173, 64)
compute_distance_to_vertices
(self, x, y, arc)[source]¶Given an observation on a network arc, return the distance to the two vertices that bound that end.
compute_snap_dist
(self, pattern, idx)[source]¶Given an observation snapped to a network arc, calculate the distance from the original location to the snapped location.
spaghetti.PointPattern
The point pattern object.
int
The point ID.
float
The euclidean distance from original location to the snapped location.
contiguityweights
(self, graph=True, weightings=None, weights_kws={})[source]¶Create a contiguitybased libpysal.weights.W
object.
libpysal.weights.W
A W
representing the binary adjacency of the network.
Notes
See [RA07] and [RWK+19] for more details.
Examples
Instantiate a network.
>>> import spaghetti
>>> from libpysal import examples
>>> import esda
>>> import numpy
>>> ntw = spaghetti.Network(examples.get_path("streets.shp"))
Snap point observations to the network with attribute information.
>>> ntw.snapobservations(
... examples.get_path("crimes.shp"), "crimes", attribute=True
... )
Find counts per network arc.
>>> counts = ntw.count_per_link(
... ntw.pointpatterns["crimes"].obs_to_arc, graph=False
... )
>>> counts[(50, 165)]
4
Create a contiguitybased W
object.
>>> w = ntw.contiguityweights(graph=False)
Using the W
object, access to
esda
functionality is provided. First, a vector of attributes is
created for all edges with observations.
>>> w = ntw.contiguityweights(graph=False)
>>> arcs = w.neighbors.keys()
>>> y = numpy.zeros(len(arcs))
>>> for i, e in enumerate(arcs):
... if e in counts.keys():
... y[i] = counts[e]
Fetch the number of observations associated with arc 3
,
where 3
is the basic 0indexed ID of w.neighbors.keys()
created through enumerate(arcs)
.
>>> y[3]
3.0
Next, a standard call to
esda.Moran
is made and the result placed into res
.
>>> res = esda.moran.Moran(y, w, permutations=99)
>>> type(res)
<class 'esda.moran.Moran'>
count_per_link
(self, obs_on, graph=True)[source]¶Compute the counts per arc or edge (link).
dict
Dictionary of observations on the network. Either in the form {(<LINK>):{<POINT_ID>:(<COORDS>)}} or {<LINK>:[(<COORD>),(<COORD>)]}.
dict
Counts per network link in the form {(<LINK>):<COUNT>}.
Examples
Note that this passes the obs_to_arc
or obs_to_edge
attribute
of a point pattern snapped to the network.
>>> import spaghetti
>>> from libpysal import examples
>>> ntw = spaghetti.Network(examples.get_path("streets.shp"))
Snap observations to the network.
>>> ntw.snapobservations(
... examples.get_path("crimes.shp"), "crimes", attribute=True
... )
>>> counts = ntw.count_per_link(
... ntw.pointpatterns["crimes"].obs_to_arc, graph=False
... )
>>> counts[(140, 142)]
10
>>> s = sum([v for v in list(counts.values())])
>>> s
287
distancebandweights
(self, threshold, n_processes=1, gen_tree=False)[source]¶Create distancebased weights.
libpysal.weights.W
A W
object representing the binary adjacency of
the network.
Notes
See [AR14] and [RAL+15] for more details regarding spatial weights.
Examples
Instantiate an instance of a network.
>>> import spaghetti
>>> from libpysal import examples
>>> streets_file = examples.get_path("streets.shp")
>>> ntw = spaghetti.Network(in_data=streets_file)
Create a contiguitybased W
object based on network distance, 500
US feet in this case.
>>> w = ntw.distancebandweights(threshold=500)
Show the number of units in the W
object.
>>> w.n
230
There are 8
units with 3
neighbors in the W
object.
>>> w.histogram[1]
(8, 3)
enum_links_vertex
(self, v0)[source]¶Returns the arcs (links) adjacent to vertices.
Examples
Create an instance of a network.
>>> import spaghetti
>>> from libpysal import examples
>>> ntw = spaghetti.Network(examples.get_path("streets.shp"))
Enumerate the links/arcs that are adjacent to vertex 24
.
>>> ntw.enum_links_vertex(24)
[(24, 48), (24, 25), (24, 26)]
extractgraph
(self)[source]¶Using the existing network representation, create a graphtheoretic representation by removing all vertices with a neighbor incidence of two (nonarticulation points). That is, we assume these vertices are bridges between vertices with higher or lower incidence.
full_distance_matrix
(self, n_processes, gen_tree=False)[source]¶All vertextovertex distances on a network. This method
is called from within allneighbordistances()
,
nearestneighbordistances()
, and distancebandweights()
.
Notes
identify_components
(self, w, graph=False)[source]¶Identify connected component information from a
libpysal.weights.W
object
libpysal.weights.W
Weights object created from the network segments (either raw or graphtheoretic).
Flag for a raw network (False
) or graphtheoretic network
(True
). Default is False
.
loadnetwork
(filename)[source]¶Load a network from a binary file saved on disk.
str
The filename where the network is saved.
spaghetti.Network
A precomputed spaghetti
network object.
nearestneighbordistances
(self, sourcepattern, destpattern=None, n_processes=1, gen_tree=False, all_dists=None, snap_dist=False, keep_zero_dist=True)[source]¶Compute the interpattern nearest neighbor distances or the intrapattern nearest neighbor distances between a source pattern and a destination pattern.
str
The key of a point pattern snapped to the network.
str
(Optional) The key of a point pattern snapped to the network.
int
, str
}Specify the number of cores to utilize. Default is 1 core.
Use "all"
to request all available cores.
Specify the exact number of cores with an integer.
Rebuild shortest path True
, or skip False
.
Default is False
.
numpy.ndarray
An array of shape (n,n) storing distances between all points.
Flag as True
to include the distance from the original
location to the snapped location along the network. Default
is False
.
Include zero values in minimum distance True
or exclude
False
. Default is True
. If the source pattern is the
same as the destination pattern the diagonal is filled with
numpy.nan
.
dict
Nearest neighbor distances keyed by the source point ID with the value as as tuple of lists containing nearest destination point ID(s) and distance.
Examples
Instantiate a network.
>>> import spaghetti
>>> from libpysal import examples
>>> ntw = spaghetti.Network(examples.get_path("streets.shp"))
Snap observations to the network.
>>> ntw.snapobservations(examples.get_path("crimes.shp"), "crimes")
Fetch nearest neighbor distances while (potentially)
keeping neighbors that have been geocoded directly on top of
each other. Here it is demonstrated that observation 11
has two neighbors (18
and 19
) at an exactly equal distance.
However, observation 18
is shown to have only one neighbor
(18
) with no distance between them.
>>> nn = ntw.nearestneighbordistances("crimes", keep_zero_dist=True)
>>> nn[11], nn[18]
(([18, 19], 165.33982412719126), ([19], 0.0))
This may be remedied by setting the keep_zero_dist
keyword
argument to False
. With this parameter set, observation 11
still has the same neighbor/distance values, but
observation 18
now has a single nearest neighbor (11
)
with a nonzero, postive distance.
>>> nn = ntw.nearestneighbordistances("crimes", keep_zero_dist=False)
>>> nn[11], nn[18]
(([18, 19], 165.33982412719126), ([11], 165.33982412719126))
There are valid reasons for both retaining or masking zero distance neighbors. When conducting analysis, thought must be given as to which model more accurately represents the specific scenario.
savenetwork
(self, filename)[source]¶Save a network to disk as a binary file.
str
The filename where the network should be saved. This should be a full path or it will be saved in the current directory.
Examples
Create a network instance.
>>> import spaghetti
>>> from libpysal import examples
>>> ntw = spaghetti.Network(examples.get_path("streets.shp"))
Save out the network instance.
>>> ntw.savenetwork("mynetwork.pkl")
shortest_paths
(self, tree, pp_orig, pp_dest=None, n_processes=1)[source]¶Return the shortest paths between observation points as
libpysal.cg.Chain
objects.
dict
See tree_nearest
in
spaghetti.Network.allneighbordistances()
.
str
Origin point pattern for shortest paths.
See name
in spaghetti.Network.snapobservations()
.
str
Destination point pattern for shortest paths.
See name
in spaghetti.Network.snapobservations()
.
Defaults pp_orig
if not declared.
int
See n_processes
in spaghetti.Network.full_distance_matrix()
.
list
The shortest paths between observations as geometric objects.
Each element of the list is a list where the first element
is an origindestination pair tuple and the second
element is a libpysal.cg.Chain
.
AttributeError
This exception is raised when an attempt to extract shortest
path geometries is being made that but the network_trees
attribute does not exist within the network object.
Examples
Instantiate a network.
>>> import spaghetti
>>> from libpysal import examples
>>> ntw = spaghetti.Network(examples.get_path("streets.shp"))
Snap observations to the network.
>>> ntw.snapobservations(examples.get_path("schools.shp"), "schools")
Create shortest path trees between observations.
>>> _, tree = ntw.allneighbordistances("schools", gen_tree=True)
Generate geometric objects from trees.
>>> paths = ntw.shortest_paths(tree, "schools")
Extract the first path, which is between observations
0
and 1
.
>>> path = paths[0]
>>> path[0]
(0, 1)
The are n
vertices in the path between observations
0
and 1
.
>>> n = len(path[1].vertices)
>>> n
10
simulate_observations
(self, count, distribution='uniform')[source]¶Generate a simulated point pattern on the network.
dict
Keys are the edge tuple. Values are lists of new point coordinates.
Examples
Instantiate a network.
>>> import spaghetti
>>> from libpysal import examples
>>> ntw = spaghetti.Network(examples.get_path("streets.shp"))
Snap observations to the network.
>>> ntw.snapobservations(
... examples.get_path("crimes.shp"), "crimes", attribute=True
... )
Isolate the number of points in the dataset.
>>> npts = ntw.pointpatterns["crimes"].npoints
>>> npts
287
Simulate npts
number of points along the network
in a uniform distribution.
>>> sim = ntw.simulate_observations(npts)
>>> isinstance(sim, spaghetti.network.SimulatedPointPattern)
True
>>> sim.npoints
287
snapobservations
(self, in_data, name, idvariable=None, attribute=False)[source]¶Snap a point pattern shapefile to a network object. The
point pattern is stored in the network.pointpattern
attribute of the network object.
geopandas.GeoDataFrame
, str
}The input geographic data. Either (1) a path to a
shapefile (str); or (2) a geopandas.GeoDataFrame
.
str
Name to be assigned to the point dataset.
str
Column name to be used as the ID variable.
Defines whether attributes should be extracted. True
for
attribute extraction. False
for no attribute extraction.
Default is False
.
Notes
See [GFH19] for a detailed discussion on the modeling consequences of snapping points to spatial networks.
Examples
Instantiate a network.
>>> import spaghetti
>>> from libpysal import examples
>>> streets_file = examples.get_path("streets.shp")
>>> ntw = spaghetti.Network(in_data=streets_file)
Snap observations to the network.
>>> pt_str = "crimes"
>>> in_data = examples.get_path(pt_str+".shp")
>>> ntw.snapobservations(in_data, pt_str, attribute=True)
Isolate the number of points in the dataset.
>>> ntw.pointpatterns[pt_str].npoints
287
split_arcs
(self, distance)[source]¶Split all of the arcs in the network at a fixed distance.
float
The distance at which arcs are split.
spaghetti.Network
A newly instantiated spaghetti.Network
object.
Examples
Instantiate a network.
>>> import spaghetti
>>> from libpysal import examples
>>> ntw = spaghetti.Network(examples.get_path("streets.shp"))
Split the network into a segments of 200 distance units in length
(US feet in this case.).
This will include “remainder” segments unless the network is
comprised of arcs with lengths exactly divisible by distance
.
>>> n200 = ntw.split_arcs(200.0)
>>> len(n200.arcs)
688