Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PKG-INFO
SOURCES.txt
dependency_links.txt
requires.txt
top_level.txt
.coverage
64 changes: 37 additions & 27 deletions ridge_map/ridge_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import matplotlib.pyplot as plt
import numpy as np
from skimage.filters import rank
from skimage.morphology import square
from skimage.morphology import footprint_rectangle
from skimage.util import img_as_ubyte
from scipy.ndimage import rotate

Expand Down Expand Up @@ -91,14 +91,15 @@ def longs(self):
"""Bottom and top longitude of bounding box."""
return (self.bbox[0], self.bbox[2])

# pylint: disable=too-many-arguments,too-many-positional-arguments
def get_elevation_data(
self,
num_lines=80,
elevation_pts=300,
viewpoint_angle=0,
self,
num_lines=80,
elevation_pts=300,
viewpoint_angle=0,
crop=False,
interpolation=0,
lock_resolution=False
interpolation=0,
lock_resolution=False,
):
"""Fetch elevation data and return a numpy array.

Expand All @@ -114,25 +115,29 @@ def get_elevation_data(
crop : bool
If the corners are cropped when rotating
interpolation : int in [0, 5]
The level of interpolation. Can smooth out sharp edges, especially
The level of interpolation. Can smooth out sharp edges, especially
when rotating. Above 1 tends to lead to an all NaN graph.
lock_resolution : bool
Locks the resolution during rotation, ensuring consistent rotation
deltas but producing potential scaling artifacts. These artifacts
Locks the resolution during rotation, ensuring consistent rotation
deltas but producing potential scaling artifacts. These artifacts
can be reduced by setting num_lines = elevation_pts.

Returns
-------
np.ndarray
"""
if (45 < (viewpoint_angle % 360) < 135 or 225 < (viewpoint_angle % 360) < 315) and not lock_resolution:
if (
45 < (viewpoint_angle % 360) < 135 or 225 < (viewpoint_angle % 360) < 315
) and not lock_resolution:
num_lines, elevation_pts = elevation_pts, num_lines

values = self._srtm_data.get_image(
(elevation_pts, num_lines), self.lats, self.longs, 5280, mode="array"
)
values = rotate(values, angle=viewpoint_angle, reshape=not crop, order=interpolation)

values = rotate(
values, angle=viewpoint_angle, reshape=not crop, order=interpolation
)

return values

def preprocess(
Expand Down Expand Up @@ -173,7 +178,10 @@ def preprocess(
values = (values - np.min(values)) / (np.max(values) - np.min(values))

is_water = values < np.percentile(values, water_ntile)
is_lake = rank.gradient(img_as_ubyte(values), square(3)) < lake_flatness
is_lake = (
rank.gradient(img_as_ubyte(values), footprint_rectangle((3, 3)))
< lake_flatness
)

values[nan_vals] = np.nan
values[np.logical_or(is_water, is_lake)] = np.nan
Expand All @@ -193,7 +201,7 @@ def plot_annotation(
background=True,
ax=None,
):
"""Plot an annotation to an existing map
"""Plot an annotation to an existing map.

It is recommended to call this function only after calling map_plot()

Expand All @@ -217,23 +225,26 @@ def plot_annotation(
If there is a background or not
ax : matplotlib Axes
You can pass your own axes, but probably best not to

Returns
-------
matplotlib.Axes
"""
"""
if ax is None and self.ax is None:
raise ValueError("No axes found: Either plot_map() beforehand or pass an matplotlib.Axes value through")
elif ax is None:
raise ValueError(
"No axes found: Either plot_map() beforehand or pass an matplotlib.Axes value "
"to the function."
)
if ax is None:
ax = self.ax

highest_zorder = max(text.zorder for text in ax.texts) if ax.texts else 1

rel_coordinates = (
(coordinates[0] - self.longs[0]) / (self.longs[1] - self.longs[0]),
(coordinates[1] - self.lats[0]) / (self.lats[1] - self.lats[0]),
)

annotation_color = "black"
if color:
annotation_color = color
Expand Down Expand Up @@ -265,10 +276,10 @@ def plot_annotation(
ms=annotation_size,
zorder=highest_zorder,
)

self.ax = ax
return ax

# pylint: disable=too-many-arguments,too-many-locals
def plot_map(
self,
Expand Down Expand Up @@ -326,7 +337,6 @@ def plot_map(
-------
matplotlib.Axes
"""

if kind not in {"gradient", "elevation"}:
raise TypeError("Argument `kind` must be one of 'gradient' or 'elevation'")
if values is None:
Expand Down Expand Up @@ -383,6 +393,6 @@ def plot_map(
for spine in ax.spines.values():
spine.set_visible(False)
ax.set_facecolor(background_color)

self.ax = ax
return ax