the boundary boxes and buffered area output is still wrong. There is no buffer for this and the Boundary boxes are POINTS not BOXES. Make sure that instead of drawing boxes and buffering useless data, the script is placing BBs 1 for every 2 ervens on the lines of the buffered polygon edges. Place the points preferably as close as possible to where there are two ervens touching sides. import geopandas as gpd
import osmnx as ox
import numpy as np
from shapely.geometry import Point, LineString
from shapely.ops import unary_union, nearest_points
import networkx as nx
from fastkml import kml
import warnings
import os
import sys
import matplotlib.pyplot as plt
Check if all necessary shapefile components are present
required_extensions = ['.shp', '.shx', '.dbf']
basename, _ = os.path.splitext(input_layer_path)
missing_files = []
for ext in required_extensions:
file_path = basename + ext
if not os.path.exists(file_path):
missing_files.append(file_path)
if missing_files:
log("Error: The following essential shapefile components are missing:")
for file in missing_files:
log(f" - {file}")
log("Please ensure all necessary shapefile components are present.")
sys.exit(1)
else:
log("All essential shapefile components are present.")
Attempt to read the shapefile in latitude/longitude (EPSG:4326)
try:
log("Loading shapefile in EPSG:4326...")
erven_gdf_latlon = gpd.read_file(input_layer_path) # Keep original in lat/lon
log("Shapefile loaded successfully.")
except Exception as e:
log(f"Error loading shapefile: {e}")
sys.exit(1)
Check if GeoDataFrame is empty
if erven_gdf_latlon.empty:
log("Error: The input shapefile is empty. Please check the file contents.")
sys.exit(1)
Validate geometries
log("Validating geometries...")
erven_gdf_latlon['geometry'] = erven_gdf_latlon['geometry'].buffer(0) # Attempt to fix invalid geometries
invalid_geoms = erven_gdf_latlon[~erven_gdf_latlon.is_valid]
if not invalid_geoms.empty:
log(f"Found {len(invalid_geoms)} invalid geometries. Attempting to fix...")
erven_gdf_latlon.loc[~erven_gdf_latlon.is_valid, 'geometry'] = erven_gdf_latlon.loc[~erven_gdf_latlon.is_valid, 'geometry'].buffer(0)
# Recheck
invalid_geoms = erven_gdf_latlon[~erven_gdf_latlon.is_valid]
if not invalid_geoms.empty:
log("Some geometries could not be fixed. Please review the input data.")
sys.exit(1)
else:
log("All geometries are now valid.")
else:
log("All geometries are valid.")
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
if erven_gdf_latlon.crs is None:
log("Error: CRS is not set for the shapefile. Please assign the correct CRS before proceeding.")
sys.exit(1)
else:
log(f"Shapefile CRS: {erven_gdf_latlon.crs}")
Create a projected version for buffering and spatial operations
log(f"Projecting GeoDataFrame to UTM Zone 34S (EPSG:{TARGET_UTM_EPSG}) for accurate distance operations...")
try:
erven_gdf_utm = erven_gdf_latlon.to_crs(epsg=TARGET_UTM_EPSG)
log("Projection successful.")
except Exception as e:
log(f"Projection failed: {e}")
sys.exit(1)
log("Creating buffered area...")
buffered_gdf_utm = erven_gdf_utm.buffer(BUFFER_DISTANCE)
dissolved_buffer_utm = unary_union(buffered_gdf_utm)
log("Buffered area created and dissolved.")
Visualize the buffered areas
visualize_geometries([dissolved_buffer_utm, *erven_gdf_utm.geometry], title="Buffered Area and Polygons")
Reproject the dissolved buffer back to lat/lon for OSMnx bounding box
log("Reprojecting dissolved buffer back to EPSG:4326 for OSMnx...")
dissolved_buffer_latlon = gpd.GeoSeries([dissolved_buffer_utm], crs=erven_gdf_utm.crs).to_crs(epsg=4326)
dissolved_buffer_latlon = dissolved_buffer_latlon.iloc[0]
Get the bounds in latitude and longitude for OSM data download
bounds = dissolved_buffer_latlon.bounds # (minx, miny, maxx, maxy) in lat/lon
minx, miny, maxx, maxy = bounds
log(f"Buffered area bounds in lat/lon: minx={minx}, miny={miny}, maxx={maxx}, maxy={maxy}")
Download road network data within the buffered area in lat/lon
try:
log("Downloading road network data from OSM...")
roads_G = ox.graph_from_bbox(maxy, miny, maxx, minx, network_type='drive', simplify=True)
log("OSM road data successfully loaded.")
except ox._errors.InsufficientResponseError:
log("Error: No road data found for the specified area. Please check the bounding box or network type.")
sys.exit(1)
except Exception as e:
log(f"Error downloading road network data: {e}")
sys.exit(1)
Convert roads graph to GeoDataFrame
try:
roads_node_gdf_latlon, roads_edge_gdf_latlon = ox.graph_to_gdfs(roads_G)
log("Converted road network graph to GeoDataFrames.")
except Exception as e:
log(f"Error converting road network to GeoDataFrames: {e}")
sys.exit(1)
Validate road geometries
log("Validating road geometries...")
roads_edge_gdf_latlon['geometry'] = roads_edge_gdf_latlon['geometry'].buffer(0) # Attempt to fix invalid geometries
invalid_roads = roads_edge_gdf_latlon[~roads_edge_gdf_latlon.is_valid]
if not invalid_roads.empty:
log(f"Found {len(invalid_roads)} invalid road geometries. Attempting to fix...")
roads_edge_gdf_latlon.loc[~roads_edge_gdf_latlon.is_valid, 'geometry'] = roads_edge_gdf_latlon.loc[~roads_edge_gdf_latlon.is_valid, 'geometry'].buffer(0)
# Recheck
invalid_roads = roads_edge_gdf_latlon[~roads_edge_gdf_latlon.is_valid]
if not invalid_roads.empty:
log("Some road geometries could not be fixed. Please review the OSM data.")
sys.exit(1)
else:
log("All road geometries are now valid.")
else:
log("All road geometries are valid.")
Reproject roads to UTM for spatial operations
log(f"Projecting road network to UTM Zone 34S (EPSG:{TARGET_UTM_EPSG})...")
try:
roads_node_gdf = roads_node_gdf_latlon.to_crs(epsg=TARGET_UTM_EPSG)
roads_edge_gdf = roads_edge_gdf_latlon.to_crs(epsg=TARGET_UTM_EPSG)
log("Projection of road network successful.")
except Exception as e:
log(f"Projection of road network failed: {e}")
sys.exit(1)
Get dissolved polygon edges as network lines in UTM
for idx, row in dissolved_lines_gdf.iterrows():
line = row.geometry
if line is None or line.is_empty:
continue
coords = list(line.coords)
for u, v in zip(coords[:-1], coords[1:]):
trench_G.add_edge(u, v, geometry=LineString([u, v]), length=Point(u).distance(Point(v)))
log(f"Trench network graph created with {trench_G.number_of_nodes()} nodes and {trench_G.number_of_edges()} edges.")
Create aggregation nodes (boundary boxes) in UTM
log("Creating boundary boxes for aggregation nodes...")
boundary_boxes = []
for idx in range(0, len(erven_gdf_utm), 2): # One box for every two erven polygons
subset = erven_gdf_utm.geometry.iloc[idx:idx + 2]
union = unary_union(subset)
# Buffer slightly outward to avoid overlapping with erven
box_geometry = union.buffer(2).envelope # Buffer of 2 meters; adjust as needed
boundary_boxes.append(box_geometry)
visualize_geometries([dissolved_buffer_utm, *erven_gdf_utm.geometry, *boundary_boxes], title="Boundary Boxes and Buffer Area")
Place a central node in UTM
log("Calculating central point...")
central_point_utm = boundary_boxes_gdf_utm.geometry.unary_union.centroid
log(f"Central point at: ({central_point_utm.x}, {central_point_utm.y})")
Add central node to trench graph
trench_G.add_node((central_point_utm.x, central_point_utm.y))
log("Central node added to trench network graph.")
Connecting boundary boxes to the central node in trench graph
log("Connecting boundary boxes to the central node...")
for boundary in boundary_boxes_gdf_utm.geometry:
# Find the nearest point on trench network to the boundary centroid
nearest_geom = nearest_points(boundary.centroid, dissolved_lines_gdf.unary_union)[1]
if nearest_geom.is_empty:
log("Error: Nearest point on trench network is empty.")
continue
# Connect to central node
trench_G.add_edge(
(nearest_geom.x, nearest_geom.y),
(central_point_utm.x, central_point_utm.y),
geometry=LineString([nearest_geom, (central_point_utm.x, central_point_utm.y)]),
length=Point(nearest_geom).distance(Point(central_point_utm))
)
log("Boundary boxes connected to the central node.")
Snapping centroids to the nearest trench graph nodes in UTM
log("Snapping centroids to the nearest trench network nodes...")
def snap_to_nearest_trench_node(point, graph):
nearest_node = None
min_dist = float('inf')
for node in graph.nodes:
dist = point.distance(Point(node))
if dist < min_dist:
min_dist = dist
nearest_node = node
return nearest_node
centroid_nodes = []
for idx, centroid in centroids_gdf_utm.iterrows():
snapped_node = snap_to_nearest_trench_node(centroid.geometry, trench_G)
centroid_nodes.append(snapped_node)
log(f"Centroid {idx + 1} snapped to node at {snapped_node}")
Connect centroids to the trench network if not already connected
log("Connecting centroids to the trench network...")
for idx, (centroid, node) in enumerate(zip(centroids_gdf_utm.geometry, centroid_nodes), start=1):
if node not in trench_G.nodes:
trench_G.add_node(node)
trench_G.add_edge(node, node, geometry=LineString([node, node]), length=0)
log(f"Connected Centroid {idx} to trench network at node {node}")
else:
log(f"Centroid {idx} is already connected to trench network at node {node}")
Define the central node in UTM
central_node = (central_point_utm.x, central_point_utm.y)
log(f"Central node defined as: {central_node}")
Ensure central node is in graph
if central_node not in trench_G.nodes:
trench_G.add_node(central_node)
log("Central node added to trench network graph.")
else:
log("Central node is already in trench network graph.")
Find shortest paths from each centroid node to the central node
log("Calculating shortest paths from centroids to the central node...")
shortest_paths = {}
for idx, centroid_node in enumerate(centroid_nodes, start=1):
if centroid_node == central_node:
shortest_paths[centroid_node] = [central_node]
log(f"Centroid {idx} is the central node.")
continue
try:
path = nx.shortest_path(trench_G, source=centroid_node, target=central_node, weight='length')
shortest_paths[centroid_node] = path
log(f"Shortest path found for Centroid {idx}: {len(path)} nodes.")
except nx.NetworkXNoPath:
log(f"No path found from Centroid {idx} at node {centroid_node} to central node.")
except Exception as e:
log(f"Error finding path for Centroid {idx}: {e}")
Define function to offset LineString to a preferred side
def offset_line(line, side, distance):
try:
if side not in ['left', 'right']:
raise ValueError("SIDE_PREFERENCE must be 'left' or 'right'")
offset = line.parallel_offset(distance, side=side, join_style=2, resolution=16)
# Handle MultiLineString by merging
if offset.type == 'MultiLineString':
offset = unary_union(offset)
return offset
except Exception as e:
log(f"Offsetting failed: {e}")
return line # Return original line if offset fails
Function to create trench lines with side preference
def create_trench_lines(trench_graph, paths, side_pref, offset_dist, roads_gdf):
trench_lines = []
for path in paths.values():
if len(path) < 2:
continue
path_coords = list(path)
path_line = LineString(path_coords)
# Offset the trench line to the preferred side
offset_geom = offset_line(path_line, side_pref, offset_dist)
# Check for intersections with roads
intersects = roads_gdf.geometry.intersects(offset_geom)
if intersects.any():
log("Trench line intersects roads. Trying opposite side.")
opposite_side = 'right' if side_pref == 'left' else 'left'
offset_geom_opposite = offset_line(path_line, opposite_side, offset_dist)
intersects_opposite = roads_gdf.geometry.intersects(offset_geom_opposite)
if intersects_opposite.any():
log("Trench line still intersects roads after opposite side offset. Using original path.")
trench_lines.append(path_line)
else:
log("Trench line fixed by offsetting to the opposite side.")
trench_lines.append(offset_geom_opposite)
else:
log("Trench line successfully offset to the preferred side.")
trench_lines.append(offset_geom)
return trench_lines
Create trench lines with side preference
log("Creating trench lines with side preference...")
trench_lines = create_trench_lines(
trench_G,
shortest_paths,
SIDE_PREFERENCE,
OFFSET_DISTANCE,
roads_edge_gdf
)
log(f"Created {len(trench_lines)} trench line(s).")
placemark_central = kml.Placemark(ns='http://www.opengis.net/kml/2.2')
placemark_central.name = "Central Node"
placemark_central.geometry = central_point_latlon
document.append(placemark_central)
log("Added Central Node to KML.")
Add boundary boxes centroids to KML
for idx, centroid in boundary_centroids_latlon.iteritems():
placemark_boundary = kml.Placemark(ns='http://www.opengis.net/kml/2.2')
placemark_boundary.name = f'Boundary Box {idx + 1}'
placemark_boundary.geometry = centroid
document.append(placemark_boundary)
log(f"Added Boundary Box {idx + 1} to KML.")
Add trench lines to KML
for idx, trench in trench_gdf_latlon.iterrows():
placemark_line = kml.Placemark(ns='http://www.opengis.net/kml/2.2')
placemark_line.name = f"Trench Line {idx + 1}"
placemark_line.geometry = trench.geometry
document.append(placemark_line)
log(f"Added Trench Line {idx + 1} to KML.")
Add centroid points to KML
for idx, centroid in centroids_gdf_latlon.iterrows():
placemark_centroid = kml.Placemark(ns='http://www.opengis.net/kml/2.2')
placemark_centroid.name = f"Centroid {idx + 1}"
placemark_centroid.geometry = centroid.geometry
document.append(placemark_centroid)
log(f"Added Centroid {idx + 1} to KML.")
Save KML
output_kml_path = "output.kml"
try:
with open(output_kml_path, "w") as f:
f.write(k.to_string(prettyprint=True))
log(f"KML output saved to '{output_kml_path}'")
except Exception as e:
log(f"Error saving KML: {e}")
sys.exit(1)
It looks like you're working on a script that processes geospatial data related to ervens (land parcels) and attempts to generate some output based on this data. You've indicated that the creation of boundary boxes should be replaced with points representing the boundaries of every two ervens and that the buffer areas are not needed. Additionally, the final output should focus on placing boundary points (BBs) at the junctions of the touching sides of two erven.
Below is a modified version of your script that focuses on creating points for each pair of adjacent ervens. The code has been updated to remove the unnecessary buffer computation and directly compute the necessary points.
Adjusted Section of Your Script:
# Create points for boundary positions for every two ervens
log("Creating points for boundary positions for every two touching ervens...")
boundary_points = []
# Iterate through the ervens (make sure to check adjacency)
for idx in range(0, len(erven_gdf_utm) - 1, 2): # One point for every two ervens
erven1 = erven_gdf_utm.geometry.iloc[idx]
erven2 = erven_gdf_utm.geometry.iloc[idx + 1]
# Check if erven geometries are touching
if erven1.touches(erven2):
# Calculate the intersection points or touching points
intersection = erven1.intersection(erven2)
# Take only points if intersection is of type Point
if intersection.is_empty:
log(f"No touching point for erven {idx + 1} and {idx + 2}.")
else:
# If intersection is multi-point, take all points
if intersection.type == 'MultiPoint':
for point in intersection.geoms:
boundary_points.append(point)
elif intersection.type == 'Point':
boundary_points.append(intersection)
# Create a GeoDataFrame for boundary points
boundary_points_gdf_utm = gpd.GeoDataFrame(geometry=boundary_points, crs=erven_gdf_utm.crs)
log(f"Created {len(boundary_points_gdf_utm)} boundary point(s).")
# Visualize boundary points
visualize_geometries(boundary_points, title="Boundary Points from Touching Ervens")
# The rest of your existing code that uses boundary_points_gdf_utm
Key Changes Made:
Boundary Points Creation:
A for loop iterates over the ERVEN geometries, checking pairs to see if they touch using the .touches() method.
If they touch, the intersection is calculated to get the touching point(s).
Added points are appended to the boundary_points list.
Validation and Geometry Check:
The script checks if the intersection is empty or of the correct type before adding it to the list.
Visualization:
Visualizes the resulting boundary points.
Usage:
Just replace your original boundary box creation logic with the code I provided above. The rest of your existing procedures for KML output and network graph creation can then refer to the boundary_points_gdf_utm GeoDataFrame for any additional operations required on these boundary points.
Note:
Ensure your shapefiles' geometric data is in a projected coordinate system that fits the distance checks (to avoid issues with touching geometries and calculations).
You'll want to adjust any subsequent portions of the script that were directly dependent on the old boundary box logic to properly use the new boundary points logic instead.
These changes should directly address the primary issues you raised regarding the bounding box functionality and buffer area processing.