def mask_layer(drawing): result = [] if buffers is not None: mask = np.random.choice(masks, size=1, replace=False, p=probs)[0] else: mask = shapely_mask for path in drawing: shapely_path = LineString(path) try: if invert: intersection = shapely_path.difference(mask) else: intersection = shapely_path.intersection(mask) except TopologicalError: continue if type(intersection) is LineString: intersection = [intersection] # simulate multilinestring elif type(intersection) is Point: continue for thing in intersection: if type(thing) is LineString: masked_line = np.array(thing.coords) if len(masked_line) > 0: result.append(np.array(thing.coords)) return result
def _find_cells_on_vector_with_line(self, start_pt, vector): """ Given a vector in coordinates (x, y), this function returns all of the cells (in world coordinates) that lie along the vector, starting from the start_position. """ # Solve for the case when one of the vector components is zero (breaks the line distance function) if vector[0] == 0: vector[0] += 1e-10 if vector[1] == 0: vector[1] += 1e-10 end_pt = np.array([start_pt[0] + vector[0], start_pt[1] + vector[1]]) line = LineString((start_pt, end_pt)) cells_on_line = [] try: for k, segment in enumerate(line.difference(self.grid_lines)): x, y = segment.xy i, j = self._cartesian_to_mesh(x[1], y[1]) cells_on_line.append((i, j)) # plt.plot(x, y) # plt.text(np.mean(x), np.mean(y), str(k)) # plt.scatter(start_pt[0], start_pt[1], color='red') # plt.text(start_pt[0], start_pt[0], f'({start_pt[0]:.1f}, {start_pt[1]:.1f})') # plt.show() except TypeError: i, j = self._cartesian_to_mesh(end_pt[0], end_pt[1]) cells_on_line.append((i, j)) return cells_on_line
def Node_translation(i, F, list_o): """This function takes 3 inputs: - i: the node to move - F: the force that moves the node - list_o: the obstacle list It returns a new node that is the result of the input node translation""" temp = i.translation(F) dist_init = i.coord.distance(temp.coord) g = F.x * 1000 h = F.y * 1000 F_temp = Vector(g, h) projection = i.translation(F_temp) line = LineString([(i.coord.x, i.coord.y), (projection.coord.x, projection.coord.y)]) dist_min = None closest_obs = None for elt in list_o: difference = line.difference(elt) if difference.geom_type == 'MultiLineString': # In this case we meet an obstacle on our way dist = list(difference.geoms)[0].length if dist_min is None or dist_min > dist: dist_min = dist closest_obs = elt if dist_min != None and dist_min < dist_init: # If dist_min is different from None, that means we meet and osbtacle and we need to reduce the translation. #print("CHANGEMENT CAR distance initale {} > dist_min {}".format(dist_init, dist_min)) ratio = (dist_min * 0.9) / dist_init F.x = F.x * ratio F.y = F.y * ratio temp = i.translation(F) #We must also verify that the node doesn't move out of our field of interest. inField = True x = temp.coord.x y = temp.coord.y if temp.coord.x < (-xfield) / 2: x = (-xfield) / 2 inField = False elif temp.coord.x > xfield / 2: x = xfield / 2 inField = False if temp.coord.y < (-yfield) / 2: y = (-yfield) / 2 inField = False elif temp.coord.y > yfield / 2: y = yfield / 2 inField = False if inField == False: temp = Node(x, y, i.id) return temp
def get_vessel_mask(plot=False): """ Returns Vessel Mask Inputs: plot - if we want to plot mask Outputs: mask - vessel mask (N_ROWS,N_COLS) if 0 - vessel not there , 1 - otherwise """ R, Z = get_vessel_COMPASS() vessel = [[R[i], Z[i], R[i + 1], Z[i + 1]] for i in range(len(R) - 1)] r_space = np.linspace(R_MIN, R_MAX, num=N_COLS + 1) z_space = np.linspace(Z_MIN, Z_MAX, num=N_ROWS + 1) grid = [] for r in r_space: grid.append([(r, Z_MIN), (r, Z_MAX)]) for z in z_space: grid.append([(R_MIN, z), (R_MAX, z)]) grid = MultiLineString(grid) mask = np.zeros((N_ROWS, N_COLS)) for (r_start, z_start, r_end, z_end) in vessel: line = LineString([(r_start, z_start), (r_end, z_end)]) for (k, segment) in enumerate(line.difference(grid)): rr, zz = segment.xy r_mean = np.mean(rr) z_mean = np.mean(zz) (i, j) = transform(r_mean, z_mean) mask[i, j] = 1. mask = np.array(mask) print 'mask:', mask.shape, mask.dtype if plot: plt.figure() plt.plot(R, Z, 'r') plt.imshow(mask, vmin=0, vmax=np.max(mask), origin='lower', extent=[R_MIN, R_MAX, Z_MIN, Z_MAX]) plt.show() return mask
def closest_polygon(x, y, angle, polygons, dist=10000): angle = angle * pi / 180.0 line = LineString([(x, y), (x + dist * sin(angle), y + dist * cos(angle))]) dist_min = None closest_polygon = None for i in range(len(polygons)): difference = line.difference(polygons[i]) if difference.geom_type == 'MultiLineString': dist = list(difference.geoms)[0].length if dist_min is None or dist_min > dist: dist_min = dist closest_polygon = i return {'closest_polygon': closest_polygon, 'distance': dist_min}
def fill_polygon(p: Polygon, pen_width: float) -> MultiLineString: minx, miny, maxx, maxy = p.bounds result = [] while not p.is_empty: if p.geom_type == "Polygon": result.append(p.boundary) elif p.geom_type == "MultiPolygon": result.extend(poly.boundary for poly in p) p = p.buffer(-pen_width) mls = unary_union(result) # we add a center line to be cut with a buffered version of the hatching lines c = 0.5 * (miny + maxy) ls = LineString([(minx, c), (maxx, c)]) return mls.union(ls.difference(mls.buffer(0.55 * pen_width)))
def closest_polygon(x, y, angle, polygons, dist = 10000): angle = angle * pi / 180.0 line = LineString([(x, y), (x + dist * sin(angle), y + dist * cos(angle))]) dist_min = None closest_polygon = None for i in range(len(polygons)): difference = line.difference(polygons[i]) if difference.geom_type == 'MultiLineString': dist = list(difference.geoms)[0].length if dist_min is None or dist_min > dist: dist_min = dist closest_polygon = i return {'closest_polygon': closest_polygon, 'distance': dist_min}
def closest_polygon(x, y, angle, polygons, dist = 100): angle = angle * pi / 180.0 line = LineString([(x, y), (x + dist * sin(angle), y + dist * cos(angle))]) i = 0 dist_min = None for polygon in polygons: difference = line.difference(polygon) print i, difference if difference.geom_type == 'MultiLineString': dist = list(difference.geoms)[0].length print dist if dist_min is None or dist_min > dist: dist_min = dist i += 1 print "Dist min: " , dist_min return i
def closest_polygon(x, y, angle, polygons, dist=100): angle = angle * pi / 180.0 line = LineString([(x, y), (x + dist * sin(angle), y + dist * cos(angle))]) i = 0 dist_min = None for polygon in polygons: difference = line.difference(polygon) print i, difference if difference.geom_type == 'MultiLineString': dist = list(difference.geoms)[0].length print dist if dist_min is None or dist_min > dist: dist_min = dist i += 1 print "Dist min: ", dist_min return i
def closest_polygon(x, y, angle, polygons, dist=100): angle = angle * pi / 180.0 line = LineString([(x, y), (x + dist * sin(angle), y + dist * cos(angle))]) i = 0 dist_min = None closest = None for polygon in polygons: difference = line.difference(polygon) if difference.geom_type == "MultiLineString": dist = list(difference.geoms)[0].length if dist_min is None or dist_min > dist: dist_min = dist closest = i i += 1 return closest
def closest_polygon(self, angle, dist=280000000): angle = angle * pi / 180.0 line = LineString([(self.xpt, self.ypt), (self.xpt + dist * sin(angle), self.ypt + dist * cos(angle))]) i = 0 dist_min = None index_min = None for polygon in self.polygons: try: difference = line.difference(polygon) if difference.geom_type == "MultiLineString": dist = list(difference.geoms)[0].length # print i, dist if dist_min is None or dist_min > dist: dist_min = dist index_min = i except Exception, ex: pass # print "%d doesn't work"%i i += 1
def Node_translation(i, F, list_o): """This function takes 3 inputs: - i: the node to move - F: the force that moves the node - list_o: the obstacle list It returns a new node that is the result of the input node translation""" temp = i.translation(F) dist_init = i.coord.distance(temp.coord) g = F.x * 1000 h = F.y * 1000 F_temp = Vector(g, h) projection = i.translation(F_temp) line = LineString([(i.coord.x, i.coord.y), (projection.coord.x, projection.coord.y)]) dist_min = None closest_obs = None for elt in list_o: difference = line.difference(elt) if difference.geom_type == 'MultiLineString': # In this case we meet an obstacle on our way dist = list(difference.geoms)[0].length if dist_min is None or dist_min > dist: dist_min = dist closest_obs = elt if dist_min != None and dist_min < dist_init: # If dist_min is different from None, that means we meet and osbtacle and we need to reduce the translation. print("CHANGEMENT CAR distance initale {} > dist_min {}".format( dist_init, dist_min)) ratio = (dist_min * 0.9) / dist_init F.x = F.x * ratio F.y = F.y * ratio temp = i.translation(F) else: print("PAS DE CHANGEMENT CAR distance initale {} < dist_min {}".format( dist_init, dist_min)) return temp
def closest_polygon(x, y, angle, polygons, dist = 900000): angle = angle * pi / 180.0 line = LineString([(x, y), (x + dist * sin(angle), y + dist * cos(angle))]) i = 0 dist_min = None index_min = None for polygon in polygons: try: difference = line.difference(polygon) #print i, difference if difference.geom_type == 'MultiLineString': dist = list(difference.geoms)[0].length #print i, dist if dist_min is None or dist_min > dist: dist_min = dist index_min = i except Exception, ex: pass #print "%d doesn't work"%i i += 1
def closest_polygon(x, y, angle, polygons, dist=900000): angle = angle * pi / 180.0 line = LineString([(x, y), (x + dist * sin(angle), y + dist * cos(angle))]) i = 0 dist_min = None index_min = None for polygon in polygons: try: difference = line.difference(polygon) #print i, difference if difference.geom_type == 'MultiLineString': dist = list(difference.geoms)[0].length #print i, dist if dist_min is None or dist_min > dist: dist_min = dist index_min = i except Exception, ex: pass #print "%d doesn't work"%i i += 1
def get_los_mask(plot = False, los = 'Full'): """ Returns los mask considering no width in los Inputs: plot - if we want to plot mask los - List with los to be calculated (by default 'Full' assumes all of them) Outputs: mask - vessel mask (N_ROWS,N_COLS) value corresponds to length of the los in each pixel """ Ri, Rf, Zi, Zf = get_los_JET() if los != 'Full': Ri = [Ri[i] for i in los] Rf = [Rf[i] for i in los] Zi = [Zi[i] for i in los] Zf = [Zf[i] for i in los] r_space = np.linspace(R_MIN, R_MAX, num = N_COLS+1) z_space = np.linspace(Z_MIN, Z_MAX, num = N_ROWS+1) grid = [] for r in r_space: grid.append([(r, Z_MIN), (r, Z_MAX)]) for z in z_space: grid.append([(R_MIN, z), (R_MAX, z)]) grid = MultiLineString(grid) mask = np.zeros((N_LOS,N_ROWS, N_COLS)) for (los,(r_start, z_start, r_end, z_end)) in enumerate(zip(Ri,Zi,Rf,Zf)): line = LineString([(r_start, z_start), (r_end, z_end)]) for (k, segment) in enumerate(line.difference(grid)): rr, zz = segment.xy r_mean = np.mean(rr) z_mean = np.mean(zz) try: (i,j) = transform(r_mean, z_mean) mask[los,i,j] = segment.length except : pass mask = np.array(mask, dtype = np.float32) print 'mask:', mask.shape, mask.dtype if plot: R,Z = get_vessel_JET() plt.figure() for (r_start, z_start, r_end, z_end) in zip(Ri,Zi,Rf,Zf): plt.plot((r_start,r_end), (z_start,z_end), 'r',lw = .7) plt.imshow(np.sum(mask, axis = 0), vmin=0, vmax=np.max(mask), origin = 'lower', extent = [R_MIN, R_MAX, Z_MIN, Z_MAX]) plt.plot(R,Z) plt.xlabel('R (m)') plt.ylabel('Z (m)') plt.colorbar() plt.show() return mask
def get_part_outside_polygon(line: LineString, polygon: Polygon) -> float: return float(line.difference(polygon).length) / float(line.length)
for x in x_grid: grid.append([(x, y_min), (x, y_max)]) for y in y_grid: grid.append([(x_min, y), (x_max, y)]) grid = MultiLineString(grid) # ------------------------------------------------------------------------- projections = [] for row in df.itertuples(): line = LineString([(row.x0, row.y0), (row.x1, row.y1)]) projection = np.zeros((n_rows, n_cols)) for segment in line.difference(grid): xx, yy = segment.xy x_mean = np.mean(xx) y_mean = np.mean(yy) (i, j) = transform(x_mean, y_mean) projection[i, j] = segment.length projections.append(projection * row.etendue) projections = np.array(projections) print('projections:', projections.shape, projections.dtype) # ------------------------------------------------------------------------- fname = 'projections.npy' print('Writing:', fname)
def yields1(agent1_traj, speed1=None, agent2_traj=None, speed2=None, params={}): """Returns True if agent1 and agent2 start on different paths, end up in the same path, and agent2 gets first to the joint part of the path. """ FILTER_PARAMS = { "turn": {"turn_threshold": 2.0, "window_size": 5, "window_std": 10.0}, "velocity": {"vel_threshold": (1.0, 0.05)}, "acceleration": {"acc_threshold": 0.04, "window_size": 5, "window_std": 0.04}, "lane_change": { "lane_threshold": 0.3, "skip_threshold": 2, "window_size": 5, "max_intersection": 3, "max_lanes_in_window": 2, "intersection_radius": 7.5, # Standard lane width is 3.7m in the US }, "follow": {"min_overlaps": 10}, "yield": { # "yielding_time_gap": 1, # "yielding_prefix": 5, # "yielding_dt": 5, # "yielding_dilation_radius": 0.5, # "yielding_initial_distance": 5, "yielding_time_gap": 1, "yielding_prefix": 1, "yielding_dt": 0.5, "yielding_dilation_radius": 0.1, "yielding_initial_distance": 1, } } params = FILTER_PARAMS['yield'] if agent1_traj.shape[0] < 3: return False def compute_trajectory_stats(traj, speed, dilation): """Compute trajectory stats needed for yielding""" #TOt positions = traj[:, :2] spd1 = speed t = 0.5*np.arange(traj.shape[0]) sl = LineString(positions.tolist()).buffer(dilation) return positions, spd1, t, sl yielding_dilation_radius = params["yielding_dilation_radius"] positions1, spd1, t1, sl1 = compute_trajectory_stats(agent1_traj, speed1, yielding_dilation_radius) positions2, spd2, t2, sl2 = compute_trajectory_stats(agent2_traj, speed2, yielding_dilation_radius) intersection = sl1.intersection(sl2) idxs1 = find_points_in_region(positions1, intersection) idxs2 = find_points_in_region(positions2, intersection) if len(idxs1) == 0 or len(idxs2) == 0: return False # The trajectories upto the intersection. positions1b = positions1[: idxs1[0], :] positions2b = positions2[: idxs2[0], :] if positions1b.shape[0] < 2 or positions2b.shape[0] < 2: return False sl1b = LineString(positions1b[:, :2]).buffer(yielding_dilation_radius) sl2b = LineString(positions2b[:, :2]).buffer(yielding_dilation_radius) # The regions that cross eachother sl1_only = (sl1b.difference(sl2)).buffer(-yielding_dilation_radius / 2.0) sl2_only = (sl2b.difference(sl1)).buffer(-yielding_dilation_radius / 2.0) # Skip if no overlaps in the trajectory. if len(idxs1) == 0 or len(idxs2) == 0: return False idx1_m1 = max(0, idxs1[0] - params["yielding_dt"]) if (sl1_only.length > params["yielding_prefix"] # long enough prefix for trajectory 1 and sl2_only.length > params["yielding_prefix"] # long enough prefix for trajectory 2 and t1[idxs1[0]] - t2[idxs2[0]] > params["yielding_time_gap"] # agent 1 is before agent 2 and spd1[idx1_m1] - spd1[idxs1[0]] > -0.5 # non-increasing speed. and Point(positions2b[0, :]).distance(sl1_only) > params["yielding_initial_distance"] # initial point for 2 is far from trajectory 1 ): print("!!!!!!!!!! yield !!!!!!!!!!!!") return True return False
def draw_text_on_line( self, coords, text, color=(0, 0, 0), font_size=10, font_family='Tahoma', font_style=cairo.FONT_SLANT_NORMAL, font_weight=cairo.FONT_WEIGHT_NORMAL, text_halo_width=1, text_halo_color=(1, 1, 1), text_halo_line_cap=cairo.LINE_CAP_ROUND, text_halo_line_join=cairo.LINE_JOIN_ROUND, text_halo_line_dash=None, text_transform=None, ): ''' Draws text on a line. Tries to find a position with the least change in gradient and which is closest to the middle of the line. :param coords: iterable containing all coordinates as ``(lon, lat)`` :param text: text to be drawn :param color: ``(r, g, b[, a])`` :param font_size: font-size in unit (pixel/point) :param font_family: font name :param font_style: ``cairo.FONT_SLANT_NORMAL``, ``cairo.FONT_SLANT_ITALIC`` or ``cairo.FONT_SLANT_OBLIQUE`` :param font_weight: ``cairo.FONT_WEIGHT_NORMAL`` or ``cairo.FONT_WEIGHT_BOLD`` :param text_halo_width: border-width in unit (pixel/point) :param text_halo_color: ``(r, g, b[, a])`` :param text_halo_line_cap: one of :const:`cairo.LINE_CAP_*` :param text_halo_line_join: one of :const:`cairo.LINE_JOIN_*` :param text_halo_line_dash: list/tuple used by :meth:`cairo.Context.set_dash` :param text_transform: one of ``'lowercase'``, ``'uppercase'`` or ``'capitalize'`` ''' text = text.strip() if not text: return coords = map(lambda c: self.transform_coords(*c), coords) self.context.select_font_face(font_family, font_style, font_weight) self.context.set_font_size(font_size) text = utils.text_transform(text, text_transform) width, height = self.context.text_extents(text)[2:4] font_ascent, font_descent = self.context.font_extents()[0:2] self.context.new_path() #: make sure line does not intersect other conflict objects line = LineString(coords) line = self.map_area.intersection(line) line = line.difference(self.map_area.exterior.buffer(height)) line = line.difference(self.conflict_area) #: check whether line is empty or is split into several different parts if line.geom_type == 'GeometryCollection': return elif line.geom_type == 'MultiLineString': longest = None min_len = width * 1.2 for seg in line.geoms: seg_len = seg.length if seg_len > min_len: longest = seg min_len = seg_len if longest is None: return line = longest coords = tuple(line.coords) seg = utils.linestring_text_optimal_segment(coords, width) # line has either to much change in gradients or is too short if seg is None: return #: crop optimal segment of linestring start, end = seg coords = coords[start:end+1] #: make sure text is rendered from left to right if coords[-1][0] < coords[0][0]: coords = tuple(reversed(coords)) # translate linestring so text is rendered vertically in the middle line = LineString(tuple(coords)) offset = font_ascent / 2. - font_descent line = line.parallel_offset(offset, 'left', resolution=3) # make sure text is rendered centered on line start_len = (line.length - width) / 2. char_coords = None chars = utils.generate_char_geoms(self.context, text) #: draw all character paths for char in utils.iter_chars_on_line(chars, line, start_len): for geom in char.geoms: char_coords = iter(geom.exterior.coords) self.context.move_to(*char_coords.next()) for lon, lat in char_coords: self.context.line_to(lon, lat) self.context.close_path() #: only add line to reserved area if text was drawn if char_coords is not None: covered = line.buffer(height) self.conflict_union(covered) #: draw border around characters self.context.set_line_cap(cairo.LINE_CAP_ROUND) self.context.set_source_rgba(*text_halo_color) self.context.set_line_width(2 * text_halo_width) self.context.set_line_cap(text_halo_line_cap) self.context.set_line_join(text_halo_line_join) self.context.set_dash(text_halo_line_dash or tuple()) self.context.stroke_preserve() #: fill actual text self.context.set_source_rgba(*color) self.context.fill()
def difference(self, geom): traj = LineString( np.column_stack((self.x[:, np.newaxis], self.y[:, np.newaxis], self.seconds[:, np.newaxis]))) return traj.difference(geom)
(293975, 253937), (297104, 249860), (301181, 246731), (305928, 244765), (311023, 244094), (316118, 244765), (320866, 246731), (324943, 249860), (328071, 253937), (330037, 258684), (330708, 263779), ]) multipolygon = MultiPolygon([poly_geom]) result = link_geom.difference(multipolygon) print(result) ''' MULTILINESTRING ( (364517 364517, 265403 364517, 265403 265403, 291551.8771344455 265403), (330494.1228655545 265403, 364517 265403, 364517 364517), (364517 364517,
def plot_port(ax, port, outer_shell=True, inner_shell=True, standoff=True, line_of_sight=True, impact_point=True, exit_point=True): """Plot port geometry. Arguments: ax -- The matplotlib axis object to plot onto. port -- The port to draw. outer_shell -- Draw the point on the outer shell. inner_shell -- Draw the point on the inner shell. standoff -- Draw the standoff. line_of_sight -- Draw the line of sight of the port. impact_point -- Draw the impact point. exit_point -- Draw the exit point on the inner shell. """ if outer_shell: x, y = port.outer_shell_center_pt() ax.plot(x, y, 'bo') if inner_shell: x, y = port.inner_shell_center_pt() ax.plot(x, y, 'bo') if line_of_sight: x1, y1 = port.outer_shell_center_pt() if standoff: x1, y1 = port.drill_hole_center_pt(port.standoff) x2, y2 = port.exit_pt() ax.plot((x1, x2), (y1, y2), 'b-') if impact_point: x, y = port.impact_pt() ax.plot(x, y, 'bo') if exit_point: x, y = port.exit_pt() ax.plot(x, y, 'bo') if standoff: from shapely.geometry import LineString, Polygon shell = Polygon(shapely_shell().exterior) pt1, pt3 = port.drill_hole_edge_pts(-(mp.a0out - mp.a0)) pt2, pt4 = port.drill_hole_edge_pts(port.standoff) l1 = LineString((pt1, pt2)) l2 = LineString((pt3, pt4)) l1 = l1.difference(shell) l2 = l2.difference(shell) x, y = l1.xy ax.plot(x, y, 'k-') x, y = l2.xy ax.plot(x, y, 'k-')
def main(): #-- Read the system arguments listed after the program long_options = [ 'subdir=', 'method=', 'step=', 'indir=', 'interval=', 'buffer=', 'manual' ] optlist, arglist = getopt.getopt(sys.argv[1:], '=D:M:S:I:V:B:m:', long_options) subdir = 'all_data2_test' method = '' step = 50 n_interval = 1000 buffer_size = 500 indir = '' set_manual = False for opt, arg in optlist: if opt in ('-D', '--subdir'): subdir = arg elif opt in ('-M', '--method'): method = arg elif opt in ('-S', '--step'): step = np.int(arg) elif opt in ('-V', '--interval'): n_interval = np.int(arg) elif opt in ('-B', '--buffer'): buffer_size = np.int(arg) elif opt in ('-I', '--indir'): indir = os.path.expanduser(arg) elif opt in ('-m', '--manual'): set_manual = True #-- directory setup #- current directory current_dir = os.path.dirname(os.path.realpath(__file__)) headDirectory = os.path.join(current_dir, '..', 'FrontLearning_data') glaciersFolder = os.path.join(headDirectory, 'Glaciers') results_dir = os.path.join(headDirectory, 'Results', subdir) #-- if user input not given, set label folder #-- else if input directory is given, then set the method based on that if indir == '': indir = os.path.join(results_dir, method, method) else: method = os.path.basename(indir) if method == '': sys.exit("Please do not put '/' at the end of indir.") print('input directory ONLY for NN output:%s' % indir) print('METHOD:%s' % method) #-- make histohtam filder if it doesn't exist histFolder = os.path.join(results_dir, 'Histograms') if (not os.path.isdir(histFolder)): os.mkdir(histFolder) outputFolder = os.path.join( histFolder, method + '_' + str(step) + '_%isegs' % n_interval + '_%ibuffer' % buffer_size) #-- make output folders if (not os.path.isdir(outputFolder)): os.mkdir(outputFolder) if set_manual: datasets = ['NN', 'Sobel', 'Manual'] else: datasets = ['NN', 'Sobel'] print(datasets) pixelFolder = {} frontFolder = {} pixelFolder['NN'] = os.path.join(results_dir, method, method + ' Pixel CSVs ' + str(step)) pixelFolder['Sobel'] = os.path.join(results_dir, 'Sobel/Sobel Pixel CSVs ' + str(step)) if 'Manual' in datasets: pixelFolder['Manual'] = os.path.join( results_dir, 'output_handrawn/output_handrawn Pixel CSVs ' + str(step)) frontFolder['NN'] = os.path.join(results_dir, method, method + ' Geo CSVs ' + str(step)) frontFolder['Sobel'] = os.path.join(results_dir, 'Sobel/Sobel Geo CSVs ' + str(step)) if 'Manual' in datasets: frontFolder['Manual'] = os.path.join( results_dir, 'output_handrawn/output_handrawn Geo CSVs ' + str(step)) def seriesToNPoints(series, N): #find the total length of the series totalDistance = 0 for s in range(len(series[:, 0]) - 1): totalDistance += ((series[s, 0] - series[s + 1, 0])**2 + (series[s, 1] - series[s + 1, 1])**2)**0.5 intervalDistance = totalDistance / (N - 1) #make the list of points newSeries = series[0, :] currentS = 0 currentPoint1 = series[currentS, :] currentPoint2 = series[currentS + 1, :] for p in range(N - 2): distanceAccrued = 0 while distanceAccrued < intervalDistance: currentLineDistance = ( (currentPoint1[0] - currentPoint2[0])**2 + (currentPoint1[1] - currentPoint2[1])**2)**0.5 if currentLineDistance < intervalDistance - distanceAccrued: distanceAccrued += currentLineDistance currentS += 1 currentPoint1 = series[currentS, :] currentPoint2 = series[currentS + 1, :] else: distance = intervalDistance - distanceAccrued newX = currentPoint1[0] + ( distance / currentLineDistance) * (currentPoint2[0] - currentPoint1[0]) newY = currentPoint1[1] + ( distance / currentLineDistance) * (currentPoint2[1] - currentPoint1[1]) distanceAccrued = intervalDistance + 1 newSeries = np.vstack([newSeries, np.array([newX, newY])]) currentPoint1 = np.array([newX, newY]) newSeries = np.vstack([newSeries, series[-1, :]]) return (newSeries) def frontComparisonErrors(front1, front2): errors = [] for ff in range(len(front1)): dist = ((front1[ff, 0] - front2[ff, 0])**2 + (front1[ff, 1] - front2[ff, 1])**2)**0.5 errors.append(dist) return (errors) def rmsError(error): return (np.sqrt(np.mean(np.square(error)))) def generateLabelList(labelFolder): labelList = [] for fil in os.listdir(labelFolder): # if fil[-6:] == 'B8.png' or fil[-6:] == 'B2.png': # labelList.append(fil[:-4]) if fil.endswith('_nothreshold.png'): labelList.append(fil.replace('_nothreshold.png', '')) return (labelList) # get glacier names def getGlacierList(labelList): f = open(os.path.join(glaciersFolder, 'Scene_Glacier_Dictionary.csv'), 'r') lines = f.read() f.close() lines = lines.split('\n') glacierList = [] for sceneID in labelList: for line in lines: line = line.split(',') if line[0] == sceneID: glacierList.append(line[1]) return (glacierList) #code to get the list of fronts and their images def getFrontList(glacierList, labelList): frontsList = [] for ind, label in enumerate(labelList): glacier = glacierList[ind] f = open( os.path.join(glaciersFolder, glacier, '%s Image Data.csv' % glacier), 'r') lines = f.read() f.close() lines = lines.split('\n') for line in lines: line = line.split(',') if line[1][:-4] == label: frontsList.append(line[0]) return (frontsList) def fjordBoundaryIndices(glacier): boundary1file = os.path.join(glaciersFolder, glacier, 'Fjord Boundaries', glacier + ' Boundary 1 V2.csv') boundary1 = np.genfromtxt(boundary1file, delimiter=',') boundary2file = os.path.join(glaciersFolder, glacier, 'Fjord Boundaries', glacier + ' Boundary 2 V2.csv') boundary2 = np.genfromtxt(boundary2file, delimiter=',') boundary1 = seriesToNPoints(boundary1, 1000) boundary2 = seriesToNPoints(boundary2, 1000) return (boundary1, boundary2) labelList = generateLabelList(indir) glacierList = getGlacierList(labelList) frontList = getFrontList(glacierList, labelList) allerrors = {} allerrors['NN'] = [] allerrors['Sobel'] = [] allerrors['Manual'] = [] N = 1 N = len(labelList) for ll in range(N): glacier = glacierList[ll] label = labelList[ll] trueFrontFile = frontList[ll] print(label) ############################################################################ # This section to get the front images trueImageFolder = os.path.join(headDirectory, 'Glaciers', glacier, 'Small Images') trueImage = Image.open( os.path.join(trueImageFolder, label + '_Subset.png')).transpose( Image.FLIP_LEFT_RIGHT).convert("L") frontImageFolder = {} frontImageFolder['NN'] = indir frontImageFolder['Sobel'] = os.path.join(results_dir, 'Sobel/Sobel') if 'Manual' in datasets: frontImageFolder['Manual'] = os.path.join(os.path.dirname(indir), 'output_handrawn') frontImage = {} pixels = {} for d, tl in zip(datasets, ['_nothreshold', '', '_nothreshold']): frontImage[d] = Image.open(os.path.join(frontImageFolder[d],label \ + '%s.png'%tl)).transpose(Image.FLIP_LEFT_RIGHT).convert("L") ############################################################################ # This section to get the front pixels # get the front pixelsFile = glacier + ' ' + label + ' Pixels.csv' pixels[d] = np.genfromtxt(os.path.join(pixelFolder[d], pixelsFile), delimiter=',') pixels[d] = seriesToNPoints(pixels[d], n_interval) ############################################################################ # Get the fjord boundaries for the current glacier bounds = {} bounds[1], bounds[2] = fjordBoundaryIndices(glacier) buff = {} for i in [1, 2]: # Form buffer around boundary lineStringSet = bounds[i] line = LineString(lineStringSet) buff[i] = line.buffer(buffer_size) ############################################################################ # This section to get the front data #get the true front trueFrontFolder = os.path.join(glaciersFolder, glacier, 'Front Locations', '3413') trueFront = np.genfromtxt(trueFrontFolder + '/' + trueFrontFile, delimiter=',') #-- make sure all fronts go in the same direction #-- if the x axis is not in increasng order, reverse if trueFront[0, 0] > trueFront[-1, 0] and glacier != 'Helheim': print('flipped true front.') trueFront = trueFront[::-1, :] trueFront = seriesToNPoints(trueFront, n_interval) #-- get rid of poitns too close to the edges l1 = LineString(trueFront) int1 = l1.difference(buff[1]) int2 = int1.difference(buff[2]) try: trueFront = np.array(shape(int2).coords) except: lengths = [ len(np.array(shape(int2)[i].coords)) for i in range(len(shape(int2))) ] max_ind = np.argmax(lengths) trueFront = np.array(shape(int2)[max_ind].coords) #-- testing print(lengths) print(lengths[max_ind]) #-- rebreak into n_interval segments trueFront = seriesToNPoints(trueFront, n_interval) front = {} errors = {} for d in datasets: #get the front frontFile = glacier + ' ' + label + ' Profile.csv' temp_front = np.genfromtxt(os.path.join(frontFolder[d], frontFile), delimiter=',') #-- make sure all fronts go in the same direction #-- if the x axis is not in increasng order, reverse #if temp_front[0,0] > temp_front[-1,0]: # print('flipped %s'%d) # temp_front = temp_front[::-1,:] front[d] = seriesToNPoints(temp_front, n_interval) #-- get rid of points to close to the edges #-- get rid of poitns too close to the edges l1 = LineString(front[d]) int1 = l1.difference(buff[1]) int2 = int1.difference(buff[2]) try: front[d] = np.array(shape(int2).coords) except: lengths = [ len(np.array(shape(int2)[i].coords)) for i in range(len(shape(int2))) ] max_ind = np.argmax(lengths) front[d] = np.array(shape(int2)[max_ind].coords) #-- testing print(lengths) print(lengths[max_ind]) #-- rebreak into n_interval segments front[d] = seriesToNPoints(front[d], n_interval) errors[d] = frontComparisonErrors(trueFront, front[d]) for error in errors[d]: allerrors[d].append(error) #-- plot fronts for debugging purposes -- double checking. # plt.plot(trueFront[:,0],trueFront[:,1],label='True') # plt.plot(front['NN'][:,0],front['NN'][:,1,],label='NN') # plt.legend() # plt.show() frontXmin = np.min( np.concatenate(([np.min(trueFront[:, 0])], [np.min(front[d][:, 0]) for d in datasets]))) frontXmax = np.max( np.concatenate(([np.max(trueFront[:, 0])], [np.max(front[d][:, 0]) for d in datasets]))) frontYmin = np.min( np.concatenate(([np.min(trueFront[:, 1])], [np.min(front[d][:, 1]) for d in datasets]))) frontYmax = np.max( np.concatenate(([np.max(trueFront[:, 1])], [np.max(front[d][:, 1]) for d in datasets]))) fig = plt.figure(figsize=(10, 8)) n_panels = len(front) + 1 plt.subplot(2, n_panels, 1) plt.imshow(trueImage, cmap='gray') plt.gca().set_xlim([0, 200]) plt.gca().set_ylim([300, 0]) plt.gca().axes.get_xaxis().set_ticks([]) plt.gca().axes.get_yaxis().set_ticks([]) plt.title('Original Image', fontsize=12) p = 2 for d in datasets: plt.subplot(2, n_panels, p) plt.imshow(frontImage[d], cmap='gray') plt.plot(pixels[d][:, 0], pixels[d][:, 1], 'y-', linewidth=3) plt.gca().set_xlim([0, 200]) plt.gca().set_ylim([300, 0]) plt.gca().axes.get_xaxis().set_ticks([]) plt.gca().axes.get_yaxis().set_ticks([]) plt.title('%s Solution' % d, fontsize=12) p += 1 plt.subplot(2, n_panels, p) plt.title('Geocoded Solutions', fontsize=12) plt.ylabel('Northing (km)', fontsize=12) plt.xlabel('Easting (km)', fontsize=12) plt.plot(trueFront[:, 0] / 1000, trueFront[:, 1] / 1000, 'k-', label='True') for d, c in zip(datasets, ['b-', 'g-', 'r-']): plt.plot(front[d][:, 0] / 1000, front[d][:, 1] / 1000, c, label=d) plt.gca().set_xlim([frontXmin / 1000, frontXmax / 1000]) plt.gca().set_ylim([frontYmin / 1000, frontYmax / 1000]) plt.gca().set_xticks([frontXmin / 1000, frontXmax / 1000]) plt.gca().set_yticks([frontYmin / 1000, frontYmax / 1000]) plt.legend(loc=0) p += 1 p_temp = copy.copy(p) x = {} y = {} for d, c in zip(datasets, ['b', 'g', 'r']): plt.subplot(2, n_panels, p) plt.title('%s Errors Histogram' % d, fontsize=12) bins = range(0, 5000, 100) y[d], x[d], _ = plt.hist(errors[d], alpha=0.5, color=c, bins=bins, label='NN') #plt.xlabel('RMS Error = '+'{0:.2f}'.format(rmsError(errors[d]))+' m',fontsize=12) plt.xlabel('Mean Diff. = ' + '{0:.2f}'.format(np.mean(np.abs(errors[d]))) + ' m', fontsize=12) p += 1 #-- set histogram bounds for d in datasets: plt.subplot(2, n_panels, p_temp) plt.gca().set_ylim([0, np.max([y[d] for d in datasets])]) plt.gca().set_xlim([0, np.max([x[d] for d in datasets])]) p_temp += 1 plt.savefig(os.path.join(outputFolder, label + '.png'), bbox_inches='tight') plt.close(fig) fig = plt.figure(figsize=(11, 4)) x = {} y = {} for i, d, c, lbl in zip(range(len(datasets)), datasets, ['b', 'g', 'r'], ['e', 'f', 'g']): plt.subplot(1, len(datasets), i + 1) plt.title(r"$\bf{%s)}$" % lbl + " %s Error Histogram" % d, fontsize=12) bins = range(0, 5000, 100) y[d], x[d], _ = plt.hist(allerrors[d], alpha=0.5, color=c, bins=bins, label=d) #plt.xlabel('RMS Error = '+'{0:.2f}'.format(rmsError(allerrors[d]))+' m',fontsize=12) plt.xlabel('Mean Difference = ' + '{0:.2f}'.format(np.mean(np.abs(allerrors[d]))) + ' m', fontsize=12) if i == 0: plt.ylabel('Count (100 m bins)', fontsize=12) for i in range(len(datasets)): plt.subplot(1, len(datasets), i + 1) plt.gca().set_ylim([0, np.max([y[d] for d in datasets])]) plt.gca().set_xlim([0, np.max([x[d] for d in datasets])]) plt.savefig(os.path.join(results_dir,\ 'Figure_4_'+'_'.join(method.split())+'_'+str(step)+'_%isegs'%n_interval+'_%ibuffer'%buffer_size+'.pdf'),bbox_inches='tight') plt.close()
# ----------------------------------------------------------------------------------------- coords = [] for i in range(n): x0 = t_detector_x[i] y0 = t_detector_y[i] x1 = t_pinhole_x y1 = t_pinhole_y m = (y1-y0)/(x1-x0) b = (y0*x1-y1*x0)/(x1-x0) y2 = -100. x2 = (y2-b)/m line = LineString([(x0, y0), (x2, y2)]) circle = Point(0., 0.).buffer(100.).boundary segment = line.difference(circle)[1] x0, y0 = segment.coords[0] x1, y1 = segment.coords[1] print('%10s %10.6f %10.6f %10.6f %10.6f' % ('top', x0, y0, x1, y1)) coords.append(['top', x0, y0, x1, y1]) for i in range(n): x0 = f_detector_x[i] y0 = f_detector_y[i] x1 = f_pinhole_x y1 = f_pinhole_y m = (y1-y0)/(x1-x0) b = (y0*x1-y1*x0)/(x1-x0) x2 = -100. y2 = m*x2+b line = LineString([(x0, y0), (x2, y2)])
# In[ ]: from shapely.geometry import LineString a = LineString([(0, 0), (1, 1), (1,2), (2,2)]) b = LineString([(0, 0), (1, 1), (2,1), (2,2)]) pa = list(a.coords) pb = list(b.coords) diff1 = set(pa) - set(pb) diff2 = set(pb) - set(pa) print diff1, diff2 print(".........") x = a.intersection(b) print x d1 = b.difference(a) d2 = a.difference(b) print "a.union(b)" ms = a.union(b) print ms upoints = [] for ls in ms: upoints +=list(ls.coords) print upoints #print "diff" #print d1,d2 S1 = set(upoints) print S1
def get_los_mask_width(self, plot=False, los='Full', N=100): """ Returns los mask considering width in los Inputs: plot - if we want to plot mask los - List with los to be calculated (by default 'Full' assumes all of them) N - number of virtual chords per los Outputs: mask - vessel mask (N_ROWS,N_COLS) if 0 - vessel not there , 1 - otherwise """ Ri, Rf, Zi, Zf = self.get_los_COMPASS_width(N) print Ri.shape, Zi.shape, Rf.shape, Zf.shape if los != 'Full': Ri = np.asarray([Ri[i] for i in los]) Rf = np.asarray([Rf[i] for i in los]) Zi = np.asarray([Zi[i, :] for i in los]) Zf = np.asarray([Zf[i, :] for i in los]) print Ri.shape, Zi.shape, Rf.shape, Zf.shape r_space = np.linspace(R_MIN, R_MAX, num=N_COLS + 1) z_space = np.linspace(Z_MIN, Z_MAX, num=N_ROWS + 1) grid = [] for r in r_space: grid.append([(r, Z_MIN), (r, Z_MAX)]) for z in z_space: grid.append([(R_MIN, z), (R_MAX, z)]) grid = MultiLineString(grid) mask = np.zeros((self.N_LOS, N_ROWS, N_COLS)) for (l, (r_start, z_start)) in enumerate(zip(Ri, Zi)): print 'los :', l for (r_end, z_end) in zip(Rf[l, :], Zf[l, :]): line = LineString([(r_start, z_start), (r_end, z_end)]) for (k, segment) in enumerate(line.difference(grid)): rr, zz = segment.xy r_mean = np.mean(rr) z_mean = np.mean(zz) try: (i, j) = transform(r_mean, z_mean) mask[l, i, j] += segment.length except: pass mask = np.array(mask, dtype=np.float32) / float(2 * N) mask = np.pad(mask, ((0, N_LOS_MAX - mask.shape[0]), (0, 0), (0, 0)), 'constant', constant_values=0) print 'mask:', mask.shape, mask.dtype if plot: for (l, (r_start, z_start, r_end, z_end)) in enumerate(zip(Ri, Zi, Rf[:, 0], Zf[:, 0])): plt.figure() plt.plot((r_start, r_end), (z_start, z_end), 'r') plt.imshow(mask[l, :, :], vmin=0, vmax=np.max(mask[l, :, :]), extent=[R_MIN, R_MAX, Z_MIN, Z_MAX]) plt.colorbar() plt.savefig(str(l) + '.png') return mask