oceanmodeling / adcircpy

Python library for managing input and output files for the ADCIRC model
https://adcircpy.readthedocs.io/en/latest/
GNU General Public License v3.0
35 stars 26 forks source link

Method for obtaining the indexes of the nodes nearest to specified locations #164

Open shinbunya opened 1 year ago

shinbunya commented 1 year ago

I needed to find the indexes of the nodes nearest to specified locations. I wonder if there is a common need for this. I wrote the following Grd class instance method for my own purpose. I used this to extract values at the locations for water level timeseries plots. I'd like to know if this kind of additional functions would add any value to ADCIRCpy.

    def get_nearest_index_to_point(
        self, point_xx: Any, point_yy: Any, point_crs: Union[CRS, str]
    ):
        """Find the nearest neighbor node to the specified points and return the indexes. """

        if point_crs is not None and self.crs is not None:
            transformer = Transformer.from_crs(point_crs, 'EPSG:4326', always_xy=True)
            point_xx_projected, point_yy_projected = transformer.transform(point_xx, point_yy)

            utm_crs_list = query_utm_crs_info(
                datum_name="WGS 84",
                area_of_interest=AreaOfInterest(
                    west_lon_degree=min(point_xx_projected),
                    south_lat_degree=min(point_yy_projected),
                    east_lon_degree=max(point_xx_projected),
                    north_lat_degree=max(point_yy_projected),
                ),
            )
            utm_crs = CRS.from_epsg(utm_crs_list[0].code)

            transformer = Transformer.from_crs(point_crs, utm_crs, always_xy=True)
            point_xx_projected, point_yy_projected = transformer.transform(point_xx, point_yy)
            grd_xx_projected, grd_yy_projected = transformer.transform(self.coords.iloc[:,0].values, self.coords.iloc[:,1].values)

            data = np.concatenate((np.vstack(grd_xx_projected),np.vstack(grd_yy_projected)),axis=1)
            tree = cKDTree(data)
            data = np.concatenate((np.vstack(point_xx_projected),np.vstack(point_yy_projected)),axis=1)
            _, idxs = tree.query(data, workers=-1)
            return idxs
        else:
            raise Exception(
                "CRS must be specified for both mesh coordinates and point coordinates"
            )
krober10nd commented 1 year ago

Can't we leverage methods in geopandas to make this simpler?

import geopandas as gpd
from geopandas.tools import nearest_points

# Load the GeoDataFrame
gdf = gpd.read_file('my_data.shp')

# Get the nearest point for each point in the GeoDataFrame
# (assuming the points are stored in a column called "geometry")
gdf['nearest_point'] = gdf['geometry'].apply(lambda x: nearest_points(x, gdf['geometry'])[1])

# Calculate the distance between each point and its nearest point
gdf['distance_to_nearest_point'] = gdf.apply(lambda row: row['geometry'].distance(row['nearest_point']), axis=1)
SorooshMani-NOAA commented 1 year ago

@shinbunya thank you for helping extending adcircpy functionalities. Can you please look into using what @krober10nd suggests and see if it helps getting the results you're looking for?

In any case it'd be great if you could please add an automated test case in the tests directory for this functionality and start a pull request.

shinbunya commented 1 year ago

Thanks @krober10nd for the suggestion. I will update it accordingly. @SorooshMani-NOAA I will add a test case and do a pull request. Thanks.