def _get_and_add_element_to_roi( elinfo, slide_annotations, ROI, roiinfo, roi_polygon, GT_code, use_shapely=True, verbose=True, monitorPrefix=""): """Get element coords and mask and add to ROI (Internal).""" try: coords, element_mask = _get_element_mask( elinfo=elinfo, slide_annotations=slide_annotations) ADD_TO_ROI = True # ignore if outside ROI (precise) if use_shapely: el_polygon = Polygon(coords) if el_polygon.distance(roi_polygon) > 2: if verbose: print("%s: OUSIDE ROI." % monitorPrefix) ADD_TO_ROI = False # Add element to ROI mask if ADD_TO_ROI: ROI = _add_element_to_roi( elinfo=elinfo, ROI=ROI, GT_code=GT_code, element_mask=element_mask, roiinfo=roiinfo) except Exception as e: if verbose: print("%s: ERROR! (see below)" % monitorPrefix) print(e) return ROI
def pmv_model(df, i, comfortzone): """ This method determines discomfort severity using the Fanger PMV-based comfort zones. In this method, the discomfort severity is calculated by finding the difference between the SET value of the point in question with that of closest point on the comfort zone boundary. Parameters ---------- df : dataframe Input dataframe of the processed input data in the "input_data_pythonCode.csv" file. i : integer Time step, [hr] comfortzone : list of tuples Output of "latest_comfortzone" method. Returns ------- discomfort : float Discomfort severity. """ poly = Polygon(comfortzone) # boundary of the polygon = LinearRing pol_ext = LinearRing(list(poly.exterior.coords)) point_t = df['OpTem'][i] point_rh = df['rh'][i] point = Point(point_t, point_rh) # Need to check whether point is inside the poly or on the boundaries of the poly if (poly.contains(point) == True) or (pol_ext.contains(point) == True): position = 'inside' # The distance will always be 0.0 discomfort = poly.distance(point) # 0.0 else: position = 'outside' SET_point = df['SET'][i] d = pol_ext.project(point) p = pol_ext.interpolate(d) closest_point_coords = list(p.coords)[0] SET_closest_point = set_tmp(list(p.coords)[0][0], list(p.coords)[0][0], df['v'][i], list(p.coords)[0][1], df['met'][i], df['clo'][i], wme=0, body_surface_area=BODY_SURFACE_AREA, patm=101325, units='SI') discomfort = SET_point - SET_closest_point return discomfort
def parcour(lista): polys = [] test = [] for pol in lista: l = [] dpol = Polygon(concavehull.extract_points(pol.geometry())) for pol1 in lista: dpol1 = Polygon(concavehull.extract_points(pol1.geometry())) dist = dpol.distance(dpol1) * 100 l.append([pol, pol1, dist]) test.append(l) return test
def distance_from_polys(p, rgb): point = Point(p[0], p[1]) out = rgb for poly in polygons: polygon = Polygon(poly) distance = polygon.distance(point) if distance < spread_distance: new_val = (0, int(100 * (1 - distance / spread_distance)), int(255 * (1 - distance / spread_distance))) if sum(new_val) > sum(out): out = new_val return (out)
def isInsideOrFrontier(p, Q, eps=1.0e-6): """ Paramètres: ========== - p = (x,y) = np.ndarray((1,2), float) un point en 2d - Q est un polygone = np.ndarray((n,2), float) supposé fermé i.e. le premier point == le dernier point, - eps : precision Retourne : ======== - vrai si p est à l'intérieur du polygone Q ou sur la frontiere (bande de largeur eps) et - faux s'il est à l'extérieur. """ P = Polygon(Q) p = Point(p) #debug(p=Point(p), lr=lr) # debug(distance=P.distance(p) - p.distance(P)) return P.distance(Point(p)) < eps
def operation_example(): patch = Point(0.0, 0.0).buffer(10.0) print('patch = {}.'.format(patch)) print('patch.area = {}.'.format(patch.area)) line = LineString([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)]) dilated = line.buffer(0.5) eroded = dilated.buffer(-0.3) ring = LinearRing([(0, 0), (1, 1), (1, 0)]) point = Point(0.5, 0.5) polygon = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) print('polygon.contains(point) = {}.'.format(polygon.contains(point))) #box = shapely.geometry.box(minx, miny, maxx, maxy, ccw=True) pa = asPoint(np.array([0.0, 0.0])) la = asLineString(np.array([[1.0, 2.0], [3.0, 4.0]])) ma = asMultiPoint(np.array([[1.1, 2.2], [3.3, 4.4], [5.5, 6.6]])) data = {'type': 'Point', 'coordinates': (0.0, 0.0)} geom = shapely.geometry.shape(data) #geom = shapely.geometry.asShape(data) #-------------------- #geom = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) geom = LineString([(0, 0), (10, 0), (10, 5), (20, 5)]) print('polygon.has_z = {}.'.format(geom.has_z)) if isinstance(geom, LinearRing): print('polygon.is_ccw = {}.'.format(geom.is_ccw)) # LinearRing only. print('polygon.is_empty = {}.'.format(geom.is_empty)) print('polygon.is_ring = {}.'.format(geom.is_ring)) print('polygon.is_simple = {}.'.format(geom.is_simple)) print('polygon.is_valid = {}.'.format(geom.is_valid)) print('polygon.coords = {}.'.format(geom.coords)) print('polygon.area = {}.'.format(geom.area)) print('polygon.bounds = {}.'.format(geom.bounds)) print('polygon.length = {}.'.format(geom.length)) print('polygon.minimum_clearance = {}.'.format(geom.minimum_clearance)) print('polygon.geom_type = {}.'.format(geom.geom_type)) print('polygon.boundary = {}.'.format(geom.boundary)) print('polygon.centroid = {}.'.format(geom.centroid)) print('polygon.convex_hull.exterior.coords.xy = {}.'.format(list((x, y) for x, y in zip(*geom.convex_hull.exterior.coords.xy)))) print('polygon.envelope = {}.'.format(geom.envelope)) print('polygon.minimum_rotated_rectangle = {}.'.format(geom.minimum_rotated_rectangle)) #polygon.parallel_offset(distance, side, resolution=16, join_style=1, mitre_limit=5.0) #polygon.simplify(tolerance, preserve_topology=True) #-------------------- poly1 = Polygon([(3, 3), (5, 3), (5, 5), (3, 5)]) poly2 = Polygon([(1, 1), (4, 1), (4, 3.5), (1, 3.5)]) print('poly1.intersection(poly2) = {}.'.format(poly1.intersection(poly2))) print('poly1.difference(poly2) = {}.'.format(poly1.difference(poly2))) print('poly1.symmetric_difference(poly2) = {}.'.format(poly1.symmetric_difference(poly2))) print('poly1.union(poly2) = {}.'.format(poly1.union(poly2))) print('poly1.equals(poly2) = {}.'.format(poly1.equals(poly2))) print('poly1.almost_equals(poly2, decimal=6) = {}.'.format(poly1.almost_equals(poly2, decimal=6))) print('poly1.contains(poly2) = {}.'.format(poly1.contains(poly2))) print('poly1.covers(poly2) = {}.'.format(poly1.covers(poly2))) print('poly1.crosses(poly2) = {}.'.format(poly1.crosses(poly2))) print('poly1.disjoint(poly2) = {}.'.format(poly1.disjoint(poly2))) print('poly1.intersects(poly2) = {}.'.format(poly1.intersects(poly2))) print('poly1.overlaps(poly2) = {}.'.format(poly1.overlaps(poly2))) print('poly1.touches(poly2) = {}.'.format(poly1.touches(poly2))) print('poly1.within(poly2) = {}.'.format(poly1.within(poly2))) #-------------------- poly1 = Polygon([(40, 50), (60, 50), (60, 90), (40, 90)]) poly2 = Polygon([(10, 100), (100, 100), (100, 150), (10, 150)]) print('The distance between two convex polygons = {}.'.format(poly1.distance(poly2))) print('The Hausdorff distance between two convex polygons = {}.'.format(poly1.hausdorff_distance(poly2))) print('The representative point of a polygon = {}.'.format(poly1.representative_point())) #-------------------- lines = [ ((0, 0), (1, 1)), ((0, 0), (0, 1)), ((0, 1), (1, 1)), ((1, 1), (1, 0)), ((1, 0), (0, 0)), ((1, 1), (2, 0)), ((2, 0), (1, 0)), ((5, 5), (6, 6)), ((1, 1), (100, 100)), ] polys = shapely.ops.polygonize(lines) result, dangles, cuts, invalids = shapely.ops.polygonize_full(lines) merged_line = shapely.ops.linemerge(lines) # Efficient unions. polygons = [Point(i, 0).buffer(0.7) for i in range(5)] shapely.ops.unary_union(polygons) shapely.ops.cascaded_union(polygons) # Delaunay triangulation. points = MultiPoint([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)]) triangles = shapely.ops.triangulate(points) # Voronoi diagram. #points = MultiPoint([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)]) #regions = shapely.ops.voronoi_diagram(points) # Nearest points. triangle = Polygon([(0, 0), (1, 0), (0.5, 1), (0, 0)]) square = Polygon([(0, 2), (1, 2), (1, 3), (0, 3), (0, 2)]) nearest_points = shapely.ops.nearest_points(triangle, square) square = Polygon([(1,1), (2, 1), (2, 2), (1, 2), (1, 1)]) line = LineString([(0,0), (0.8, 0.8), (1.8, 0.95), (2.6, 0.5)]) result = shapely.ops.snap(line, square, 0.5) g1 = LineString([(0, 0), (10, 0), (10, 5), (20, 5)]) g2 = LineString([(5, 0), (30, 0), (30, 5), (0, 5)]) forward, backward = shapely.ops.shared_paths(g1, g2) pt = Point((1, 1)) line = LineString([(0, 0), (2, 2)]) result = shapely.ops.split(line, pt) ls = LineString((i, 0) for i in range(6)) shapely.ops.substring(ls, start_dist=1, end_dist=3) shapely.ops.substring(ls, start_dist=3, end_dist=1) shapely.ops.substring(ls, start_dist=1, end_dist=-3) shapely.ops.substring(ls, start_dist=0.2, end_dist=-0.6, normalized=True)
def dist_poly_to_curve(self, curve, poly_road): closest_pt_road, dist = self.closest_pts_to_curve(curve, poly_road) qry_point = Point(closest_pt_road) qry_poly = Polygon(poly_road) return qry_poly.distance(qry_point)