-
Notifications
You must be signed in to change notification settings - Fork 7
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
proximity tool to convert the 0's and 1's in the polygon #432
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import numpy as np | ||
import geopandas as gpd | ||
from numbers import Number | ||
from beartype import beartype | ||
from beartype.typing import Union | ||
from rasterio import profiles, transform | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. transform is unused import |
||
from eis_toolkit.vector_processing.distance_computation import distance_computation | ||
|
||
@beartype | ||
def calculate_proximity(geodataframe: gpd.GeoDataFrame, raster_profile: Union[profiles.Profile, dict], maximum_distance: Number) -> np.ndarray: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line, as some other lines, exceeds the allowed limit. If you use pre-commit locally, it will fix/point out these kind of cosmetic issues. Also, perhaps it is better if the tool is named similarly as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had an issue with the pre-commit hooke However, is now resolved. I ran the precommit hooke and cleared the cosmetic problems |
||
|
||
""" Interpolates the distance values calculated by the distance_computation function between 0 and 1. | ||
1 denots the value inside the polygon and 0 at the maximum distance. | ||
If maximum_distance value is not provided, the program sets this value to the maximum value | ||
in the provided distance matrix. | ||
Uses linear interpolation to calculate the distance from the polygon. | ||
Comment on lines
+12
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The format of the docstring should follow that of other tools. I believe the line "If maximum_distance value is not provided, the program sets this value to the maximum value " is also outdated as |
||
|
||
Args: | ||
geodataframe: The GeoDataFrame with geometries to determine distance to. | ||
raster_profile: The raster profile of the raster in which the distances | ||
to the nearest geometry are determined. | ||
max_distance: The maximum distance in the output array. | ||
|
||
Returns: | ||
A 2D numpy array with the the distance values inverted. | ||
""" | ||
|
||
out_matrix = distance_computation(geodataframe, raster_profile, maximum_distance) | ||
|
||
minimum = np.min(out_matrix) | ||
difference = maximum_distance - minimum | ||
out_matrix = maximum_distance - out_matrix | ||
out_matrix = out_matrix/difference | ||
Comment on lines
+30
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe you could utilize There's also the point that if we extend the scaling options in the future, it is good to use our own There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
return out_matrix | ||
|
||
@beartype | ||
def calculate_logarithmic_proximity(geodataframe: gpd.GeoDataFrame, raster_profile: Union[profiles.Profile, dict], maximum_distance: Number) -> np.ndarray: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better if the choice of scaling should be parameterized, not formulated as another public function. So, let's expose only one |
||
|
||
""" Logarithmically interpolates the distance values calculated by the distance_computation function between 0 and 1. | ||
1 denots the value inside the polygon and 0 at the maximum distance. | ||
If maximum_distance value is not provided, the program sets this value to the maximum value | ||
in the provided distance matrix. | ||
Uses linear interpolation to calculate the distance from the polygon. | ||
|
||
Args: | ||
geodataframe: The GeoDataFrame with geometries to determine distance to. | ||
raster_profile: The raster profile of the raster in which the distances | ||
to the nearest geometry are determined. | ||
max_distance: The maximum distance in the output array. | ||
|
||
Returns: | ||
A 2D numpy array with the the distance values inverted. | ||
""" | ||
|
||
distance_array = distance_computation(geodataframe, raster_profile,maximum_distance) | ||
|
||
modified_distance_array = np.where(distance_array==0.0,np.nan,distance_array) | ||
out_matrix = np.log(modified_distance_array) | ||
|
||
log_maximum = np.log(maximum_distance) | ||
minimum = np.min(distance_array) | ||
if(minimum != 0): | ||
log_minimum = np.log(minimum) | ||
else : | ||
log_minimum = minimum | ||
difference = log_maximum - log_minimum | ||
out_matrix = log_maximum - out_matrix | ||
out_matrix = out_matrix/difference | ||
|
||
out_matrix = np.nan_to_num(out_matrix,nan=log_maximum) | ||
Comment on lines
+58
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check also if the |
||
|
||
return out_matrix |
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rename this file to
proximity_computation.py
(and related files accordingly) to follow the naming schema of distance computation.