Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function to return nearest met sites to snow Ex sites #7

Open
beautah opened this issue Jul 13, 2022 · 0 comments
Open

function to return nearest met sites to snow Ex sites #7

beautah opened this issue Jul 13, 2022 · 0 comments

Comments

@beautah
Copy link
Member

beautah commented Jul 13, 2022

as a developer I need to know what n # of met sites are closest to any given snow ex sites.

The function should take a lat/long of a snow ex site, a input geojson (mostly snotels, but any sites) and a n # of sites and return a geojson representation of the n # of snotels that are closest to that snow ex site. The calculation does not need to be exact but should be accurate enough to be trusted and with low computational overhead.

A snippet which may help taken from another project takes a input gdf and returns other sites within that gdf that are closest to a given site. Just need to modify it to use a lat and long as input rather than a reference to another site

def nearest_sites(df_meta, resp_site=None, pred_sites=None, num_sites=10):
    try:
        num_sites = int(num_sites)
    except ValueError:
        num_sites = 10
    tx = df_meta[df_meta["triplet"] == resp_site]["latitude"]
    ty = df_meta[df_meta["triplet"] == resp_site]["longitude"]

    def calc_dist(row, tx=tx, ty=ty):
        return (row["latitude"] - tx) ** 2 + (row["longitude"] - ty) ** 2

    df_meta["proximity"] = df_meta.apply(calc_dist, axis=1)
    df_meta.sort_values(by="proximity", inplace=True)
    df_nearest = df_meta.head(num_sites + 1).copy()
    triplets_in_use = tuple(set([i for i in (resp_site,) + pred_sites if i]))
    df_in_use = df_meta[df_meta["triplet"].isin(triplets_in_use)]
    df = pd.concat([df_nearest, df_in_use]).drop_duplicates(ignore_index=True)
    return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant