Skip to content

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

@shinbunya

Description

@shinbunya

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"
            )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions