range_finder package
Subpackages
- range_finder.tests_range_finder package
- Submodules
- range_finder.tests_range_finder.test_range_finder module
test_calculate_elevation()test_calculate_elevations()test_get_colors()test_origin_initialization()test_plot_initialization()test_point_annotate_node_distances_from_centre()test_point_annotate_origin_coordinates()test_point_annotate_street_names()test_point_get_gdf()test_point_get_interactive_map()test_point_get_origin()test_point_get_plot()test_point_get_points_in_range()test_point_get_range_graph()test_point_initialization()test_point_is_in_state()test_point_plot_network_graph()test_rangefinder_add_edge_colors()test_rangefinder_add_markers_to_map()test_rangefinder_add_points()test_rangefinder_create_interactive_map()test_rangefinder_get_plots()test_rangefinder_initialization()
- Module contents
Submodules
range_finder.rangeFinder module
rangeFinder.py: Finds and interactively visualizes points within a certain range of a central point.
__author__ = “pankajrsingla”
- class Origin(id, lat=None, long=None)[source]
Bases:
objectA class to represent the origin point in a network graph.
This class defines an origin point with attributes for its ID, latitude, and longitude. The ID is a unique identifier for the origin, while latitude and longitude represent its geographic coordinates.
- Variables:
id (int) – The unique identifier for the origin point.
lat (float) – The latitude of the origin point.
long (float) – The longitude of the origin point.
Example
>>> origin1 = Origin(1, 48.155322, 11.442776) >>> origin2 = Origin(2)
- class Plot(fig=None, ax=None)[source]
Bases:
objectA class to manage a Matplotlib plot.
This class provides a simple interface for managing Matplotlib plots. It takes a figure (fig) and an axes (ax) as parameters, which can be optionally provided during initialization.
- Variables:
fig (matplotlib.figure.Figure) – The figure object for the plot.
ax (matplotlib.axes._axes.Axes) – The axes object for the plot.
Example
>>> import matplotlib.pyplot as plt >>> fig, ax = plt.subplots() >>> my_plot = Plot(fig, ax)
- class Point(lat, long, length=1000, mode='drive', point_type='hydrant')[source]
Bases:
objectA class to represent a geographic point for analysis.
This class defines a point with geographic coordinates (latitude and longitude). It includes various attributes for customizing analysis parameters and later features additional methods for interacting with the point.
- Variables:
lat (float) – The latitude of the geographic point.
long (float) – The longitude of the geographic point.
length (float) – The length parameter for analysis (default is 100).
mode (str) – The mode of analysis (default is “drive”).
point_type (str) – Type of point, either “hydrant” or “fire”
elevation (float) – Height of the point above sea level.
graph (networkx.MultiDiGraph) – Networkx graph object representing the filtered street network.
origin – An object representing the origin point in the network graph.
gdf (geopandas.GeoDataFrame) – Geodataframe representing the network graph.
plot – An object containing the Matplotlib figure and axes for the network plot.
points_in_range (list of tuple) – List of points within the specified length from the point.
interactive (folium.Map) – Folium map for the network with interactive layers and markers.
debug (bool) – Boolean flag to display error/warning messages.
Example
>>> point = Point(48.155322, 11.442776)
- annotate_node_distances_from_centre(annotate_coordinates=False)[source]
Annotate node distances from the origin point on the network graph.
This function annotates the nodes on the network graph with their distances from the origin point. It optionally allows annotation of node coordinates and provides the option to show debugging information.
- Parameters:
annotate_coordinates (bool, optional) – Whether to annotate
node coordinates (default is False)
- Returns:
None
- annotate_origin_coordinates()[source]
Annotate the origin point’s coordinates on the network graph.
This function annotates the origin point on the network graph with its coordinates.
- Returns:
None
- annotate_street_names(repeat_street_names=False)[source]
Annotate street names on the network graph.
This function annotates street names on the network graph. Street names are only annotated once for each unique name unless ‘repeat_street_names’ is set to True.
- Parameters:
repeat_street_names (bool, optional) – Whether to repeat annotating
street names (default is False)
- Returns:
None
- calculate_elevation()[source]
Retrieve the elevation of a geographical point using an external API.
This method sends a GET request to an elevation API to obtain the elevation of a specific geographic point based on its latitude and longitude coordinates.
- Parameters:
self – The current object instance.
- Returns:
The elevation data is stored in the object’s ‘elevation’ attribute.
- Return type:
None
- get_gdf()[source]
Convert the graph to a GeoDataFrame.
This function converts the network graph to GeoDataFrame using OSMnx. It assigns the resulting GeoDataFrame to the ‘gdf’ attribute of the Point object.
- Returns:
None
Example
>>> point = Point(48.155322, 11.442776) >>> point.get_gdf()
- get_interactive_map(default_style='OpenStreetMap', edge_color='red')[source]
Generate an interactive Folium map displaying the network graph.
This function generates an interactive Folium map using network GeoDataFrame with a customizable style. It includes markers for the origin and the specified point.
- Parameters:
default_style (str, optional) – Default map style (default is “OpenStreetMap”).
edge_color (str, optional) – Color for the edges in the map (default is “red”).
- Returns:
None
- get_origin()[source]
Retrieve the nearest origin point on the graph.
This function determines the nearest origin point on the graph using OSMnx based on the point’s geographic coordinates. It assigns the origin’s latitude and longitude to the ‘origin’ attribute of the Point object.
- Returns:
None
Example
>>> point = Point(48.155322, 11.442776) >>> point.get_origin()
- get_plot(dpi=100, fig_size=(20, 16), annotate_distances=True, annotate_origin=False, annotate_streets=True, repeat_street_names=False)[source]
Visualizes points in range of a point.
Args: - dpi (int, optional): Dots per inch (DPI) resolution for the plot. Default is 100. - fig_size (tuple): A tuple containing width and height of the plot figure. - annotate_distances (bool, optional): Whether to annotate distances from the point. Default is True. - annotate_streets (bool, optional): Whether to annotate street names on the plot. Default is True. - annotate_origin (bool, optional): Whether to annotate the point’s origin point on the plot. Default is True. - repeat_street_names (bool, optional): Whether to repeat annotating
street names (default is False).
- Returns:
None
- get_points_in_range()[source]
Calculate distances from the origin point to each node in the graph.
This function calculates the distances from the origin point to each node in the graph and appends the tuple of results to the ‘points_in_range’ list of the RangeFinder object. Each tuple consists of the node’s OSMid, latitude, longitude, and its distance from the origin node.
- Returns:
None
- get_range_graph()[source]
Create a network graph around the point with a specified range.
This function generates a network graph using OSMnx from the geographic point, considering the specified range and mode of travel. It assigns the graph to the ‘graph’ attribute of the Point object.
If the graph cannot be created, an exception is raised with an informative error message.
- Returns:
None
Example
>>> point = Point(48.155322, 11.442776) >>> point.get_range_graph()
- is_in_state(state='Bayern')[source]
Check if the point is located in a specific state.
This function queries the Nominatim API to determine whether the geographic point is located in the specified state.
- Parameters:
state (str, optional) – The name of the state to check (default is “Bayern”).
- Returns:
True if the point is located in the specified state, False otherwise.
- Return type:
bool
Example
>>> point = Point(48.155322, 11.442776) >>> point.is_in_state("Bayern") True
- plot_network_graph(dpi=100, fig_size=(20, 16))[source]
Plot the network graph centered around the origin point.
This function generates a plot of the network graph using OSMnx and Matplotlib. The origin point is highlighted in red, while other nodes are shown in cyan.
- Parameters:
dpi (int, optional) – Dots per inch for the plot resolution (default is 100).
fig_size (tuple, optional) – Figure size in inches (default is (20, 16)).
- Returns:
None
- class RangeFinder[source]
Bases:
objectA class to manage a collection of geographic points and their analysis.
This class defines a RangeFinder object that can store multiple geographic points, their GeoDataFrames, plots, and interactive maps. It provides methods for merging and visualizing the collected data.
- Variables:
points (list) – A list to store Point objects representing geographic points.
merged_gdf (geopandas.GeoDataFrame) – A GeoDataFrame to store merged data.
plots (list) – A list to store Plot objects for each point.
merged_interactive (folium.Map) – An interactive Folium map for merged data.
show_elevations (bool) – Whether to show elevation data for points (default is False)
Example
>>> rf = RangeFinder() >>> point1 = Point(48.155322, 11.442776) >>> point2 = Point(40.712776, -74.005974) >>> rf.points.append(point1) >>> rf.points.append(point2)
- add_edge_colors()[source]
Assigns colors to edges in a GeoDataFrame representing a network graph based on their association with multiple points’ networks.
This function iterates through each point’s GeoDataFrame and identifies common edges among different points’ networks. It then assigns colors to these common edges based on a proportional blend of colors associated with each point’s network.
The resulting colored edges are added to the merged GeoDataFrame, and an interactive Folium map is generated to visualize the network graph with the assigned edge colors.
- Returns:
This method updates the instance attributes to store the merged GeoDataFrame with colored edges and the interactive Folium map.
- Return type:
None
- add_markers_to_map()[source]
Add markers (for points, edges, origin nodes) to the interactive folium map.
This method annotates the different components of the folium interactive map, including input points, origin nodes, and edges between the input points.
- Returns:
The updated interactive map is stored in the ‘merged_interactive’ attribute of the rangeFinder object.
- Return type:
None
- add_points(input_df)[source]
Add points from an input DataFrame to the RangeFinder.
This function adds points to the RangeFinder based on an input DataFrame. It creates Point objects for each row in the DataFrame, performs validation, and adds valid points along with their data to the RangeFinder.
- Parameters:
input_df (pandas.DataFrame) – Input DataFrame containing point information.
- Returns:
None
- calculate_elevations()[source]
Calculate elevations for multiple geographic points using an external API.
This method sends a single GET request to an elevation API with multiple coordinates concatenated together to obtain elevations for multiple geographic points simultaneously. The elevations are then stored in the ‘elevation’ attribute of each respective point.
- Returns:
The elevations are stored in the ‘elevation’ attribute of each individual point object.
- Return type:
None
- create_interactive_map(map_style='OpenStreetMap')[source]
Generate an interactive Folium map displaying merged data.
This function generates an interactive Folium map using merged GeoDataFrames and customizes the map with unique edge colors for each point. It includes markers for each point and its origin point.
- Parameters:
map_style (str, optional) – map layout style (default is “OpenStreetMap”).
- chuck_geojson_constructor(original_gdf, color)[source]
Converts a geopandas dataframe to a folium geojson, but does so by first splitting the gdf into chunks, then processing them in parallel.
- find_intersection(pair: tuple)[source]
Receive a tuple of two geopandas dataframes, return their intersection
- get_colors(n)[source]
Generate a list of distinct colors.
This function generates a list of distinct colors using the TABLEAU_COLORS color palette from the matplotlib library. It ensures that if the requested number of colors (n) is smaller than the number of available distinct colors, the complete palette is returned. If n is larger, the palette is repeated and extended as needed to accommodate the requested number.
- Parameters:
n (int) – The number of distinct colors to generate.
- Returns:
A list of distinct colors.
- Return type:
list
Example
>>> get_colors(5) ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd']