pghelpers module¶
-
tripsender.pghelpers.amenity_from_kml(mypath)¶ A function to fetch all kml data from a folder containing
KMLfiles.- Parameters
mypath – (str) A string containing the directory path to a folder with
KMLfiles- Returns
(GeoPandas GeoDataFrame) A
GDFof amenity lat and lon
-
tripsender.pghelpers.flatten_gdf(gdf)¶ A utility function to flatten the nested hierarchy of a
GeoPandas GeoDataFrame.- Parameters
gdf – (GeoPandas GeoDataFrame) A GDF to flatten
- Returns
(GeoPandas GeoDataFrame) A flattened GDF
-
tripsender.pghelpers.get_amenities(gdf, amenity)¶ A function to fetch amenities using a keyword from the
OpenStreetMapsOverpass api. (not recommended!) Use overpass turbo to help frame the keywords.- Parameters
gdf – (GeoPandas GeoDataFrame) A GDF containing the origins of homes
amenity – (str) A query string formatted for the OSM overpass API. (See overpass turbo)
- Returns
(GeoPandas GeoDataFrame)
-
tripsender.pghelpers.get_closest_from_kml(kmlPath, gdfAgentHomes)¶ A function to return a GDF of matched closest amenities from a
KMLdirectory and aGDFof agent origins/homes.- Parameters
kmlPath – (str) A string containing the directory path to a folder with
KMLfilesgdfAgentHomes – (GeoPandas GeoDataFrame) A
GeoDataFramecontaining an origin column with point geometry.
- Returns
(GeoPandas GeoDataFrame, list) A GDF containing the matched closest amenities and a list of amenity names
-
tripsender.pghelpers.get_gdf(area, feature, KEY='', VAL='', title='Data from Server', filter=False, local=True, web=True, plot=False)¶ A function to fetch spatial geometry from the
PostGISserver asGeoPandas GeoDataFrame. This function is simply a wrapper for thepsycopg2module. It constructs anSQLquery based on the the params provided and requests data from the server.- Parameters
area – (str) The name of the Primary area in the Model naming format.
feature – (str) The feature name to select from the
PostGISdatabase. (Refer to PostGIS naming convention)KEY – (str: Optional) An optional attribute to filter from the data at the server level
VAL – (str: Optional) An optional value for a given key to match from the data at the server level
title – (str: Optional) An optional title for the plot
filter – (bool: Optional) An optional input to specify if the data must be filtered at the server level
local – (bool: Optional) Which server to fetch the data from
web – (bool: Optional) If
True, the result will be reprojectedplot – (bool: Optional) If
True, the result will be plotted
- Returns
(GeoPandas GeoDataFrame, plot(Optional)) The resulting GeoPandas GeoDataFrame.
-
tripsender.pghelpers.get_nearest_amenity(gdA, gdfAmenity, amenityList)¶ A function to get the nearest amenities from a source and destination
GDF.- Parameters
gdA – (GeoPandas GeoDataFrame) A
GDFwith the origins of homesgdfAmenity – (GeoPandas GeoDataFrame) A
GDFcontaining the amenities with a unique name for each amenityamenityList – (list) A list of amenity names as string
- Returns
-
tripsender.pghelpers.get_pg_query(sql, local=True, web=True)¶ A utility function that fetches any data using an
SQLquery from thePostGISServer This function is a basic wrapper for thepsycopg2module with secrets included.- Parameters
sql – (str) An SQL query string
local – (bool: Optional) If
True, data will be fetched from the local serverweb – (bool: Optional) If
True, result will be reprojected toEPSG:4326
- Returns
-
tripsender.pghelpers.get_pg_route(source, target, plot=True, local=True)¶ A function to perform routing on the
PostGISserver usingPG_Routing.- Parameters
source – (tuple) A coordinate of the source point for routing
target – (tuple) A coordinate of the destination point for routing
plot – (bool: Optional) If
True, the result will be plottedlocal – (bool: Optional) If
True, data will be fetched from the local server
- Returns
(list) A list of tuples representing the nodes along the shortest path from source to target
-
tripsender.pghelpers.get_random_source(gdfAgentHomes)¶ A function to select a random source from a GeoPandas GeoDataframe.
- Parameters
gdfAgentHomes – (GeoPandas GeoDataFrame) A Geodataframe containing an origin column with point geometry.
- Returns
(tuple) Coordinates as a tuple
-
tripsender.pghelpers.get_road(local=True, web=True, ped=True)¶ A function that is hardcoded to only fetch the complete street network as a
GeoPandas GeoDataFrame. :param local: (bool: Optional) IfTrue, data will be fetched from the local server :param web: (bool: Optional) IfTrue, result will be reprojected toEPSG:4326:param ped: (bool: Optional) IfTrue, data will be fetched for the pedestrian network, else car network. :return: (GeoPandas GeoDataFrame) AGDFcontaining the entire street network on thePostGISserver.
-
tripsender.pghelpers.plot_gdf_pretty(gdf, column=None, figsize=(10, 10), legend=False)¶ A function that plots the
GeoDataFrameusing thecontextilylibrary against a map background.- Parameters
gdf – (GeoPandas GeoDataFrame) GDF to be plotted
column – (str: Optional) Name of the column to be plotted
figsize – (tuple: Optional)
legend – (bool: Optional)
- Returns
(Plot)