Fix this: import os import pandas as pd import geopandas as gpd import osmnx as ox from…
- User
Fix this: import os import pandas as pd import geopandas as gpd import osmnx as ox from fastkml import kml import zipfile import logging
Set up logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
Convert KMZ to KML
def kmz_to_kml(kmz_file, kml_file): if not os.path.exists(kmz_file): logging.error(f"KMZ file not found: {kmz_file}") return try: with zipfile.ZipFile(kmz_file, 'r') as kmz: kml_files = [name for name in kmz.namelist() if name.endswith('.kml')] if not kml_files: raise FileNotFoundError("No KML file found in KMZ archive.") kmz.extract(kml_files[0]) if os.path.exists(kml_file): os.remove(kml_file) os.rename(kml_files[0], kml_file) logging.info("KMZ converted to KML successfully.") except FileNotFoundError as fnfe: logging.error(f"File not found in KMZ: {fnfe}") except zipfile.BadZipFile as bzf: logging.error(f"Bad zip file: {bzf}") except PermissionError as pe: logging.error(f"Permission error: {pe}") except Exception as e: logging.error(f"Error in kmz_to_kml: {e}")
Parse KML to GeoDataFrame
def parse_kml_to_gdf(kml_file): if not os.path.exists(kml_file): logging.error(f"KML file not found: {kml_file}") return None
try: k = kml.KML() with open(kml_file, 'rb') as f: k.from_string(f.read()) features = list(k.features()) placemarks = [placemark for feature in features for placemark in feature.features()] if not placemarks: logging.warning("No placemarks found in KML file.") return None names = [] geometries = [] for placemark in placemarks: if hasattr(placemark, 'geometry'): names.append(placemark.name) geometries.append(placemark.geometry) if not names or not geometries: logging.warning("No valid placemarks found with geometry.") gdf = gpd.GeoDataFrame({'name': names, 'geometry': geometries}, crs="EPSG:4326") if gdf.empty: logging.warning("GeoDataFrame is empty after parsing KML file.") else: logging.info(f"Parsed {len(gdf)} destinations from KML file.") return gdf except pd.errors.EmptyDataError as ede: logging.error(f"Empty data error in parse_kml_to_gdf: {ede}") except Exception as e: logging.error(f"Error in parse_kml_to_gdf: {e}") return NoneLoad the road network
def load_road_network(shp_file): if not os.path.exists(shp_file): logging.error(f"Shapefile not found: {shp_file}") return None
try: gdf = gpd.read_file(shp_file) if 'geometry' not in gdf.columns: logging.error("Shapefile missing 'geometry' column.") return None gdf_nodes = gdf[gdf.geometry.type == 'Point'].copy() gdf_edges = gdf[gdf.geometry.type != 'Point'].copy() if gdf_nodes.empty: logging.warning("No point geometries found in shapefile.") # Ensure 'x' and 'y' columns are present in the nodes dataframe if 'x' not in gdf_nodes.columns or 'y' not in gdf_nodes.columns: gdf_nodes[['x', 'y']] = gdf_nodes.geometry.apply(lambda geom: pd.Series(geom.coords[0]) if geom else pd.Series([None, None])) if gdf_edges.empty: logging.warning("No edge geometries found in shapefile.") # Create graph G = ox.graph_from_gdfs(gdf_nodes[['x', 'y']], gdf_edges) logging.info("Road network loaded successfully.") return G except pd.errors.EmptyDataError as ede: logging.error(f"Empty data error in load_road_network: {ede}") except MemoryError as me: logging.error(f"Memory error in load_road_network: {me}") except Exception as e: logging.error(f"Error in load_road_network: {e}") return NoneMain execution
def main(): kmz_file = 'input.kmz' kml_file = 'destinations.kml' shp_file = 'area.shp'
try: # Convert KMZ to KML kmz_to_kml(kmz_file, kml_file) # Parse KML to GeoDataFrame gdf_destinations = parse_kml_to_gdf(kml_file) if gdf_destinations is None: raise ValueError("Failed to parse KML file into GeoDataFrame.") # Load road network G = load_road_network(shp_file) if G is None: raise ValueError("Failed to load road network.") # Do further processing with gdf_destinations and G logging.info("Road network and destinations processed successfully.") except ValueError as ve: logging.error(f"ValueError in main: {ve}") except MemoryError as me: logging.error(f"MemoryError in main: {me}") except Exception as e: logging.error(f"Unexpected error in main: {e}")if name == 'main': main()
- Assistant
It looks like you're trying to process a KMZ file, extract destination points from it, and load a road network from a shapefile to perform further analysis. The code you've provided has a few issues that need to be addressed to make it work correctly.
Below, I've fixed the code and added explanations for each change to help you understand what's going on.
import os import pandas as pd import geopandas as gpd import osmnx as ox import networkx as nx from shapely.geometry import Point from fastkml import kml import zipfile import logging # Set up logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') # Convert KMZ to KML def kmz_to_kml(kmz_file, kml_file): if not os.path.exists(kmz_file): logging.error(f"KMZ file not found: {kmz_file}") return try: with zipfile.ZipFile(kmz_file, 'r') as kmz: # Extract all files from KMZ kmz.extractall() # Find the KML file (may be in a subdirectory) kml_files = [os.path.join(root, name) for root, dirs, files in os.walk('.') for name in files if name.endswith(".kml")] if not kml_files: raise FileNotFoundError("No KML file found in KMZ archive.") # Use the first KML file found extracted_kml_file = kml_files[0] # Move and rename the KML file to the specified kml_file if os.path.exists(kml_file): os.remove(kml_file) os.replace(extracted_kml_file, kml_file) logging.info("KMZ converted to KML successfully.") except FileNotFoundError as fnfe: logging.error(f"File not found in KMZ: {fnfe}") except zipfile.BadZipFile as bzf: logging.error(f"Bad zip file: {bzf}") except PermissionError as pe: logging.error(f"Permission error: {pe}") except Exception as e: logging.error(f"Error in kmz_to_kml: {e}") # Parse KML to GeoDataFrame def parse_kml_to_gdf(kml_file): if not os.path.exists(kml_file): logging.error(f"KML file not found: {kml_file}") return None try: with open(kml_file, 'rb') as f: doc = f.read() k = kml.KML() k.from_string(doc) features = list(k.features()) placemarks = [] # Recursive function to extract placemarks def extract_placemarks(features): for feature in features: if isinstance(feature, kml.Placemark): placemarks.append(feature) elif hasattr(feature, 'features'): extract_placemarks(feature.features()) extract_placemarks(features) if not placemarks: logging.warning("No placemarks found in KML file.") return None names = [] geometries = [] for placemark in placemarks: if hasattr(placemark, 'geometry') and placemark.geometry is not None: names.append(placemark.name) # Convert KML geometry to shapely geometry geometries.append(placemark.geometry.geometry) if not names or not geometries: logging.warning("No valid placemarks found with geometry.") return None gdf = gpd.GeoDataFrame({'name': names, 'geometry': geometries}, crs="EPSG:4326") if gdf.empty: logging.warning("GeoDataFrame is empty after parsing KML file.") else: logging.info(f"Parsed {len(gdf)} destinations from KML file.") return gdf except Exception as e: logging.error(f"Error in parse_kml_to_gdf: {e}") return None # Load the road network def load_road_network(shp_file): if not os.path.exists(shp_file): logging.error(f"Shapefile not found: {shp_file}") return None try: # Read edges from the shapefile gdf_edges = gpd.read_file(shp_file) if 'geometry' not in gdf_edges.columns: logging.error("Shapefile missing 'geometry' column.") return None # Initialize dictionaries to collect nodes and edges data nodes_data = {} edges_data = [] node_id = 0 # starting node ID node_coords_to_id = {} for idx, row in gdf_edges.iterrows(): geom = row.geometry if geom is None: continue if geom.geom_type == 'LineString': coords = list(geom.coords) elif geom.geom_type == 'MultiLineString': coords = [] for linestring in geom.geoms: coords.extend(linestring.coords) else: continue # Skip other geometry types # Get start and end coordinates u_coord = coords[0] v_coord = coords[-1] # Add nodes to nodes_data if they don't exist for coord in [u_coord, v_coord]: if coord not in node_coords_to_id: node_coords_to_id[coord] = node_id nodes_data[node_id] = {'osmid': node_id, 'x': coord[0], 'y': coord[1]} node_id += 1 u_id = node_coords_to_id[u_coord] v_id = node_coords_to_id[v_coord] # Add edge data edge_attributes = row.drop('geometry').to_dict() edge_attributes.update({'u': u_id, 'v': v_id, 'key': 0, 'geometry': geom}) edges_data.append(edge_attributes) # Create GeoDataFrames for nodes and edges nodes_gdf = gpd.GeoDataFrame.from_dict(nodes_data, orient='index') nodes_gdf.set_index('osmid', inplace=True) nodes_gdf['geometry'] = nodes_gdf.apply(lambda row: Point(row['x'], row['y']), axis=1) edges_gdf = gpd.GeoDataFrame(edges_data) edges_gdf.set_geometry('geometry', inplace=True) # Ensure proper columns nodes_gdf = nodes_gdf[['x', 'y', 'geometry']] edges_gdf = edges_gdf[['u', 'v', 'key', 'geometry'] + [col for col in edges_gdf.columns if col not in ['u', 'v', 'key', 'geometry']]] # Create graph G = ox.graph_from_gdfs(nodes_gdf, edges_gdf) logging.info("Road network loaded successfully.") return G except Exception as e: logging.error(f"Error in load_road_network: {e}") return None # Main execution def main(): kmz_file = 'input.kmz' kml_file = 'destinations.kml' shp_file = 'area.shp' try: # Convert KMZ to KML kmz_to_kml(kmz_file, kml_file) # Parse KML to GeoDataFrame gdf_destinations = parse_kml_to_gdf(kml_file) if gdf_destinations is None: raise ValueError("Failed to parse KML file into GeoDataFrame.") # Load road network G = load_road_network(shp_file) if G is None: raise ValueError("Failed to load road network.") # Do further processing with gdf_destinations and G logging.info("Road network and destinations processed successfully.") except ValueError as ve: logging.error(f"ValueError in main: {ve}") except MemoryError as me: logging.error(f"MemoryError in main: {me}") except Exception as e: logging.error(f"Unexpected error in main: {e}") if __name__ == '__main__': main()
Explanation of Changes:
kmz_to_kmlFunction:-
Issue: The original code assumed the KML file inside the KMZ would be extracted to the current directory without considering internal directory structures, which could lead to
FileNotFoundError. -
Fix:
- Used
kmz.extractall()instead of extracting a single file. This extracts all files, preserving the directory structure. - Walked through the extracted files using
os.walk()to find the KML file regardless of its location within nested directories. - Used
os.replace()instead ofos.rename()to handle cross-filesystem moves and to overwrite the existing file if necessary.
- Used
parse_kml_to_gdfFunction:-
Issue:
- The original code didn't recursively extract placemarks from nested features like folders or documents, potentially missing many placemarks.
- Attempted to use
placemark.geometrydirectly, which is afastkmlobject, not directly compatible with GeoPandas.
-
Fix:
- Added a recursive
extract_placemarksfunction to traverse all nested features within the KML. - Converted
fastkmlgeometry objects to Shapely geometries usingplacemark.geometry.geometrybefore adding them to the GeoDataFrame. - Added a check to return
Noneif no valid placemarks with geometry are found.
- Added a recursive
load_road_networkFunction:-
Issue:
- The original code attempted to use
osmnx.graph_from_gdfswithout properly setting up the required node and edge data structures. - The expected columns (
osmid,u,v,key) were missing, leading to errors when creating the graph.
- The original code attempted to use
-
Fix:
- Extracted nodes from the start and end points of each edge (LineString) and assigned unique IDs (
osmid). - Built a mapping from coordinate tuples to node IDs to avoid duplicates.
- Created nodes and edges GeoDataFrames with the required columns for
osmnx.graph_from_gdfs. - Ensured that the edges have
u,v, andkeyattributes, and that the nodes haveosmid,x,y, andgeometry.
- Extracted nodes from the start and end points of each edge (LineString) and assigned unique IDs (
Additional Notes:
-
Dependencies: Make sure all necessary Python packages are installed. You can install any missing packages using
pip. For example:pip install pandas geopandas osmnx networkx shapely fastkml -
File Paths: Ensure that the
input.kmzandarea.shpfiles are in the correct directory or provide the full path to these files. -
Logging: The logging statements will help you trace the execution and identify any runtime issues.
-
Further Processing: In the
mainfunction, after successfully loading the destinations and road network, you can proceed with your specific analysis or processing needs.
If you have any questions or need further assistance with the code, feel free to ask!
-