def find_nearest_node(center_cord, node_cord): """ Computes the nearest node in the dictionary 'node_cord' to the point denoted by the 'center_cord' center_cord: TYPE: list of two entries DESCRIPTION: geographical coordinates of the center denoted by a list of two entries node_cord: TYPE: dictionary DESCRIPTION: dictionary of nodelist with values as the geographical coordinate """ xmin, ymin = np.min(np.array(list(node_cord.values())), axis=0) xmax, ymax = np.max(np.array(list(node_cord.values())), axis=0) bbox = (xmin, ymin, xmax, ymax) idx = Index(bbox) nodes = [] for i, n in enumerate(list(node_cord.keys())): node_geom = Point(node_cord[n]) node_bound = bounds(node_geom, 0.0) idx.insert(i, node_bound) nodes.append((node_geom, node_bound, n)) pt_center = Point(center_cord) center_bd = bounds(pt_center, 0.1) matches = idx.intersect(center_bd) closest_node = min(matches, key=lambda i: geodist(nodes[i][0], pt_center)) return nodes[closest_node][-1]
def query(self, file, chr, start, end, zoomlvl=-2): chromLength = self.genome[chr] dims = 2 hlevel = math.ceil(math.log2(chromLength) / dims) # print("hlevel", hlevel) x_y_dim = math.ceil(math.pow(2, hlevel)) # print("max x|y =", x_y_dim) tree = Index(bbox=(0, 0, x_y_dim, x_y_dim), disk=base_path + "quadtree." + chr + ".index") xstart, ystart, _ = hcoords(start, chromLength) xend, yend, _ = hcoords(end, chromLength) overlapbbox = (start - 1, start - 1, end + 1, end + 1) matches = tree.intersect(overlapbbox) df = pandas.DataFrame( matches, columns=["start", "end", "offset", "size", "fileid"]) df = df[df["fileid"] == self.file_mapping[file]] bw = self.file_objects[file] chrmId = bw.chrmIds[chr] result = [] for i, row in df.iterrows(): result += bw.parseLeafDataNode(chrmId, start, end, zoomlvl, chrmId, row["start"], chrmId, row["end"], row["offset"], row["size"]) result = toDataFrame(values, bw.columns) result["chr"] = chr return result
def fit_transform(self, X): self.check_data(X) max_x, max_y = X.max(axis=0) quadtree = Index(bbox=[0, 0, max_x + 10, max_y + 10]) for i, x in enumerate(X): bbox = (x[0], x[1], x[0], x[1]) quadtree.insert(item={ 'point': x, 'bbox': bbox, 'id': i }, bbox=bbox) maxy = int(max_x / self.size + 50) maxx = int(max_y / self.size + 50) cells = [] for i in range(1, maxy + 1): for j in range(1, maxx + 1): bbox = ((i - 1) * self.size, (j - 1) * self.size, i * self.size, j * self.size) d = quadtree.intersect(bbox) if len(d) != 0: cells.append({'x': (i - 1), 'y': (j - 1), 'points': d}) samples = self._sample(quadtree, cells, self.size, self.alpha, self.size_search, maxx, maxy, 1) return np.array([u['id'] for u in samples])
def nearest(main_points, aux_points, radius=1): """ Objective is to find the nearest point to the points in the aux_points set. The nearest point is to be identified among the points in the main_points set. """ xmin, ymin = np.min(np.array(list(main_points.values())), axis=0) xmax, ymax = np.max(np.array(list(main_points.values())), axis=0) bbox = (xmin, ymin, xmax, ymax) idx = Index(bbox) # keep track of points so we can recover them later points = [] # create bounding box around each point in main_points set for i, p in enumerate(main_points): pt_main = Point(main_points[p]) pt_bounds_main = pt_main.x-radius, pt_main.y-radius, \ pt_main.x+radius, pt_main.y+radius idx.insert(i, pt_bounds_main) points.append((pt_main, pt_bounds_main, p)) # find intersection with bounding box around aux point set for n in aux_points: pt_aux = Point(aux_points[n]) pt_bounds_aux = pt_aux.x-radius, pt_aux.y-radius, \ pt_aux.x+radius, pt_aux.y+radius matches = idx.intersect(pt_bounds_aux) ind_closest = min(matches, key=lambda i: geodist(points[i][0], pt_aux)) print(points[ind_closest][-1]) return
def add_to_index(self, file, zoomlvl=-2): tree, bw = self.get_file_btree(file, zoomlvl) df = self.get_leaf_nodes(tree, bw, zoomlvl) chrmTree = self.get_file_chr(bw) self.file_mapping[file] = self.file_counter self.file_counter += 1 self.file_objects[file] = bw for chrm in chrmTree.keys(): chromLength = self.genome[chrm] dims = 2 hlevel = math.ceil(math.log2(chromLength) / dims) # print("hlevel", hlevel) x_y_dim = math.ceil(math.pow(2, hlevel)) # print("max x|y =", x_y_dim) tree = Index(bbox=(0, 0, x_y_dim, x_y_dim), disk=base_path + "quadtree." + chrm + ".index") chrmId = chrmTree[chrm] df = df[df["rStartChromIx"] == chrmId] # print("\t df shape - ", df.shape) for i, row in df.iterrows(): x, y, _ = hcoords(row["rStartBase"], chromLength) tree.insert( (row["rStartBase"], row["rEndBase"], row["rdataOffset"], row["rDataSize"], fileIds[file]), (x, y, x + 1, y + 1))
def query_quadtree(chr, start, end, files): chromLength = chromosomes[chr] print("chromLength", chromLength) dims = 2 hlevel = math.ceil(math.log2(chromLength) / dims) print("hlevel", hlevel) x_y_dim = math.ceil(math.pow(2, hlevel)) print("max x|y =", x_y_dim) # tree = Index(bbox=(0, 0, x_y_dim, x_y_dim), disk = "quadtree/indexes/roadmap." + chr + ".quadtree.index") tree = Index(bbox=(0, 0, x_y_dim, x_y_dim), disk="./roadmap." + chr + ".quadtree.index") xstart, ystart, _ = hcoords(start, chromLength) xend, yend, _ = hcoords(end, chromLength) print("xstart, ystart, xend, yend", xstart, ystart, xend, yend) margin = 0 overlapbbox = (xstart - margin, ystart - margin, xend + margin, yend + margin) matches = tree.intersect(overlapbbox) df = pandas.DataFrame(matches, columns=["start", "end", "offset", "size", "fileid"]) print("before file filter") print(df.shape) print(df) df = df[(df["fileid"].isin(files)) & (df["start"] <= end) & (df["end"] >= start)] print("after file filter") print(df.shape) print(df) return df
def __init__(self,road,radius=0.01): ''' ''' longitudes = [road.nodes[n]['x'] for n in road.nodes()] latitudes = [road.nodes[n]['y'] for n in road.nodes()] xmin = min(longitudes); xmax = max(longitudes) ymin = min(latitudes); ymax = max(latitudes) bbox = (xmin,ymin,xmax,ymax) # keep track of lines so we can recover them later all_link = list(road.edges()) self.lines = [] # initialize the quadtree index self.idx = Index(bbox) # add edge bounding boxes to the index for i, link in enumerate(all_link): # create line geometry link_geom = road.edges[link]['geometry'] # bounding boxes, with padding x1, y1, x2, y2 = link_geom.bounds bounds = x1-radius, y1-radius, x2+radius, y2+radius # add to quadtree self.idx.insert(i, bounds) # save the line for later use self.lines.append((link_geom, bounds, link)) return
def produce_fov_ids(lookers, viewed, count, fov_dilation=[-0.1, -0.1, 0.1, 0.1]): """ Produce ids of bboxes each of lookers can 'see' (intersect) in the viewed wordbox. bbox = l, t, r, b """ xs = [item[0] for item in viewed] + [item[2] for item in viewed] ys = [item[1] for item in viewed] + [item[3] for item in viewed] l = min(xs) t = min(ys) r = max(xs) b = max(ys) spindex = Index(bbox=(l, t, r, b)) # this example assumes you have a list of items with bbox attribute for i, bbox in enumerate(viewed): spindex.insert(i, bbox) ids_r = np.full((len(lookers), count), -1) for i, bbox in enumerate(lookers): matches = spindex.intersect(fov_dilate(bbox, fov_dilation)) # now find the closest to the center of the original: center = bb_center(bbox) matches.sort( key=lambda j: ss_to_center(center, viewed[j])) # ascending cnt = min(len(matches), count) ids_r[i, :cnt] = matches[:cnt] return ids_r
def test_should_empty_index_after_removing_multiple_added_nodes_in_all_quad_nodes(): index = Index(INDEX_BBOX, max_items=1) index.insert(ITEM1, BBOX1) index.insert(ITEM2, INDEX_BBOX) index.remove(ITEM1, BBOX1) index.remove(ITEM2, INDEX_BBOX) assert len(index) == 0 assert index.intersect(INDEX_BBOX) == []
def combine_osm_components(road,radius = 0.01): """ Combines network components by finding nearest nodes based on a QD Tree Approach. Parameters ---------- graph : TYPE DESCRIPTION. Returns ------- None. """ # Initialize QD Tree longitudes = [road.nodes[n]['x'] for n in road.nodes()] latitudes = [road.nodes[n]['y'] for n in road.nodes()] xmin = min(longitudes); xmax = max(longitudes) ymin = min(latitudes); ymax = max(latitudes) bbox = (xmin,ymin,xmax,ymax) idx = Index(bbox) # Differentiate large and small components comps = [c for c in list(nx.connected_components(road))] lencomps = [len(c) for c in list(nx.connected_components(road))] indlarge = lencomps.index(max(lencomps)) node_main = list(road.subgraph(comps[indlarge]).nodes()) del(comps[indlarge]) # keep track of nodes so we can recover them later nodes = [] # create bounding box around each point in large component for i,node in enumerate(node_main): pt = Point([road.nodes[node]['x'],road.nodes[node]['y']]) pt_bounds = pt.x-radius, pt.y-radius, pt.x+radius, pt.y+radius idx.insert(i, pt_bounds) nodes.append((pt, pt_bounds, node)) # find intersection and add edges edgelist = [] for c in comps: node_comp = list(road.subgraph(c).nodes()) nodepairs = [] for n in node_comp: pt = Point([road.nodes[n]['x'],road.nodes[n]['y']]) pt_bounds = pt.x-radius, pt.y-radius, pt.x+radius, pt.y+radius matches = idx.intersect(pt_bounds) closest_pt = min(matches,key=lambda i: nodes[i][0].distance(pt)) nodepairs.append((n,nodes[closest_pt][-1])) # Get the geodesic distance dist = [MeasureDistance([road.nodes[p[0]]['x'],road.nodes[p[0]]['y']], [road.nodes[p[1]]['x'],road.nodes[p[1]]['y']]) \ for p in nodepairs] edgelist.append(nodepairs[np.argmin(dist)]+tuple([0])) return edgelist
def combine_components(graph,cords,radius = 0.01): """ Combines network components by finding nearest nodes based on a QD Tree Approach. Parameters ---------- graph : TYPE DESCRIPTION. Returns ------- None. """ # Initialize QD Tree xmin,ymin = np.min(np.array(list(cords.values())),axis=0) xmax,ymax = np.max(np.array(list(cords.values())),axis=0) bbox = (xmin,ymin,xmax,ymax) idx = Index(bbox) # Differentiate large and small components comps = [c for c in list(nx.connected_components(graph))] lencomps = [len(c) for c in list(nx.connected_components(graph))] indlarge = lencomps.index(max(lencomps)) node_main = list(graph.subgraph(comps[indlarge]).nodes()) del(comps[indlarge]) # keep track of lines so we can recover them later nodes = [] # create bounding box around each point in large component for i,node in enumerate(node_main): pt = Point(cords[node]) pt_bounds = pt.x-radius, pt.y-radius, pt.x+radius, pt.y+radius idx.insert(i, pt_bounds) nodes.append((pt, pt_bounds, node)) # find intersection and add edges edgelist = [] for c in comps: node_comp = list(graph.subgraph(c).nodes()) nodepairs = [] for n in node_comp: pt = Point(cords[n]) pt_bounds = pt.x-radius, pt.y-radius, pt.x+radius, pt.y+radius matches = idx.intersect(pt_bounds) closest_pt = min(matches,key=lambda i: nodes[i][0].distance(pt)) nodepairs.append((n,nodes[closest_pt][-1])) dist = [MeasureDistance(cords[p[0]],cords[p[1]]) for p in nodepairs] edgelist.append(nodepairs[np.argmin(dist)]) return edgelist
def build_qtree(self): bounds_prep = prep(self.bounds) bbox = [] for val in self.bounds.bounds: bbox.append(val * 2) #ret = Index(bbox=bbox, maxdepth=100000) ret = Index(bbox=bbox) for segment in self.parentage.nodes(): if segment.abs_polygon: seg_poly = segment.abs_polygon if not bounds_prep.disjoint(seg_poly): ret.insert(segment, segment.abs_polygon.bounds) return ret
def update(self): collisionIndex = Index((0, 0, 800, 600)) for o in self.gameObjects: o.update() if o.collidable: collisionIndex.insert( o, (o.rect.left, o.rect.top, o.rect.right, o.rect.bottom)) for i in self.collidableObjects: collisions = collisionIndex.intersect( (i.rect.left, i.rect.top, i.rect.right, i.rect.bottom)) if (len(collisions) > 1): # Intersecting more than self # logging.info("Collision!") CollisionHandler(collisions)
def __init__(self,road,radius=0.01): """ Initializes the class object by creating a bounding box of known radius around each OSM road link. Parameters ---------- road : networkx Multigraph The Open Street Map multigraph with node and edge attributes. radius : float, optional The radius of the bounding box around each road link. The default is 0.01. Returns ------- None. """ longitudes = [road.nodes[n]['x'] for n in road.nodes()] latitudes = [road.nodes[n]['y'] for n in road.nodes()] xmin = min(longitudes); xmax = max(longitudes) ymin = min(latitudes); ymax = max(latitudes) bbox = (xmin,ymin,xmax,ymax) # keep track of edges so we can recover them later all_link = list(road.edges(keys=True)) self.links = [] # initialize the quadtree index self.idx = Index(bbox) # add edge bounding boxes to the index for i, link in enumerate(all_link): # create line geometry link_geom = road.edges[link]['geometry'] # bounding boxes, with padding x1, y1, x2, y2 = link_geom.bounds bounds = x1-radius, y1-radius, x2+radius, y2+radius # add to quadtree self.idx.insert(i, bounds) # save the line for later use self.links.append((link_geom, bounds, link)) return
def __init__(self, filename, scene_scale, min_distance): super(Converter, self).__init__() print("Reading OpenDrive file: " + filename) self.opendrive = OpenDrive(filename) self.lat, self.lon = self.get_center() minx, miny, maxx, maxy = self.set_scale() self.min_distance = min_distance # print(self.scale) print("Converting...") self.node_id = 0 self.ways = dict() self.nodes = list() self.spindex = Index(bbox=(minx, miny, maxx, maxy)) self.convert() print("done")
def create_quadtree(file): # go through the file and create a dot object for each line and add it to a list dot_list = [] for line in file: line = line.split() x = float(line[0]) y = float(line[1]) label = line[2] dot_list.append(Dot(label, (x, y, x, y))) file.close() # add all dots to the quadtree spindex = Index(bbox=(0, 0, 1, 1)) for dot in dot_list: spindex.insert(dot, dot.bbox) return spindex
def evaluate(im, save=False): width, height = im.shape rect_im = (0, 0, width, height) # fractal method qtree = traverse(im, rect_im, 1, Index(bbox=rect_im)) regions = qtree.intersect(rect_im) transformations = compress(im, 8, 4, 8) # rmse: np.sqrt(np.mean(np.square(target - img))) PCt0 = sum(1 for r in regions if r.level < max_depth - 1) PCt1 = sum(1 for r in regions if r.level < max_depth) PC = len(regions) / im.size # processing_compression_ratio # jpeg method im_pil = Image.fromarray(im * 255) im_pil = im_pil.convert('RGB') jpegbuf = io.BytesIO() im_pil.save(jpegbuf, format='JPEG', quality=95) nbytes_rgb_raw = im.size nbytes_jpeg = jpegbuf.getbuffer().nbytes IC = nbytes_jpeg / nbytes_rgb_raw # complexity compression ratio # fitness fitness = IC**a / (PCt0 * PCt1)**b * ((PCt1 - PCt0) / PCt1)**c #fitness = IC / PC if save: fig, ax = plt.subplots(1) ax.set_xlim(0, width) ax.set_ylim(0, height) ax.imshow(im) for r in regions: x1, y1 = r.location w, h = r.size rect = patches.Rectangle((x1, y1), w, h, linewidth=0.5, edgecolor='r', facecolor='none') ax.add_patch(rect) plt.text(0, 1, f'{round(fitness, 8)}', fontsize=12, color='w') print(f'P compression ratio: {PC}\nC compression ratio: {IC}') print( f'{nbytes_rgb_raw} raw bytes\n{nbytes_jpeg} compressed bytes\n{len(regions)} regions / {im.size} px' ) plt.savefig( f'results/eval/{datetime.now().strftime("%Y%m%d_%H%M%S")}.png') return fitness
def test_should_empty_index_after_removing_multiple_added_nodes_in_separate_quad_nodes(): index = Index(INDEX_BBOX, max_items=1) index.insert(ITEM1, BBOX1) index.insert(ITEM2, BBOX2) index.remove(ITEM1, BBOX1) index.remove(ITEM2, BBOX2) assert len(index) == 0 assert index.intersect(INDEX_BBOX) == []
def __init__(self, blocks): bboxs = [block.bounding_box for block in blocks] xmax = max([bb.x + bb.width for bb in bboxs]) ymax = max([bb.y + bb.height for bb in bboxs]) self.spindex = PqtreeIndex(bbox=(0, 0, xmax, ymax)) self.wrapper_map = {} for block in blocks: wrapper = DeletableWrapper(block) self.wrapper_map[block] = wrapper self.spindex.insert(wrapper, _to_bbox(block.bounding_box))
def test_should_empty_index_after_removing_multiple_added_nodes_in_multiple_vertical_quad_nodes(): index = Index(INDEX_BBOX, max_items=1) bbox2 = (0, 0, 1, INDEX_BBOX[3]) index.insert(ITEM1, BBOX1) index.insert(ITEM2, bbox2) index.remove(ITEM1, BBOX1) index.remove(ITEM2, bbox2) assert len(index) == 0 assert index.intersect(INDEX_BBOX) == []
def __apply_quadtree_pyqtree(self, ntwk, ltype): ntwk_w_data_ltype = ntwk[ntwk['LinkID_ptv'].isin( self._links_with_data)] print 'ltype', ltype, '# of links w/ data =', ntwk_w_data_ltype.shape[ 0] spindex = Index(bbox=self._bbox, max_items=int( min(ntwk_w_data_ltype.shape[0] * PCT_LINKS_IN_CELL, MAX_CROWD)), max_depth=MAX_DEPTH) for i in xrange(ntwk_w_data_ltype.shape[0]): #print ltype, i, '/ ', ntwk_w_data_ltype.shape[0], '(', float(i)/ntwk_w_data_ltype.shape[0]*100, '%)' link = ntwk_w_data_ltype.iloc[i] spindex.insert(link['LinkID_ptv'], bbox=(link['centroid'] * 2)) link_cellid_map = self.__get_link_cube_tuple(spindex, ltype) assert len(link_cellid_map) == ntwk_w_data_ltype.shape[0] #now assign cell id;s to the links without data by looking at their nearest neighboring link's cell_id link_cellid_map = self.__find_cellid_neighbors(spindex, ntwk, link_cellid_map, ltype) return link_cellid_map
class BlockSearch(object): def __init__(self, blocks): bboxs = [block.bounding_box for block in blocks] xmax = max([bb.x + bb.width for bb in bboxs]) ymax = max([bb.y + bb.height for bb in bboxs]) self.spindex = PqtreeIndex(bbox=(0, 0, xmax, ymax)) self.wrapper_map = {} for block in blocks: wrapper = DeletableWrapper(block) self.wrapper_map[block] = wrapper self.spindex.insert(wrapper, _to_bbox(block.bounding_box)) def find_intersection_with(self, search_bounding_box): return [ wrapper.data for wrapper in self.spindex.intersect( _to_bbox(search_bounding_box)) if not wrapper.deleted ] def remove(self, block): wrapper = self.wrapper_map.get(block) if wrapper is not None: wrapper.deleted = True
def test_should_add_multiple_nodes_and_find_them(): index = Index(INDEX_BBOX) index.insert(ITEM1, BBOX1) index.insert(ITEM2, BBOX2) assert len(index) == 2 assert index.intersect(BBOX1) == {ITEM1} assert index.intersect(BBOX2) == {ITEM2} assert index.intersect(INDEX_BBOX) == {ITEM1, ITEM2}
class TreeStruct: bbox = (-10000, -10000, 10000, 10000) ## # Maintain link from points to rectangle objects? def __init__(self): self.index = Index(bbox=self.bbox, max_items=40, max_depth=15) def addRect(self, rect): if (bboxoutside(rect.bbox, self.bbox)): print('outside') pass # TODO self.index.insert(rect, rect.bbox) def removeRect(self, rect): self.index.remove(rect, rect.bbox) def query(self, rect): candidates = self.index.intersect(rect.bbox) # return candidates # TBD if we want to make the check both ways or are okay with overlaps only being detected on one end return [candidate for candidate in candidates if rect.overlaps(candidate) or candidate.overlaps(rect)]
def create_quadTree(chr): print("processing ", chr) chromLength = chromosomes[chr] dims = 2 hlevel = math.ceil(math.log2(chromLength) / dims) print("hlevel", hlevel) x_y_dim = math.ceil(math.pow(2, hlevel)) print("max x|y =", x_y_dim) # f = open("quadtree/chrmids.json") # chromIndexes = json.loads(f) # tree = Index(bbox=(0, 0, x_y_dim, x_y_dim), disk="quadtree/indexes/roadmap." + chr + ".quadtree.index", first_run=True) tree = Index(bbox=(0, 0, x_y_dim, x_y_dim), disk="./roadmap." + chr + ".quadtree.index") for file in chromIndexes.keys(): print("\t file - ", file) if chr in chromIndexes[file]: chrmId = chromIndexes[file][chr] df = pandas.read_csv("quadtree/processed/" + file + ".leaves", header=0) df = df[df["rStartChromIx"] == chrmId] print("\t df shape - ", df.shape) for i, row in df.iterrows(): # print(row) x_start, y_start, _ = hcoords(row["rStartBase"], chromLength) x_end, y_end, hlevel = hcoords(row["rEndBase"], chromLength) bbox = range2bbox(hlevel, { "start": row["rStartBase"], "end": row["rEndBase"] }) tree.insert( (row["rStartBase"], row["rEndBase"], row["rdataOffset"], row["rDataSize"], fileIds[file]), bbox) else: print("\t !!!!!!! chrm doesn't exist - ", file)
def test_should_empty_index_after_removing_multiple_added_nodes_in_multiple_horizontal_quad_nodes(): index = Index(INDEX_BBOX, max_items=1) bbox2 = (0, 0, INDEX_BBOX[2], 1) index.insert(ITEM1, BBOX1) index.insert(ITEM2, bbox2) index.remove(ITEM1, BBOX1) index.remove(ITEM2, bbox2) assert len(index) == 0 assert index.intersect(INDEX_BBOX) == []
def main(): file_path = os.path.dirname(os.path.realpath(__file__)) + '/datafromndr' + "/norrkoping.las" lasdata = LasFileHandler(file_path) lasdata.printshit() bb = lasdata.get_bounding_box() temp_bb = bounding_box(bb.center[0],bb.center[1],200,200) aoi_bb = BoundingBox2d(temp_bb[2],temp_bb[0],temp_bb[3],temp_bb[1]) print(bb) #(xmin, ymin, xmax, ymax) n_boxes = 2000000 x_vals = np.random.randint(bb.xmin,bb.xmax,(n_boxes,1)) y_vals = np.random.randint(bb.ymin,bb.ymax,(n_boxes,1)) centers = np.concatenate([x_vals,y_vals],1); #print("Centers: \n",centers); spindex = Index((bb.xmin,bb.ymin,bb.xmax,bb.ymax),maxitems=100, maxdepth=100) #patches = [] #items = [] #start = time.time() #print("Inserting {} points in quad-tree...".format(n_boxes)) #for i in range(0,centers.shape[0]): # x = centers[i,0] # y = centers[i,1] # item = PcPointBox((x,y),i) # #items.append(item) # spindex.insert(item,item.bbox) # #polygon = Polygon(item.bbox_coordinates, True) # #patches.append(polygon) #end = time.time() #print("Operation took ",(end - start)*1000," ms") #matches = spindex.intersect((aoi_bb.xmin,aoi_bb.ymin,aoi_bb.xmax,aoi_bb.ymax)) #print("Matches found: ",len(matches)) start = time.time() print("Inserting {} points in KD-tree...".format(n_boxes)) tree = spt.cKDTree(centers,8) end = time.time() print("Operation took ",(end - start)*1000," ms")
def test_should_add_multiple_nodes_and_find_them(): index = Index(INDEX_BBOX) index.insert(ITEM1, BBOX1) index.insert(ITEM2, BBOX2) assert len(index) == 2 assert index.intersect(BBOX1) == [ITEM1] assert index.intersect(BBOX2) == [ITEM2] res = index.intersect(INDEX_BBOX) assert ITEM1 in res assert ITEM2 in res
def remove_overlapping_points(missed_points_coord): missed_points_qtree = Index(bbox=(np.amin(missed_points_coord[:, 0]), np.amin(missed_points_coord[:,1]), np.amax(missed_points_coord[:,0]), np.amax(missed_points_coord[:, 1]))) d_y = 1/444444.0 ## 0.25 meters d_x = np.cos(missed_points_coord[0][1] / (180 / np.pi))/444444.0 ## 0.25 meters for i in trange(len(missed_points_coord), desc='check for double points'): mp = missed_points_coord[i] if not missed_points_qtree.intersect((mp[0] - d_x, mp[1] - d_y, mp[0] + d_x, mp[1] + d_y)): missed_points_qtree.insert(mp, (mp[0] - d_x, mp[1] - d_y, mp[0] + d_x, mp[1] + d_y)) missed_points_coord = missed_points_qtree.intersect(bbox=(np.amin(missed_points_coord[:, 0]), np.amin(missed_points_coord[:,1]), np.amax(missed_points_coord[:,0]), np.amax(missed_points_coord[:, 1]))) return missed_points_coord
def prepare_pdf_pages(doc, page_images): pdf_processed_pages = {} # prep the PDF interpreter to grab the text fields laparams = LAParams() rsrcmgr = PDFResourceManager() device = PDFPageAggregator(rsrcmgr, laparams=laparams) interpreter = PDFPageInterpreter(rsrcmgr, device) for i, pdfPage in enumerate(PDFPage.create_pages(doc)): quadtree_index = Index(bbox=(0, 0, pdfPage.mediabox[2], pdfPage.mediabox[3])) pdf_processed_pages[pdfPage.pageid] = (page_images[i], quadtree_index) # read the page into a layout object interpreter.process_page(pdfPage) layout = device.get_result() # extract text from this object inject_text_to_quadtree(layout._objs, quadtree_index) return pdf_processed_pages
def reset(self, image_path, image_width, image_height, num_intended_pieces, snap_distance_percent=0.5): self.image_path = image_path self.image_width = image_width self.image_height = image_height self.num_intended_pieces = num_intended_pieces self.nx, self.ny, self.num_pieces = create_jigsaw_dimensions( num_intended_pieces, image_width, image_height) self.snap_distance = snap_distance_percent * image_width / self.nx self.cheated = False self.pieces = make_jigsaw_cut(self.image_width, self.image_height, self.nx, self.ny) self.trays = Tray(num_pids=self.num_pieces) self.quadtree = QuadTree(bbox=(-100000, -100000, 100000, 100000)) for piece in tqdm(self.pieces.values(), desc="Building quad-tree"): self.quadtree.insert(piece, piece.bbox) self.current_max_z_level = self.num_pieces self.timer.reset() self.start_time = datetime.now()
def update(self): qt = Index(bbox=(0, 0, self.w, self.h), max_items=5) for i in range(len(self.people)): s = self.people[i] qt.insert(i, (s.x, s.y, s.x, s.y)) s.update(self.peopleSpeed) for s in self.people: if self.distanceRadius > 0: # 사회적 거리두기 distance = self.distanceRadius for i in qt.intersect((s.x - distance, s.y - distance, s.x + distance, s.y + distance)): other = self.people[i] if np.sqrt((s.x - other.x) * (s.x - other.x) + (s.y - other.y) * (s.y - other.y)) > distance: continue if s != self.people[i]: direct = np.arctan2(s.y - self.people[i].y, s.x - self.people[i].x) s.dx += np.cos(direct) * 2 s.dy += np.sin(direct) * 2 mag = np.sqrt(s.dx * s.dx + s.dy * s.dy) s.dx /= mag s.dy /= mag for i in qt.intersect( (s.x - self.infectionRadius, s.y - self.infectionRadius, s.x + self.infectionRadius, s.y + self.infectionRadius)): other = self.people[i] if np.sqrt((s.x - other.x) * (s.x - other.x) + (s.y - other.y) * (s.y - other.y)) > self.infectionRadius: continue if s.status == 1 and np.random.rand() < self.infectionPercent: self.people[i].GetInfection(self.recoverTime, self.reinfectionPercent) self.setLogData()
import csv from pyqtree import Index from pcpoint import PostCodePoint pcFile = "postcodes.csv" # Latitudes: 54.596553 to 54.603728 # Longitudes: -5.937032 to -5.922495 pcBounds = (54.596552, -5.937033, 54.603729, -5.922494) pcIndex = Index(bbox = pcBounds) with open(pcFile) as csvfile: pcReader = csv.DictReader(csvfile) print "populating qtree" for row in pcReader: postcode = PostCodePoint(row['Postcode']) lat = row['Latitude'] lon = row['Longitude'] pcIndex.insert(postcode, (lat, lon, lat, lon)) print "qtree populated" print "attempt delaunay?"
def test_should_add_single_node_and_find_its_intersection(): index = Index(INDEX_BBOX) index.insert(ITEM1, BBOX1) assert len(index) == 1 assert index.intersect(BBOX1) == [ITEM1]
def test_should_empty_index_after_removing_added_node(): index = Index(INDEX_BBOX) index.insert(ITEM1, BBOX1) index.remove(ITEM1, BBOX1) assert len(index) == 0 assert index.intersect(BBOX1) == []