dopecarpet/plot.py
randogoth 9902419b41 init
2025-02-26 10:53:07 +00:00

54 lines
1.8 KiB
Python

import argparse
import matplotlib
matplotlib.use("Agg") # Use Agg backend for non-GUI environments
import geopandas as gpd
import matplotlib.pyplot as plt
from pathlib import Path
def plot_geometries(parquet_file: str, save_as: str):
"""
Plots the geometries inside a GeoParquet file and saves the image instead of showing it.
Args:
parquet_file (str): Path to the Parquet file containing geometries.
save_as (str): Path to save the output plot image.
"""
parquet_path = Path(parquet_file)
if not parquet_path.exists():
raise FileNotFoundError(f"❌ File '{parquet_file}' not found.")
# Load the Parquet file into a GeoDataFrame
gdf = gpd.read_parquet(parquet_path)
# Ensure it has a valid geometry column
if "geometry" not in gdf.columns:
raise ValueError("❌ No 'geometry' column found in the file.")
# Plot the geometries
fig, ax = plt.subplots(figsize=(8, 8))
gdf.plot(ax=ax, edgecolor="black", facecolor="lightblue", alpha=0.5)
# Add labels
ax.set_title(f"Geometries in {parquet_file}")
ax.set_xlabel("Longitude")
ax.set_ylabel("Latitude")
# Save the figure instead of showing it
plt.savefig(save_as, dpi=300)
print(f"✅ Plot saved as {save_as}")
def main():
parser = argparse.ArgumentParser(description="Plot geometries from a GeoParquet file and save as an image.")
parser.add_argument("-i", "--input", required=True, help="Path to the input GeoParquet file.")
parser.add_argument("-o", "--output", required=True, help="Path to save the output plot image.")
args = parser.parse_args()
try:
plot_geometries(args.input, args.output)
except Exception as e:
print(f"❌ Error: {e}")
if __name__ == "__main__":
main()