def isInsidePolygon(point, polyPoints): # adapted from http://stackoverflow.com/questions/36399381/whats-the-fastest-way-of-checking-if-a-point-is-inside-a-polygon-in-python shapelyPoint = Point(point['lat'], point['lon']) shapelyPolygon = Polygon(polyPoints) isInside = shapelyPolygon.contains(shapelyPoint) #print(isInside) return isInside
def _contour_to_poly(contour): poly = Polygon(contour) if not poly.is_valid: poly = poly.buffer(0) assert poly.is_valid, \ "Contour %r did not make valid polygon %s because %s" \ % (contour, poly.wkt, explain_validity(poly)) return poly
def isInsideAnyOfPolygons(point, polys): for poly in polys: shapelyPoint = Point(point['lat'], point['lon']) shapelyPolygon = Polygon(poly) isInside = shapelyPolygon.contains(shapelyPoint) if isInside: return True return False
def subset(self, ra_min, ra_max, dec_min, dec_max): poly = Polygon(LinearRing([(ra_min, dec_min), (ra_min, dec_max), (ra_max, dec_max), (ra_max, dec_min)])) mask = np.array( [poly.intersects(self.table['polygon'][i]) for i in range(len(self.table))] ) log.debug('{} catalogs intersect.'.format(mask.sum())) return VphasCatalogSet(self.table[mask])
def polygon_union(anomalies): p = Polygon() for position, target in anomalies.values(): new_x = pol_x+position[0] new_y = pol_y+position[1] new_p = Polygon(zip(new_x, new_y)) p = p.union(new_p) return p
def is_clicked(self, click, handles): from shapely.geometry import Point from shapely.geometry.polygon import Polygon point = Point(*click) polygon = Polygon([handles["1BL"].position, handles["2TL"].position, handles["3TR"].position, handles["4BR"].position]) return polygon.contains(point)
def getIntersectionArea(bldPoly,line,perpLine,bldID): if not line.intersects(bldPoly): return 0.0 pt1 = list(line.coords)[0] pt2 = list(line.coords)[1] perppt1 = list(perpLine.coords)[0] perppt2 = list(perpLine.coords)[1] dx = perppt2[0] - perppt1[0] dy = perppt2[1] - perppt1[1] pt3 = (pt1[0]-dx,pt1[1]-dy) pt4 = (pt2[0]-dx,pt2[1]-dy) linePoly = Polygon([pt1,pt3,pt4,pt2]) try: intersection_area = linePoly.intersection(bldPoly).area return intersection_area/bldPoly.area except: return -1
def hull_accuracy(problem, result, target): nzr = numpy.nonzero(result)[0] nzt = numpy.nonzero(target)[0] result = result[nzr] target = target[nzt] if len(result) < 3 or len(set(result)) != len(result): return -1.0, 0.0 pp = Polygon(problem[result]) if pp.is_valid: # intersected area tt = Polygon(problem[target]) intersection = tt.intersection(pp) intersec_per = intersection.area / tt.area if set(result) == set(target): return 1.0, intersec_per else: return 0.0, intersec_per else: return -1.0, 0.0
def ugrid_area(): # path = '/home/benkoziol/htmp/src_subset_1.nc' path = '/home/benkoziol/l/data/ocgis/ugrid-cesm-subsetting/UGRID_1km-merge-10min_HYDRO1K-merge-nomask_c130402.nc' rd = RequestDataset(path) vc = rd.get_raw_field() vc.load() face_nodes = vc['landmesh_face_node'].get_value() face_node_x = vc['landmesh_node_x'].get_value() face_node_y = vc['landmesh_node_y'].get_value() face_center_x = vc['landmesh_face_x'].get_value() face_center_y = vc['landmesh_face_y'].get_value() areas = [] for ctr, idx in enumerate(range(face_nodes.shape[0])): if ctr % 10000 == 0: print '{} of {}'.format(ctr, face_nodes.shape[0]) curr_face_indices = face_nodes[idx, :] curr_face_node_x = face_node_x[curr_face_indices] curr_face_node_y = face_node_y[curr_face_indices] face_coords = np.zeros((4, 2)) face_coords[:, 0] = curr_face_node_x face_coords[:, 1] = curr_face_node_y poly = Polygon(face_coords) parea = poly.area poly = shapely.geometry.box(*poly.bounds) pt = Point(face_center_x[idx], face_center_y[idx]) if not poly.intersects(pt): print idx, np.array(pt), poly.bounds # if parea > 1: # print idx # print face_nodes[idx, :] # print face_coords # print poly.bounds # sys.exit() areas.append(parea)
def __init__(self, axis_resolution, orientation_resolution, average_path_length, vehicle_length, x_variance, y_variance, orientation_variance, obstacles=None, ): ''' resolution is integers representing the cardinality of the dimensions. For exaple, axis_resolution=4 would create a 4x4 grid. average_path_length is the average length of a path taken for the transition probability. ''' self.axis_resolution = axis_resolution self.orientation_resolution = orientation_resolution self.average_path_length = average_path_length self.vehicle_length = vehicle_length self.x_variance = x_variance self.y_variance = y_variance self.orientation_variance = orientation_variance self.orientation_tolerance = ANGLE_MOD * 1.0 / orientation_resolution is_bad = [[False] * axis_resolution for _ in range(axis_resolution)] if obstacles: for r, c in itertools.product(range(axis_resolution), range(axis_resolution)): (x1, x2), (y1, y2), (_, _) = self.StateToCoorRange((r, c, 0)) # CCW square = Polygon([(x2, y2), (x1, y2), (x1, y1), (x2, y1)]) if square.intersects(obstacles) or obstacles.contains(square): is_bad[r][c] = True self.is_bad = is_bad
def __init__(self, width, segment, linker): global pipe_count p1 = np.array(segment[0]) p4 = np.array(segment[1]) rot_matrix = np.array([[0,-1],[1,0]]) rot = rot_matrix.dot((p4 - p1)) p2 = p1 + rot/(np.linalg.norm(rot))*width p3 = p4 + rot/(np.linalg.norm(rot))*width pts = map(to_latlon,map(list,[p1,p2,p3,p4,p1])) self.pipe_count = pipe_count pipe_count += 1 pipes.poly(shapeType=shapefile.POLYLINE, parts=zip(list(pts[:-1]),list(pts[1:]))) pipes.record(self.pipe_count) self.polygon = Polygon([p1,p2,p3,p4])
class Pipe(object): def __init__(self, width, segment, linker): global pipe_count p1 = np.array(segment[0]) p4 = np.array(segment[1]) rot_matrix = np.array([[0,-1],[1,0]]) rot = rot_matrix.dot((p4 - p1)) p2 = p1 + rot/(np.linalg.norm(rot))*width p3 = p4 + rot/(np.linalg.norm(rot))*width pts = map(to_latlon,map(list,[p1,p2,p3,p4,p1])) self.pipe_count = pipe_count pipe_count += 1 pipes.poly(shapeType=shapefile.POLYLINE, parts=zip(list(pts[:-1]),list(pts[1:]))) pipes.record(self.pipe_count) self.polygon = Polygon([p1,p2,p3,p4]) def __contains__(self, building): building_poly = Polygon(building) intersection_area = self.polygon.intersection(building_poly).area return intersection_area/building_poly.area > 0.1
class VoronoiData: def __init__(self, hydrants): self.vor = Voronoi(hydrants, incremental=True) self.poly = Polygon([(float(x.split(', ')[0]), float(x.split(', ')[1])) for x in open('coords.txt').read().split('\n')[:-1]]) def polygons(self): l={} for point_index, region_index in enumerate(self.vor.point_region): region = self.vor.regions[region_index] if region == [] or -1 in region: continue points = self.vor.vertices[region] l[tuple(self.vor.points[point_index])] = points return l def valid(self, candidate): #lat, long = candidate #return (lat <= 41.861571 and lat >= 41.772414) and (long <= -71.369694 and long >= -71.472667) return self.poly.contains(Point(*candidate)) def most_vulnerable_point(self): l = self.polygons() candidates = [] for center, points in l.items(): farthest = None farthestDistance = 0.0 for p in points: dist = (p[0] - center[0])**2 + (p[1] - center[1])**2 if dist > farthestDistance: farthestDistance = dist farthest = p candidates.append((farthestDistance, farthest)) candidates = sorted(candidates, reverse=True) for candidate in candidates: if self.valid(candidate[1]): return candidate raise Exception("vulnerable point not found") def add_hydrant(self, p): self.vor.add_points([p[1]])
def genNetInput(input_directory, output_directory, cityPoly, polyAmount=100): try: outputFile = open(output_directory, 'w') bounds = cityPoly.bounds minX = bounds[0] minY = bounds[1] maxX = bounds[2] maxY = bounds[3] imgSize = 1000 def convX(x): return (round(0.98 * imgSize * (x - minX) / (maxX - minX)) + imgSize * 0.01) def convY(y): return (round(0.98 * imgSize * (y - minY) / (maxY - minY)) + imgSize * 0.01) scaledCity = shapely.ops.transform(lambda x, y, z=None: (convX(x), convY(y)), cityPoly) def reducedPolySort(reduced): return reduced.y result = open('PyACO/PyClusterize/Temp/district_input.csv', 'w') with open(input_directory, 'r') as inputFile: rdr = csv.reader(inputFile) for r in rdr: result.write(str(r[0]) + ' ' + str(r[1]) + ' 1' + '\n') result.close() os.system('./PyACO/PyClusterize/do_redistrict ' + str(polyAmount) + ' PyACO/PyClusterize/Temp/district_input.csv') rawPolies = genPolies(cityPoly) polies = [] for poly in rawPolies: polies.append( shapely.ops.transform(lambda x, y, z=None: (convX(x), convY(y)), poly)) newPolies = [] for poly in polies: x, y = poly.exterior.coords.xy polyPts = [] for i in range(len(x)): pt = Point(x[i], y[i]) if (scaledCity.contains(pt)): polyPt = [x[i], y[i]] polyPts.append(polyPt) try: newPolies.append(Polygon(polyPts)) except: pass reducedPolies = [] for poly in newPolies: reducedPolies.append(ReducedPoly(poly)) reducedPolies.sort(key=reducedPolySort, reverse=True) outputFile.write('x-coord, y-coord, radius\n') for redPol in reducedPolies: outputFile.write( str(redPol.x) + "," + str(redPol.y) + "," + str(redPol.radius) + '\n') outputFile.close() except Exception as e: print(e)
def get_roi_mask(slide_annotations, element_infos, GTCodes_df, idx_for_roi, iou_thresh=0.0, roiinfo=None, crop_to_roi=True, use_shapely=True, verbose=False, monitorPrefix=""): """Parse annotations and gets a ground truth mask for a single ROI. This will look at all slide annotations and get ones that overlap with the region of interest (ROI) and assigns them to mask. Parameters ----------- slide_annotations : list of dicts response from server request element_infos : pandas DataFrame. The columns annidx and elementidx encode the dict index of annotation document and element, respectively, in the original slide_annotations list of dictionaries. This can be obain by get_bboxes_from_slide_annotations() method GTCodes_df : pandas Dataframe the ground truth codes and information dataframe. WARNING: Modified indide this method so pass a copy. This is a dataframe that is indexed by the annotation group name and has the following columns: - group: group name of annotation (string), eg. mostly_tumor - overlay_order: int, how early to place the annotation in the mask. Larger values means this annotation group is overlayed last and overwrites whatever overlaps it. - GT_code: int, desired ground truth code (in the mask) Pixels of this value belong to corresponding group (class) - is_roi: Flag for whether this group encodes an ROI - is_background_class: Flag, whether this group is the default fill value inside the ROI. For example, you may descide that any pixel inside the ROI is considered stroma. idx_for_roi : int index of ROI within the element_infos dataframe. iou_thresh : float how much bounding box overlap is enough to consider an annotation to belong to the region of interest roiinfo : pandas series or dict contains information about the roi. Keys will be added to this index containing info about the roi like bounding box location and size. crop_to_roi : bool flag of whether to crop polygons to roi (prevent overflow beyond roi edge) use_shapely : bool flag of whether to precisely determine whether an element belongs to an ROI using shapely polygons. Slightly slower. If set to False, overlapping bounding box is used as a cheap but less precise indicator of inclusion. verbose : bool Print progress to screen? monitorPrefix : str text to prepend to printed statements Returns -------- Np array (N x 2), where pixel values encode class membership. IMPORTANT NOTE: Zero pixels have special meaning and do NOT encode specific ground truth class. Instead, they simply mean Outside ROI and should be IGNORED during model training or evaluation. Dict information about ROI """ # This stores information about the ROI like bounds, slide_name, etc # Allows passing many parameters and good forward/backward compatibility if roiinfo is None: roiinfo = dict() # isolate annotations that potentially overlap (belong to) mask (incl. ROI) overlaps = get_idxs_for_annots_overlapping_roi_by_bbox( element_infos, idx_for_roi=idx_for_roi, iou_thresh=iou_thresh) idxs_for_all_rois = _get_idxs_for_all_rois(GTCodes=GTCodes_df, element_infos=element_infos) overlaps = list(set(overlaps) - set(idxs_for_all_rois)) elinfos_roi = element_infos.loc[[ idx_for_roi, ] + overlaps, :] # Add roiinfo roiinfo['XMIN'] = int(np.min(elinfos_roi.xmin)) roiinfo['YMIN'] = int(np.min(elinfos_roi.ymin)) roiinfo['XMAX'] = int(np.max(elinfos_roi.xmax)) roiinfo['YMAX'] = int(np.max(elinfos_roi.ymax)) roiinfo['BBOX_WIDTH'] = roiinfo['XMAX'] - roiinfo['XMIN'] roiinfo['BBOX_HEIGHT'] = roiinfo['YMAX'] - roiinfo['YMIN'] # get roi polygon if use_shapely: coords, _ = _get_element_mask(elinfo=elinfos_roi.loc[idx_for_roi], slide_annotations=slide_annotations) roi_polygon = Polygon(coords) # Init mask ROI = np.zeros((roiinfo['BBOX_HEIGHT'], roiinfo['BBOX_WIDTH']), dtype=np.uint8) # only parse if roi is polygonal or rectangular assert elinfos_roi.loc[idx_for_roi, 'type'] != 'point' # make sure ROI is overlayed first & assigned background class if relevant roi_group = elinfos_roi.loc[idx_for_roi, 'group'] GTCodes_df.loc[roi_group, 'overlay_order'] = np.min( GTCodes_df.loc[:, 'overlay_order']) - 1 bck_classes = GTCodes_df.loc[GTCodes_df.loc[:, 'is_background_class'] == 1, :] if bck_classes.shape[0] > 0: GTCodes_df.loc[roi_group, 'GT_code'] = bck_classes.iloc[0, :]['GT_code'] # Add annotations in overlay order overlay_orders = sorted(set(GTCodes_df.loc[:, 'overlay_order'])) N_elements = elinfos_roi.shape[0] elNo = 0 for overlay_level in overlay_orders: # get indices of relevant groups relevant_groups = list( GTCodes_df.loc[GTCodes_df.loc[:, 'overlay_order'] == overlay_level, 'group']) relIdxs = [] for group_name in relevant_groups: relIdxs.extend( list( elinfos_roi.loc[elinfos_roi.group == group_name, :].index)) # get relevnt infos and sort from largest to smallest (by bbox area) # so that the smaller elements are layered last. This helps partially # address issues describe in: # https://github.com/DigitalSlideArchive/HistomicsTK/issues/675 elinfos_relevant = elinfos_roi.loc[relIdxs, :].copy() elinfos_relevant.sort_values('bbox_area', axis=0, ascending=False, inplace=True) # Go through elements and add to ROI mask for elId, elinfo in elinfos_relevant.iterrows(): elNo += 1 elcountStr = "%s: Overlay level %d: Element %d of %d: %s" % ( monitorPrefix, overlay_level, elNo, N_elements, elinfo['group']) if verbose: print(elcountStr) # now add element to ROI ROI = _get_and_add_element_to_roi( elinfo=elinfo, slide_annotations=slide_annotations, ROI=ROI, roiinfo=roiinfo, roi_polygon=roi_polygon, GT_code=GTCodes_df.loc[elinfo['group'], 'GT_code'], use_shapely=use_shapely, verbose=verbose, monitorPrefix=elcountStr) # save a copy of ROI-only mask to crop to it later if needed if crop_to_roi and (overlay_level == GTCodes_df.loc[roi_group, 'overlay_order']): roi_only_mask = ROI.copy() # Crop polygons to roi if needed (prevent 'overflow' beyond roi edge) if crop_to_roi: ROI[roi_only_mask == 0] = 0 # tighten boundary --remember, so far we've use element bboxes to # make an over-estimated margin around ROI boundary. nz = np.nonzero(ROI) ymin, xmin = [np.min(arr) for arr in nz] ymax, xmax = [np.max(arr) for arr in nz] ROI = ROI[ymin:ymax, xmin:xmax] # update roi offset roiinfo['XMIN'] += xmin roiinfo['YMIN'] += ymin roiinfo['XMAX'] += xmin roiinfo['YMAX'] += ymin roiinfo['BBOX_WIDTH'] = roiinfo['XMAX'] - roiinfo['XMIN'] roiinfo['BBOX_HEIGHT'] = roiinfo['YMAX'] - roiinfo['YMIN'] return ROI, roiinfo
def coords_to_ccg(lng, lat): """ takes coordinates and returns ccg name and code """ import json import operator import ast from shapely.geometry import Point from shapely.geometry.polygon import Polygon fd = open("ccg_kml_dicts") sd = fd.read() fd.close() ldData = ast.literal_eval(sd) lNames = [ldData[i]['CCG'] for i in range(len(ldData))] lCodes = [ldData[i]['CCG Code'] for i in range(len(ldData))] lBoundaries = [ldData[i]['boundary'] for i in range(len(ldData))] ldUnsorted = [] #clean up boundaries for i in range(len(lBoundaries)): lBoundaries[i] = lBoundaries[i].split("\n") lx = [] ly = [] for j in range(len(lBoundaries[i])): lBoundaries[i][j] = lBoundaries[i][j].strip() lx.append(float(lBoundaries[i][j].split(",")[0])) lx.append(float(lBoundaries[i][j].split(",")[0])) ly.append(float(lBoundaries[i][j].split(",")[1])) ly.append(float(lBoundaries[i][j].split(",")[1])) lBoundaries[i][j] = (float(lBoundaries[i][j].split(",")[0]), float(lBoundaries[i][j].split(",")[1])) fXmid = ((min(lx))+(max(lx)))/2 fYmid = ((min(ly))+(max(ly)))/2 ldUnsorted.append({ "ccg_name": lNames[i], "dist": ((fXmid - lat)**2 + (fYmid - lng)**2)**0.5, "boundary": lBoundaries[i], "ccg_code": lCodes[i] }) #order all boundaries by how close they are to the lat lng ldSorted = sorted(ldUnsorted, key=lambda t:t['dist']) lNamesSorted = [ldSorted[i]["ccg_name"] for i in range(len(ldSorted))] lCodesSorted = [ldSorted[i]["ccg_code"] for i in range(len(ldSorted))] lBoundariesSorted = [ldSorted[i]["boundary"] for i in range(len(ldSorted))] for iB in range(len(lBoundariesSorted)): coordPoint = Point(lat, lng) coordPolygon = Polygon(lBoundariesSorted[iB]) if coordPolygon.contains(coordPoint): sName = lNamesSorted[iB] sCode = lCodesSorted[iB] return sName,sCode return None,None
#Allow the user the option to scale their entered shape Scale = int( input("What scale factor would you like to scale the shape to? ")) #Creating new scaling variables threeInputXOneNew = threeInputXOne * Scale threeInputXTwoNew = threeInputXTwo * Scale threeInputXThreeNew = threeInputXThree * Scale threeInputYOneNew = threeInputYOne * Scale threeInputYTwoNew = threeInputYTwo * Scale threeInputYThreeNew = threeInputYThree * Scale #Using the new variables and matplotlib to display the final result poly = Polygon([(threeInputXOneNew, threeInputYOneNew), (threeInputXTwoNew, threeInputYTwoNew), (threeInputXThreeNew, threeInputYThreeNew)]) x, y = poly.exterior.xy fig = plt.figure(1, figsize=(5, 5), dpi=90) ax = fig.add_subplot(111) ax.plot(x, y, color='#6699cc', alpha=0.7, linewidth=3, solid_capstyle='round', zorder=2) ax.set_title('Shape') plt.show()
import plotly.offline as py_off from plotly.graph_objs import * import cmocean # Some parameters lonLims = [4, 18] # Khanh boxes latLims = [58, 71] # Box 1 lat1 = [58.23, 59.23] lon1 = [4, 5] x1 = lon1[0] x2 = lon1[1] y1 = lat1[0] y2 = lat1[1] polygon1 = Polygon([[x1, y1], [x1, y2], [x2, y2], [x2, y1]]) # Box2 lat2 = [69.78, 70.5] lon2 = [17.5, 18] xx1 = lon2[0] xx2 = lon2[1] yy1 = lat2[0] yy2 = lat2[1] polygon2 = Polygon([[xx1, yy1], [xx1, yy2], [xx2, yy2], [xx2, yy1]]) ## --------- Get SSTs -------- #### # Load ESA SST #ds = xr.open_dataset('/home/cyrf0006/data/SST_ESACCI/ESACCI-GLO-SST-L4-REP-OBS-SST_1570728723070.nc') # Load NOAA SST #ds = xr.open_mfdataset('/home/cyrf0006/data/NOAA_SST/sst.day.mean*.nc', combine='by_coords') print('load mfdataset ...')
def polygon(self): return Polygon([self.top_left, self.top_right, self.bottom_right, self.bottom_left])
class Labeler: """ Class to create, label & process subframes for the task of recognizing charge transitions. All maps located in subfolders of folder_path are labeled, processed and saved in output_folder as a pickle file. Workflow Example ---------------- label = Labeler(path_to_folder, path_out, p, f) while(label.next_file()): label.create_shapes() label.create_frames(10) label.plotter() label.save_frames() Output ------ The output files saved in output_folder are structured as follows. path: output_folder/m/m_n.pkl where: m: map number (~name) n: augmentation number (0 = identity) File structure: The file is a pickled pandas.DataFrame Columns: frame : np.array(), np.shape(f+2p, f+2p) processed subframes. "data" with which the models will be learned label : list list of tuples indicating a charge transition for each dot for the three corners of the frame. [(1, 0), (1,1), (0,1)] right-top_right-left is_feas : int indicates wheter or not frame is in feasible reagion. 1: feasible region 0: not feasible region """ def __init__(self, folder_path, output_folder, p, f): """ Parameters ---------- folder_path : os.path object folder where files are read output_folder : os.path object folder where files are saved p : int padding f : int frame size """ self.folderpaths = [ os.path.normpath((os.path.join(folder_path, x))) for x in os.listdir(folder_path) if not x.endswith('.gitkeep') ] self.output_folder = output_folder # list of data files for last folderpath self.filepaths = None self.files_L = None self.files_R = None self.files_B = None self.files_border = None self.filename = None # contains data to plot self.raw_data = None self.data = None # DataFrame containing all coordinates for Left/Right dot charge transitions # created by get_files() self.L = None self.R = None self.B = None self.border = None self.boundaries = None # list of shapely objects for Left/Right dot charge transitions # created by create_shapes() self.L_transitions = None self.R_transitions = None self.n_regions = None # define frame size self.p = p self.f = f self.frames = None # define plot instance self.ax = None def create_shapes(self): """ Given self.L & self.R, creates shapely objects for dot transition. Those objects are saved in self.L_transitions & self.R_transitions. Additionally if a non feasible region is defined, a Polygon is created defining this non feasible region. The Polygon is stored in self.region """ self.L_transitions = [] self.R_transitions = [] self.n_regions = [] for s in range(len(self.L['Lx'])): tuples = [(self.L['Lx'][s][k], self.L['Ly'][s][k]) for k in range(len(self.L['Lx'][s]))] self.L_transitions.append(LineString(tuples)) for s in range(len(self.R['Rx'])): tuples = [(self.R['Rx'][s][k], self.R['Ry'][s][k]) for k in range(len(self.R['Rx'][s]))] self.R_transitions.append(LineString(tuples)) for s in range(len(self.B['Bx'])): tuples = [(self.B['Bx'][s][k], self.B['By'][s][k]) for k in range(len(self.B['Bx'][s]))] self.n_regions.append(Polygon(tuples)) # create polygon within frames can be drawn self.boundaries = Polygon([(self.border['x'][0], self.border['y'][0]), (self.border['x'][1], self.border['y'][1]), (self.border['x'][2], self.border['y'][2]), (self.border['x'][3], self.border['y'][3])]) def create_frames(self, rand): """ Creates self.frames : a list of tuples (frames, labels) Example: [(x_0, label_0, is_feas_0, shapes_0), (x_1, label_1, is_feas_1, shapes_1), ...] with x_i: np.array() <- shape(n, n) label_i: list of tuples indicating a charge transition for each dot for the three corners of the frame. [(1, 0), (1,1), (0,1)] right-top_right-left is_feas_i: int indicates wheter or not frame is in feasible reagion. 1: feasible region 0: not feasible region shapes_i: shapely shapes of the frame boundaries for plotting reasons It is n = f + 2p Parameters ---------- rand : int randomness, the acutal frame will deviate from the grid by a random number drawn from [-rand/2, rand/2] """ w, h = np.shape(self.data) self.frames = [] if w > 300: x_v = np.arange(int(w / 6), int(5 * w / 6), self.f) y_v = np.arange(int(h / 6), int(5 * h / 6), self.f) else: x_v = np.arange(self.p, w - self.p, self.f) y_v = np.arange(self.p, h - self.p, self.f) x_p, y_p = np.meshgrid(x_v, y_v) for (x_g, y_g) in np.nditer((x_p, y_p)): x = int(x_g + randint(-rand / 2, rand / 2)) y = int(y_g + randint(-rand / 2, rand / 2)) # generate corners of the frames including padding tl = Point(x - self.p, y - self.p) tr = Point(x + self.p + self.f + 1, y - self.p) bl = Point(x - self.p, y + self.p + self.f + 1) br = Point(x + self.p + self.f + 1, y + self.p + self.f + 1) # check whether frame lies within data if all(self.boundaries.contains(l) for l in [tl, tr, bl, br]): # create the data frame raw_frame = self.raw_data[y - self.p:y + self.p + self.f + 1, x - self.p:x + self.p + self.f + 1] # reorder the points. The ordering is wrong due to how the measurement is taken raw_frame = raw_frame[::-1, :] if np.all(raw_frame == 0): print('x: {}, y: {}\n' 'shape: {}'.format(x, y, np.shape(raw_frame))) print(self.filepaths[0]) frame = preprocessor(raw_frame) # create shapely lines top = LineString([(x, y), (x + self.f, y)]) left = LineString([(x, y), (x, y + self.f)]) right = LineString([(x + self.f, y), (x + self.f, y + self.f)]) bottom = LineString([(x, y + self.f), (x + self.f, y + self.f)]) label = [] # label the frames label.append( (any(l.crosses(bottom) for l in self.L_transitions), any(l.crosses(bottom) for l in self.R_transitions))) # following line needs review label.append((any( l.crosses(bottom) or l.crosses(right) for l in self.L_transitions), any( l.crosses(bottom) or l.crosses(right) for l in self.R_transitions))) label.append( (any(l.crosses(left) for l in self.L_transitions), any(l.crosses(left) for l in self.R_transitions))) if self.n_regions is None: is_feas = True else: is_feas = all( l.disjoint(k) for l in [top, left, right, bottom] for k in self.n_regions) self.frames.append( [frame, label, is_feas, (top, left, right, bottom)]) def plotter(self, marks=True): """ Plots shapely lines, processed data plus the drawn frames including labels. Parameters ---------- marks: bool If true, the charge indication lines are plotted as well. """ fig, self.ax = plt.subplots() # plot marks / shapely lines x_axis = np.arange(0, np.shape(self.data)[0]) y_axis = np.arange(0, np.shape(self.data)[1]) self.ax.set_ylim(np.shape(self.data)[1] - 1, 0) self.ax.set_xlim(0, np.shape(self.data)[0] - 1) norm = colors.Normalize(vmin=-3, vmax=3) self.ax.pcolormesh(x_axis, y_axis, self.data, norm=norm) if marks: for k in range(len(self.L_transitions)): x, y = self.L_transitions[k].xy self.ax.plot(x, y, color='red') for k in range(len(self.R_transitions)): x, y = self.R_transitions[k].xy self.ax.plot(x, y, color='blue') for k in range(len(self.n_regions)): if not self.n_regions[0].area == 0: x, y = self.n_regions[k].boundary.xy self.ax.plot(x, y, color='brown') if not self.border.empty: x, y = self.boundaries.boundary.xy self.ax.plot(x, y, color='black') # plot frames x_0 = None y_0 = None for frame in self.frames: for k, line in enumerate(frame[3]): x, y = line.xy self.ax.plot(x, y, color='black') if k == 0: x_0 = x[0] y_0 = y[0] plt.annotate('({},{})'.format(int(frame[1][0][0]), int(frame[1][0][1])), (x_0 + self.f, y_0 + self.f), fontsize=8) plt.annotate('({},{})'.format(int(frame[1][1][0]), int(frame[1][1][1])), (x_0 + self.f, y_0), fontsize=8) plt.annotate('({},{})'.format(int(frame[1][2][0]), int(frame[1][2][1])), (x_0, y_0), fontsize=8) plt.annotate(int(frame[2]), (x_0, y_0 + self.f), fontsize=8) filename = os.path.split(self.filepaths[0])[-1] filename = filename.split('.')[0] plt.title(filename) plt.draw() def save_frames(self): """ Saves the drawn frames from one map as a pandas.DataFrame object which is pickled. Thereby, the data from every frame is rescaled. columns: frames, label, is_feas """ np_frames = np.array(self.frames) df_frames = pd.DataFrame() df_frames['frames'] = np_frames[:, 0] df_frames['label'] = np_frames[:, 1] df_frames['is_feas'] = np_frames[:, 2] filename = os.path.split(self.filepaths[0])[-1] filename = filename.split('.')[0] filename = filename.split('_') folder_path = os.path.join(self.output_folder, filename[0]) if not os.path.exists(folder_path): os.makedirs(folder_path) file_path = os.path.join(folder_path, '{}_{}.pkl'.format(filename[0], filename[1])) df_frames.to_pickle(file_path) def next_file(self): """ Reads the next map from input_folder. """ if self.filepaths is not None: self.filepaths = self.filepaths[1:] self.files_B = self.files_B[1:] self.files_L = self.files_L[1:] self.files_R = self.files_R[1:] self.files_border = self.files_border[1:] if self.filepaths == [] or self.filepaths is None: if self.filepaths is not None: self.folderpaths = self.folderpaths[1:] if not self.folderpaths: # check whether list folderpaths is empty print('All files processed') return False else: self.filepaths = [(os.path.join(self.folderpaths[0], x)) for x in os.listdir(self.folderpaths[0]) if x.endswith('_data.pkl')] self.files_L = [ os.path.join(self.folderpaths[0], x) for x in os.listdir(self.folderpaths[0]) if x.endswith('_L.pkl') ] self.files_R = [ os.path.join(self.folderpaths[0], x) for x in os.listdir(self.folderpaths[0]) if x.endswith('_R.pkl') ] self.files_B = [ os.path.join(self.folderpaths[0], x) for x in os.listdir(self.folderpaths[0]) if x.endswith('_B.pkl') ] self.files_border = [ os.path.join(self.folderpaths[0], x) for x in os.listdir(self.folderpaths[0]) if x.endswith('_border.pkl') ] # Read files self.L = pd.read_pickle(self.files_L[0]) self.R = pd.read_pickle(self.files_R[0]) self.B = pd.read_pickle(self.files_B[0]) self.border = pd.read_pickle(self.files_border[0]) # read raw data self.raw_data = pd.read_pickle(self.filepaths[0]) # process data for plotting purpose self.raw_data = np.array(self.raw_data) self.data = subtract_median(x_y_derivator(self.raw_data)) return True
def __init__(self, cornerCoord): self.__xMin = -1 * cornerCoord self.__xMax = cornerCoord self.__yMin = -1 * cornerCoord self.__yMax = cornerCoord self.__y_diff = cornerCoord - int(cornerCoord / (2 * math.sqrt(3))) self.radii = int(cornerCoord / 3 * math.sqrt(2)) self.__shapes["Online"].update({ "polygon": Polygon([(self.__xMax, self.__yMax), (0, self.__yMax), (0, 0), (self.__xMax, self.__y_diff)]), "point": Point(self.__xMax, self.__yMax), "txtpos": Point(self.__xMax - 4.5, self.__yMax + 0.5) }) self.__shapes["BranchLeft"].update({ "polygon": Polygon([(self.__xMin, self.__y_diff), (0, 0), (0, self.__yMax), (self.__xMin, self.__yMax)]), "point": Point(self.__xMin, self.__yMax), "txtpos": Point(self.__xMin + 1, self.__yMax + 0.5) }) self.__shapes["Perp"].update({ "polygon": Polygon([(self.__xMin, -1 * self.__y_diff), (0, 0), (self.__xMin, self.__y_diff)]), "point": Point(self.__xMin, 0), "txtpos": Point(self.__xMin + 1, 0) }) self.__shapes["Lost"].update({ "polygon": Polygon([(0, self.__yMin), (0, 0), (self.__xMin, -1 * self.__y_diff), (self.__xMin, self.__yMin)]), "point": Point(self.__xMin, self.__yMin), "txtpos": Point(self.__xMin + 1, self.__yMin - 1.5) }) self.__shapes["Fork"].update({ "polygon": Polygon([(self.__xMax, -1 * self.__y_diff), (0, 0), (0, self.__yMin), (self.__xMax, self.__yMin)]), "point": Point(self.__xMax, self.__yMin), "txtpos": Point(self.__xMax - 3, self.__yMin - 1.5) }) self.__shapes["BranchRight"].update({ "polygon": Polygon([(self.__xMax, self.__y_diff), (0, 0), (self.__xMax, -1 * self.__y_diff)]), "point": Point(self.__xMax, 0), "txtpos": Point(self.__xMax + 1, 0) }) self.__populatePoints()
def Seaangles_mod(numviews, thresholdimg): ''' From Seaangles_mod.m Takes an image and extracts its Opening Angle Map Returns shorelines and shallowsea???? ''' Sx, Sy = np.gradient(thresholdimg) G = np.sqrt(Sx**2 + Sy**2) edges = (G > 0) & (thresholdimg > 0) bordermap = np.pad(np.zeros_like(edges), 1, 'edge') bordermap[:-2, 1:-1] = edges bordermap[0, :] = 1 points = np.fliplr(np.array(np.where(edges > 0)).T) hull = ConvexHull(points, qhull_options='Qc') sea = np.fliplr(np.array(np.where(thresholdimg > 0.5)).T) points_to_test = [Point(i[0], i[1]) for i in sea] polygon = Polygon(points[hull.vertices]).buffer(0.01) In = np.array(map(lambda pt: polygon.contains(pt), points_to_test)) Shallowsea_ = sea[In] seamap = np.zeros(bordermap.shape) flat_indices = map(lambda x: np.ravel_multi_index(x, seamap.shape), np.fliplr(Shallowsea_)) seamap.flat[flat_indices] = 1 seamap[:3, :] = 0 Deepsea_ = sea[~In] Deepsea = np.zeros((7, len(Deepsea_))) Deepsea[:2, :] = np.flipud(Deepsea_.T) Deepsea[-1, :] = 200. # where does this 200 come from? Shallowsea = np.array(np.where(seamap > 0.5)) shoreandborder = np.array(np.where(bordermap > 0.5)) c1 = len(Shallowsea[0]) c2 = len(shoreandborder[0]) maxtheta = np.zeros((numviews, c1)) for i in range(c1): diff = shoreandborder - Shallowsea[:, i, np.newaxis] x = diff[0] y = diff[1] angles = np.arctan2(x, y) angles = np.sort(angles) * 180. / np.pi dangles = angles[1:] - angles[:-1] dangles = np.concatenate( (dangles, [360 - (angles.max() - angles.min())])) dangles = np.sort(dangles) maxtheta[:, i] = dangles[-numviews:] allshore = np.array(np.where(edges > 0)) c3 = len(allshore[0]) maxthetashore = np.zeros((numviews, c3)) for i in range(c3): diff = shoreandborder - allshore[:, i, np.newaxis] x = diff[0] y = diff[1] angles = np.arctan2(x, y) angles = np.sort(angles) * 180. / np.pi dangles = angles[1:] - angles[:-1] dangles = np.concatenate( (dangles, [360 - (angles.max() - angles.min())])) dangles = np.sort(dangles) maxthetashore[:, i] = dangles[-numviews:] waves1 = np.vstack([ np.hstack([Shallowsea, Deepsea[:2, :]]), np.hstack([maxtheta.sum(axis=0), Deepsea[-1, :]]) ]) waves1s = sparse.csr_matrix((waves1[2, :], (waves1[0, :], waves1[1, :])), shape=thresholdimg.shape) shoreline = np.vstack([allshore, maxthetashore.sum(axis=0)]) picshore = sparse.csr_matrix( (shoreline[2, :], (shoreline[0, :], shoreline[1, :])), shape=thresholdimg.shape) shoreangles = np.vstack([allshore, maxthetashore]) seaangles = np.hstack([np.vstack([Shallowsea, maxtheta]), Deepsea]) return shoreangles, waves1s, seaangles, picshore
def points_in_poly(vertices, s, sig, name, lat, lon, m, d, x0, x1, save, days): ''' finds points in a polygon from vertices plots polygon on stressdrop map plots stressdrop versus time and stressdrop vs mag/depth Input: vertices: array of vertex points of polygon s: list/array of event log stress drops sig: list/array of event log stress drops sigma name: string name of polygon lat: list/array of event latitudes lon: list/array of event longitudes m: list/array of event magnitudes d: list/array of event depths (km) x0: utc datetime, first in range for plotting x1: utc datetime, first in range for plotting save: True or False to save Output: plots 2 scatter plots of stress drop vs mag and stress drop versus depth ''' import matplotlib.pyplot as plt import matplotlib as mpl from shapely.geometry.polygon import Polygon import matplotlib.path as mpltPath import cartopy.crs as ccrs import cartopy.io.img_tiles as cimgt poly = Polygon(vertices) x,y = poly.exterior.xy cmap = mpl.cm.get_cmap('gist_rainbow') normalize = mpl.colors.Normalize(vmin=min(s), vmax=max(s)) colors = [cmap(normalize(value)) for value in s] s_m = mpl.cm.ScalarMappable(cmap = cmap, norm=normalize) s_m.set_array([]) stamen_terrain = cimgt.StamenTerrain(desired_tile_form="L") fig = plt.figure(figsize = (18,16)) ax = plt.axes(projection=stamen_terrain.crs) ax.set_extent([-118.01, -114.99, 32.29, 34.41]) ax.add_image(stamen_terrain, 10, alpha = 0.5, cmap = 'gray') plt.tick_params(axis='both', which='major', labelsize=18) plt.tick_params(axis='both', which='both', length = 5, width = 1) xticks = [-118, -117, -116, -115] yticks = [32.3, 33, 33.5, 34, 34.4] ax.gridlines(xlocs=xticks, ylocs=yticks, draw_labels = True) for segment_i in range(len(fault_segments)): fault=fault_segments[segment_i] fault_z=np.zeros(len(fault)) ax.plot(fault[:,0],fault[:,1],fault_z,color='k', transform=ccrs.Geodetic()) plt.scatter(lon, lat, marker='o', color= colors, s=25, alpha=1.0, transform=ccrs.Geodetic()) plt.plot(x, y, color = 'blue', transform=ccrs.Geodetic()) # geodetic_transform = ccrs.Geodetic()._as_mpl_transform(ax) fig.subplots_adjust(right=0.88) cbar_ax = fig.add_axes([0.92, 0.15, 0.02, 0.75]) cbar = plt.colorbar(s_m, cax=cbar_ax) cbar.set_label(ur"stressdrop", fontsize = 22)#"$azimuth$ (\u00b0)" cbar.ax.tick_params(labelsize = 18) plt.savefig(top_dir + '/source_params/polygon/polygon_' + name + '.png') plt.show() points = zip(lon,lat) path = mpltPath.Path(vertices) inside = path.contains_points(points) ev = [] t = [] s = [] m = [] sig = [] d = [] for i in range(len(inside)): if inside[i] == True: x = (sobj.evid[i]).split('_') x = (datetime(int(x[0]), int(x[1]), int(x[2]), int(x[3]), int(x[4]), int(x[5]))) #if in time range if (x >= x0) and (x <= x1): x = (sobj.evid[i]).split('_') ev.append(sobj.evid[i]) t.append(datetime(int(x[0]), int(x[1]), int(x[2]), int(x[3]), int(x[4]), int(x[5]))) s.append(sobj.log_stressdrop[i]) m.append(sobj.m_cat[i]) sig.append(sobj.log_stressdrop_sig[i]) d.append(sobj.depth[i]) stressdrop_vs_time(ev = ev, lat = lat, lon = lon, s = s, sig = sig, m = m, name = name, x0 = x0, x1 = x1, save = save, days = days, regions = False) stressdrop_vs_time(ev = ev, lat = lat, lon = lon, s = s, sig = sig, m = m, name = name + '_entire_window', x0 = datetime(2010, 01, 01, 0, 0, 0), x1 = datetime(2017, 01, 01, 0, 0, 0), save = save, days = 90, regions = False) stressdrop_vs_depth_mag(s = s, d = d, mag = m, sig = sig, name = name, save = save)
def stressdrop_map(s, lat, lon, save, regions): ''' Maps stress drops for all event on map Input: s: list/array of log stressdrops lat: list/array of event latitudes lon: list/array of event longitudes save: yes or no to save figure Output: plots a map ''' import matplotlib.pyplot as plt import matplotlib as mpl import cartopy.crs as ccrs import cartopy.io.img_tiles as cimgt cmap = mpl.cm.get_cmap('gist_rainbow') normalize = mpl.colors.Normalize(vmin=min(s), vmax=max(s)) colors = [cmap(normalize(value)) for value in s] s_m = mpl.cm.ScalarMappable(cmap = cmap, norm=normalize) s_m.set_array([]) stamen_terrain = cimgt.StamenTerrain(desired_tile_form="L") fig = plt.figure(figsize = (18,16)) ax = plt.axes(projection=stamen_terrain.crs) ax.set_extent([-118.01, -114.99, 32.29, 34.41]) ax.add_image(stamen_terrain, 10, alpha = 0.5, cmap = 'gray') plt.tick_params(axis='both', which='major', labelsize=18) plt.tick_params(axis='both', which='both', length = 5, width = 1) xticks = [-118, -117, -116, -115] yticks = [32.3, 33, 33.5, 34, 34.4] ax.gridlines(xlocs=xticks, ylocs=yticks, draw_labels = True) for segment_i in range(len(fault_segments)): fault=fault_segments[segment_i] fault_z=np.zeros(len(fault)) ax.plot(fault[:,0],fault[:,1],fault_z,color='k', transform=ccrs.Geodetic()) plt.scatter(lon, lat, marker='o', color= colors, s=30, alpha=1.0, transform=ccrs.Geodetic()) # geodetic_transform = ccrs.Geodetic()._as_mpl_transform(ax) #if regions is True, add polygons if regions == True: for i in range(len(vertex_list)): poly = Polygon(vertex_list[i]) x,y = poly.exterior.xy plt.plot(x,y, color = color[i], transform=ccrs.Geodetic(), label = labels[i], lw = 2) plt.legend(loc=(0.1,-0.15),ncol=4) fig.subplots_adjust(right=0.88) cbar_ax = fig.add_axes([0.92, 0.15, 0.02, 0.75]) cbar = plt.colorbar(s_m, cax=cbar_ax) cbar.set_label(ur"stressdrop", fontsize = 22)#"$azimuth$ (\u00b0)" cbar.ax.tick_params(labelsize = 18) plt.show() if save == True: plt.savefig(top_dir + '/source_params/stressdrop_map_regions.png')
def plot_heatmap(prefix, feature='asymmetry1', vmin=0, vmax=1, resolution=512, rows=4, cols=4, upload=True, dpi=None, figsize=(12, 10), remote_folder = "01_18_Experiment", bucket='ccurtis.data'): """ Plot heatmap of trajectories in video with colors corresponding to features. Parameters ---------- prefix: string Prefix of file name to be plotted e.g. features_P1.csv prefix is P1. feature: string Feature to be plotted. See features_analysis.py vmin: float64 Lower intensity bound for heatmap. vmax: float64 Upper intensity bound for heatmap. resolution: int Resolution of base image. Only needed to calculate bounds of image. rows: int Rows of base images used to build tiled image. cols: int Columns of base images used to build tiled images. upload: boolean True if you want to upload to s3. dpi: int Desired dpi of output image. figsize: list Desired dimensions of output image. Returns ------- """ # Inputs # ---------- merged_ft = pd.read_csv('features_{}.csv'.format(prefix)) string = feature leveler = merged_ft[string] t_min = vmin t_max = vmax ires = resolution # Building points and color schemes # ---------- zs = ma.masked_invalid(merged_ft[string]) zs = ma.masked_where(zs <= t_min, zs) zs = ma.masked_where(zs >= t_max, zs) to_mask = ma.getmask(zs) zs = ma.compressed(zs) xs = ma.compressed(ma.masked_where(to_mask, merged_ft['X'].astype(int))) ys = ma.compressed(ma.masked_where(to_mask, merged_ft['Y'].astype(int))) points = np.zeros((xs.shape[0], 2)) points[:, 0] = xs points[:, 1] = ys vor = Voronoi(points) # Plot # ---------- fig = plt.figure(figsize=figsize, dpi=dpi) regions, vertices = voronoi_finite_polygons_2d(vor) my_map = cm.get_cmap('viridis') norm = mpl.colors.Normalize(t_min, t_max, clip=True) mapper = cm.ScalarMappable(norm=norm, cmap=cm.viridis) test = 0 p2 = 0 counter = 0 for i in range(0, points.shape[0]-1): try: polygon = vertices[regions[p2]] point1 = Point(points[test, :]) poly1 = Polygon(polygon) check = poly1.contains(point1) if check: plt.fill(*zip(*polygon), color=my_map(norm(zs[test])), alpha=0.7) p2 = p2 + 1 test = test + 1 else: p2 = p2 test = test + 1 except IndexError: print('Index mismatch possible.') mapper.set_array(10) plt.colorbar(mapper) plt.xlim(0, ires*cols) plt.ylim(0, ires*rows) plt.axis('off') print('Plotted {} heatmap successfully.'.format(prefix)) outfile = 'hm_{}_{}.png'.format(feature, prefix) fig.savefig(outfile, bbox_inches='tight') if upload == True: aws.upload_s3(outfile, remote_folder+'/'+outfile, bucket_name=bucket)
def cleanVision(self,obstaclesInView): forbiddenAreas = [] # on ajoute les obstacles 1 à 1 à la liste, seulement s'ils sont bien visibles # on détermine, en regardant tous les obstacles, un ensemble de zones rectangulaires dans lesquelles ne peuvent pas se trouver des obstacles visibles for wall in obstaclesInView.keys(): ### 1: si on est face à un mur, on ne peut pas voir ce qu'il y à derrière ### 2: si on voit un coin de mur, ça cache une partie de la pièce en plus # à droite d'un mur vertical if wall.orientation == 'v': if self.x > max(wall.Xs): if min(wall.Ys) <= self.y <= max(wall.Ys): # 1: rectangle forbiddenAreas.append([(0,min(wall.Ys)), (max(wall.Xs)-1,min(wall.Ys)), (max(wall.Xs)-1,max(wall.Ys)), (0,max(wall.Ys))]) # 2: triangles ou trapèzes, on trace la droite qui relie le robot avec le coin et on voit où elle intersecte le bord de la fenêtre cx, cy = max(wall.Xs),min(wall.Ys) if np.sqrt((cx-self.x)**2+(cy-self.y)**2) < self.radiusDetection: # si le coin est vu par le robot if cy < self.y: # coin au dessus du robot res_top = linesIntersect([(self.x,self.y),(cx,cy)],[(0,0),(self.room.width,0)]) if res_top != None: resX, resY = res_top[0], res_top[1] if resX > 0: # trapèze forbiddenAreas.append([(0,0),(resX,0),(cx,cy),(0,cy)]) else: # triangle res_left = linesIntersect([(self.x,self.y),(cx,cy)],[(0,0),(0,self.room.height)]) if res_left != None: resX, resY = res_left[0], res_left[1] forbiddenAreas.append([(0,resY),(cx,cy),(0,cy)]) cx, cy = max(wall.Xs),max(wall.Ys) if np.sqrt((cx-self.x)**2+(cy-self.y)**2) < self.radiusDetection: # si le coin est vu par le robot if cy > self.y: # coin en dessous du robot res_bot = linesIntersect([(self.x,self.y),(cx,cy)],[(0,self.room.height),(self.room.width,self.room.height)]) if res_bot != None: resX, resY = res_bot[0], res_bot[1] if resX > 0: # trapèze forbiddenAreas.append([(0,self.room.height),(resX,self.room.height),(cx,cy),(0,cy)]) else: # triangle res_left = linesIntersect([(self.x,self.y),(cx,cy)],[(0,0),(0,self.room.height)]) if res_left != None: resX, resY = res_left[0], res_left[1] forbiddenAreas.append([(0,resY),(cx,cy),(0,cy)]) # cas supplémentaires avec les coins, quand le robot n'est pas en face du mur elif self.y < min(wall.Ys): # robot au dessus du mur cx, cy = min(wall.Xs),min(wall.Ys) if np.sqrt((cx-self.x)**2+(cy-self.y)**2) < self.radiusDetection + wall.width: # si le coin est vu par le robot res_left = linesIntersect([(self.x,self.y),(cx,cy)],[(0,0),(0,self.room.height)]) if res_left != None: resX, resY = res_left[0], res_left[1] if cy <= resY < self.room.height: # trapèze forbiddenAreas.append([(0,resY),(0,self.room.height),(cx,self.room.height),(cx,cy)]) else: # triangle res_bot = linesIntersect([(self.x,self.y),(cx,cy)],[(0,self.room.height),(self.room.width,self.room.height)]) if res_bot != None: resX, resY = res_bot[0], res_bot[1] forbiddenAreas.append([(resX,self.room.height),(cx,cy),(cx,self.room.height)]) elif self.y > max(wall.Ys): # robot en dessous du mur cx, cy = min(wall.Xs),max(wall.Ys) if np.sqrt((cx-self.x)**2+(cy-self.y)**2) < self.radiusDetection + wall.width: # si le coin est vu par le robot res_left = linesIntersect([(self.x,self.y),(cx,cy)],[(0,0),(0,self.room.height)]) if res_left != None: resX, resY = res_left[0], res_left[1] if cy >= resY > 0: # trapèze forbiddenAreas.append([(0,resY),(0,0),(cx,0),(cx,cy)]) else: # triangle res_top = linesIntersect([(self.x,self.y),(cx,cy)],[(0,0),(self.room.width,0)]) if res_top != None: resX, resY = res_top[0], res_top[1] forbiddenAreas.append([(resX,0),(cx,cy),(cx,0)]) # à gauche d'un mur vertical else: if min(wall.Ys) <= self.y <= max(wall.Ys): # 1: rectangle forbiddenAreas.append([(min(wall.Xs)+1,min(wall.Ys)), (self.room.width,min(wall.Ys)), (self.room.width,max(wall.Ys)), (min(wall.Xs)+1,max(wall.Ys))]) # 2: triangle ou trapèze cx, cy = min(wall.Xs),min(wall.Ys) if np.sqrt((cx-self.x)**2+(cy-self.y)**2) < self.radiusDetection: # si le coin est vu par le robot if cy < self.y: # coin au dessus du robot res_top = linesIntersect([(self.x,self.y),(cx,cy)],[(0,0),(self.room.width,0)]) if res_top != None: resX, resY = res_top[0], res_top[1] if resX < self.room.width: # trapèze forbiddenAreas.append([(self.room.width,0),(resX,0),(cx,cy),(self.room.width,cy)]) else: # triangle res_right = linesIntersect([(self.x,self.y),(cx,cy)],[(self.room.width,0),(self.room.width,self.room.height)]) if res_right != None: resX, resY = res_right[0], res_right[1] forbiddenAreas.append([(self.room.width,resY),(cx,cy),(self.room.width,cy)]) cx, cy = min(wall.Xs),max(wall.Ys) if np.sqrt((cx-self.x)**2+(cy-self.y)**2) < self.radiusDetection: # si le coin est vu par le robot if cy > self.y: # coin en dessous du robot res_bot = linesIntersect([(self.x,self.y),(cx,cy)],[(0,self.room.height),(self.room.width,self.room.height)]) if res_bot != None: resX, resY = res_bot[0], res_bot[1] if resX < self.room.width: # trapèze forbiddenAreas.append([(self.room.width,self.room.height),(resX,self.room.height),(cx,cy),(self.room.width,cy)]) else: # triangle res_right = linesIntersect([(self.x,self.y),(cx,cy)],[(self.room.width,0),(self.room.width,self.room.height)]) if res_right != None: resX, resY = res_right[0], res_right[1] forbiddenAreas.append([(self.room.width,resY),(cx,cy),(self.room.width,cy)]) # cas supplémentaires avec les coins, quand le robot n'est pas en face du mur elif self.y < min(wall.Ys): # robot au dessus du mur cx, cy = max(wall.Xs),min(wall.Ys) if np.sqrt((cx-self.x)**2+(cy-self.y)**2) < self.radiusDetection + wall.width: # si le coin est vu par le robot res_right = linesIntersect([(self.x,self.y),(cx,cy)],[(self.room.width,0),(self.room.width,self.room.height)]) if res_right != None: resX, resY = res_right[0], res_right[1] if cy <= resY < self.room.height: # trapèze forbiddenAreas.append([(self.room.width,resY),(self.room.width,self.room.height),(cx,self.room.height),(cx,cy)]) else: # triangle res_bot = linesIntersect([(self.x,self.y),(cx,cy)],[(0,self.room.height),(self.room.width,self.room.height)]) if res_bot != None: resX, resY = res_bot[0], res_bot[1] forbiddenAreas.append([(resX,self.room.height),(cx,cy),(cx,self.room.height)]) elif self.y > max(wall.Ys): cx, cy = max(wall.Xs),max(wall.Ys) if np.sqrt((cx-self.x)**2+(cy-self.y)**2) < self.radiusDetection + wall.width: # si le coin est vu par le robot res_right = linesIntersect([(self.x,self.y),(cx,cy)],[(self.room.width,0),(self.room.width,self.room.height)]) if res_right != None: resX, resY = res_right[0], res_right[1] if self.room.height >= resY > cy: # trapèze forbiddenAreas.append([(self.room.width,resY),(self.room.width,0),(cx,0),(cx,cy)]) else: # triangle res_top = linesIntersect([(self.x,self.y),(cx,cy)],[(0,0),(self.room.width,0)]) if res_top != None: resX, resY = res_top[0], res_top[1] forbiddenAreas.append([(resX,0),(cx,cy),(cx,0)]) # en dessous d'un mur horizontal elif wall.orientation == 'h': if self.y > max(wall.Ys): if min(wall.Xs) <= self.x <= max(wall.Xs) and wall.orientation == 'h': # 1: rectangle forbiddenAreas.append([(min(wall.Xs),0), (max(wall.Xs),0), (max(wall.Xs),max(wall.Ys)-1), (min(wall.Xs),max(wall.Ys)-1)]) # 2: triangle ou trapèze cx, cy = min(wall.Xs),max(wall.Ys) if np.sqrt((cx-self.x)**2+(cy-self.y)**2) < self.radiusDetection: # si le coin est vu par le robot if cx < self.x: # coin à gauche du robot res_left = linesIntersect([(self.x,self.y),(cx,cy)],[(0,0),(0,self.room.height)]) if res_left != None: resX, resY = res_left[0], res_left[1] if resY > 0: # trapèze forbiddenAreas.append([(0,resY),(0,0),(cx,0),(cx,cy)]) else: # triangle res_top = linesIntersect([(self.x,self.y),(cx,cy)],[(0,0),(self.room.width,0)]) if res_top != None: resX, resY = res_top[0], res_top[1] forbiddenAreas.append([(resX,0),(cx,cy),(cx,0)]) cx, cy = max(wall.Xs),max(wall.Ys) if np.sqrt((cx-self.x)**2+(cy-self.y)**2) < self.radiusDetection: # si le coin est vu par le robot if cx > self.x: # coin à droite du robot res_right = linesIntersect([(self.x,self.y),(cx,cy)],[(self.room.width,0),(self.room.width,self.room.height)]) if res_right != None: resX, resY = res_right[0], res_right[1] if resY > 0: # trapèze forbiddenAreas.append([(self.room.width,resY),(self.room.width,0),(cx,0),(cx,cy)]) else: # triangle res_top = linesIntersect([(self.x,self.y),(cx,cy)],[(0,0),(self.room.width,0)]) if res_top != None: resX, resY = res_top[0], res_top[1] forbiddenAreas.append([(resX,0),(cx,cy),(cx,0)]) # cas supplémentaires avec les coins, quand le robot n'est pas en face du mur elif self.x < min(wall.Xs): # robot à gauche du mur cx, cy = min(wall.Xs),min(wall.Ys) if np.sqrt((cx-self.x)**2+(cy-self.y)**2) < self.radiusDetection + wall.height: # si le coin est vu par le robot res_top = linesIntersect([(self.x,self.y),(cx,cy)],[(0,0),(self.room.width,0)]) if res_top != None: resX, resY = res_top[0], res_top[1] if cx <= resX < self.room.width: # trapèze forbiddenAreas.append([(resX,0),(self.room.width,0),(self.room.width,cy),(cx,cy)]) else: # triangle res_right = linesIntersect([(self.x,self.y),(cx,cy)],[(self.room.width,0),(self.room.width,self.room.height)]) if res_right != None: resX, resY = res_right[0], res_right[1] forbiddenAreas.append([(self.room.width,resY),(cx,cy),(self.room.width,cy)]) elif self.x > max(wall.Xs): # robot à droite du mur cx, cy = max(wall.Xs),min(wall.Ys) if np.sqrt((cx-self.x)**2+(cy-self.y)**2) < self.radiusDetection + wall.height: # si le coin est vu par le robot res_top = linesIntersect([(self.x,self.y),(cx,cy)],[(0,0),(self.room.width,0)]) if res_top != None: resX, resY = res_top[0], res_top[1] if cx >= resX > 0: # trapèze forbiddenAreas.append([(resX,0),(0,0),(0,cy),(cx,cy)]) else: # triangle res_left = linesIntersect([(self.x,self.y),(cx,cy)],[(0,0),(0,self.room.height)]) if res_left != None: resX, resY = res_left[0], res_left[1] forbiddenAreas.append([(0,resY),(cx,cy),(0,cy)]) # au dessus d'un mur horizontal else: if min(wall.Xs) <= self.x <= max(wall.Xs): # 1: rectangle forbiddenAreas.append([(min(wall.Xs),min(wall.Ys)+1), (max(wall.Xs),min(wall.Ys)+1), (max(wall.Xs),self.room.height), (min(wall.Xs),self.room.height)]) # 2: triangle ou trapèze cx, cy = min(wall.Xs),min(wall.Ys) if np.sqrt((cx-self.x)**2+(cy-self.y)**2) < self.radiusDetection: # si le coin est vu par le robot if cx < self.x: # coin à gauche du robot res_left = linesIntersect([(self.x,self.y),(cx,cy)],[(0,0),(0,self.room.height)]) if res_left != None: resX, resY = res_left[0], res_left[1] if 0 <= resY < self.room.height: # trapèze forbiddenAreas.append([(0,resY),(0,self.room.height),(cx,self.room.height),(cx,cy)]) else: # triangle res_bot = linesIntersect([(self.x,self.y),(cx,cy)],[(0,self.room.height),(self.room.width,self.room.height)]) if res_bot != None: resX, resY = res_bot[0], res_bot[1] forbiddenAreas.append([(resX,self.room.height),(cx,cy),(cx,self.room.height)]) cx, cy = max(wall.Xs),min(wall.Ys) if np.sqrt((cx-self.x)**2+(cy-self.y)**2) < self.radiusDetection: # si le coin est vu par le robot if cx > self.x: # coin à droite du robot res_right = linesIntersect([(self.x,self.y),(cx,cy)],[(self.room.width,0),(self.room.width,self.room.height)]) if res_right != None: resX, resY = res_right[0], res_right[1] if resY < self.room.height: # trapèze forbiddenAreas.append([(self.room.width,resY),(self.room.width,self.room.height),(cx,self.room.height),(cx,cy)]) else: # triangle res_bot = linesIntersect([(self.x,self.y),(cx,cy)],[(0,self.room.height),(self.room.width,self.room.height)]) if res_bot != None: resX, resY = res_bot[0], res_bot[1] forbiddenAreas.append([(resX,self.room.height),(cx,cy),(cx,self.room.height)]) # cas supplémentaires avec les coins, quand le robot n'est pas en face du mur elif self.x < min(wall.Xs): # robot à gauche du mur cx, cy = min(wall.Xs),max(wall.Ys) if np.sqrt((cx-self.x)**2+(cy-self.y)**2) < self.radiusDetection + wall.height: # si le coin est vu par le robot res_bot = linesIntersect([(self.x,self.y),(cx,cy)],[(0,self.room.height),(self.room.width,self.room.height)]) if res_bot != None: resX, resY = res_bot[0], res_bot[1] if cx <= resX < self.room.width: # trapèze forbiddenAreas.append([(resX,self.room.height),(self.room.width,self.room.height),(self.room.width,cy),(cx,cy)]) else: # triangle res_right = linesIntersect([(self.x,self.y),(cx,cy)],[(self.room.width,0),(self.room.width,self.room.height)]) if res_right != None: resX, resY = res_right[0], res_right[1] forbiddenAreas.append([(self.room.width,resY),(cx,cy),(self.room.width,cy)]) elif self.x > max(wall.Xs): # robot à droite du mur cx, cy = max(wall.Xs),max(wall.Ys) if np.sqrt((cx-self.x)**2+(cy-self.y)**2) < self.radiusDetection + wall.height: # si le coin est vu par le robot res_bot = linesIntersect([(self.x,self.y),(cx,cy)],[(0,self.room.height),(self.room.width,self.room.height)]) if res_bot != None: resX, resY = res_bot[0], res_bot[1] if cx >= resX > 0: # trapèze forbiddenAreas.append([(resX,self.room.height),(0,self.room.height),(0,cy),(cx,cy)]) else: # triangle res_left = linesIntersect([(self.x,self.y),(cx,cy)],[(0,0),(0,self.room.height)]) if res_left != None: resX, resY = res_left[0], res_left[1] forbiddenAreas.append([(0,resY),(cx,cy),(0,cy)]) result = {} for wall in obstaclesInView.keys(): result[wall] = [] for obstacle in obstaclesInView[wall]: isVisible = True compteur = 0 while isVisible and compteur < len(forbiddenAreas): area = forbiddenAreas[compteur] point = Point(obstacle.x, obstacle.y) # utilisation de la bibliothèque shapely polygon = Polygon(area) if polygon.contains(point): isVisible = False compteur += 1 if isVisible: result[wall].append(obstacle) return result, forbiddenAreas
def inner_point(polygon): point = Polygon(polygon).representative_point() x, y = point.x, point.y return x, y
def compute_compactness(polygon): feature_geom = Polygon(polygon) feature = feature_geom.area unit_circle = feature_geom.length ** 2 / (4 * pi) compactness = feature / unit_circle return compactness
def sceneSynthesis(rj): print(rj['origin']) start_time = time.time() pend_obj_list = [] bbindex = [] blocks = [] random.shuffle(rj['objList']) # bounding boxes of given furniture objects; boundingboxes = [] max_points = [] min_points = [] # identifying objects to arrange; for o in rj['objList']: if o is None: print('this is a None object; ') continue if 'coarseSemantic' not in o: print('a given object does not have coarseSemantic; ') continue if o['coarseSemantic'] in BANNED: print('a given object is not a furniture;') continue if o['coarseSemantic'] == 'door' or o['coarseSemantic'] == 'window': blocks.append(windoorblock_f(o)) continue # if o['modelId'] not in obj_semantic: # print(f'a given object {o["modelId"]} is not a furniture;' ) # continue # bbindex.append(name_to_ls[o['modelId']]) try: boundingboxes.append(load_boundingbox(o['modelId'])) except Exception as e: continue aabb = load_AABB(o['modelId']) max_points.append(aabb['max']) min_points.append(aabb['min']) o['childnum'] = {} o['myparent'] = None pend_obj_list.append(o) # load priors; csrrelation = torch.zeros((len(pend_obj_list), len(pend_obj_list)), dtype=torch.float) for center in pend_obj_list: for obj in pend_obj_list: preload_prior(center['modelId'], obj['modelId']) for centerid in range(len(pend_obj_list)): center = pend_obj_list[centerid] for objid in range(len(pend_obj_list)): if objid == centerid: continue obj = pend_obj_list[objid] # if the obj has a parent, we have to continue; # because if multiple parents exist, two parent may share a same child while another child has no parent; if obj['myparent'] is not None: continue pid = "{}-{}".format(center['modelId'], obj['modelId']) if pid in priors['pos']: if obj['modelId'] not in center['childnum']: center['childnum'][obj['modelId']] = 0 if center['childnum'][ obj['modelId']] >= priors['chainlength'][pid]: continue csrrelation[centerid, objid] = 1. obj['myparent'] = center center['childnum'][obj['modelId']] += 1 # partition coherent groups; pend_groups = connected_component(np.arange(len(pend_obj_list)), csrrelation) cgs = [] for pend_group in pend_groups: cg = {} cg['objList'] = [pend_obj_list[i] for i in pend_group] cg['csrrelation'] = csrrelation[pend_group][:, pend_group] cg['translate'] = [0.0, 0.0, 0.0] cg['orient'] = 0.0 # determine layouts of each group; heuristic(cg) # the following code is for chain pattern; # if only one object exists in a cg && the object follows chain layout, # e.g., kitchen cabinet, shelving, etc; if len(cg['objList'] ) == 1 and cg['objList'][0]['coarseSemantic'] in NaiveChainList: cg['chain'] = cg['objList'][0]['coarseSemantic'] else: cg['chain'] = 'n' if cg['objList'][cg['leaderID']]['modelId'] in [ '781' ] and cg['objList'][cg['leaderID']]['translate'][1] == 0: cg['objList'][cg['leaderID']]['translate'][1] = 1.04 cgs.append(cg) # load and process room shapes; room_meta = p2d( '.', '/dataset/room/{}/{}f.obj'.format(rj['origin'], rj['modelId'])) room_polygon = Polygon(room_meta[:, 0:2]) # requires python library 'shapely' translate = torch.zeros((len(pend_obj_list), 3)).float() orient = torch.zeros((len(pend_obj_list))).float() scale = torch.zeros((len(pend_obj_list), 3)).float() for i in range(len(pend_obj_list)): translate[i][0] = pend_obj_list[i]['translate'][0] translate[i][1] = pend_obj_list[i]['translate'][1] translate[i][2] = pend_obj_list[i]['translate'][2] orient[i] = pend_obj_list[i]['orient'] scale[i][0] = pend_obj_list[i]['scale'][0] scale[i][1] = pend_obj_list[i]['scale'][1] scale[i][2] = pend_obj_list[i]['scale'][2] # bb = four_points_xz[bbindex].float() # max_points = max_bb[bbindex].float() # min_points = min_bb[bbindex].float() bb = torch.tensor(boundingboxes).float() max_points = torch.tensor(max_points).float() min_points = torch.tensor(min_points).float() for i in range(len(pend_obj_list)): bb[i] = rotate_bb_local_para(bb[i], orient[i], scale[i][[0, 2]]) max_points[i] = transform_a_point(max_points[i], translate[i], orient[i], scale[i]) min_points[i] = transform_a_point(min_points[i], translate[i], orient[i], scale[i]) bb_tran = translate.reshape( len(pend_obj_list), 1, 3)[:, :, [0, 2]] + bb # note that bbs are around (0,0,0) after heuristic(cg) # calculate bounding box of coherent groups; for gid in range(len(pend_groups)): pend_group = pend_groups[gid] cg = cgs[gid] points = bb_tran[pend_group].reshape(-1, 2) max_points_of_cg = max_points[pend_group] min_points_of_cg = min_points[pend_group] maxp = torch.max(points, dim=0)[0] minp = torch.min(points, dim=0)[0] cg['bb'] = torch.zeros((4, 2), dtype=torch.float) cg['bb'][0] = maxp cg['bb'][1][0] = minp[0] cg['bb'][1][1] = maxp[1] cg['bb'][2] = minp cg['bb'][3][0] = maxp[0] cg['bb'][3][1] = minp[1] cg['height'] = torch.max(max_points_of_cg, dim=0)[0][1].item() cg['ground'] = torch.min(min_points_of_cg, dim=0)[0][1].item() # generate attempt_heuristic(cgs, room_meta, blocks) for cg in cgs: # the following code is reserving for lifting of each coherent group; cg['translate'][0] += np.sin(cg['orient']) * 0.08 cg['translate'][2] += np.cos(cg['orient']) * 0.08 # if cg['objList'][cg['leaderID']]['coarseSemantic'] in ['sink', 'dressing_table', 'picture_frame', 'television', 'mirror', 'clock', 'dining_table']: # cg['translate'][0] += np.sin(cg['orient']) * 0.08 # cg['translate'][2] += np.cos(cg['orient']) * 0.08 for o in cg['objList']: o['translate'][0], o['translate'][2] = rotate( [0, 0], [o['translate'][0], o['translate'][2]], cg['orient']) o['orient'] += cg['orient'] o['rotate'][0] = 0.0 o['rotate'][1] = o['orient'] o['rotate'][2] = 0.0 o['translate'][0] += cg['translate'][0] o['translate'][1] += cg['translate'][1] o['translate'][2] += cg['translate'][2] # log coherent groups; for i in range(len(pend_obj_list)): o = pend_obj_list[i] if 'coarseSemantic' not in o: break print(o['modelId'], o['coarseSemantic']) for j in range(len(pend_obj_list)): if csrrelation[i][j] == 1.0: print("--->>>", pend_obj_list[j]['modelId'], pend_obj_list[j]['coarseSemantic']) print("\r\n--- %s secondes ---" % (time.time() - start_time)) return rj
def get_regional_catalog(NELat_local, NELng_local, SWLat_local, SWLng_local, minimum_mag, max_depth,\ region_catalog_date_start, region_catalog_date_end): # This code will use shapely.py to filter the base catalog # into a rectangular region # settings_params = SEISRUtilities.get_settings() # region_type = settings_params[0] # location = settings_params[3] data_string = 'Building catalog for local region...' print('') print('------------------------------------------') print('') print(data_string) print('') print('------------------------------------------') print('') number_polygon_vertices = 4 # Construct the string of polygon vertices. Note that the order is lat, long pairs vertex_lat = [] vertex_lng = [] # Order of vertices of large rectangular region: NW, NE, SE, SW vertex_lat.append(NELat_local) vertex_lat.append(NELat_local) vertex_lat.append(SWLat_local) vertex_lat.append(SWLat_local) vertex_lng.append(SWLng_local) vertex_lng.append(NELng_local) vertex_lng.append(NELng_local) vertex_lng.append(SWLng_local) point_list = [] for i in range(number_polygon_vertices): point_list.append((float(vertex_lat[i]), float(vertex_lng[i]))) polygon = Polygon(point_list) # # ------------------------------------------------------- # input_file_name = "USGS_Base.catalog" input_file = open(input_file_name, "r") output_file_name = "USGS_regional.catalog" output_file = open(output_file_name, "w") for line in input_file: items = line.strip().split() dep = items[6] mag = items[5] eq_lat = items[4] eq_lng = items[3] point = Point((float(eq_lat), float(eq_lng))) if (float(dep) <= float(max_depth) and float(mag) >= float(minimum_mag) and polygon.contains(point) == True): print(items[0], items[1], items[2], items[3], items[4], items[5], items[6], file=output_file) output_file.close() input_file.close() return
returnDict[row[0]] = [float(tmpList[1][3:]), float(tmpList[0][3:])] return returnDict ''' ''' tmpList = polyWater() ansDict = {} disDict = [] classify = 4 pointDict = hole() count = 0 for level in tmpList[::-1]: print(classify) for route in level: polygon = Polygon(np.array(route)) for key in list(pointDict.keys()): point = Point(pointDict[key][0], pointDict[key][1]) if point.within(polygon): count += 1 ansDict[key] = classify del pointDict[key] calculateDict = {} for route in level: polygon = Polygon(np.array(route)) for key in list(pointDict.keys()): point = Point(pointDict[key][0], pointDict[key][1]) if key in calculateDict: calculateDict[key] = min(polygon.exterior.distance(point), calculateDict[key]) else:
def count_point_in_area(img, points): polygon_shapes = [] polygon_boxes = [] h, w, _ = img.shape r_w = w / 200 r_h = h / 200 first = 0 second = 0 third = 0 fourth = 0 for polygon in polygons: polygon_points = [] point_x = [] point_y = [] for i in range(len(polygon)): cur = polygon[i] if i != len(polygon) - 1: nxt = polygon[i + 1] nxtX = int(nxt['x'] * r_w) nxtY = int(nxt['y'] * r_h) else: nxt = polygon[0] nxtX = nxt['x'] nxtY = nxt['y'] cur['x'] = int(cur['x'] * r_w) cur['y'] = int(cur['y'] * r_h) polygon_points.append(Point(cur['x'], cur['y'])) point_x.append(cur['x']) point_y.append(cur['y']) # cv2.line(img, (cur['x'], cur['y']), (nxtX, nxtY), (255, 0, 0), 2) polygon_shapes.append(Polygon(polygon_points)) max_x = max(point_x) min_x = min(point_x) max_y = max(point_y) min_y = min(point_y) polygon_boxes.append([(min_x, min_y), (max_x, max_y)]) for x, y, _ in points: point = Point(x, y) if polygon_shapes[0].contains(point): first += 1 if polygon_shapes[1].contains(point): second += 1 if polygon_shapes[2].contains(point): third += 1 if polygon_shapes[3].contains(point): fourth += 1 return (first, second, third, fourth, len(points) - sum([first, second, third, fourth]))
def create_initial_positions3(xdivide=10, ydivide=10,lon0=55.,lon1=56.,lat0=-21.5, lat1=-20.5 ,start=1, zdivide=10, k0=56., k1=60., crop=False, outfile='initial_positions.txt', domain_dir='/Users/agn/Data/NEMO0083'): lons = np.linspace(lon0, lon1, xdivide+1) lats = np.linspace(lat0, lat1, ydivide+1) kdepths = np.linspace(k0, k1, zdivide+1) path = pjoin(domain_dir,'mask.nc') print('path for mask file', path) with Dataset(path) as f: Ndlat = f.variables['nav_lat'] meridional = Ndlat[:,0] jeq = meridional.searchsorted(0.) Ndlon = f.variables['nav_lon'] zonal = Ndlon[jeq,:] ibreak = np.argmax(zonal) + 1 if lon0 > zonal[0] and lon1 < zonal[ibreak-1]: zonal_part = zonal[:ibreak] iadd = 0 elif lon0 > zonal[ibreak] and lon1 < zonal[-1]: zonal_part = zonal[ibreak:] iadd = ibreak else: sys.exit('lon0 and lon1 bracket dateline; not implemented yet') i0 = zonal_part.searchsorted(lon0) - 1 + iadd i1 = zonal_part.searchsorted(lon1) + 1 + iadd imid = (i0 + i1)//2 meridional = Ndlat[:,imid] j0 = meridional.searchsorted(lat0) - 1 j1 = meridional.searchsorted(lat1) + 1 jmid = (j0 + j1)//2 zonal = Ndlon[jmid,:] dlon = np.diff(zonal) ibreak = np.argmin(dlon) ri = np.interp(lons, zonal[i0:i1], np.arange(i0,i1,dtype=zonal.dtype)) rj = np.interp(lats, meridional[j0:j1], np.arange(j0,j1,dtype=meridional.dtype)) print('specified longitudes are','\n',lons) print('i values are','\n',ri + 1. -.5) print('specified latitudes are','\n',lats) print('j values are','\n',rj + 1. -.5) print('k values are','\n',kdepths) i00, i11 = i0 - 1, i1 + 1 j00, j11 = j0 - 1, j1 + 1 tmask = ~(f.variables['tmask'][0,:,j00:j11,i00:i11].astype(np.bool)) uvmask = tmask[:,1:-1,1:-1] + tmask[:,:-2,1:-1] + tmask[:,2:,1:-1] \ + tmask[:,1:-1,:-2] + tmask[:,1:-1,2:] if crop: inner_circle, outer_circle, fuel_circle, box = get_circles() polygon = Polygon( zip(*box) ) npoints = 0 with open(outfile, 'w') as f: for kdepth in kdepths: for y, lat in zip(rj, lats): for x, lon in zip(ri, lons): if (not crop) or (crop and polygon.contains(Point(lon, lat))): i, j, k = int(x), int(y), int(kdepth) if not uvmask[k-1,j-j0, i-i0]: # ri and rj are c-indices relative to T-cells # subtract .5 to produce u v indices as required by ariane # ... and add 1 to convert from C to fortran numbering f.write('%10g%10g%10g%10g%5g\n' %(x + 1. -.5, y + 1. -.5 , kdepth, start, 1)) npoints +=1 print('# of sea points started is %g out of %g' % (npoints, len(rj)*len(ri)*len(kdepths)))
coorDict = {} matplotDict = [] muniFinal = {} #transform the polygon representing each neighborhood into a list of coordinates for shape in sf.shapeRecords(): x = [i[0] for i in shape.shape.points[:] ] #Initially for use in matplotlib to check shapefile y = [i[1] for i in shape.shape.points[:] ] #Initially for use in matplotlib to check shapefile for i in x: matplotDict.append( (x[x.index(i)], y[x.index(i)])) #Convert coordinates to be read by Shapely pkg munishp = Polygon(matplotDict) nhood = [j[0] for j in sfRec] muniFinal[nhood[ n]] = munishp #Store shape in dictionary with key of neighborhood matplotDict = [] #refresh coordinate store for next shape n += 1 #write the output CSV with each incident and its coordinates with open(fileName) as csvfile: reader = csv.DictReader(csvfile) for row in reader: coor = (row['Latitude'], row['Longitude']) rlat = float(row['Latitude']) rlong = float(row['Longitude']) if coor == ' , ' or coor == ', ':
def parse_slide_annotations_into_tables( slide_annotations, cropping_bounds=None, cropping_polygon_vertices=None, use_shapely=False): """Given a slide annotation list, parse into convenient tabular format. If the annotation is a point, then it is just treated as if it is a rectangle with zero area (i.e. xmin=xmax). Rotated rectangles are treated as polygons for simplicity. Parameters ----------- slide_annotations : list of dicts response from server request cropping_bounds : dict or None if given, must have keys XMIN, XMAX, YMIN, YMAX. These are the bounds to which the polygons may be cropped using shapely, if the param use_shapely is True. Otherwise, the polygon coordinates are just shifted relative to these bounds without actually cropping. cropping_polygon_vertices : nd array or None if given, is an (m, 2) nd array of vertices to crop bounds. if the param use_shapely is True. Otherwise, the polygon coordinates are just shifted relative to these bounds without actually cropping. use_shapely : bool see cropping_bounds description. Returns --------- Pandas DataFrame Summary of key properties of the annotation documents. It has the following columns: - annotation_girder_id - _modelType - _version - itemId - created - creatorId - public - updated - updatedId - groups - element_count - element_details Pandas DataFrame The individual annotation elements (polygons, points, rectangles). The columns annidx and elementidx encode the dict index of annotation document and element, respectively, in the original slide_annotations list of dictionaries. It has the following columns: - annidx - annotation_girder_id - elementidx - element_girder_id - type - group - label - color - xmin - xmax - ymin - ymax - bbox_area - coords_x - coords_y """ # we use this object to pass params to split method into sub-methods # and avoid annoying linting ("method too complex") issue class Cfg: def __init__(self): pass cfg = Cfg() cfg.cropping_bounds = cropping_bounds cfg.cropping_polygon_vertices = cropping_polygon_vertices cfg.use_shapely = use_shapely cfg.annotation_infos = DataFrame(columns=[ 'annotation_girder_id', '_modelType', '_version', 'itemId', 'created', 'creatorId', 'public', 'updated', 'updatedId', 'groups', 'element_count', 'element_details', ]) cfg.element_infos = DataFrame(columns=[ 'annidx', 'annotation_girder_id', 'elementidx', 'element_girder_id', 'type', 'group', 'label', 'color', 'xmin', 'xmax', 'ymin', 'ymax', 'bbox_area', 'coords_x', 'coords_y' ]) if cfg.cropping_bounds is not None: assert cfg.cropping_polygon_vertices is None, \ "either give cropping bouns or vertices, not both" xmin, xmax, ymin, ymax = ( cfg.cropping_bounds['XMIN'], cfg.cropping_bounds['XMAX'], cfg.cropping_bounds['YMIN'], cfg.cropping_bounds['YMAX']) bounds_polygon = Polygon(np.array([ (xmin, ymin), (xmax, ymin), (xmax, ymax), (xmin, ymax), (xmin, ymin), ], dtype='int32')) cfg.x_shift = xmin cfg.y_shift = ymin elif cfg.cropping_polygon_vertices is not None: bounds_polygon = Polygon(np.int32(cfg.cropping_polygon_vertices)) cfg.x_shift, cfg.y_shift = np.min(cfg.cropping_polygon_vertices, 0) # go through annotation elements and add as needed for cfg.annidx, cfg.ann in enumerate(slide_annotations): annno = cfg.annotation_infos.shape[0] # Add annotation document info to annotations dataframe cfg.annotation_infos.loc[annno, 'annotation_girder_id'] = cfg.ann[ '_id'] for key in [ '_modelType', '_version', 'itemId', 'created', 'creatorId', 'public', 'updated', 'updatedId', ]: cfg.annotation_infos.loc[annno, key] = cfg.ann[key] cfg.annotation_infos.loc[annno, 'groups'] = str(cfg.ann['groups']) cfg.annotation_infos.loc[annno, 'element_count'] = cfg.ann[ '_elementQuery']['count'] cfg.annotation_infos.loc[annno, 'element_details'] = cfg.ann[ '_elementQuery']['details'] for cfg.elementidx, cfg.element in enumerate( cfg.ann['annotation']['elements']): coords = _get_coords_from_element(copy.deepcopy(cfg.element)) # crop using shapely to desired bounds if needed # IMPORTANT: everything till this point needs to be # relative to the whole slide image if ((cfg.cropping_bounds is None) and (cfg.cropping_polygon_vertices is None)) \ or (cfg.element['type'] == 'point') \ or (not use_shapely): all_coords = [coords, ] else: all_coords = _maybe_crop_polygon(coords, bounds_polygon) # now add polygons one by one for vertices in all_coords: _add_element_to_final_df(vertices, cfg=cfg) return cfg.annotation_infos, cfg.element_infos
def h__getEccentricityAndOrientation(x_mc,y_mc,xRange_all,yRange_all,gridAspectRatio_all,N_GRID_POINTS,eccentricity,orientation,run_mask): # h__getEccentricityAndOrientation for iFrame in np.nditer(utils.find(run_mask)): cur_aspect_ratio = gridAspectRatio_all[iFrame] # x_range = xRange_all[iFrame] #y_range = yRange_all[iFrame] #------------------------------------------------------ cur_cx = x_mc[:, iFrame] cur_cy = y_mc[:, iFrame] poly = Polygon(zip(cur_cx, cur_cy)) if cur_aspect_ratio > 1: # x size is larger so scale down the number of grid points in # the y direction n1 = N_GRID_POINTS n2 = np.round(N_GRID_POINTS / cur_aspect_ratio) else: # y size is larger so scale down the number of grid points in # the x direction n1 = np.round(N_GRID_POINTS * cur_aspect_ratio) n2 = N_GRID_POINTS wtf1 = np.linspace(np.min(x_mc[:, iFrame]), np.max(x_mc[:, iFrame]), num=n1) wtf2 = np.linspace(np.min(y_mc[:, iFrame]), np.max(y_mc[:, iFrame]), num=n2) m, n = np.meshgrid(wtf1, wtf2) n_points = m.size m_lin = m.reshape(n_points) n_lin = n.reshape(n_points) in_worm = np.zeros(n_points, dtype=np.bool) for i in range(n_points): p = Point(m_lin[i], n_lin[i]) # try: in_worm[i] = poly.contains(p) # except ValueError: # import pdb # pdb.set_trace() x = m_lin[in_worm] y = n_lin[in_worm] eccentricity[iFrame],orientation[iFrame] = h__calculateSingleValues(x,y) # First eccentricity value should be: 0.9743 """ TODO: Finish this plot(xOutline_mc(:,iFrame),yOutline_mc(:,iFrame),'g-o') hold on scatter(x,y,'r') hold off axis equal title(sprintf('%d',iFrame)) pause """ return (eccentricity,orientation)
def getSA3(lon, lat, polygon): #149.57900670400008, -35.5 polygon = Polygon(polygon) # create polygon point = Point(lon, lat) # create point return (point.within(polygon))
for ti, t in enumerate(alarmtab): plt.close() fig = plt.figure(figsize=(10, 10)) ax1 = start_axes(title='Map1', extent=exts[extformap], fig=fig) tinds = (data_xarray['time'] > t) & (data_xarray['time'] <= t + np.timedelta64(outputdt)) sc1 = ax1.scatter(data_xarray['lon'].values[tinds], data_xarray['lat'].values[tinds], 5, data_xarray['age'].values[tinds] / 86400, vmin=0, vmax=5, alpha=.5) if polys[0] is not None: pol = Polygon(poly1) ax1.add_geometries([pol], facecolor='orange', edgecolor='red', alpha=0.8, crs=ccrs.PlateCarree()) ax1.scatter(sourcepoint[0], sourcepoint[1], 25, 'red') clb = plt.colorbar(sc1) clb.ax.set_xlabel('Age [d]') ax1.set_title(np.datetime_as_string(timerange[i], unit='m')) scale_bar(ax1, 1) plt.savefig(figdir + 'AllTracks_Alarm' + str(ti) + '.png') # ## Level 3
def checkConstraints(turb_coords, turb_diam): """ -**-THIS FUNCTION SHOULD NOT BE MODIFIED-**- Checks if the turbine configuration satisfies the two constraints:(i) perimeter constraint,(ii) proximity constraint Prints which constraints are violated if any. Note that this function does not quantifies the amount by which the constraints are violated if any. :called from main :param turb_coords - 2d np array containing turbine x,y coordinates turb_diam - Diameter of the turbine (m) :return None. Prints messages. """ bound_clrnc = 50 prox_constr_viol = False peri_constr_viol = False constr = False # create a shapely polygon object of the wind farm farm_peri = [(0, 0), (0, 4000), (4000, 4000), (4000, 0)] farm_poly = Polygon(farm_peri) # checks if for every turbine perimeter constraint is satisfied. # breaks out if False anywhere for turb in turb_coords: turb = Point(turb) inside_farm = farm_poly.contains(turb) correct_clrnc = farm_poly.boundary.distance(turb) >= bound_clrnc if (inside_farm == False or correct_clrnc == False): peri_constr_viol = True break # checks if for every turbines proximity constraint is satisfied. # breaks out if False anywhere for i, turb1 in enumerate(turb_coords): for turb2 in np.delete(turb_coords, i, axis=0): if np.linalg.norm(turb1 - turb2) < 4 * turb_diam: prox_constr_viol = True break # print messages if peri_constr_viol == True and prox_constr_viol == True: constr = True print( 'Somewhere both perimeter constraint and proximity constraint are violated\n' ) elif peri_constr_viol == True and prox_constr_viol == False: constr = True print('Somewhere perimeter constraint is violated\n') elif peri_constr_viol == False and prox_constr_viol == True: constr = True print('Somewhere proximity constraint is violated\n') else: constr = False print('Both perimeter and proximity constraints are satisfied !!\n') return (constr)
def apply_mask(df, isoc_mag1, isoc_mag2, mag_key='g', color_key='g-r', err_cols=('gerr', 'rerr'), save_plot=False, obj_name=''): import numpy as np from scipy import interpolate from astropy.io import ascii from matplotlib import pyplot as plt from mf_code import colormag_error import importlib importlib.reload(colormag_error) color_keys = color_key.split('-') color = df[color_keys[0]] - df[color_keys[1]] mag = df[mag_key] color_err = np.sqrt(df[err_cols[0]]**2 + df[err_cols[1]]**2) popt, pcov, err_func = colormag_error.error_func(mag, color_err, xlab=mag_key, ylab=color_key, plot=True) err_func2 = lambda x: err_func(x, *popt) ##mask creation isoc_color = isoc_mag1 - isoc_mag2 lim = 4.0 delta_color = lim * err_func2(isoc_mag1) isoc_min_color = isoc_color - delta_color isoc_max_color = isoc_color + delta_color msto_mag = 21.5 delta_mag = np.copy(delta_color) delta_mag[isoc_mag1 > msto_mag] = 0 isoc_min_mag = isoc_mag1 - delta_mag isoc_max_mag = isoc_mag1 + delta_mag from shapely.geometry import Point from shapely.geometry.polygon import Polygon cmd_points = zip(mag, color) polygon_points_min = sorted(zip(isoc_min_mag, isoc_min_color)) polygon_points_max = sorted(zip(isoc_max_mag, isoc_max_color), reverse=True) polpoints = polygon_points_min + polygon_points_max polygon = Polygon(polpoints) isinside = list(map(lambda p: polygon.contains(Point(p)), cmd_points)) df_cut = df.loc[isinside, :] #Plot applied mask color = df[color_keys[0]] - df[color_keys[1]] mag = df[mag_key] plt.figure(figsize=(10, 10)) plt.plot(color, mag, ls='none', marker='o', ms=2) plt.plot(isoc_color, isoc_mag1) plt.plot(isoc_min_color, isoc_min_mag, color='red') plt.plot(isoc_max_color, isoc_max_mag, color='red') plt.xlim(min(color), max(color)) plt.ylim(min(mag), max(mag)) plt.gca().invert_yaxis() plt.ylabel(mag_key) plt.xlabel('{}'.format(color_key)) plt.title('Stars selected by isochrone mask') if save_plot: plt.savefig('Results/' + obj_name + '_mask.pdf') else: plt.show() return df_cut, popt, err_func2
def calculate_erection_operation_time(project_specs, project_data, construct_duration, operational_construction_time): """ Calculates operation time required for each type of equipment included in project data. :param project_specs: data frame with project details (from project input file) :param project_data: dictionary of data frames for each of the csv files loaded for the project :param construct_duration: duration of construction (in months) :param operational_construction_time: operational hours of construction :return: list of possible cranes that could be used to erect tower and turbine """ erection_construction_time = 1/3 * construct_duration print('Calculating operation time for erection...') # group project data by project ID project = project_specs # for components in component list determine if base or topping project_data['components']['Operation'] = project_data['components']['Lift height m'] > (float(project['Hub height m'] * project['Breakpoint between base and topping (percent)'])) boolean_dictionary = {True: 'Top', False: 'Base'} project_data['components']['Operation'] = project_data['components']['Operation'].map(boolean_dictionary) # create groups for operations top_v_base = project_data['components'].groupby(['Operation']) # group crane data by boom system and crane name to get distinct cranes crane_grouped = project_data['crane_specs'].groupby(['Equipment name', 'Crane name', 'Boom system', 'Crane capacity tonne']) crane_poly = pd.DataFrame(columns=['Equipment name', 'Crane name', 'Boom system', 'Crane capacity tonne', 'Crane poly']) for name, crane in crane_grouped: crane = crane.reset_index(drop=True) x = crane['Max capacity tonne'] y = crane['Hub height m'] wind_speed = min(crane['Max wind speed m per s']) hoist_speed = min(crane['Hoist speed m per min']) travel_speed = min(crane['Speed of travel km per hr']) setup_time = max(crane['Setup time hr']) crew_type = crane['Crew type ID'][0] #todo: fix this so it's not a hack... need to rethink data structure - right now just picking first crew type - this is correct because same for all crane/boom combinations but we should come up with a better way to do it polygon = Polygon([(0, 0), (0, max(y)), (min(x), max(y)), (max(x), min(y)), (max(x), 0)]) df = pd.DataFrame([[name[0], name[1], name[2], name[3], wind_speed, setup_time, hoist_speed, travel_speed, crew_type, polygon]], columns=['Equipment name', 'Crane name', 'Boom system', 'Crane capacity tonne', 'Max wind speed m per s', 'Setup time hr', 'Hoist speed m per min', 'Speed of travel km per hr', 'Crew type ID', 'Crane poly']) crane_poly = crane_poly.append(df, sort=True) # loop through operation type (topping vs. base) rownew = pd.Series() component_max_speed = pd.DataFrame() crane_poly_new = crane_poly for name_operation, component_group in top_v_base: # calculate polygon for crane capacity and check if component can be lifted by each crane without wind loading for idx, crane in crane_poly.iterrows(): polygon = crane['Crane poly'] for component in component_group['Component']: # get weight and height of component in each component group component_only = component_group.where(component_group['Component'] == component).dropna(thresh=1) point = Point(component_only['Mass tonne'], component_only['Lift height m']) crane['Lift boolean {component}'.format(component=component)] = polygon.contains(point) rownew = rownew.append(crane) bool_list = list() for component in component_group['Component']: if crane['Lift boolean {component}'.format(component=component)] is False: crane_bool = False else: crane_bool = True bool_list.append(crane_bool) # calculate max permissible wind speed # equation for calculating permissible wind speed: # vmax = max_TAB * sqrt(1.2 * mh / aw), where # mh = hoist load # aw = area exposed to wind = surface area * coeff drag # 1.2 = constant in m^2 / t # vmax_tab = maximum load speed per load chart # source: pg. 33 of Liebherr mh = component_group['Mass tonne'] aw = component_group['Surface area sq m'] * component_group['Coeff drag'] vmax_tab = crane['Max wind speed m per s'] vmax_calc = vmax_tab * sqrt(1.2 * mh / aw) # if vmax_calc is less than vmax_tab then vmax_calc, otherwise vmax_tab (based on pg. 33 of Liebherr) # todo: check vmax - should it be set to calculated value rather than vmax_tab if greater? component_group_new = pd.DataFrame(component_group, columns=list(component_group.columns.values) + ['vmax', 'Crane name', 'Boom system', 'crane_bool']) component_group_new['vmax'] = list((min(vmax_tab, x) for x in vmax_calc)) component_group_new['Crane name'] = crane['Crane name'] component_group_new['Boom system'] = crane['Boom system'] component_group_new['crane_bool'] = bool_list component_max_speed = component_max_speed.append(component_group_new, sort=True) crane_poly_new['Crane bool {operation}'.format(operation=name_operation)] = min(bool_list) crane_poly = crane_poly_new # join crane polygon to crane specs crane_component = pd.merge(crane_poly, component_max_speed, on=['Crane name', 'Boom system']) # select only cranes that could lift the component possible_cranes = crane_component.where(crane_component['crane_bool'] == True).dropna(thresh=1).reset_index(drop=True) # calculate travel time per cycle turbine_spacing = float(project['Turbine spacing (times rotor diameter)'] * project['Rotor diameter m'] * km_per_m) turbine_num = float(project['Number of turbines']) possible_cranes['Travel time hr'] = turbine_spacing / possible_cranes['Speed of travel km per hr'] * turbine_num # calculate erection time possible_cranes['Operation time hr'] = ((possible_cranes['Lift height m'] / possible_cranes['Hoist speed m per min'] * hr_per_min) + (possible_cranes['Cycle time installation hrs']) ) * turbine_num # store setup time possible_cranes['Setup time hr'] = possible_cranes['Setup time hr'] * turbine_num erection_time = possible_cranes.groupby(['Crane name', 'Equipment name', 'Crane capacity tonne', 'Crew type ID', 'Boom system', 'Operation'])['Operation time hr'].sum() travel_time = possible_cranes.groupby(['Crane name', 'Equipment name', 'Crane capacity tonne', 'Crew type ID', 'Boom system', 'Operation'])['Travel time hr'].max() setup_time = possible_cranes.groupby(['Crane name', 'Equipment name', 'Crane capacity tonne', 'Crew type ID', 'Boom system', 'Operation'])['Setup time hr'].max() rental_time_without_weather = erection_time + travel_time + setup_time operation_time = rental_time_without_weather.reset_index() operation_time = operation_time.rename(columns={0: 'Operation time all turbines hrs'}) operation_time['Operational construct days'] = (operation_time['Operation time all turbines hrs'] / operational_construction_time) # if more than one crew needed to complete within construction duration then assume that all construction happens # within that window and use that time frame for weather delays; if not, use the number of days calculated operation_time['time_construct_bool'] = (operation_time['Operational construct days'] > erection_construction_time * 30) boolean_dictionary = {True: erection_construction_time * 30, False: np.NAN} operation_time['time_construct_bool'] = operation_time['time_construct_bool'].map(boolean_dictionary) operation_time['Time construct days'] = operation_time[['time_construct_bool', 'Operational construct days']].min(axis=1) #print(possible_cranes[['Crane name', 'Component', 'Operation time hr', 'Operation']]) for operation, component_group in top_v_base: unique_component_crane = possible_cranes.loc[possible_cranes['Operation'] == operation]['Component'].unique() for component in component_group['Component']: if component not in unique_component_crane: error = 1 sys.exit('Error: Unable to find installation crane for {} operation and {} component'.format(operation, component)) else: error = 0 if error == 0: print('Crane(s) found for all components for {} installation'.format(operation)) return possible_cranes, operation_time, error
def define_campuses(): '''Attempts to breakup stops to different dictionaries based off of campus. Instead of just hardcoding the campuses we attempt to split it up goegraphically. This is just in case there are new bus stops added so they automatically group to the appropriate campus''' global allstops_hashtable global buschtable global livitable global cactable global cdtable global othertable #Each of these polygons was created using: https://multiplottr.com busch = Polygon([(40.521760, -74.488335), (40.511907, -74.459667), (40.525217, -74.452543), (40.528936, -74.467564)]) livingston = Polygon([(40.524174, -74.446020), (40.517339, -74.431193), (40.523815, -74.427953), (40.530763, -74.440956)]) collegeave = Polygon([(40.499116, -74.462543), (40.492328, -74.446235), (40.499801, -74.438381), (40.508350, -74.453144)]) cookdoug = Polygon([(40.478521, -74.453616), (40.466376, -74.425120), (40.480251, -74.414349), (40.490826, -74.440227)]) #Temp list that will be used for list logic later on busch_list = [] livi_list = [] collegeave_list = [] cookdoug_list = [] other_list = [] #Every stop has a point that is mapped to it and then for key, value in allstops_hashtable.items(): if busch.contains(Point(value[1])): busch_list.append(key) elif livingston.contains(Point(value[1])): livi_list.append(key) elif collegeave.contains(Point(value[1])): collegeave_list.append(key) elif cookdoug.contains(Point(value[1])): cookdoug_list.append(key) else: other_list.append(key) #Use list logic to constructure dictionaries that are per campus buschtable = {k: allstops_hashtable[k] for k in busch_list} livitable = {k: allstops_hashtable[k] for k in livi_list} cactable = {k: allstops_hashtable[k] for k in collegeave_list} cdtable = {k: allstops_hashtable[k] for k in cookdoug_list} othertable = {k: allstops_hashtable[k] for k in other_list}
def get_eccentricity_and_orientation(contour_x, contour_y): """ get_eccentricity [eccentricity, orientation] = seg_worm.feature_helpers.posture.getEccentricity(xOutline, yOutline, gridSize) Given x and y coordinates of the outline of a region of interest, fill the outline with a grid of evenly spaced points and use these points in a center of mass calculation to calculate the eccentricity and orientation of the equivalent ellipse. Placing points in the contour is a well known computer science problem known as the Point-in-Polygon problem. http://en.wikipedia.org/wiki/Point_in_polygon This function became a lot more complicated in an attempt to make it go much faster. The complication comes from the simplication that can be made when the worm doesn't bend back on itself at all. OldName: getEccentricity.m Inputs: ======================================================================= xOutline : [96 x num_frames] The x coordinates of the contour. In particular the contour starts at the head and goes to the tail and then back to the head (although no points are redundant) yOutline : [96 x num_frames] The y coordinates of the contour " " N_ECCENTRICITY (a constant from config.py): (scalar) The # of points to place in the long dimension. More points gives a more accurate estimate of the ellipse but increases the calculation time. Outputs: a namedtuple containing: ======================================================================= eccentricity - [1 x num_frames] The eccentricity of the equivalent ellipse orientation - [1 x num_frames] The orientation angle of the equivalent ellipse Nature Methods Description ======================================================================= Eccentricity. ------------------ The eccentricity of the worm’s posture is measured using the eccentricity of an equivalent ellipse to the worm’s filled contour. The orientation of the major axis for the equivalent ellipse is used in computing the amplitude, wavelength, and track length (described below). Status ======================================================================= The code below is finished although I want to break it up into smaller functions. I also need to submit a bug report for the inpoly FEX code. Translation of: SegwormMatlabClasses / +seg_worm / +feature_helpers / +posture / getEccentricity.m """ t_obj = time.time() N_GRID_POINTS = 50 #TODO: Get from config ... x_range_all = np.ptp(contour_x,axis=0) y_range_all = np.ptp(contour_y,axis=0) x_mc = contour_x - np.mean(contour_x,axis=0) #mc - mean centered y_mc = contour_y - np.mean(contour_y,axis=0) grid_aspect_ratio = x_range_all/y_range_all #run_mask = np.logical_not(np.isnan(grid_aspect_ratio)) n_frames = len(x_range_all) eccentricity = np.empty(n_frames) eccentricity[:] = np.NAN orientation = np.empty(n_frames) orientation[:] = np.NAN #h__getEccentricityAndOrientation for iFrame in range(n_frames): cur_aspect_ratio = grid_aspect_ratio[iFrame] #------------------------------------------------------ if not np.isnan(cur_aspect_ratio): cur_cx = x_mc[:,iFrame] cur_cy = y_mc[:,iFrame] poly = Polygon(zip(cur_cx,cur_cy)) if cur_aspect_ratio > 1: #x size is larger so scale down the number of grid points in the y direction n1 = N_GRID_POINTS n2 = np.round(N_GRID_POINTS / cur_aspect_ratio) else: #y size is larger so scale down the number of grid points in the x direction n1 = np.round(N_GRID_POINTS * cur_aspect_ratio) n2 = N_GRID_POINTS wtf1 = np.linspace(np.min(x_mc[:,iFrame]), np.max(x_mc[:,iFrame]), num=n1); wtf2 = np.linspace(np.min(y_mc[:,iFrame]), np.max(y_mc[:,iFrame]), num=n2); m,n = np.meshgrid( wtf1 , wtf2 ); n_points = m.size m_lin = m.reshape(n_points) n_lin = n.reshape(n_points) in_worm = np.zeros(n_points,dtype=np.bool) for i in range(n_points): p = Point(m_lin[i],n_lin[i]) # try: in_worm[i] = poly.contains(p) # except ValueError: # import pdb # pdb.set_trace() x = m_lin[in_worm] y = n_lin[in_worm] """ TODO: Finish this plot(xOutline_mc(:,iFrame),yOutline_mc(:,iFrame),'g-o') hold on scatter(x,y,'r') hold off axis equal title(sprintf('%d',iFrame)) pause """ #First eccentricity value should be: 0.9743 #h__calculateSingleValues N = float(len(x)) # Calculate normalized second central moments for the region. uxx = np.sum(x*x)/N uyy = np.sum(y*y)/N uxy = np.sum(x*y)/N # Calculate major axis length, minor axis length, and eccentricity. common = np.sqrt((uxx - uyy)**2 + 4*(uxy**2)) majorAxisLength = 2*np.sqrt(2)*np.sqrt(uxx + uyy + common) minorAxisLength = 2*np.sqrt(2)*np.sqrt(uxx + uyy - common) eccentricity[iFrame] = 2*np.sqrt((majorAxisLength/2)**2 - (minorAxisLength/2)**2) / majorAxisLength # Calculate orientation. if (uyy > uxx): num = uyy - uxx + np.sqrt((uyy - uxx)**2 + 4*uxy**2) den = 2*uxy else: num = 2*uxy den = uxx - uyy + np.sqrt((uxx - uyy)**2 + 4*uxy**2) orientation[iFrame] = (180/np.pi) * np.arctan(num/den) #[eccentricity(iFrame),orientation(iFrame)] = h__calculateSingleValues(x,y); elapsed_time = time.time() - t_obj print('Elapsed time in seconds for eccentricity: %d' % elapsed_time) return (eccentricity,orientation)
# There are probably hundreds approaches to this task # only showing one # Imported additional packages methods from matplotlib from shapely.geometry.polygon import Polygon from matplotlib.collections import PatchCollection from matplotlib import cm from descartes import PolygonPatch from matplotlib import colors, colorbar fig, axes = plt.subplots(1, 1) # Add a weird shape polygon # Use coordinates for the polygon ring_mixed = Polygon([(0, 0), (0, 2), (1, 1), (2, 2), (2, 0), (1, 0.8), (0, 0)]) # Add it as a patch object. This is where you apply aesethic arguments such # as color, edge color, etc... patch1 = PolygonPatch(ring_mixed, color='coral') # Add the patch to the axes axes.add_patch(patch1) # Add a rectangle rect1 = Polygon([(3, 1), (3, 3), (9, 3), (9, 1), (3, 1)]) patch2 = PolygonPatch(rect1, color='lightblue') axes.add_patch(patch2) axes.set_xlim([0, 10])
for ob in x: x, y = ob.xy if len(x) == 1: plt.plot(x, y, 'o', color='m', zorder=2) else: plt.plot(x, y, color='m', alpha=0.7, linewidth=3, solid_capstyle='round', zorder=2) # test erosion of polygon from shapely.geometry.polygon import Polygon from shapely.geometry import Polygon p = Polygon([(0, 0), (1, 1), (1, 0)]) x,y = p.boundary.xy plt.plot(x, y, color='r', alpha=0.7, linewidth=3, solid_capstyle='round', zorder=2) p2 = p.buffer(-0.2); x,y = p2.boundary.xy plt.plot(x, y, color='r', alpha=0.7, linewidth=3, solid_capstyle='round', zorder=2) b = p2.boundary; for o in b: x,y = o.xy plt.plot(x, y, color='b', alpha=0.7, linewidth=3, solid_capstyle='round', zorder=2)
def update(self, dt): global brain global last_reward global punishment global counter global scores global last_distance global goal_x global goal_y global longueur global largeur global point global poly global sand_penalty longueur = self.width largeur = self.height if first_update: init() punishment = 0 counter = 0 sand_penalty = -4 poly = [ Polygon([(self.car.x - 50, self.car.y - 50), (self.car.x - 50, self.car.y + 50), (self.car.x + 50, self.car.y + 50), (self.car.x + 50, self.car.y - 50)]) ] xx = goal_x - self.car.x yy = goal_y - self.car.y orientation = Vector(*self.car.velocity).angle((xx, yy)) / 180. last_signal = [ self.car.signal1, self.car.signal2, self.car.signal3, orientation, -orientation ] action = brain.update(last_reward, last_signal) scores.append(brain.score()) rotation = action2rotation[action] self.car.move(rotation) self.imageCar.rotate_car(rotation) distance = np.sqrt((self.car.x - goal_x)**2 + (self.car.y - goal_y)**2) self.ball1.pos = self.car.sensor1 self.ball2.pos = self.car.sensor2 self.ball3.pos = self.car.sensor3 if sand[int(self.car.x), int(self.car.y)] > 0: self.car.velocity = Vector(1, 0).rotate(self.car.angle) last_reward = sand_penalty else: # otherwise self.car.velocity = Vector(6, 0).rotate(self.car.angle) last_reward = -0.002 if distance < last_distance: last_reward = 0.01 if time.process_time() - self.time_passed > 2: punishment += 0.1 print("Time punishment now: " + str(punishment)) self.time_passed = time.process_time() if self.car.x < 10: self.car.x = 10 last_reward = -1 if self.car.x > self.width - 10: self.car.x = self.width - 10 last_reward = -1 if self.car.y < 10: self.car.y = 10 last_reward = -1 if self.car.y > self.height - 10: self.car.y = self.height - 10 last_reward = -1 if counter % 100 == 0: poly.append( Polygon([(self.car.x - 10, self.car.y - 10), (self.car.x - 10, self.car.y + 10), (self.car.x + 10, self.car.y + 10), (self.car.x + 10, self.car.y - 10)])) point = Point(self.car.x, self.car.y) for p in poly: if p.contains(point): punishment += 0.01 if distance < 100: # once it reaches goal goal_x = self.width - goal_x goal_y = self.height - goal_y punishment = 0 last_reward = 5 print("Destination reached! --> reset punishment") self.time_passed = time.process_time() last_distance = distance last_reward -= punishment counter += 1 if counter % 40 == 0: print("Last reward --> " + str(last_reward))
lines_bottom_exclude = lines_bottom_exclude.replace("\t", "") lines_bottom_exclude = lines_bottom_exclude.replace("] [", "), (") lines_bottom_exclude = lines_bottom_exclude.replace("]", ")]") lines_bottom_exclude = lines_bottom_exclude.replace("[", "[(") lines_bottom_exclude = literal_eval(lines_bottom_exclude) lines_bottom_exclude2 = lines_bottom_exclude2.replace("mm ", ", ") lines_bottom_exclude2 = lines_bottom_exclude2.replace("mm", "") lines_bottom_exclude2 = lines_bottom_exclude2.replace("\n", "") lines_bottom_exclude2 = lines_bottom_exclude2.replace("\t", "") lines_bottom_exclude2 = lines_bottom_exclude2.replace("] [", "), (") lines_bottom_exclude2 = lines_bottom_exclude2.replace("]", ")]") lines_bottom_exclude2 = lines_bottom_exclude2.replace("[", "[(") lines_bottom_exclude2 = literal_eval(lines_bottom_exclude2) polygon_top = Polygon(lines_top) polygon_top_exclude = Polygon(lines_top_exclude) polygon_top_exclude2 = Polygon(lines_top_exclude2) polygon_bottom = Polygon(lines_bottom) polygon_bottom_exclude = Polygon(lines_bottom_exclude) polygon_bottom_exclude2 = Polygon(lines_bottom_exclude2) x = 0.0 y = 0.0 for x in range(int((board_width + x_offset * 2) / spacing)): x *= spacing x += x_offset for y in range(int((board_height + y_offset * 2) / spacing)): y *= spacing y += y_offset
def contains_point(point, voro_points): """ """ polygon = Polygon(voro_points) return polygon.contains(Point(point[0], point[1]))
def __init__(self, hydrants): self.vor = Voronoi(hydrants, incremental=True) self.poly = Polygon([(float(x.split(', ')[0]), float(x.split(', ')[1])) for x in open('coords.txt').read().split('\n')[:-1]])
def post(self, request, format=None): data = request.data user_id = data.get('user_id') if data.get('step') == '0' or data.get('step') == 0: search = SearchV2.objects.create(user_identify=user_id, created_at=timezone.now(), last_step=1) search.save() area_list = Area.objects.all() serialized = AreaSerializer(area_list, many=True) resp_data = { "step": 1, "template": "step_1", "answers": serialized.data, "count": RealtyObject.objects.all().count() } return Response(data=resp_data, status=200) elif data.get('step') == '1' or data.get('step') == 1: search = SearchV2.objects.filter(user_identify=user_id, finished_at__isnull=True).last() try: areas_pk = ast.literal_eval(data.get('data')) except ValueError: areas_pk = data.get('data') search.last_step = 2 search.save() realty_objects = RealtyObject.objects.filter( realty_complex__area_id__in=areas_pk, realty_complex__lat__isnull=False, realty_complex__lng__isnull=False) count = realty_objects.count() step_1 = get_or_create_step(search=search, step_pos=1) step_1.answer = areas_pk step_1.result = [ realty_object.pk for realty_object in realty_objects ] step_1.save() travel_types = TravelType.objects.filter(is_active=True) serialized = TravelTypeSerializer(travel_types, many=True) bounds_data = { "topLeft": [59.509087, 24.527257], "btmRight": [59.353837, 24.945407] } resp_data = { "step": 2, "template": "step_2", "answers": serialized.data, "bounds": bounds_data, "count": count } return Response(data=resp_data, status=200) elif data.get('step') == '2' or data.get('step') == 2: search = SearchV2.objects.filter(user_identify=user_id, finished_at__isnull=True).last() try: place_data = ast.literal_eval(data.get('data')) except ValueError: place_data = data.get('data') search.last_step = 2 search.save() step_2 = get_or_create_step(search=search, step_pos=2) step_2.answer = place_data step_2.result = get_or_create_step(search=search, step_pos=1).result step_2.save() realty_objects = RealtyObject.objects.filter( pk__in=ast.literal_eval( get_or_create_step(search=search, step_pos=2).result)) count = realty_objects.count() step_2.result = [ realty_object.pk for realty_object in realty_objects ] step_2.save() room_list = realty_objects.distinct('rooms_count').values( 'rooms_count') resp_data = { "step": 3, "template": "step_3", "answers": room_list, "count": count } return Response(data=resp_data, status=200) elif data.get('step') == '3' or data.get('step') == 3: search = SearchV2.objects.filter(user_identify=user_id, finished_at__isnull=True).last() try: rooms_count = ast.literal_eval(data.get('data')) except ValueError: rooms_count = data.get('data') search.last_step = 4 search.save() min_room = rooms_count[0] max_room = rooms_count[1] step_3 = get_or_create_step(search=search, step_pos=3) step_3.answer = rooms_count step_3.created_at = timezone.now() realty_objects = RealtyObject.objects.filter( pk__in=ast.literal_eval( get_or_create_step(search=search, step_pos=2).result), rooms_count__range=(min_room, max_room)) step_3.result = [ realty_object.pk for realty_object in realty_objects ] step_3.save() count = realty_objects.count() min_price = realty_objects.aggregate(Min('rent_price_eur')) max_price = realty_objects.aggregate(Max('rent_price_eur')) resp_data = { "step": 4, "template": "step_4", "answers": { "min_price": min_price, "max_price": max_price }, "count": count } return Response(data=resp_data, status=200) elif data.get('step') == '4' or data.get('step') == 4: search = SearchV2.objects.filter(user_identify=user_id, finished_at__isnull=True).last() try: min_max_price = ast.literal_eval(data.get('data')) except ValueError: min_max_price = data.get('data') search.last_step = 5 search.save() step_4 = get_or_create_step(search=search, step_pos=4) min_price = min_max_price[0] max_price = min_max_price[1] step_4.answer = min_max_price realty_objects = RealtyObject.objects.filter( pk__in=ast.literal_eval( get_or_create_step(search=search, step_pos=3).result), rent_price_eur__range=(min_price, max_price)) step_4.result = [ realty_object.pk for realty_object in realty_objects ] step_4.save() count = realty_objects.count() choices_list = DistanceChooseSerializer( DistanceChoose.objects.all(), many=True) resp_data = { "step": 5, "template": "step_5", "answers": choices_list.data, "count": count } return Response(data=resp_data, status=200) elif data.get('step') == '5' or data.get('step') == 5: search = SearchV2.objects.filter(user_identify=user_id, finished_at__isnull=True).last() step_5 = get_or_create_step(search=search, step_pos=5) step_5.answer = data.get('data')[0] search.last_step = 6 search.save() realty_objects = RealtyObject.objects.filter( pk__in=ast.literal_eval( get_or_create_step(search=search, step_pos=4).result), ) _school_distance = DistanceChoose.objects.get( pk=int(step_5.answer)) percent = PercentPass.objects.last().percent if _school_distance.distance > 0: step_5.result = [ r_obj.pk for r_obj in realty_objects.filter( realty_complex__nearest_school__distance__lte= _school_distance.distance / percent * 100) ] elif _school_distance.distance < 0: step_5.result = [ r_obj.pk for r_obj in realty_objects.filter( realty_complex__nearest_school__distance__gte=( -_school_distance.distance) / percent * 100) ] else: step_5.result = ast.literal_eval( get_or_create_step(search=search, step_pos=4).result) step_5.save() count = len(step_5.result) choices_list = DistanceChooseSerializer( DistanceChoose.objects.all(), many=True) resp_data = { "step": 6, "template": "step_6", "answers": choices_list.data, "count": count } return Response(data=resp_data, status=200) elif data.get('step') == '6' or data.get('step') == 6: search = SearchV2.objects.filter(user_identify=user_id, finished_at__isnull=True).last() step_6 = get_or_create_step(search=search, step_pos=6) step_6.answer = data.get('data')[0] step_6.save() search.last_step = 7 search.save() realty_objects = RealtyObject.objects.filter( pk__in=ast.literal_eval( get_or_create_step(search=search, step_pos=5).result), ) _park_distance = DistanceChoose.objects.get(pk=int(step_6.answer)) percent = PercentPass.objects.last().percent if _park_distance.distance > 0: step_6.result = [ r_obj.pk for r_obj in realty_objects.filter( realty_complex__nearest_park__distance__lte= _park_distance.distance / percent * 100) ] elif _park_distance.distance < 0: step_6.result = [ r_obj.pk for r_obj in realty_objects.filter( realty_complex__nearest_park__distance__lte=( -_park_distance.distance) / percent * 100) ] else: step_6.result = ast.literal_eval( get_or_create_step(search=search, step_pos=5).result) step_6.save() count = len(step_6.result) choices_list = DistanceChooseSerializer( DistanceChoose.objects.all(), many=True) resp_data = { "step": 7, "template": "step_7", "answers": choices_list.data, "count": count } return Response(data=resp_data, status=200) elif data.get('step') == '7' or data.get('step') == 7: search = SearchV2.objects.filter(user_identify=user_id, finished_at__isnull=True).last() step_7 = get_or_create_step(search=search, step_pos=7) step_7.answer = data.get('data')[0] search.last_step = 8 search.save() realty_objects = RealtyObject.objects.filter( pk__in=ast.literal_eval( get_or_create_step(search=search, step_pos=6).result), ) _market_distance = DistanceChoose.objects.get( pk=int(step_7.answer)) percent = PercentPass.objects.last().percent if _market_distance.distance > 0: step_7.result = [ r_obj.pk for r_obj in realty_objects.filter( realty_complex__nearest_market__distance__lte= _market_distance.distance / percent * 100) ] elif _market_distance.distance < 0: step_7.result = [ r_obj.pk for r_obj in realty_objects.filter( realty_complex__nearest_market__distance__lte=( -_market_distance.distance) / percent * 100) ] else: step_7.result = ast.literal_eval( get_or_create_step(search=search, step_pos=6).result) step_7.save() count = len(step_7.result) choices_list = DistanceChooseSerializer( DistanceChoose.objects.all(), many=True) resp_data = { "step": 8, "template": "step_8", "answers": choices_list.data, "count": count } return Response(data=resp_data, status=200) elif data.get('step') == '8' or data.get('step') == 8: search = SearchV2.objects.filter(user_identify=user_id, finished_at__isnull=True).last() step_8 = get_or_create_step(search=search, step_pos=8) step_8.answer = data.get('data')[0] search.last_step = 9 search.save() realty_objects = RealtyObject.objects.filter( pk__in=ast.literal_eval( get_or_create_step(search=search, step_pos=7).result), ) _pharmacy_distance = DistanceChoose.objects.get( pk=int(step_8.answer)) percent = PercentPass.objects.last().percent if _pharmacy_distance.distance > 0: step_8.result = [ r_obj.pk for r_obj in realty_objects.filter( realty_complex__nearest_pharmacy__distance__lte= _pharmacy_distance.distance / percent * 100) ] elif _pharmacy_distance.distance < 0: step_8.result = [ r_obj.pk for r_obj in realty_objects.filter( realty_complex__nearest_pharmacy__distance__lte=( -_pharmacy_distance.distance) / percent * 100) ] else: step_8.result = ast.literal_eval( get_or_create_step(search=search, step_pos=7).result) step_8.save() count = len(step_8.result) choices_list = DistanceChooseSerializer( DistanceChoose.objects.all(), many=True) resp_data = { "step": 9, "template": "step_9", "answers": choices_list.data, "count": count } return Response(data=resp_data, status=200) elif data.get('step') == '9' or data.get('step') == 9: search = SearchV2.objects.filter(user_identify=user_id, finished_at__isnull=True).last() step_9 = get_or_create_step(search=search, step_pos=9) step_9.answer = data.get('data')[0] search.last_step = 10 search.save() realty_objects = RealtyObject.objects.filter( pk__in=ast.literal_eval( get_or_create_step(search=search, step_pos=8).result), ) _night_distance = DistanceChoose.objects.get(pk=int(step_9.answer)) percent = PercentPass.objects.last().percent if _night_distance.distance > 0: step_9.result = [ r_obj.pk for r_obj in realty_objects.filter( realty_complex__nearest_nightclub__distance__lte= _night_distance.distance / percent * 100) ] elif _night_distance.distance < 0: step_9.result = [ r_obj.pk for r_obj in realty_objects.filter( realty_complex__nearest_nightclub__distance__lte=( -_night_distance.distance) / percent * 100) ] else: step_9.result = ast.literal_eval( get_or_create_step(search=search, step_pos=8).result) step_9.save() count = len(step_9.result) choices_list = DistanceChooseSerializer( DistanceChoose.objects.all(), many=True) resp_data = { "step": 10, "template": "step_10", "answers": choices_list.data, "count": count } return Response(data=resp_data, status=200) elif data.get('step') == '10' or data.get('step') == 10: search = SearchV2.objects.filter(user_identify=user_id, finished_at__isnull=True).last() step_10 = get_or_create_step(search=search, step_pos=10) step_10.answer = data.get('data')[0] search.last_step = 11 search.save() realty_objects = RealtyObject.objects.filter( pk__in=ast.literal_eval( get_or_create_step(search=search, step_pos=9).result), ) _gym_distance = DistanceChoose.objects.get(pk=int(step_10.answer)) percent = PercentPass.objects.last().percent if _gym_distance.distance > 0: step_10.result = [ r_obj.pk for r_obj in realty_objects.filter( realty_complex__nearest_gym__distance__lte=_gym_distance .distance / percent * 100) ] elif _gym_distance.distance < 0: step_10.result = [ r_obj.pk for r_obj in realty_objects.filter( realty_complex__nearest_gym__distance__lte=( -_gym_distance.distance) / percent * 100) ] else: step_10.result = ast.literal_eval( get_or_create_step(search=search, step_pos=9).result) step_10.save() """ _step_1 - Выбор районов _step_2 - Выбор кол-ва комнат _step_3 - Выбор мин/макс суммы _step_4 - Школы _step_5 - парки _step_6 - Супермаркеты _step_7 - Аптеки _step_8 - Ночная жизнь _step_9 - Спортзалы """ favorite_place_data = ast.literal_eval( get_or_create_step(search=search, step_pos=2).answer) favorite_lat = favorite_place_data[0] favorite_lng = favorite_place_data[1] favorite_type_pk = favorite_place_data[2] favorite_minutes = favorite_place_data[3] favorite_type = TravelType.objects.get( pk=int(favorite_type_pk)).tomtom_type range_data = maps.get_range(favorite_lat, favorite_lng, favorite_type, favorite_minutes) logger.info(range_data) range_data = range_data.get('reachableRange').get('boundary') logger.info(range_data) prepared_polygon = [] for point_bound in range_data: prepared_polygon.append((point_bound.get('latitude'), point_bound.get('longitude'))) polygon = Polygon(prepared_polygon) realty_objects = realty_objects.filter(pk__in=step_10.result) realty_objects_pk = [] for realty_object in realty_objects: if polygon.contains( Point(float(realty_object.realty_complex.lat), float(realty_object.realty_complex.lng))): realty_objects_pk.append(realty_object.pk) realty_objects = RealtyObject.objects.filter( pk__in=realty_objects_pk) _school_distance = DistanceChoose.objects.get( pk=int(get_or_create_step(search, 5).answer)) _park_distance = DistanceChoose.objects.get( pk=int(get_or_create_step(search, 6).answer)) _pharmacy_distance = DistanceChoose.objects.get( pk=int(get_or_create_step(search, 8).answer)) _night_distance = DistanceChoose.objects.get( pk=int(get_or_create_step(search, 9).answer)) _market_distance = DistanceChoose.objects.get( pk=int(get_or_create_step(search, 7).answer)) _gym_distance = DistanceChoose.objects.get( pk=int(get_or_create_step(search, 10).answer)) _school_percent_list = dict() _park_percent_list = dict() _pharmacy_percent_list = dict() _nightclub_percent_list = dict() _market_percent_list = dict() _gym_percent_list = dict() for realty_object in realty_objects: try: if _gym_distance.distance == 0: _percent = 100 elif _gym_distance.distance > 0: _distance = realty_object.realty_complex.tom_gym_dist.distance if _gym_distance.distance > _distance: _percent = 100 else: _percent = 100 - (_distance - _gym_distance.distance ) / _gym_distance.distance * 100 if _percent < 0: _percent = 0 else: _distance = realty_object.realty_complex.tom_gym_dist.distance if -_gym_distance.distance < _distance: _percent = 100 else: _percent = 100 - ( -_gym_distance.distance - _distance) / -_gym_distance.distance * 100 if _percent < 0: _percent = 0 _gym_json = {realty_object.pk: _percent} _gym_percent_list.update(_gym_json) except AttributeError as e: logger.warning('Problem for complex {}\n\n{}\n{}'.format( realty_object, realty_object.realty_complex.tom_gym_dist, e)) _gym_json = {realty_object.pk: 0} _gym_percent_list.update(_gym_json) try: if _school_distance.distance == 0: _percent = 100 elif _school_distance.distance > 0: _distance = realty_object.realty_complex.tom_school_dist.distance if _school_distance.distance > _distance: _percent = 100 else: _percent = 100 - ( _distance - _school_distance.distance ) / _school_distance.distance * 100 if _percent < 0: _percent = 0 else: _distance = realty_object.realty_complex.tom_school_dist.distance if -_school_distance.distance < _distance: _percent = 100 else: _percent = 100 - ( -_school_distance.distance - _distance) / -_school_distance.distance * 100 if _percent < 0: _percent = 0 _school_json = {realty_object.pk: _percent} _school_percent_list.update(_school_json) except AttributeError as e: logger.warning('Problem for complex {}\n\n{}\n{}'.format( realty_object, realty_object.realty_complex.tom_school_dist, e)) _gym_json = {realty_object.pk: 0} _gym_percent_list.update(_gym_json) ## try: if _pharmacy_distance.distance == 0: _percent = 100 elif _pharmacy_distance.distance > 0: _distance = realty_object.realty_complex.tom_pharmacy_dist.distance if _pharmacy_distance.distance > _distance: _percent = 100 else: _percent = 100 - (_distance - _pharmacy_distance.distance) / \ _pharmacy_distance.distance * 100 if _percent < 0: _percent = 0 else: _distance = realty_object.realty_complex.tom_pharmacy_dist.distance if -_pharmacy_distance.distance < _distance: _percent = 100 else: _percent = 100 - (- _pharmacy_distance.distance - _distance) / \ - _pharmacy_distance.distance * 100 if _percent < 0: _percent = 0 _pharmacy_json = {realty_object.pk: _percent} _pharmacy_percent_list.update(_pharmacy_json) except AttributeError as e: logger.warning('Problem for complex {}\n\n{}\n{}'.format( realty_object, realty_object.realty_complex.tom_pharmacy_dist, e)) _pharmacy_json = {realty_object.pk: 0} _pharmacy_percent_list.update(_pharmacy_json) ## try: if _night_distance.distance == 0: _percent = 100 elif _night_distance.distance > 0: _distance = realty_object.realty_complex.tom_nightclub_dist.distance if _night_distance.distance > _distance: _percent = 100 else: _percent = 100 - ( _distance - _night_distance.distance ) / _night_distance.distance * 100 if _percent < 0: _percent = 0 else: _distance = realty_object.realty_complex.tom_nightclub_dist.distance if -_night_distance.distance < _distance: _percent = 100 else: _distance = realty_object.realty_complex.tom_nightclub_dist.distance _percent = 100 - ( -_night_distance.distance - _distance) / -_night_distance.distance * 100 if _percent < 0: _percent = 0 _nightclub_json = {realty_object.pk: _percent} _nightclub_percent_list.update(_nightclub_json) except AttributeError as e: logger.warning('Problem for complex {}\n\n{}\n{}'.format( realty_object, realty_object.realty_complex.tom_nightclub_dist, e)) _nightclub_json = {realty_object.pk: 0} _nightclub_percent_list.update(_nightclub_json) ## try: if _market_distance.distance == 0: _percent = 100 elif _market_distance.distance > 0: _distance = realty_object.realty_complex.tom_market_dist.distance if _market_distance.distance > _distance: _percent = 100 else: _percent = 100 - ( _distance - _market_distance.distance ) / _market_distance.distance * 100 if _percent < 0: _percent = 0 else: _distance = realty_object.realty_complex.tom_market_dist.distance if -_market_distance.distance < _distance: _percent = 100 else: _percent = 100 - \ (- _market_distance.distance - _distance) / - _market_distance.distance * 100 if _percent < 0: _percent = 0 _market_json = {realty_object.pk: _percent} _market_percent_list.update(_market_json) except AttributeError as e: logger.warning('Problem for complex {}\n\n{}\n{}'.format( realty_object, realty_object.realty_complex.tom_market_dist, e)) _market_json = {realty_object.pk: 0} _market_percent_list.update(_market_json) try: if _park_distance.distance == 0: _percent = 100 elif _park_distance.distance > 0: _distance = realty_object.realty_complex.tom_park_dist.distance if _park_distance.distance > _distance: _percent = 100 else: _percent = 100 - (_distance - _park_distance.distance ) / _park_distance.distance * 100 if _percent < 0: _percent = 0 else: _distance = realty_object.realty_complex.tom_park_dist.distance if -_park_distance.distance < _distance: _percent = 100 else: _percent = 100 - ( -_park_distance.distance - _distance) / -_park_distance.distance * 100 if _percent < 0: _percent = 0 _park_json = {realty_object.pk: _percent} _park_percent_list.update(_park_json) except AttributeError as e: logger.warning('Problem for complex {}\n\n{}\n{}'.format( realty_object, realty_object.realty_complex.tom_park_dist, e)) _park_json = {realty_object.pk: 0} _park_percent_list.update(_park_json) _final_list = [] for realty_object in realty_objects: try: _object_json = { "object_id": realty_object.pk, "scoring": { "gym": int(_gym_percent_list.get(realty_object.pk)), "school": int(_school_percent_list.get(realty_object.pk)), "park": int(_park_percent_list.get(realty_object.pk)), "pharmacy": int(_pharmacy_percent_list.get(realty_object.pk)), "cafe": int(_nightclub_percent_list.get(realty_object.pk)), "market": int(_market_percent_list.get(realty_object.pk)), "total": (int(_school_percent_list.get(realty_object.pk)) + int(_park_percent_list.get(realty_object.pk)) + int(_pharmacy_percent_list.get( realty_object.pk)) + int(_nightclub_percent_list.get(realty_object.pk)) + int(_gym_percent_list.get(realty_object.pk)) + int(_market_percent_list.get(realty_object.pk))) / 6 }, } _final_list.append(_object_json) except Exception as e: logger.warning('Some shit happens \n{}'.format(e)) def extract_score(json): try: return int(json['scoring']['total']) except KeyError: return 0 _final_list.sort(key=extract_score, reverse=True) _short_list = _final_list[:10] for i in _short_list: realty_object = RealtyObject.objects.get(pk=i.get('object_id')) data = { "info": RealtyObjectShortSerializer(realty_object).data, "nearby": { "school": TomTomDistanceMatrixSerializer( realty_object.realty_complex.tom_school_dist).data, "gym": TomTomDistanceMatrixSerializer( realty_object.realty_complex.tom_gym_dist).data, "park": TomTomDistanceMatrixSerializer( realty_object.realty_complex.tom_park_dist).data, "pharmacy": TomTomDistanceMatrixSerializer( realty_object.realty_complex.tom_pharmacy_dist). data, "cafe": TomTomDistanceMatrixSerializer( realty_object.realty_complex.tom_nightclub_dist). data, "market": TomTomDistanceMatrixSerializer( realty_object.realty_complex.tom_market_dist).data, } } i.update(data) search.result = json.dumps(_short_list) search.save() search.result_full = json.dumps(_final_list) search.save() prepare_final_json.apply_async(args=[search.pk]) resp_data = { "step": 11, "template": "step_final", "search_id": search.hashed_id, "count": realty_objects.count() } return Response(data=resp_data, status=200) else: return Response(request.data)
def print_closest_dis(): # Load data df = pd.read_csv( 'https://raw.githubusercontent.com/trendct/dunkin-donuts-ct/master/dunkindonuts.csv' ) ma_dunkin_donuts = df.loc[df['state'] == 'MA'] dunkin_donuts_coordinates = ma_dunkin_donuts[['lng', 'lat']].to_numpy() with urlopen( 'https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json' ) as response: counties = json.load(response) df = pd.json_normalize(counties['features']) ma_geodata = pd.json_normalize(counties['features'])[ df['properties.STATE'] == '25']['geometry.coordinates'].to_numpy() ma_coordinates = flatten(ma_geodata) # Create Voronoi and Polygons vor = Voronoi(dunkin_donuts_coordinates) polygons = [Polygon(county) for county in ma_coordinates] polygon = MultiPolygon(polygons) biggestDistanceYet = 0 bestVertex = [] closest_dunkin_coord = [] # Calculate best vertex for vertex in vor.vertices: closest_dunkin_branch = closest_dunkin(vertex, dunkin_donuts_coordinates) minDis = distance.cdist([vertex], [closest_dunkin_branch]) if biggestDistanceYet < minDis and polygon.contains(Point(vertex)): biggestDistanceYet = minDis bestVertex = vertex closest_dunkin_coord = closest_dunkin_branch print( "The furthest away you can get from a dunkin donuts is {} km at the coordinates {}. " "The closest dunkin donuts would be at {}.".format( haversine_np(bestVertex, closest_dunkin_coord), bestVertex, closest_dunkin_coord)) # Plot everything fig = voronoi_plot_2d(vor, show_vertices=False, line_width=0) ax = fig.gca() ax.add_patch( plt.Circle(bestVertex, biggestDistanceYet, color='g', fill=False, linewidth=2)) plt.plot(bestVertex[0], bestVertex[1], 'o') plt.ylim(40.5, 43) for a in polygons: x, y = a.exterior.xy plt.plot(x, y) ax.set_aspect('equal') ax.set_ylabel('Latitude') ax.set_xlabel('Longitude') plt.show() fig.savefig('vonoroi.png')
def categorizeBuildings(self): def isLeft(a,b,c): return ((b[0] - a[0])*(c[1] - a[1]) - (b[1] - a[1])*(c[0] - a[0])) > 0; def getIntersectionArea(bldPoly,line,perpLine,bldID): if not line.intersects(bldPoly): return 0.0 pt1 = list(line.coords)[0] pt2 = list(line.coords)[1] perppt1 = list(perpLine.coords)[0] perppt2 = list(perpLine.coords)[1] dx = perppt2[0] - perppt1[0] dy = perppt2[1] - perppt1[1] pt3 = (pt1[0]-dx,pt1[1]-dy) pt4 = (pt2[0]-dx,pt2[1]-dy) linePoly = Polygon([pt1,pt3,pt4,pt2]) try: intersection_area = linePoly.intersection(bldPoly).area return intersection_area/bldPoly.area except: return -1 def overlapsNeighbor(bldPoly,neighborPoly): try: intersection_area = bldPoly.intersection(neighborPoly).area return intersection_area/bldPoly.area > 0.05 except: return False Rd2BldLeft = {} Rd2BldRight = {} for bld in self.buildingCenters.keys(): bldID = self.buildingCenters[bld][0] #if bldID < 3700 or bldID > 3800: #if bldID != 3751: #continue roads = self.segment_lookup([bld[0],bld[1]],10) p1 = (bld[0], bld[1]) p1LatLon = to_latlon(p1) res = self.buildingKDTree.query([bld[0],bld[1]], 20) neighbors = [] for item in res[1][1:]: #start from index 1 because index 0 is always the original building bld buildingID = self.buildingCenters[self.buildingCenters.keys()[item]][0] neighbor = self.buildings[buildingID] #this part removes a bug that some neighbor shapes have extra information, which will result in Polygon error later on. start = neighbor[0] for i in range(1,len(neighbor)): if neighbor[i] == start and i > 2: break neighbor = neighbor[:i+1] neighbors.append(neighbor) roadDict = {} bldVertices = self.buildingCenters[bld][1] bldPolygon = Polygon(bldVertices) for rd in roads: rdLine = LineString([rd[0],rd[1]]) intersectsSomething = False p3Intersect = self.perpIntersectPoint(p1,rd[0],rd[1]) if not p3Intersect: continue bldVertices.append(p1) for vertex in bldVertices: if intersectsSomething: break p3 = self.perpIntersectPoint(vertex,rd[0],rd[1]) vertexLatLon = to_latlon(vertex) if p3: p3LatLon = to_latlon(p3) perpLine = LineString([p1,p3]) for neighbor in neighbors: neighborPoly = Polygon(neighbor) if overlapsNeighbor(bldPolygon,neighborPoly): continue if perpLine.intersects(neighborPoly): intersectRd = False intersectionRdArea = 0 if neighborPoly.intersects(rdLine): intersectRd = True intersectionRdArea = getIntersectionArea(neighborPoly,rdLine,perpLine,bldID) if intersectionRdArea > 0.4 or not intersectRd: #if bldID == 4287 or bldID == 4288: #print intersectionRdArea #road_lines.record(0) #road_lines.poly(shapeType=shapefile.POLYLINE, parts=[[vertexLatLon,p3LatLon]]) intersectsSomething = True break for rd2 in roads: if rd2 == rd: continue roadLine2 = LineString([rd2[0],rd2[1]]) if perpLine.intersects(roadLine2): intersectsSomething = True break if not intersectsSomething: perpLine = LineString([p1,p3Intersect]) if perpLine.length > (3*bldPolygon.length)/4: continue #if rdLine.length < (bldPolygon.length/3): # continue p3IntersectLatLon = to_latlon(p3Intersect) road_lines.record(0) road_lines.poly(shapeType=shapefile.POLYLINE, parts=[[p3IntersectLatLon,p1LatLon]]) if isLeft(rd[0],rd[1],p1): if rd not in Rd2BldLeft: Rd2BldLeft[rd] = [bldID] else: Rd2BldLeft[rd].append(bldID) else: if rd not in Rd2BldRight: Rd2BldRight[rd] = [bldID] else: Rd2BldRight[rd].append(bldID) return (Rd2BldLeft, Rd2BldRight)