def test_get_coord(): """Test get_coord function.""" point = Point((73, 19)) feature = Feature(geometry=point) flist = [73, 19] c1 = get_coord(point) c2 = get_coord(feature) c3 = get_coord(flist) assert c1 == [73, 19] assert c1 == c2 == c3 # Test for derived class from Feature class Vertex(Feature): """Derived from Feature""" def __init__(self, node: str, point: Point): Feature.__init__(self, geometry=point) self.nodeid = node p1 = Point((25.25458, 51.623879)) f1 = Feature(geometry=p1) v1 = Vertex("v1", point=p1) p2 = Point((25.254626, 51.624053)) f2 = Feature(geometry=p2) v2 = Vertex("v2", point=p2) df = distance(f1, f2) dv = distance(v1, v2) assert df == dv
def grid(bbox, cell_width, cell_height, units): """ This function generates the grids for the given bounding box :param bbox: bounding box coordinates :param cell_width: Cell width in specified in units :param cell_height: Cell height in specified in units :param units: Units for given sizes :return: FeatureCollection of grid boxes genarated for the giving bounding box """ results = [] west = bbox[0] south = bbox[1] east = bbox[2] north = bbox[3] start = Feature(geometry=Point((west, south))) end = Feature(geometry=Point((east, south))) x_fraction = cell_width / (distance(start, end, units)) cell_width_deg = x_fraction * (east - west) start = Feature(geometry=Point((west, south))) end = Feature(geometry=Point((west, north))) y_fraction = cell_height / (distance(start, end, units)) cell_height_deg = y_fraction * (north - south) # rows & columns bbox_width = east - west bbox_height = north - south columns = math.ceil(bbox_width / cell_width_deg) rows = math.ceil(bbox_height / cell_height_deg) # if the grid does not fill the bbox perfectly, center it. delta_x = (bbox_width - columns * cell_width_deg) / 2 delta_y = (bbox_height - rows * cell_height_deg) / 2 # iterate over columns & rows current_x = west + delta_x for column in range(0, columns): current_y = south + delta_y for row in range(0, rows): cell_poly = Feature(geometry=Polygon([[ [current_x, current_y], [current_x, current_y + cell_height_deg], [ current_x + cell_width_deg, current_y + cell_height_deg, ], [current_x + cell_width_deg, current_y], [current_x, current_y], ]])) results.append(cell_poly) current_y += cell_height_deg current_x += cell_width_deg return FeatureCollection(results)
def _callback_feature_each(pt, feature_index): nonlocal min_dist, best_feature_index distance_to_point = turf.distance(target_point, pt) # print(distance_to_point) if float(distance_to_point) < min_dist: best_feature_index = feature_index min_dist = distance_to_point # print(min_dist) return True
def _callback_feature_each(pt, feature_index): nonlocal found_dist, best_feature_index distance_to_point = turf.distance(target_point, pt) # print(distance_to_point) if (float(distance_to_point) >= min_dist) and (float(distance_to_point) < max_dist): best_feature_index = feature_index found_dist = distance_to_point # print(min_dist) return False # return False will break the loop once inside a polygon return True
def _callback_feature_each(pt, feature_index): nonlocal min_dist, best_feature_index distance_to_point = turf.distance(target_point, pt) # print(distance_to_point) if float(distance_to_point) <= cellSide: best_feature_index = feature_index min_dist = distance_to_point # print(min_dist) return False # return False will break the loop once inside a polygon return True
def dist(pt1, pt2, options): if "units" in options: return distance(pt1, pt2, options["units"]) else: return distance(pt1, pt2)