def extend_boundary(self, r): """ Extends a 2d contour out from points in self.xycoords by a distance <r> (radius) in all directions. """ s = "::: extending boundary by %i meters :::" % r print_text(s, self.color) xycoords = self.xycoords polygons = [] for i, v in enumerate(xycoords): polygons.append(shapelyPoint(v[0], v[1]).buffer(r)) # union of our original polygon and convex hull p1 = cascaded_union(polygons) p2 = Polygon(zip(xycoords[:, 0], xycoords[:, 1])) p3 = cascaded_union([p1, p2]) xycoords_buf = array(zip(p3.exterior.xy[:][0], p3.exterior.xy[:][1])) self.plot_coords["xycoords_buf"] = xycoords_buf self.xycoords = xycoords_buf s = "::: extended contour created of length %i :::" % len( self.xycoords) print_text(s, self.color)
def __create_grid(self): """ Create uniform grid over region. """ polygon_coords = self.__resample_polygon() # define polygon from polygon coords polygon = Polygon(polygon_coords) # get bounding box coordinates bounding_box = polygon.bounds min_lon = bounding_box[0] max_lon = bounding_box[2] min_lat = bounding_box[1] max_lat = bounding_box[3] # compute number of nodes along lat and lon n_lat = int(round((max_lat - min_lat) / self.grid_spacing)) + 1 n_lon = int(round((max_lon - min_lon) / self.grid_spacing)) + 1 # create grid of points inside polygon grid = [] for i in range(n_lat): for j in range(n_lon): lat = min_lat + i * self.grid_spacing lon = min_lon + j * self.grid_spacing p = shapelyPoint(lon,lat) if polygon.contains(p) or polygon.touches(p): grid.append((lon,lat)) return grid
def extend_edge(self, r): """ Extends a 2d contour out from points labeled in self.edge by a distance <r> (radius) in all directions. NOTE: this only works for greenland. """ s = "::: extending edge by %i meters :::" % r print_text(s, self.color) xycoords = self.xycoords edge = self.edge polygons = [] for i, v in enumerate(xycoords): if edge[i]: polygons.append(shapelyPoint(v[0], v[1]).buffer(r)) # union of our original polygon and convex hull p1 = cascaded_union(polygons) p2 = Polygon(zip(xycoords[:, 0], xycoords[:, 1])) p3 = cascaded_union([p1, p2]) xycoords_buf = array(zip(p3.exterior.xy[:][0], p3.exterior.xy[:][1])) self.plot_coords["xycoords_buf"] = xycoords_buf self.xycoords = xycoords_buf
def in_fence(geo, latitude, longitude): dataset = Point.objects.filter(fence_id__id=geo.id).order_by('order') lats_longs_vect = dataset.values_list('x', 'y') # print(lats_longs_vect) polygon = Polygon(lats_longs_vect) point = shapelyPoint(latitude, longitude) _in = polygon.contains(point) return _in
def contains_point(self, point): if self.geometry.is_empty: return False sh_point = shapelyPoint(get_coords(point)) return ( point_in_rectangle(point, self.geometry.bounds) and self.geometry.intersects(sh_point) )
def extend_edge(self, r): """ Extends a 2d contour out from points labeled in self.edge by a distance <r> (radius) in all directions. """ xycoords = self.longest_cont polygons = [] for i, v in enumerate(xycoords): polygons.append(shapelyPoint(v[0], v[1]).buffer(r)) # union of our original polygon and convex hull p1 = cascaded_union(polygons) p3 = cascaded_union(p1) xycoords_buf = array(zip(p3.exterior.xy[:][0], p3.exterior.xy[:][1])) self.longest_cont = xycoords_buf
def get_approx_tumor_mask(polygons, thumbnail_nrow, thumbnail_ncol, patch_size=256): """Indicate whether a particular tile overlaps with tumor annotation. The method has an approx in its name, as the entire tile might not be coming from a tumor annotated region as parts of it might actually be normal. We just report here if the patch overlaps with a tumor annotation. It might have an overlap with a normal region as well, but that is filtered in a later method so we don't worry about it here. Parameters ---------- polygons: dict dict with keys ['normal', 'tumor'] as obtained from `get_annotation_polygons` thumbnail_nrow: int Number of rows in the thumbnail image thumbnail_ncol: int Number of columns in the thumbnail Returns ------- mask: array tumor mask """ scaled_tumor_polygons = [] for tpol in polygons["tumor"]: scaled = translate_and_scale_polygon(tpol, 0, 0, 1 / 256) scaled_tumor_polygons.append(scaled) polymasked = poly2mask(scaled_tumor_polygons, (thumbnail_nrow, thumbnail_ncol)) # Is any of the masked out points inside a normal annotated region? poly_x, poly_y = np.where(polymasked > 0) set_to_zero = [] for px, py in zip(poly_x, poly_y): point = shapelyPoint(px, py) for npol in polygons["normal"]: scaled = translate_and_scale_polygon(npol, 0, 0, 1 / 256) pol = shapelyPolygon(scaled.get_xy()) if pol.contains(point): set_to_zero.append((px, py)) if len(set_to_zero): set_to_zero = np.array(set_to_zero) polymasked[set_to_zero] = 0 return polymasked
import numpy from shapely.geometry import Point as shapelyPoint from shapely.geometry.polygon import Polygon v0 = [7.115125222, -2.5] v1 = [2, 3.5] v2 = [-2, 4] v3 = [-3, -3] v4 = [0, -10] np = numpy lats_vect = np.array([v0[0], v1[0], v2[0], v3[0], v4[0]]) lons_vect = np.array([v0[1], v1[1], v2[1], v3[1], v4[1]]) x, y = 1.123654, 1 lats_longs_vect = np.column_stack((lats_vect, lons_vect)) polygon = Polygon(lats_longs_vect) point = shapelyPoint(x, y) _in = polygon.contains(point) if _in: print('Entering the fence') else: print('leaving from fence')
def contains_point(self, point): if self.geometry.is_empty: return False pure_point = feature_to_point(featurize(point)) shPoint = shapelyPoint(pure_point) return point_in_rectangle(pure_point, self.geometry.bounds) and self.geometry.contains(shPoint)
if x['properties']['ZONECLASS'] == 'OVERLAY': continue tmp = np.array(coords) if tmp.ndim == 1: # Shape is a MultiLineString poly = shapelyPolygon(coords[0]) mls.append(poly) mlsz.append(x['properties']['ZONECLASS']) elif tmp.ndim == 2: # Shape is a LineString poly = shapelyPolygon(coords) ls.append(poly) lsz.append(x['properties']['ZONECLASS']) n = len(x_coordinates) i = 0 while i < n: pnts.append(shapelyPoint(x_coordinates[i], y_coordinates[i])) i += 1 output_file = open('output.csv', mode='w') out_write = csv.writer(output_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) c_res_1, c_res_2, c_res_3, c_res_4, c_res_5, c_res_6, c_res_7, c_res_8, c_res_9 = 0, 0, 0, 0, 0, 0, 0, 0, 0 c_ofc_1, c_ofc_2, c_ofc_3 = 0, 0, 0 c_prk_1 = 0 c_ind_1, c_ind_2 = 0, 0 c_com_1, c_com_2, c_com_3, c_com_4, c_com_5 = 0, 0, 0, 0, 0 c_nzn = 0 n = len(pnts) #something