<HTML><BODY>Dear Python users,<br><br>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<br>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 <a href="http://stackoverflow.com/questions/16377826/distance-for-each-intersected-points-of-a-line-in-increased-order-in-2d-coordina" data-mce-href="http://stackoverflow.com/questions/16377826/distance-for-each-intersected-points-of-a-line-in-increased-order-in-2d-coordina">http://stackoverflow.com/questions/16377826/distance-for-each-intersected-points-of-a-line-in-increased-order-in-2d-coordina</a>)<br><br><p>import numpy as np<br> import scipy as sp<br><br> def distance_of_crossed_line(x0, x1, y0, y1):<br>       # slope<br>       m = (y1 - y0) / (x1 - x0)<br>        # Boundary of the selected points<br>        x_ceil = np.ceil(min(x0, x1))<br>        x_floor = np.floor(max(x0, x1))<br>        y_ceil = np.ceil(min(y0, y1))<br>        y_floor = np.floor(max(y0, y1))</p><p>       # calculate all intersected x coordinate<br>        x = np.arange(x_ceil, x_floor + 1)<br>        y = m * (x - x0) + y0<br>        ax = zip(x, y)<br>        # calculate all intersected y coordinate<br>        y = np.arange(y_ceil, y_floor + 1)<br>        x = (y - y0) / m + x0<br>        ax.extend(zip(x, y))<br>        ax.append((x0, y0))<br>        ax.append((x1, y1))<br>        ax.sort()<br> <br>        # Transpose<br>        ax = np.array(ax).T<br>        # Calculate difference of intersections in X<br>        dist_x = np.diff(ax[0])<br>        # Calculate difference of intersections in Y<br>        dist_y = np.diff(ax[1])</p><p>       return np.sqrt(dist_x**2 + dist_y**2)<br> <br> # PLEASE, note that line points are different from 2D array axis. they should be matched with each other. <br># 2D array.<br> 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]]<br> <br> # Two sample points as a line<br> x = np.array([ -80, -40 ])<br> y = np.array([ 60, 55 ])<br> <br> # The problem: <br> F = intersected_line_length * array_grid_values_where_line_crossed_area</p><p>* It is not necessary for me to overlay lines onto grid cells properly, JUST, I need to calculate numerically accurate F function</p>Thanks for the answer and guidance in advance,<br><br>-- <br>Bakhtiyor Zokhidov</BODY></HTML>