Matching line points with grid coordinates numerically

Dear Python users,

I am having difficulty with numerically scaling to match line coordinates vs grid cell size coordinates. I want to calculate the following function: F = distance_of_crossed_line x intersected_cell_value
The problem here is that when I calculate crossed_line_length in line coordinates that will unmatch vs grid coordinate, which is also another x,y with step e.g., dx=dy=2.5 each grid size. I want to do numerical calculation , say, F(distance, intersected_grid_value) function where, intersected_grid_value - values in intersected grid, distance - intersected_line_length (given below or can be found

import numpy as np
import scipy as sp

def distance_of_crossed_line(x0, x1, y0, y1):
# slope
m = (y1 - y0) / (x1 - x0)
# Boundary of the selected points
x_ceil = np.ceil(min(x0, x1))
x_floor = np.floor(max(x0, x1))
y_ceil = np.ceil(min(y0, y1))
y_floor = np.floor(max(y0, y1))

calculate all intersected x coordinate

    x = np.arange(x_ceil, x_floor + 1)
    y = m * (x - x0) + y0
    ax = zip(x, y)
    # calculate all intersected y coordinate
    y = np.arange(y_ceil, y_floor + 1)
    x = (y - y0) / m + x0
    ax.extend(zip(x, y))
    ax.append((x0, y0))
    ax.append((x1, y1))

    # Transpose
    ax = np.array(ax).T
    # Calculate difference of intersections in X
    dist_x = np.diff(ax[0])
    # Calculate difference of intersections in Y
    dist_y = np.diff(ax[1])

return np.sqrt(dist_x2 + dist_y2)

PLEASE, note that line points are different from 2D array axis. they should be matched with each other.

2D array.

d_array = np.array[[4.5, 4.5, 4.5, 3.4, 2.5],[ 3.9, 4.5, 5.2, 4.5, 3.4],[3.9, 3.9, 2.5, 2.2, 1.9]]

Two sample points as a line

x = np.array([ -80, -40 ])
y = np.array([ 60, 55 ])

The problem:

F = intersected_line_length * array_grid_values_where_line_crossed_area

  • It is not necessary for me to overlay lines onto grid cells properly, JUST, I need to calculate numerically accurate F function

Thanks for the answer and guidance in advance,


Bakhtiyor Zokhidov