Skip to content

Commit

Permalink
Merge pull request #64 from ArtesiaWater/dev
Browse files Browse the repository at this point in the history
Update to new version
  • Loading branch information
OnnoEbbens authored Aug 13, 2021
2 parents ad8b3b5 + 04ea69c commit 448b867
Show file tree
Hide file tree
Showing 31 changed files with 1,996 additions and 1,690 deletions.
1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
'navigation_depth': 4,
'includehidden': True,
'titles_only': False,
"github_url": "https://github.com/ArtesiaWater/hydropandas",
}

# Add any paths that contain custom static files (such as style sheets) here,
Expand Down
5 changes: 4 additions & 1 deletion docs/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ Dependencies
This module has several optional dependencies that have to be installed.
These include:

- ... (to be added)
- pastastore (create pastas models of an ObsCollection)
- folium and bokeh (make an interactive map of an ObsCollection)
- xarray and netCDF4 (get regis layers for groundwater observations)
- flopy (interaction with modflow data)

See the :ref:`examples` section for some quick examples on how to get started.

1,195 changes: 783 additions & 412 deletions examples/01_groundwater_observations.ipynb

Large diffs are not rendered by default.

527 changes: 374 additions & 153 deletions examples/02_knmi_observations.ipynb

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions hydropandas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
from .extensions import plots as _plots
from .extensions import stats as _stats
from .version import __version__

import logging
logging.getLogger('hydropandas').addHandler(logging.NullHandler())
131 changes: 108 additions & 23 deletions hydropandas/extensions/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def get_extent(self, xcol='x', ycol='y', buffer=0):
return (xmin, xmax, ymin, ymax)

def set_lat_lon(self, in_epsg='epsg:28992', out_epsg='epsg:4326',
add_to_meta=True, verbose=False):
add_to_meta=True):
"""create columns with lat and lon values of the observation points.
Parameters
Expand All @@ -72,8 +72,6 @@ def set_lat_lon(self, in_epsg='epsg:28992', out_epsg='epsg:4326',
add_to_meta : bool, optional
if True the lat and lon values are added to the observation meta
dictionary. The default is True.
verbose : boolean, optional
Print additional information to the screen (default is False).
Returns
-------
Expand All @@ -84,12 +82,10 @@ def set_lat_lon(self, in_epsg='epsg:28992', out_epsg='epsg:4326',
for iname in df_lat_lon.index:
self._obj._set_metadata_value(iname, 'lat',
df_lat_lon.loc[iname,
'lat'], add_to_meta,
verbose)
'lat'], add_to_meta)
self._obj._set_metadata_value(iname, 'lon',
df_lat_lon.loc[iname,
'lon'], add_to_meta,
verbose)
'lon'], add_to_meta)

def get_lat_lon(self, in_epsg='epsg:28992', out_epsg='epsg:4326'):
"""get lattitude and longitude from x and y attributes.
Expand Down Expand Up @@ -118,7 +114,7 @@ def get_lat_lon(self, in_epsg='epsg:28992', out_epsg='epsg:4326'):

def get_nearest_point(self, obs_collection2=None, gdf2=None,
xcol_obs1='x', ycol_obs1='y',
xcol_obs2='x', ycol_obs2='y', verbose=False):
xcol_obs2='x', ycol_obs2='y'):
"""get nearest point of another obs collection for each point in the
current obs collection.
Expand All @@ -136,8 +132,6 @@ def get_nearest_point(self, obs_collection2=None, gdf2=None,
x column in obs_collection2 used to get geometry
ycol_obs2 : str, optional
y column in self._obj used to get geometry
verbose : boolean, optional
Print additional information to the screen (default is False).
Returns
-------
Expand Down Expand Up @@ -174,10 +168,12 @@ def distance_nearest_point(point_gdf1, pts=pts_gdf2):

return gdf1[['nearest point', 'distance nearest point']]

def get_nearest_polygon(self, gdf=None,
xcol_obs='x', ycol_obs='y',
verbose=False):
"""get nearest polygon for each point in the obs collection.
def _get_nearest_geometry(self, gdf=None,
xcol_obs='x', ycol_obs='y',
geometry_type='polygon',
multiple_geometries='error'):
"""get nearest geometry for each point in the obs collection. Function
works for line and polygon geometries.
Parameters
----------
Expand All @@ -187,26 +183,115 @@ def get_nearest_polygon(self, gdf=None,
x column in self._obj used to get geometry
ycol_obs : str, optional
y column in self._obj used to get geometry
verbose : boolean, optional
Print additional information to the screen (default is False).
geometry_type : str, optional
geometry type, can be 'polygon' or 'line'.
multiple_geometries : str, optional
keyword on how to deal with multiple geometries being nearest.
Options are:
'error' -> raise a ValueError
'keep_all' -> return the indices of multiple geometries as a
string seperated by a comma
'keep_first' -> return the index of the first geometry
Returns
-------
pandas.DataFrame
with columns 'nearest polygon' and 'distance nearest polygon'
with columns 'nearest geometry' and 'distance nearest geometry'
"""

gdf_obs = self._obj.to_gdf(xcol=xcol_obs, ycol=ycol_obs)

gdf = gdf.copy()
for i, point in gdf_obs.geometry.items():
distances = [point.distance(pol) for pol in gdf.geometry.values]
if (np.array(distances) == np.min(distances)).sum() > 1:
raise ValueError('multiple polygons are nearest')
gdf_obs.loc[i, 'nearest polygon'] = gdf.iloc[np.argmin(
distances)].name
gdf_obs.loc[i, 'distance nearest polygon'] = np.min(distances)
if multiple_geometries == 'error':
raise ValueError(f'multiple {geometry_type}s are nearest')
elif multiple_geometries == 'keep_all':
ids = []
for i_min in np.where(np.array(distances) == np.min(distances))[0]:
ids.append(gdf.index[i_min])
gdf_obs.loc[i, f'nearest {geometry_type}'] = ', '.join(ids)
gdf_obs.loc[i, f'distance nearest {geometry_type}'] = np.min(distances)
elif multiple_geometries == 'keep_first':
gdf_obs.loc[i, f'nearest {geometry_type}'] = gdf.iloc[np.argmin(
distances)].name
gdf_obs.loc[i, f'distance nearest {geometry_type}'] = np.min(distances)
else:
raise ValueError(f'invalid value for multiple_geometries -> {multiple_geometries}')
else:
gdf_obs.loc[i, f'nearest {geometry_type}'] = gdf.iloc[np.argmin(
distances)].name
gdf_obs.loc[i, f'distance nearest {geometry_type}'] = np.min(distances)

return gdf_obs[[f'nearest {geometry_type}', f'distance nearest {geometry_type}']]

def get_nearest_line(self, gdf=None,
xcol_obs='x', ycol_obs='y',
multiple_lines='error'):
"""get nearest line for each point in the obs collection. Function
calls the nearest_polygon function.
Parameters
----------
gdf : GeoDataFrame
dataframe with line features
xcol_obs : str, optional
x column in self._obj used to get geometry
ycol_obs : str, optional
y column in self._obj used to get geometry
multiple_lines : str, optional
keyword on how to deal with multiple lines being nearest.
Options are:
'error' -> raise a ValueError
'keep_all' -> return the indices of multiple lines as a
string seperated by a comma
'keep_first' -> return the index of the first line
Returns
-------
pandas.DataFrame
with columns 'nearest polygon' and 'distance nearest polygon'
"""
return self._get_nearest_geometry(gdf=gdf,
xcol_obs=xcol_obs,
ycol_obs=ycol_obs,
multiple_geometries=multiple_lines,
geometry_type='line')

return gdf_obs[['nearest polygon', 'distance nearest polygon']]
def get_nearest_polygon(self, gdf=None,
xcol_obs='x', ycol_obs='y',
multiple_polygons='error'):
"""get nearest polygon for each point in the obs collection. Function
also works for lines instead of polygons
Parameters
----------
gdf : GeoDataFrame
dataframe with polygon features
xcol_obs : str, optional
x column in self._obj used to get geometry
ycol_obs : str, optional
y column in self._obj used to get geometry
multiple_polygons : str, optional
keyword on how to deal with multiple polygons being nearest.
Options are:
'error' -> raise a ValueError
'keep_all' -> return the indices of multiple polygons as a
string seperated by a comma
'keep_first' -> return the index of the first polygon
Returns
-------
pandas.DataFrame
with columns 'nearest polygon' and 'distance nearest polygon'
"""
return self._get_nearest_geometry(gdf=gdf,
xcol_obs=xcol_obs,
ycol_obs=ycol_obs,
multiple_geometries=multiple_polygons,
geometry_type='polygon')

def get_distance_to_point(self, point, xcol='x', ycol='y'):
"""get distance of every observation to a point.
Expand Down
Loading

0 comments on commit 448b867

Please sign in to comment.