Skip to content
Snippets Groups Projects
Commit 1a1c63d9 authored by Raphael Sturgis's avatar Raphael Sturgis
Browse files

generating linearly interpolated links

parent 6bcb6ef5
Branches
No related tags found
2 merge requests!22Resolve "Add link interpolation",!13Draft: Develop
This commit is part of merge request !22. Comments created here will be created in the context of that merge request.
...@@ -9,7 +9,7 @@ from scipy.interpolate import interp1d ...@@ -9,7 +9,7 @@ from scipy.interpolate import interp1d
from skais.utils.geography import great_circle, position_from_distance, get_coord from skais.utils.geography import great_circle, position_from_distance, get_coord
from skais.ais.ais_points import AISPoints from skais.ais.ais_points import AISPoints
from skais.utils.geometry import bresenham from skais.utils.geometry import bresenham, dist_on_grid
@jit(nopython=True) @jit(nopython=True)
...@@ -153,7 +153,7 @@ def generate_points_with_features(data, positions, features_vectors, bounds, nod ...@@ -153,7 +153,7 @@ def generate_points_with_features(data, positions, features_vectors, bounds, nod
@jit(nopython=True) @jit(nopython=True)
def generate_links(data, positions, height, width, lower_lat, upper_lat, lower_lon, upper_lon, values): def generate_links(data, positions, height, width, lower_lat, upper_lat, lower_lon, upper_lon, values, interpolate=None):
lon, lat = positions[0, 0], positions[0, 1] lon, lat = positions[0, 0], positions[0, 1]
current_value = values[0] current_value = values[0]
...@@ -164,6 +164,16 @@ def generate_links(data, positions, height, width, lower_lat, upper_lat, lower_l ...@@ -164,6 +164,16 @@ def generate_links(data, positions, height, width, lower_lat, upper_lat, lower_l
x_nxt, y_nxt = get_coord(latitude, longitude, height, width, lower_lat, upper_lat, lower_lon, x_nxt, y_nxt = get_coord(latitude, longitude, height, width, lower_lat, upper_lat, lower_lon,
upper_lon) upper_lon)
lon, lat = longitude, latitude lon, lat = longitude, latitude
if interpolate is not None and nxt_value != current_value:
dist = dist_on_grid(x_prv, y_prev, x_nxt, y_nxt)
for x, y in bresenham(x_prv, y_prev, x_nxt, y_nxt):
dist_prev = dist_on_grid(x_prv, y_prev, x, y)
dist_next = dist_on_grid(x, y, x_nxt, y_nxt)
pixel_color = current_value * (1-dist_prev/dist) + nxt_value * (1-dist_next/dist)
for i in range(len(pixel_color)):
data[x, y, i] = pixel_color[i]
else:
for x, y in bresenham(x_prv, y_prev, x_nxt, y_nxt): for x, y in bresenham(x_prv, y_prev, x_nxt, y_nxt):
for i in range(len(current_value)): for i in range(len(current_value)):
data[x, y, i] = current_value[i] data[x, y, i] = current_value[i]
...@@ -349,7 +359,7 @@ class AISTrajectory(AISPoints): ...@@ -349,7 +359,7 @@ class AISTrajectory(AISPoints):
return result return result
def generate_array_from_positions(self, height=256, width=256, link=True, bounding_box='fit', ref_index=-1, def generate_array_from_positions(self, height=256, width=256, link=True, bounding_box='fit', ref_index=-1,
features=None, node_size=0): features=None, node_size=0, interpolation=None):
positions = self.df[['longitude', 'latitude']].to_numpy() positions = self.df[['longitude', 'latitude']].to_numpy()
...@@ -377,7 +387,7 @@ class AISTrajectory(AISPoints): ...@@ -377,7 +387,7 @@ class AISTrajectory(AISPoints):
bounds = np.array(bounds) bounds = np.array(bounds)
else: else:
nb_channels = 1 nb_channels = 1
data = np.zeros((height, width, nb_channels), dtype=np.float) data = np.zeros((height, width, nb_channels), dtype=float)
if features_vectors is None: if features_vectors is None:
...@@ -392,5 +402,5 @@ class AISTrajectory(AISPoints): ...@@ -392,5 +402,5 @@ class AISTrajectory(AISPoints):
lower_lat, upper_lat, lower_lon, upper_lon, ) lower_lat, upper_lat, lower_lon, upper_lon, )
if link: if link:
generate_links(data, positions, height, width, lower_lat, upper_lat, lower_lon, upper_lon, generate_links(data, positions, height, width, lower_lat, upper_lat, lower_lon, upper_lon,
generate_values(features_vectors, bounds)) generate_values(features_vectors, bounds), interpolation)
return data return data
...@@ -653,7 +653,6 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase): ...@@ -653,7 +653,6 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase):
np.testing.assert_array_equal(result, expected) np.testing.assert_array_equal(result, expected)
def test_generate_array_interpolate_links(self): def test_generate_array_interpolate_links(self):
trajectory = AISTrajectory( trajectory = AISTrajectory(
pd.DataFrame( pd.DataFrame(
...@@ -667,15 +666,17 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase): ...@@ -667,15 +666,17 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase):
) )
result = trajectory.generate_array_from_positions(height=9, width=18, link=True, bounding_box='fit', result = trajectory.generate_array_from_positions(height=9, width=18, link=True, bounding_box='fit',
features={"sog": (0, 80)}, node_size=0).reshape((9, 18), interpolation='linear') features={"sog": (0, 80)}, node_size=0,
interpolation='linear').reshape((9, 18))
print(result)
expected = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5], expected = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.46875],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.4375],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.40625],
[0, 0, 0, 0, 0, 0, 0, 0.25, 0.25, 0.25, 0, 0, 0, 0, 0, 0, 0, 0.5], [0, 0, 0, 0, 0, 0, 0, 0.125, 0.125, 0.13526987, 0, 0, 0, 0, 0, 0, 0, 0.375],
[0, 0, 0, 0, 0, 0.25, 0.25, 0, 0, 0, 0.25, 0.25, 0, 0, 0, 0, 0, 0.5], [0, 0, 0, 0, 0, 0.125, 0.125, 0, 0, 0, 0.15330406, 0.16458619, 0, 0, 0, 0, 0, 0.34375],
[0, 0, 0, 0.25, 0.25, 0, 0, 0, 0, 0, 0, 0, 0.25, 0.25, 0, 0, 0, 0.5], [0, 0, 0, 0.125, 0.125, 0, 0, 0, 0, 0, 0, 0, 0.18154526, 0.19313327, 0, 0, 0, 0.3125],
[0, 0.25, 0.25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.25, 0.25, 0, 0.5], [0, 0.125, 0.125, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.20959047, 0.22158235, 0, 0.28125],
[0.25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.25, 0.5]]) / 2 [0.125, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.23609719, 0.25]])
np.testing.assert_array_almost_equal(result, expected) np.testing.assert_array_almost_equal(result, expected)
import numpy as np
from numba import jit from numba import jit
@jit(nopython=True)
def dist_on_grid(x1, y1, x2, y2):
dx = int(x2 - x1)
dy = int(y2 - y1)
return np.sqrt(dx ** 2 + dy ** 2)
@jit(nopython=True) @jit(nopython=True)
def bresenham(x1, y1, x2, y2): def bresenham(x1, y1, x2, y2):
...@@ -12,7 +19,6 @@ def bresenham(x1, y1, x2, y2): ...@@ -12,7 +19,6 @@ def bresenham(x1, y1, x2, y2):
if dy < 0: if dy < 0:
sy = -1 sy = -1
pixels = [] pixels = []
if abs(dx) > abs(dy): # slope < 1 if abs(dx) > abs(dy): # slope < 1
if x1 > x2: if x1 > x2:
...@@ -59,4 +65,3 @@ def bresenham(x1, y1, x2, y2): ...@@ -59,4 +65,3 @@ def bresenham(x1, y1, x2, y2):
pixels.append((x, y)) pixels.append((x, y))
return pixels return pixels
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment