77 lines
2.3 KiB
Python
77 lines
2.3 KiB
Python
|
import osmnx as ox
|
||
|
import geopandas as gpd
|
||
|
from shapely import wkt
|
||
|
from shapely.geometry import Point
|
||
|
from qgis.core import QgsProject, QgsVectorLayer, QgsFeature, QgsGeometry, QgsWkbTypes, QgsField
|
||
|
|
||
|
print("Getting road centerlines...")
|
||
|
|
||
|
# Get the HOME_POINTS layer
|
||
|
home_points_layer = QgsProject.instance().mapLayersByName('HOME_POINTS')[0]
|
||
|
|
||
|
# Check if there's at least one feature in the layer
|
||
|
if home_points_layer.featureCount() > 0:
|
||
|
# Generate a buffer around each point
|
||
|
buffer_distance = 0.01 # change to your desired buffer distance
|
||
|
buffers = []
|
||
|
for feature in home_points_layer.getFeatures():
|
||
|
point = wkt.loads(feature.geometry().asWkt())
|
||
|
buffer_polygon = point.buffer(buffer_distance)
|
||
|
buffers.append(buffer_polygon)
|
||
|
|
||
|
# Combine all buffers into a single polygon
|
||
|
union_polygon = gpd.GeoSeries(buffers).unary_union
|
||
|
|
||
|
# Create PROJECTAREA layer in memory
|
||
|
project_area_layer = QgsVectorLayer("Polygon?crs=epsg:4326", "PROJECTAREA", "memory")
|
||
|
pr = project_area_layer.dataProvider()
|
||
|
|
||
|
# Start editing the layer
|
||
|
project_area_layer.startEditing()
|
||
|
|
||
|
# Create new feature for the layer
|
||
|
f = QgsFeature()
|
||
|
f.setGeometry(QgsGeometry.fromWkt(union_polygon.wkt))
|
||
|
pr.addFeature(f)
|
||
|
|
||
|
# Commit changes
|
||
|
project_area_layer.commitChanges()
|
||
|
|
||
|
# Add the layer to the Layers panel in QGIS
|
||
|
project_area_layer.setOpacity(0.3)
|
||
|
QgsProject.instance().addMapLayer(project_area_layer)
|
||
|
|
||
|
# Use OSMnx to download the street network
|
||
|
G = ox.graph_from_polygon(union_polygon, network_type='drive')
|
||
|
|
||
|
# Convert the graph into GeoDataFrames
|
||
|
gdf_nodes, gdf_edges = ox.graph_to_gdfs(G)
|
||
|
|
||
|
# Convert gdf_edges (GeoDataFrame) to WKT format for QGIS
|
||
|
wkt_list = gdf_edges['geometry'].to_wkt().tolist()
|
||
|
|
||
|
# Create a new layer for the street centerlines
|
||
|
vl = QgsVectorLayer("LineString?crs=epsg:4326", "CENTERLINES", "memory")
|
||
|
|
||
|
# Start editing the layer
|
||
|
vl.startEditing()
|
||
|
|
||
|
# Get the data provider
|
||
|
pr = vl.dataProvider()
|
||
|
|
||
|
# Create new features for the layer
|
||
|
for wkt in wkt_list:
|
||
|
f = QgsFeature()
|
||
|
f.setGeometry(QgsGeometry.fromWkt(wkt))
|
||
|
pr.addFeature(f)
|
||
|
|
||
|
# Commit changes
|
||
|
vl.commitChanges()
|
||
|
|
||
|
# Add the layer to the Layers panel in QGIS
|
||
|
QgsProject.instance().addMapLayer(vl)
|
||
|
|
||
|
else:
|
||
|
print("No points found in the HOME_POINTS layer.")
|
||
|
|
||
|
print("Done.")
|