def find_sites_in_area(self): if not self.sites: return 0 area_geometry = ([(d.geometry) for d in self.area.values()][0]) idx = index.Index() for site in self.sites.values(): idx.insert(0, Point(site.coordinates).bounds, site) sites_in_area = [] for n in idx.intersection(shape(area_geometry).bounds, objects=True): point = Point(n.object.coordinates) if shape(area_geometry).contains(point): sites_in_area.append(n.object) return sites_in_area
def intersectGeom(poly1, poly2, id1, id2, precDigit=10): """return the intersection ids between two lists of polygons""" gO = otree.h3tree() idx = index.Index() for i, p in enumerate(poly2): idx.insert(i, p.bounds) sectL = {} numL = [] for i, p in zip(id1, poly1): merged_cells = cascaded_union( [poly2[i] for i in idx.intersection(p.bounds)]) pint = p.intersection(merged_cells) if isinstance(pint, sh.geometry.Point): l = [gO.encode(pint.x, pint.y, precision=precDigit)] else: l = [gO.encode(x.x, x.y, precision=precDigit) for x in pint] sectL[i] = l numL.append(len(l)) print("%.2f points per polygon" % (np.mean(numL))) return sectL
def knnRTree(size, k, image): p = index.Property() p.dimension = 128 #D p.buffering_capacity = 3#M p.dat_extension = 'dat' p.idx_extension = 'idx' idx = index.Index(f"RTree/RTree_{size}", properties=p) picture = face_recognition.load_image_file(image) vector = face_recognition.face_encodings(picture) q = tuple(vector[0]) if not len(q): return [] lres = list(idx.nearest(coordinates=q, num_results=k)) res = [] for x in lres: line=json.loads(linecache.getline(f"Sequential/Sequential_{size}.json", x+1)) res.append(line[0]) return res
def _create_spatial_index(self): """ This creates the spatial index for the input mesh """ # Setting rtree properties prop = index.Property() prop.dimension = 3 # Set the geographic projection lons = self.mesh.lons.flatten('F') mean_lat = np.mean(self.mesh.lats) self.p = Proj(proj='lcc', lon_0=np.mean(lons), lat_1=mean_lat-10., lat_2=mean_lat+10.) # Create the spatial index for the grid mesh r = index.Index(_generator(self.mesh, self.p), properties=prop) # Set the rtree self.rtree = r
def add_lad_to_exchanges(exchanges, lads): joined_exchanges = [] # Initialze Rtree idx = index.Index() for rtree_idx, exchange in enumerate(exchanges): idx.insert(rtree_idx, shape(exchange['geometry']).bounds, exchange) # Join the two for lad in lads: for n in idx.intersection((shape(lad['geometry']).bounds), objects='raw'): lad_shape = shape(lad['geometry']) premise_shape = shape(n['geometry']) if lad_shape.contains(premise_shape): n['properties']['lad'] = lad['properties']['name'] joined_exchanges.append(n) return joined_exchanges
def build_rtree_index(geom): """Builds an rtree index. Useful for multiple intersections with same index. Parameters ========== geom : list list of shapely geometry objects Returns idx : rtree spatial index object """ from rtree import index # build spatial index for items in geom1 print('\nBuilding spatial index...') ta = time.time() idx = index.Index() for i, g in enumerate(geom): idx.insert(i, g.bounds) print("finished in {:.2f}s".format(time.time() - ta)) return idx
def create_rtree_index(inshp): """Create rtree bounding index for an input shape. Args: inshp (obj): Shapefile with features. Returns: obj: rtree bounding box index. """ print("creating node index.....") feature_list = [] for feature in inshp: line = shape(feature['geometry']) feature_list.append(line) idx = index.Index() for i in range(len(feature_list)): idx.insert(i, feature_list[i].bounds) return idx
def demo2(ports): print "Checking " + str(len(ports)) + " ports" violations = [] idx = index.Index() ## add everything to the spatial index for p in ports: (pid, (pminx,pminy,pmaxx,pmaxy)) = p.boundingBox() idx.insert(pid, (pminx,pminy,pmaxx,pmaxy)) ## now check the rules for p in ports: if not rule28.apply((p,)): violations.append(Violation(rule28, [p])) # find things that could violate rule 27? possible = p.center.buffer(250).bounds ## How do I do this generally? candidates = idx.intersection(possible) for c in candidates: pj = ports[c] if not rule27.apply((p,pj)): violations.append(Violation(rule27, [p,pj])) return violations
def similarity(directory, filename, k): # first extract features from query file featureVector = extractFeatures(directory,filename) # then open and query spatial index p = index.Property() p.dat_extension = 'data' p.idx_extension = 'index' p.dimension = 13 rtree = index.Index('rtreez', properties=p, interleaved=True) similar = list(rtree.nearest((featureVector[0], featureVector[1], featureVector[2], featureVector[3], featureVector[4],featureVector[5], featureVector[6], featureVector[7], featureVector[8], featureVector[9], featureVector[10], featureVector[11], featureVector[12], featureVector[0], featureVector[1], featureVector[2], featureVector[3], featureVector[4], featureVector[5], featureVector[6], featureVector[7], featureVector[8], featureVector[9], featureVector[10], featureVector[11], featureVector[12]), k)) return similar
def load_shapes(geojson_file): from shapely.geometry import shape from rtree import index import json # load the initial collection from the geojson collection = json.load(geojson_file) # create a spatial index idx = index.Index() for feature in collection['features']: # add the shapely-parsed shape onto each feature feature['shape'] = shape(feature['geometry']) feature['centroid'] = feature['shape'].centroid # spatially index each feature objid = feature['properties']['OBJECTID'] idx.insert(objid, feature['shape'].bounds, obj=feature) return collection, idx
def selectByIntersection(allAdmin, inD, exact=False): ''' Select features from allAdmin that intersect inD allAdmin [shapely geometry] - describes the feature of interest inD [geopandas dataframe] - features to be intersected exact (optional) [boolean] - perform exact geometry match, default is False, which will only calculate bounding box intersection RETURNS [geopandas dataframe] - subset of inD that intersects allAdmin EXAMPLE: inS = gpd.read_file(sourceFile) inD = gpd.read_file(intersectingFile) if inS.crs != inD.crs: inS = inS.to_crs(inD.crs) allAdmin = shapely.ops.cascaded_union(MultiPolygon(inS['geometry'].tolist())) selectByIntersection(allAdmin, inD, exact=False) TODO: Projection checks ''' #Read the source boundaries into an rtree opbject idxAdmin = index.Index() try: for i, shape in enumerate(allAdmin): idxAdmin.insert(i, shape.bounds) except: idxAdmin.insert(0, allAdmin.bounds) #Enumerate through the intersecting file intersectingList = [] for i, row in inD.iterrows(): interSection = list(idxAdmin.intersection(row['geometry'].bounds)) if len(interSection): intersectingList.append(row) returnDF = gpd.GeoDataFrame(intersectingList) if exact: selData = [] for j, origRow in returnDF.iterrows(): xx = allAdmin.intersects(origRow['geometry']) print("%s - %s" % (j, xx)) if xx: selData.append(origRow) returnDF = gpd.GeoDataFrame(selData) return returnDF
def run_images(valid_output_full, validated_image_filename, graph, input_image_filename_list): idxprop = index.Property() idxprop.dimension = DIMENSIONS idxprop.dat_extension = 'data' idxprop.idx_extension = 'index' idx = index.Index('3d_index', properties=idxprop) idx_nr = 0 cv2.namedWindow(CV_WINDOW_NAME) for input_image_file in input_image_filename_list: print("Processing [", idx_nr, "] file ", input_image_file) # read one of the images to run an inference on from the disk infer_image = cv2.imread(IMAGES_DIR + input_image_file) # run a single inference on the image and overwrite the # boxes and labels test_output_full = run_inference( infer_image, graph, input_image_file + " -- " + str(idx_nr)) test_output = rmsList(test_output_full, 13) print("test_output=", test_output) testTupl = toRecTuple(test_output) print("testTuble=", testTupl) idx.insert(idx_nr, testTupl, obj=idx_nr) valid_output = rmsList(valid_output_full, 13) print("valid_output=", valid_output) validTupl = toRecTuple(valid_output) print("validTuble=", validTupl) a = list(idx.nearest(validTupl)) print("R-tree near: ", a) idx_nr = idx_nr + 1 # Test the inference results of this image with the results # from the known valid face. if (face_match(valid_output_full, test_output_full)): print('PASS! File ' + input_image_file + ' matches ' + validated_image_filename) else: print('FAIL! File ' + input_image_file + ' does not match ' + validated_image_filename)
def addSeparatorFeature(self): """ We load the graphical separators COmpute a set of shapely object In turn, for each edge, we compute the intersection """ # graphical separators from xml_formats.PageXml import PageXml dNS = {"pc": PageXml.NS_PAGE_XML} ndRoot = self.lNode[0].node.getroottree() lNdSep = ndRoot.xpath(".//pc:SeparatorRegion", namespaces=dNS) loSep = [ShapeLoader.node_to_LineString(_nd) for _nd in lNdSep] if self.bVerbose: traceln(" %d graphical separators" % len(loSep)) # make an indexed rtree idx = index.Index() for i, oSep in enumerate(loSep): idx.insert(i, oSep.bounds) # take each edge in turn and list the separators it crosses nCrossing = 0 for edge in self.lEdge: # bottom-left corner to bottom-left corner oEdge = geom.LineString([(edge.A.x1, edge.A.y1), (edge.B.x1, edge.B.y1)]) prepO = prep(oEdge) bCrossing = False for i in idx.intersection(oEdge.bounds): # check each candidate in turn if prepO.intersects(loSep[i]): bCrossing = True nCrossing += 1 break edge.bCrossingSep = bCrossing if self.bVerbose: traceln( " %d (/ %d) edges crossing at least one graphical separator" % (nCrossing, len(self.lEdge)))
def mapper(): PICKUP_TIME = 5 index = rtree.Index() neighborhoods = [] readNeighborhood('ZillowNeighborhoods-NY.shp', index, neighborhoods) agg = {} for values in parseInput(): pickup_location = (float(values[10]), float(values[11])) pickup_time = values[PICKUP_TIME] weekhour = getWeekday(pickup_time) pickup_neighborhood = findNeighborhood(pickup_location, index, neighborhoods) time_neighbor_key = weekhour+'|'+str(pickup_neighborhood) if pickup_neighborhood!=-1: agg[time_neighbor_key] = agg.get(time_neighbor_key , 0) + 1 for item in agg.iteritems(): key_array = item[0].split('|') neighborhood_name = neighborhoods[int(key_array[1])][0] output_key = key_array[0]+'|'+ neighborhood_name print '%s\t%s' % (output_key, item[1])
def __init__(self, filename): #PATH='/Users/elaineyang/Documents/WeatherServer/data/shapefiles/' PATH = 'data/shapefiles/' print("processing " + filename + ".shp") myshp = open(PATH + filename + ".shp", "rb") mydbf = open(PATH + filename + ".dbf", "rb") r = shapefile.Reader(shp=myshp, dbf=mydbf) records = r.records() self.records = records idx = index.Index() shapes = r.shapes() id_num = 0 for shape in shapes: sh = shape.bbox xmin = sh[1] ymin = sh[0] xmax = sh[3] ymax = sh[2] idx.insert(id_num, (xmin, ymin, xmax, ymax)) id_num = id_num + 1 self.idx = idx
def create_index(self, location): photos = self.get_photos(location) # If interleaved=False, the bbox format looks like xmin, xmax, ymin,ymax prop = index.Property() prop.overwrite = True index_file = "index_" + str(location) idx = index.Index(index_file, interleaved=False, properties=prop) _id = 1 for photo in photos: latitude = float(photo['latitude']) longitude = float(photo['longitude']) idx.insert(_id, (latitude, latitude, longitude, longitude), obj={ 'photo_id': photo['photo_id'], 'owner': photo['owner'], 'latitude': latitude, 'longitude': longitude }) _id+=1 idx.close() print _id
def get_intersecting_buildings(postcode_sector, buildings): intersecting_buildings = [] # Initialze Rtree idx = index.Index() [ idx.insert(0, shape(building['geometry']).bounds, building) for building in buildings ] for n in idx.intersection((shape(postcode_sector['geometry']).bounds), objects=True): postcode_sector_shape = shape(postcode_sector['geometry']) premise_shape = shape(n.object['geometry']) if postcode_sector_shape.contains( premise_shape.representative_point()): intersecting_buildings.append(n.object) return intersecting_buildings
def load_n(i): nl = {} with open("result.pkl", "rb") as f: data = pickle.load(f) contador = 0 for d in data: if (contador == i): break nl[d[0]] = parse_row(d[1]) contador += 1 prop = index.Property() prop.dat_extension = "data" prop.idx_extension = "index" prop.dimension = 128 idx = index.Index('3d_index' + str(i), properties=prop, interleaved=False) c_id = 0 for key in nl: idx.insert(c_id, nl[key], key) c_id += 1 return idx
def __init__(self, dim_range, Obs=None): ''' Initialize Search Space dim_range: upper bound of each dimension, lower bound is defaultly set as 0 Obs: default value=None ''' self.dim_range = dim_range self.dim = len(dim_range) # sanity process if self.dim < 2: raise Exception('at least initialize 2D map.') if self.dim > 3: raise Exception('at most initialize 3D map.') self.obs = Obs if Obs is not None: # empty initialize self.obs # index member: interleaved=True xmin, ymin, ..., kmin, xmax, ymax, ..., kmax self.obs = index.Index(interleaved=True) for i in Obs: self.obs.insert(i[0], i[1])
def index(self): if not self.indexed: self.minx = sys.maxint self.miny = sys.maxint self.maxx = sys.minint self.maxy = sys.minint self.index = index.Index() self.human_rectangles.clear() for entity in self.entities.values(): left, bottom, right, top = self.make_rectangle(entity) if left is not None: self.index.insert(entity.get_id().get_value(), (left, bottom, right, top)) self.minx = min(self.minx, left, right) self.maxx = max(self.maxx, left, right) self.miny = min(self.miny, bottom, top) self.maxy = max(self.maxy, bottom, top) if isinstance(entity, Human): self.human_rectangles[entity] = (left, bottom, right, top) self.indexed = True
def get_intersecting_rtree(x, y, windows): """ Returns a list of I3 window objects who intersect witht he point x, y. Args: x (int): Query x. 0 <= x <= Screen width y (int): Query y. 0 <= y <= Screen height windows (list): Possibly empty list of i3ipc.window connections Returns: list: Possibly empty list of intersecting windows. """ top = lambda window: window.rect.y right = lambda window: window.rect.x + window.rect.width bottom = lambda window: window.rect.y + window.rect.height left = lambda window: window.rect.x rtree = index.Index() for idx, w in enumerate(windows): rtree.insert(idx, (left(w), top(w), right(w), bottom(w))) return [windows[idx] for idx in rtree.intersection((x, y))]
def get_spatial_points(self, x, y, distance): """Use spatial index to retrieve pulsewave within a given bounding box :param left: :param right: :param bottom : :param top: """ if not os.path.isfile(os.path.splitext(self.filename)[0] + ".idx"): print("Spatial index not found!!!") return spatial_index = index.Index(os.path.splitext(self.filename)[0]) intersected_pulses = list( spatial_index.intersection( (x - distance, y - distance, x + distance, y + distance))) spatial_index.close() return intersected_pulses
def __init__(self): if osp.exists("state_index.dat"): os.remove("state_index.dat") os.remove("state_index.idx") self.obs_dimension = 16 self.visited_state_value = np.loadtxt( "visited_state_value.txt", usecols=(i for i in range(self.obs_dimension + 1, self.obs_dimension + 3, 1))) self.visited_state_value = self.visited_state_value.tolist() self.visited_state_counter = len(self.visited_state_value) visited_state_tree_prop = rindex.Property() visited_state_tree_prop.dimension = self.obs_dimension + 1 self.visited_state_dist = np.array( [[1, 0.3, 3, 1, 10, 0.3, 3, 1, 10, 0.3, 3, 1, 10, 0.3, 3, 1, 0.1]]) #, 10, 0.3, 3, 1, 0.1]]) self.visited_state_tree = rindex.Index( 'state_index', properties=visited_state_tree_prop) self.recostruct_state_counter = 0 self.recostruct_rtree()
def __init__(self, crs="epsg:3857"): """Create a GeoSpace for GIS enabled mesa modeling. Args: crs: Coordinate reference system of the GeoSpace If `crs` is not set, epsg:3857 (Web Mercator) is used as default. However, this system is only accurate at the equator and errors increase with latitude. Properties: crs: Project coordinate reference system idx: R-tree index for fast spatial queries bbox: Bounding box of all agents within the GeoSpace agents: List of all agents in the Geospace Methods: add_agents: add a list or a single GeoAgent. remove_agent: Remove a single agent from GeoSpace agents_at: List all agents at a specific position distance: Calculate distance between two agents get_neighbors: Returns a list of (touching) neighbors get_intersecting_agents: Returns list of agents that intersect get_agents_within: Returns a list of agents within get_agent_contains: Returns a list of agents contained get_agents_touches: Returns a list of agents that touch update_bbox: Update the bounding box of the GeoSpace """ self.crs = pyproj.CRS(crs) self.WGS84 = pyproj.CRS("epsg:4326") self.Transformer = pyproj.Transformer.from_crs(self.crs, self.WGS84, skip_equivalent=True, always_xy=True) self.bbox = None self._neighborhood = None # Set up rtree index self.idx = index.Index() self.idx.agents = {}
def load(self): # load elevation data self.elevation = rasterio.open("../Material/elevation/SZ.asc") self.elevation_band = self.elevation.read(1) # for task 1, restrict user point in a range that is more than 5km from the bounding edge self.bound_polygon = self.bound_polygon(self.elevation.bounds) self.inner_polygon = self.bound_polygon.buffer(-5000) # load itn data self.itndata = self.read_geojson("../Material/itn/solent_itn.json") self.road_nodes = self.itndata["roadnodes"] # for task 3, build rtree from itn data count = 0 self.itn_rtree = index.Index() for fid, node in self.road_nodes.items(): corrds = node["coords"].copy() self.itn_rtree.insert(count, corrds, fid) count += 1 # for task 4, build networkx graph from itn data, we store link feature id in Graph for # computing self.itn_graph = networkx.Graph() road_links = self.itndata['roadlinks'] for link in road_links: s = road_links[link]['start'] e = road_links[link]['end'] # get start point elevation and end point elevation s_ele = self.get_elevation(self.road_nodes[s]["coords"][0], self.road_nodes[s]["coords"][1]) e_ele = self.get_elevation(self.road_nodes[e]["coords"][0], self.road_nodes[e]["coords"][1]) length = road_links[link]['length'] # compute travel time according to Naismith's rule time = length * 60 / 5000 + max(e_ele - s_ele, 0) / 10 self.itn_graph.add_edge(s, e, fid=link, weight=time)
def create_rtree(shape_file: str, inEPSG='EPSG:4326', outEPSG='EPSG:4326'): try: buildings = fiona.open(shape_file) # outEPSG = 'EPSG:4326' # inEPSG = 'EPSG:3857' transformer = Transformer.from_crs(inEPSG, outEPSG, always_xy=True) # rtree rtree_path = shape_file.replace(".shp", '_rtree') p = index.Property() p.overwrite = True r_tree = index.Index(rtree_path, properties=p) print_interval = int(len(buildings) / 1000) for idx, building in tqdm(enumerate(buildings[:])): try: if idx % print_interval == 0: logger.info("Processing polyogn #: %d", idx) bound = fiona.bounds(building) # logger.info("bound: %s", bound) bound = list(bound) # logger.info("bound: %s", bound) bound[0], bound[1] = transformer.transform(bound[0], bound[1]) # logger.info("bound: %s", bound) bound[2], bound[3] = transformer.transform(bound[2], bound[3]) r_tree.insert(idx, bound) except Exception as e: logger.error("Error in building polygons: %s", e) continue r_tree.close() except Exception as e: logger.error("Error in creating rtree: %s", e)
def extract_tributary(eList1, eList2): """将上一级每条河流和下一级每条河流求交,求出支流信息""" #建立索引 idxP = index.Property() idxP.filename = 'road_index' idxP.overwrite = True idxP.storage = index.RT_Disk idxP.dimension = 2 idx = index.Index(idxP.filename, item_build(eList2), properties=idxP, interleaved=True, overwrite=True) # linkNum = 0 for currentEntity in eList1: currentGeom = shapely.geometry.asShape(currentEntity['geometry']) currentQueryBox = currentGeom.bounds #对上一级河流求bbox currentID = currentEntity['properties']['UUID'] currentLevel = currentEntity['properties']['LEVEL'] currentBuffer = currentGeom.buffer(0.2) judgeIds = list(idx.intersection(currentQueryBox)) #求出相交的下一级河流 for jid in judgeIds: judgeEntity = eList2[jid] judgeGeom = shapely.geometry.asShape(judgeEntity['geometry']) if currentBuffer.intersects(judgeGeom): jID = judgeEntity['properties']['UUID'] jLevel = judgeEntity['properties']['LEVEL'] jNAME = judgeEntity['properties']['NAME'] if jID != currentID and jID not in currentEntity['relation'][ 'Tributary'] and jLevel > currentLevel: currentEntity['relation']['Tributary'].append(jID) currentEntity['properties']['Tributary'].append(jID) currentEntity['properties']['Tributary_NAME'].append(jNAME) # linkNum += 1 print('Step4: extracting relations finished') return eList1
def simpleRTree(): print("R-tree test") idx = index.Index() pt1 = Point(1, 1) pt2 = Point(4, 1) pt3 = Point(3, 3) # Insert the points into the R-tree index idx.insert(1, (pt1.x, pt1.y, pt1.x, pt1.y)) idx.insert(2, (pt2.x, pt2.y, pt2.x, pt2.y)) idx.insert(3, (pt3.x, pt3.y, pt3.x, pt3.y)) # Query. This library only supports rectangular queries # Specify the bounds leftBottom = Point(0, 0) rightTop = Point(3, 4) # Perform the query results = list( idx.intersection((leftBottom.x, leftBottom.y, rightTop.x, rightTop.y))) print("Query result:") print(results)
def __init__(self, vertices, indices): # need to find self-intersections if Polygon2d.check_ccw(vertices, indices): self.outline = indices[:] else: self.outline = indices[::-1] self.vertices = vertices[:] self.rti = index.Index() self.rti.interleaved = True # insert vertices into spatial index for vid in self.outline: vert = self.vertices[vid] self.rti.insert(vid, (vert.x, vert.y, vert.x, vert.y)) # resolve self-intersections (sinters) self._resolve_sinters() self.holes = []
def chunk(featureFileName, sectionFileName, pattern, key=None): # Load and index with collection(featureFileName, "r") as featureFile: featureIdx = index.Index() features = [] for feature in featureFile: features.append(feature) featureIdx.add( len(features) - 1, asShape(feature['geometry']).bounds) # Break up by sections and export with collection(sectionFileName, "r") as sectionFile: i = 0 for section in sectionFile: fileName = pattern % i if key: fileName = pattern % section['properties'][key] properties = {} try: with collection(fileName, 'w', 'ESRI Shapefile', schema=featureFile.schema, crs=featureFile.crs) as output: sectionShape = asShape(section['geometry']) for j in featureIdx.intersection( sectionShape.bounds): if asShape(features[j]['geometry']).intersects( sectionShape): properties = features[j]['properties'] output.write(features[j]) print "Exported %s" % fileName i = i + 1 except ValueError: print "Error exporting " + fileName pprint(properties) pprint(featureFile.schema)