def _compute_distance(self, row): pt0 = row['prev_pt'] pt1 = row['geometry'] if type(pt0) != Point: return 0.0 if pt0 == pt1: return 0.0 if self.is_latlon(): dist_meters = measure_distance_spherical(pt0, pt1) else: # The following distance will be in CRS units that might not be meters! dist_meters = measure_distance_euclidean(pt0, pt1) return dist_meters
def get_closest_centroid(self, pt, max_dist=100000000): (i, j) = self.get_grid_position(pt) shortest_dist = self.cell_size * 100 nearest_centroid = None for k in range(max(i - 1, 0), min(i + 2, self.n_cols)): for m in range(max(j - 1, 0), min(j + 2, self.n_rows)): if not self.cells[k][m]: # no centroid in this cell yet continue dist = measure_distance_euclidean(pt, self.cells[k][m].centroid) if dist <= max_dist and dist < shortest_dist: nearest_centroid = (k, m) shortest_dist = dist return nearest_centroid
def _compute_speed(self, row): pt0 = row['prev_pt'] pt1 = row['geometry'] if type(pt0) != Point: return 0.0 if type(pt1) != Point: raise ValueError('Invalid trajectory! Got {} instead of point!'.format(pt1)) if pt0 == pt1: return 0.0 if self.is_latlon(): dist_meters = measure_distance_spherical(pt0, pt1) else: # The following distance will be in CRS units that might not be meters! dist_meters = measure_distance_euclidean(pt0, pt1) return dist_meters / row['delta_t'].total_seconds()
def test_euclidean_distance(self): assert (measure_distance_euclidean(Point(0, 0), Point(0, 1))) == 1
def test_measure_distance_euclidean_throws_type_error(self): with pytest.raises(TypeError): measure_distance_euclidean((0, 0), (0, 1))