The following script creates dissolved polygon edges from erven data. I need these edges…
- User
The following script creates dissolved polygon edges from erven data. I need these edges to interconnect to eachother to form a network, please modify the script accordingly - indicate the lines interconnecting these polygon edges using BLUE linestrings. import geopandas as gpd import osmnx as ox import numpy as np from shapely.geometry import LineString from shapely.ops import unary_union import warnings import os import sys import matplotlib.pyplot as plt
Suppress specific warnings
warnings.filterwarnings("ignore", category=FutureWarning)
Define verbose logging function
def log(message): print(message)
Function to visualize geometries
def visualize_geometries(geometries, title): plt.figure(figsize=(10, 10)) for geom in geometries: if geom.type == 'LineString': x, y = geom.xy plt.plot(x, y, color='red', linewidth=2) plt.title(title) plt.xlabel("Longitude") plt.ylabel("Latitude") plt.axis("equal") plt.grid() plt.show()
Parameters
TARGET_UTM_EPSG = 32734 # EPSG code for WGS 84 / UTM zone 34S input_layer_path = r"D:\Spatial\Piketberg Erven.shp"
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) 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 valid.")
Ensure CRS is set
- Assistant