55import geopandas as gpd
66import pandas as pd
77import warnings
8+ import warnings
89from shapely .geometry import Point
910from schimpy .schism_sources_sinks import yaml2df
1011import yaml
@@ -24,6 +25,9 @@ def points_to_shp(fpath, points):
2425
2526def points_to_yaml (fpath , points ):
2627 # Ensure columns exist
28+ if "sites" not in points .columns or "stype" not in points .columns :
29+ raise ValueError ("Shapefile must contain 'sites' and 'stype' fields." )
30+ # Ensure columns exist
2731 if "sites" not in points .columns or "stype" not in points .columns :
2832 raise ValueError ("Shapefile must contain 'sites' and 'stype' fields." )
2933
@@ -39,27 +43,20 @@ def points_to_bp(fpath, points):
3943
4044 # Calculate cumulative distance along the transect
4145 points = points .reset_index (drop = True )
42- coords = points [['x' , 'y' ]].values
43-
44- # Calculate distances between consecutive points
45- distances = np .sqrt (np .sum (np .diff (coords , axis = 0 )** 2 , axis = 1 ))
46-
47- # Cumulative distance starting at 0
48- cumulative_distances = np .concatenate (([0 ], np .cumsum (distances )))
4946
5047 # Write to file
5148 with open (fpath , 'w' ) as f :
5249 # Write header
53- f .write ("point x y z distance \n " )
50+ f .write ("point x y z name \n " )
5451 f .write (f"{ len (points )} ! Number of points\n " )
5552
5653 # Write each point
5754 for i , (_ , row ) in enumerate (points .iterrows (), start = 1 ):
5855 x = float (row ['x' ])
5956 y = float (row ['y' ])
60- z = - 10000.0
61- distance = float ( cumulative_distances [ i - 1 ])
62- f .write (f"{ i } { x } { y } { z } { distance } \n " )
57+ z = float ( row [ 'z' ])
58+ name = row [ 'sites' ] if 'sites' in row else f"point_ { i } "
59+ f .write (f"{ i } { x } { y } { z } { name } \n " )
6360
6461 print (f"\n Build points file written to { fpath } ." )
6562
@@ -75,12 +72,15 @@ def shp_to_df(fpath):
7572 gdf ["x" ] = gdf .geometry .x
7673 gdf ["y" ] = gdf .geometry .y
7774
75+
7876 # Select relevant columns
7977 cols = ["x" , "y" ]
8078 if "sites" in gdf .columns :
8179 cols .insert (0 , "sites" )
8280 if "stype" in gdf .columns :
8381 cols .insert (1 if "sites" in gdf .columns else 0 , "stype" )
82+ if "z" in gdf .columns :
83+ cols .insert (1 if "z" in gdf .columns else 0 , "z" )
8484
8585 if "sites" not in gdf .columns or "stype" not in gdf .columns :
8686 warnings .warn ("Shapefile must contain 'sites' and 'stype' fields for conversion to yaml" )
@@ -125,6 +125,7 @@ def write_points(fpath, points):
125125 required = True ,
126126 type = click .Path (),
127127 help = "Output file (.yml, .yaml, .in, .bp, or .shp)." ,
128+ help = "Output file (.yml, .yaml, .in, .bp, or .shp)." ,
128129)
129130@click .help_option ("-h" , "--help" )
130131def convert_points_cli (input , output ):
@@ -140,3 +141,4 @@ def main(input, output):
140141
141142if __name__ == "__main__" :
142143 convert_points_cli ()
144+
0 commit comments