def sample_uniform_random_point( area: MultiPolygon, max_sample_trials: int = 1000) -> Optional[Point]: """ Try to sample a point uniform at random over an arbitrary polygon shape by uniformly sampling from its covering rectangle. Exit, if it takes more than max_sample_trials. Args: area (MultiPolygon): subset of the Markov partition from which we sample a point at random max_sample_trials: number of trials to sample a point that is also within the polygon Returns: """ min_x, min_y, max_x, max_y = area.bounds point = Point( [np.random.uniform(min_x, max_x), np.random.uniform(min_y, max_y)]) sample_trials = 1 while not point.intersects(area) and sample_trials < max_sample_trials: point = Point([ np.random.uniform(min_x, max_x), np.random.uniform(min_y, max_y) ]) sample_trials += 1 return point if point.intersects(area) else None
def analyze_ROIs_on_dlc_output(h5_path, save=True): """Appends ROI analysis to the DLC output using analysis_rois.csv file""" # Read in analysis_rois.csv data, determine the folder and the video folder, video = os.path.split(h5_path) video = video[:video.find('DLC')] + '.mp4' analysis_rois = pd.read_csv(os.path.join(folder, 'analysis_rois.csv'), index_col=0) # Create shapely ROI objects social = literal_eval(analysis_rois.loc[video, '4:circle']) social = Point(social[0]).buffer(social[1]) drinking = literal_eval(analysis_rois.loc[video, '2:circle']) drinking = Point(drinking[0]).buffer(drinking[1]) marble = literal_eval(analysis_rois.loc[video, '0:rectangle']) marble = box(marble[0], marble[1], marble[0] + marble[2], marble[1] + marble[3]) nest = literal_eval(analysis_rois.loc[video, '1:rectangle']) nest = box(nest[0], nest[1], nest[0] + nest[2], nest[1] + nest[3]) mask_circle = literal_eval(analysis_rois.loc[video, '3:circle']) mask_circle = Point(mask_circle[0]).buffer(mask_circle[1]) # Read in DLC tracking data (.h5) dlc_df = pd.read_hdf(h5_path) dlc_df.columns = dlc_df.columns.droplevel(0) # Computational block bodypart_dfs = [] dtypes = {'social': 'bool', 'drinking': 'bool', 'marble': 'bool', 'nest': 'bool', 'mask_circle': 'bool', 'zones_sum': 'int'} pd.options.mode.chained_assignment = None # default='warn', this is to suppress Pandas warnings. print('Analyzing', len(dlc_df.columns.get_level_values(0).unique()), 'bodyparts..') for bodypart in tqdm.tqdm(dlc_df.columns.get_level_values(0).unique()): # loop over bodyparts bodypart_df = dlc_df.loc[:, bodypart] # take the bodypart x, y, likelihood as a separate df for frame in bodypart_df.index: bodypart_obj = Point(bodypart_df.loc[frame, 'x'], bodypart_df.loc[frame, 'y']) bodypart_df.loc[frame, 'drinking'] = int(bodypart_obj.intersects(drinking)) bodypart_df.loc[frame, 'social'] = int(bodypart_obj.intersects(social)) bodypart_df.loc[frame, 'marble'] = int(bodypart_obj.intersects(marble)) bodypart_df.loc[frame, 'nest'] = int(bodypart_obj.intersects(nest)) bodypart_df.loc[frame, 'mask_circle'] = int(bodypart_obj.intersects(mask_circle)) bodypart_df['zones_sum'] = bodypart_df['drinking'] + bodypart_df['social'] + bodypart_df['marble'] + \ bodypart_df['nest'] + bodypart_df['mask_circle'] bodypart_df = bodypart_df.astype(dtypes) # convert dtypes bodypart_df.columns = pd.MultiIndex.from_product([[bodypart], bodypart_df.columns]) # return to multiindex bodypart_dfs.append(bodypart_df) dlc_df = pd.concat(bodypart_dfs, axis=1) dlc_df.columns = dlc_df.columns.set_names(['bodyparts', 'coords']) # Writing to disk & returning if save: if not os.path.exists(os.path.join(folder, 'withROIs')): os.mkdir(os.path.join(folder, 'withROIs')) dlc_df.to_hdf(path_or_buf=os.path.join(folder, 'withROIs', video[:-4] + '_withROIs.h5'), key="withROIs") return dlc_df
def _checkforPointinBox(self, filterbbox, location): """ Uses Shapely Polygons to calculate bounding box intersections """ log.debug('comparing against %s' % str(filterbbox)) filterpolygon = Polygon( ((filterbbox[0], filterbbox[1]), (filterbbox[0], filterbbox[3]), (filterbbox[2], filterbbox[3]), (filterbbox[2], filterbbox[1]))) featurepoint = Point(float(location[0]), float(location[1])) log.debug(featurepoint.within(filterpolygon)) log.debug('intersect result%s' % featurepoint.intersects(filterpolygon)) return featurepoint.intersects(filterpolygon)
def valid_path(self, edge_name, trajectory): rs = edge_name.split('_', 1) start_region = self.waypoints_regions[rs[0]] end_region = self.waypoints_regions[rs[1]] start_point = Point(trajectory[0].pose.position.x, trajectory[0].pose.position.y) end_point = Point(trajectory[-1].pose.position.x, trajectory[-1].pose.position.y) intersects_s = False intersects_e = False for region_area in self.regions.values(): intersects_s = intersects_s or start_point.intersects(region_area) for region_area in self.regions.values(): intersects_e = intersects_e or end_point.intersects(region_area) if (intersects_s and intersects_e): if start_point.intersects( self.regions[start_region]) and end_point.intersects( self.regions[end_region]): #print 'Start and End both lie in respective regions' return True elif start_point.intersects( self.regions[start_region]) and end_point.intersects( self.regions[start_region]): #print 'Start and End both lie in same region' return True elif start_point.intersects( self.regions[end_region]) and end_point.intersects( self.regions[end_region]): #print 'Start and End both lie in same region' return True else: return False elif (intersects_s and not intersects_e): #print 'End Point Lying In No Region'; if start_point.intersects(self.regions[start_region]): return True else: return False elif (intersects_e and not intersects_s): #print 'Start Point Lying In No Region' if end_point.intersects(self.regions[end_region]): return True else: return False else: #print 'Both Trajectory points Not In Any Region' return True
def angle_to_point(point: Point, nearest_point: Point, comparison_point: Point) -> float: """ Calculate the angle between two vectors. Vectors are made from the given points: Both vectors have the same first point, nearest_point, and second point is either point or comparison_point. Returns angle in degrees. E.g. >>> point = Point(1, 1) >>> nearest_point = Point(0, 0) >>> comparison_point = Point(-1, 1) >>> angle_to_point(point, nearest_point, comparison_point) 90.0 >>> point = Point(1, 1) >>> nearest_point = Point(0, 0) >>> comparison_point = Point(-1, 2) >>> angle_to_point(point, nearest_point, comparison_point) 71.56505117707799 """ if (point.intersects(nearest_point) or point.intersects(comparison_point) or nearest_point.intersects(comparison_point)): raise ValueError("Points in angle_to_point intersect.") unit_vector_1 = point_to_point_unit_vector(point=nearest_point, other_point=point) unit_vector_2 = point_to_point_unit_vector(point=nearest_point, other_point=comparison_point) dot_product = np.dot(unit_vector_1, unit_vector_2) if dot_product > 1 or dot_product < -1: rad_angle = np.nan else: rad_angle = np.arccos(dot_product) if np.isnan(rad_angle): # Cannot determine with angle. unit_vector_sum_len = np.linalg.norm(unit_vector_1 + unit_vector_2) if np.isclose(unit_vector_sum_len, 0.0, atol=1e-07): return 180.0 if np.isclose(unit_vector_sum_len, 2.0, atol=1e-07): return 0.0 logging.error(unit_vector_1, unit_vector_2, unit_vector_sum_len) raise ValueError( "Could not determine point relationships. Vectors printed above.") degrees = numpy_to_python_type(np.rad2deg(rad_angle)) assert 360.0 >= degrees >= 0.0 assert isinstance(degrees, float) return degrees
def inside(a): point = Point(pliades.iloc[a, 0], pliades.iloc[a, 1]) breaches = numpy.array([], dtype=long) for b in range(0, 25): poly = cell_poly[b] if point.intersects(poly): cell = b break for c in fishing_area_grid[cell]: if point.intersects(shp1[c]): breaches = numpy.append(breaches, c) if len(breaches) > 0: breaches = numpy.append(breaches, a) return breaches
def query_index_polygon(index, polygon_geometry): """Query database for CPTs within given polygon.""" # Setup KDTree based on points npindex = np.array(index) tree = KDTree(npindex[:, 0:2]) # Find center of polygon bounding box # and circle encompasing bbox and thus polygon polygon = shape(polygon_geometry) minx, miny, maxx, maxy = polygon.bounds xr, yr = (maxx - minx) / 2, (maxy - miny) / 2 radius = math.sqrt(xr**2 + yr**2) x, y = minx + xr, miny + yr # Find all points in circle and do # intersect with actual polygon indices = [] points_slice = tree.query_ball_point((x, y), radius) points = npindex[points_slice, :] for point in points: x, y, start, end = point p = Point(x, y) if p.intersects(polygon): indices.append((int(start), int(end))) return indices
def __assign_entry_points(self, directory): """ Assign entry points for all visitation objects using references of nodes :param directory: hash table needed information relating each visitation object's sub-directory :return: None """ polygon = directory['shp_reference'] count = 0 for cell in directory['assigned_cell_partitions']: x = cell[0] y = cell[1] if len(self.grid.grid[x][y]) != 0: for node in self.grid.grid[x][y]: node_pt = Point((float(node[0]), float(node[1]))) if node_pt.intersects(polygon) and (float(node[0]), float(node[1])) not in \ directory['entry_nodes']: count += 1 directory['entry_nodes'].append((float(node[0]), float(node[1]))) if count == 0: found_nodes = list() mx = max(directory['assigned_cell_partitions']) mx_x = mx[0] mx_y = mx[1] mn = min(directory['assigned_cell_partitions']) mn_x = mn[0] mn_y = mn[1] node = self.shp_graph.midpoint(polygon.bounds[0], polygon.bounds[1], polygon.bounds[2], polygon.bounds[3]) while len(found_nodes) == 0 and mx_x <= len(self.grid.grid) and mx_y < len(self.grid.grid) \ and mn_y >= 0 and mx_x >= 0: for r in range(mn_y, mx_y, 1): for c in range(mn_x, mx_x, 1): if len(self.grid.grid[c][r]) != 0: found_nodes.extend(self.grid.grid[c][r]) if mx_x < len(self.grid.grid): mx_x += 1 if mx_y < len(self.grid.grid): mx_y += 1 if mn_x > 0: mn_x -= 1 if mn_y > 0: mn_y -= 1 if len(found_nodes) == 0: found_nodes = self.shp_graph.graph.nodes tree = spatial.KDTree(found_nodes) tree.query([node]) output = tree.query([node]) directory['entry_nodes'].append(list(found_nodes).__getitem__(output[1][0]))
def get_reward_and_done(self, action, info): pld = MultiPoint(self.to_ego_frame(self.vertices[:, :-1])).convex_hull reward = 0 done, success = False, False d2g = self.to_ego_frame(self.goal_state) if any(abs(d2g) > self.epsr): return -1, False, True for obstacle_xy in self.to_ego_frame(self.obstacle_state): if any(obstacle_xy > self.epsr): continue obstacle = Point(*obstacle_xy).buffer(self.obstacle_radius) collision = obstacle.intersects(pld) if collision: print('COLLISION!') return -1, False, True # r1 = -norm(self.agent_state[2:4]) r2 = norm(self.prev_state[:2] - self.goal_state)\ - norm(self.agent_state[:2] - self.goal_state) # r3 = -norm(self.prev_action - action) # reward = 0.05*r1 + r2 + 0.1*r3 + (-0.1*self.dt) # step_penalty reward = r2 - 0.1 * self.dt if norm(d2g) < 0.3: reward = 1 success = True info['d2g'] = norm(d2g) return reward, success, done or success
def calcula_areaponto(pontos, pontos2, size): import numpy as np from shapely.geometry import Point, Polygon countp = 0 poly = Polygon(pontos) area = 0 areaponto = [] for i in range(size): armazena = np.array(pontos2[i]) pcertos = 0 for x in range(size - 1): inside = 0 countp = 0 for y in range(size): if verifica( resolve_matriz(pontos2[i], pontos2[i + x + 1], pontos2[y], pontos2[y + 1])) == True: countp += 1 if countp <= 4: p1x = (pontos2[i][0] + pontos2[i + x + 1][0]) / 2 p1y = (pontos2[i][1] + pontos2[i + x + 1][1]) / 2 p1 = Point(p1x, p1y) if p1.within(poly) or p1.intersects(poly): armazena = np.append(armazena, pontos2[i + x + 1]) if recebecalcula(armazena, (len(armazena) - 1)) == area: areaponto.append(i) if recebecalcula(armazena, (len(armazena) - 1)) > area: area = recebecalcula(armazena, (len(armazena) - 1)) areaponto = [i] return areaponto
def points_in_polygon(poly_file, points_file): # obtain lists of polygon and point coordinate-tuples poly_ls = obtain_coord_list(poly_file) points_ls = obtain_coord_list(points_file) # create a polygon from the list of polygon coordinates # the Polygon constructor takes two positional parameters; the first is an # ordered sequence of (x, y[, z]) point tuples, which wil be implicitly # closed by copying the first tuple to the last index; the second is an # optional unordered sequence of ring-like sequences specifying interior # boundaries or "holes" of the feature polygon = Polygon(poly_ls) for point in points_ls: # create Point objects from test point coordinates # the Point constructor takes positional coordinate values or # point tuple parameters point_obj = Point(point) # check if point_obj lies inside (incl. boundary) the polygon, and # output the result of the test with coordinates of the point # object.intersects(other) method returns True if the boundary or # interior of the object intersect in any way with those of the other if point_obj.intersects(polygon): print(point[0], point[1], "inside") else: print(point[0], point[1], "outside") return
def get(self): radius = floatParser(request.args.get("radius", "")) x = floatParser(request.args.get("x", "")) y = floatParser(request.args.get("y", "")) res = True if radius <= 0: res = { "Error": "Invalid Input. Radius must be positive integer or float" }, 201 elif radius and x and y: circle = Point(x, y).buffer(radius) if circle.intersects(self.circle_default): intersection_area = circle.intersection( self.circle_default).area res = { "intersec": "yes", "ratio": intersection_area / (circle.area) } else: res = {"intersect": "no", "ratio": 0.0} else: res = { "Error": "Invalid Input. Make sure radius, x, y are integers or floats" }, 201 return res
def find_nearby_nodes(start, map_graph, map_edges): global lane_width node_idx_list = [] ball = Point(start[0], start[1]).buffer(lane_width / 2.0) # dist_min = lane_width*1000 for key, line in map_edges.items(): if ball.intersects(line): x, y = line.xy p0 = [x[0], y[0]] p1 = [x[1], y[1]] line_slope = np.array(p1) - np.array(p0) line_slope = line_slope / np.linalg.norm(line_slope) dist = dist_pt_line(np.array(start), p0, line_slope) idx0 = str(round(x[0], 2)) + str(round(y[0], 2)) idx1 = str(round(x[1], 2)) + str(round(y[1], 2)) node_idx_list.append(idx0) node_idx_list.append(idx1) # if dist < dist_min: # dist_min = dist # node_idx_list.append(idx0) # node_idx_list.append(idx1) # node_idx_list = [idx0, idx1] # if dist < lane_width/2.0: # print 'return' # return node_idx_list return node_idx_list
def geometric_clustering_by_circles_intersection(boxes, labels_boxes, radius): mx = max(labels_boxes) + 1 for id_1, box_1 in enumerate(boxes): vertexs = [box_1.x0, box_1.x1, box_1.x2, box_1.x3] for v in vertexs: circle = Point(v[0], v[1]).buffer(radius) if WEIGHTED_DISTANCE_CLUSTERING: circle = shapely.affinity.scale(circle, 1, 1 / Y_WEIGHT) for id_2, box_2 in enumerate(boxes): if id_1 != id_2: if labels_boxes[id_1] != labels_boxes[id_2] or ( labels_boxes[id_1] == -1 and labels_boxes[id_2] == -1): polygon2 = Polygon( [box_2.x0, box_2.x1, box_2.x2, box_2.x3]) if circle.intersects(polygon2): tmp = labels_boxes[id_2] if tmp == -1: labels_boxes[id_1] = mx labels_boxes[id_2] = mx mx += 1 else: for label_id, label in enumerate(labels_boxes): if label == tmp: labels_boxes[label_id] = labels_boxes[ id_1] return labels_boxes
def is_on_edge(self, moveable_obj, edge_index): edge = self.vertices_of_edges[edge_index] radius = moveable_obj.fixtures[0].shape.radius center = moveable_obj.position circle = Point(center).buffer(radius) edge_line = Line(edge).buffer(1) return circle.intersects(edge_line)
def gen_pontos(gdf, raio_metros): pontos_novos = [] for i in gdf.index: geo = gdf['geometry'][i].buffer(raio_metros) minx, miny, maxx, maxy = geo.bounds while True: if distrib == "uniforme": x = random.uniform(minx, maxx) y = random.uniform(miny, maxy) elif distrib == "normal": x, y = gdf['geometry'][i].xy x = x[0] y = y[0] x = random.gauss(x, desvpad) y = random.gauss(y, desvpad) elif distrib == "log-normal": x, y = gdf['geometry'][i].xy x = x[0] y = y[0] x = random.lognormvariate(x, desvpad) y = random.lognormvariate(y, desvpad) else: raise NameError(f"A distribuição {distrib} não existe") p = Point(x, y) if distrib == "uniforme": if p.intersects(geo): pontos_novos.append(p) break else: pontos_novos.append(p) break return pontos_novos
def game_loop(self): while 1: for event in pygame.event.get(): if event.type == QUIT: return elif event.type == KEYDOWN and event.key == K_ESCAPE: return elif event.type == MOUSEBUTTONDOWN: pt = Point(pygame.mouse.get_pos()) #this list comprehension gets the clicked region pt_match = ([[key for key,val in regns.iteritems() if pt.intersects(Polygon(val))] for regns in self.all_regions]) if pt_match: #clear out the empty lists so that pt_match #only contains the region string pt_match = [match for match in pt_match if match] try: #send click info to the world object self.world.process_action(pt_match[0][0]) except IndexError: pass self.screen.blit(self.background, (0, 0)) pygame.display.flip()
def get_admissible_data(observer, building_polygons): """ Validate the GPS location of a panoramic image and the size of a building. """ observer_point = Point(observer) admissible_building_polygons = {} for k, v in list(building_polygons.items()): building_poly = Polygon(v) # Check if point q is too close to a building if building_poly.exterior.distance(observer_point) < MIN_DISTANCE: print("GNSS/INS measurement error found! Too close to building.") return {} # Check if point q is inside a building (GNSS/INS error) if observer_point.intersects(building_poly): print("GNSS/INS measurement error found! Point inside building.") return {} # Check for building area sizes that we ignore (Too large: Bijenkorf, too small: mini snackbar) if MIN_BUILDING_AREA < building_poly.area < MAX_BUILDING_AREA: admissible_building_polygons[k] = v return admissible_building_polygons
class PointAdder(EdgePropogator): """ point_node Node with geom such that intersects an edge that edge will be split by the point_to add """ def __init__(self, point_node, **kwargs): super(PointAdder, self).__init__(name=point_node, **kwargs) self.point = point_node self.geom = Point(point_node.geom) self.seen = set() def on_default(self, edge, _, **kwargs): self.seen.add(edge.id) return edge, _ def next_fn(self, edge): return [x for x in edge.target.neighbors(edges=True) \ + edge.source.neighbors(edges=True) if x.id not in self.seen] def is_terminal(self, edge, _, **kwargs): ix = self.geom.intersects(LineString(edge.geom)) return ix def on_terminal(self, edge, _, **kwargs): e2 = edge.split(self.point) return e2, _
def _local_goal_pose_callback(self, request): local_pose_goal = request.local_pose_goal is_manipulation = request.is_manipulation response = LocalGoalPoseResponse() goal_map_coords = Utils.map_coord_from_ros_pose(local_pose_goal, self.simulated_map.info.resolution) expected_polygon_footprint = Point((local_pose_goal.pose.position.x, local_pose_goal.pose.position.y)).buffer(self.simulated_robot_state.metadata.radius) # Check if expected robot polygon is going to intersect any obstacle's polygons obstacles_colliding_with_robot = set() for obstacle in self.simulated_map.obstacles.values(): if expected_polygon_footprint.intersects(obstacle.polygon): # If the robot entered in collision with at least one unmovable obstacle, the pose is not updated if obstacle.movability == Movability.unmovable: response.real_pose = self.simulated_robot_state.pose return response obstacles_colliding_with_robot.add(obstacle) # Check if robot is going to enter in contact with static obstacles is_colliding_with_static_obstacles = ( Utils._is_in_matrix(goal_map_coords[0], goal_map_coords[1], self.simulated_map.info.width, self.simulated_map.info.height) and self.simulated_map.inflated_static_occ_grid[goal_map_coords[0]][goal_map_coords[1]] >= Utils.ROS_COST_POSSIBLY_CIRCUMSCRIBED) # If no obstacles collided with the robot, simply update the pose if not (bool(obstacles_colliding_with_robot) or is_colliding_with_static_obstacles): self.simulated_robot_state.set_pose(local_pose_goal, self.simulated_map.info.resolution) self.simulated_fov_pointcloud = self.simulated_map.get_point_cloud_in_fov(self.simulated_robot_state.pose) else: # Else we must check if these obstacles collide with other when translation is applied translation = (local_pose_goal.pose.position.x - self.simulated_robot_state.pose.pose.position.x, local_pose_goal.pose.position.y - self.simulated_robot_state.pose.pose.position.y) for colliding_obstacle in obstacles_colliding_with_robot: colliding_obstacle_with_push = copy.deepcopy(colliding_obstacle) colliding_obstacle_with_push.move(translation) if colliding_obstacle_with_push.movability == Movability.movable: # Check collision between obstacles for other_obstacle_id, other_obstacle in self.simulated_map.obstacles.items(): # If the movable obstacle we are moving enters in collision with another, the pose is not updated if (other_obstacle_id != colliding_obstacle_with_push.obstacle_id and colliding_obstacle_with_push.polygon.intersects(other_obstacle.polygon)): response.real_pose = self.simulated_robot_state.pose return response # Check collision between pushed obstacle and static obstacles for map_point in colliding_obstacle_with_push.discretized_polygon: if self.simulated_map.static_occ_grid[map_point[0]][map_point[1]] == Utils.ROS_COST_LETHAL: response.real_pose = self.simulated_robot_state.pose return response # If no obstacle bothered us while pushing a movable obstacle, apply translation to it and update robot pose self.simulated_robot_state.set_pose(local_pose_goal, self.simulated_map.info.resolution) if is_manipulation: for colliding_obstacle in obstacles_colliding_with_robot: self.simulated_map.manually_move_obstacle(colliding_obstacle.obstacle_id, translation) self.simulated_fov_pointcloud = self.simulated_map.get_point_cloud_in_fov(self.simulated_robot_state.pose) response.real_pose = self.simulated_robot_state.pose return response
def Max_Local_Height_From_DEM(lon, lat, DEM_FILES_DIRECTORY, nom_height=0, scale=(3, 3)): #form=['tiff','tif','.img']#DEM FORMATS #fNames=filesinsidefolder(DEM_FILES_DIRECTORY,form) indexFile = path.join(DEM_FILES_DIRECTORY, 'index.shp') # driver= ogr.GetDriverByName('ESRI Shapefile') # indexLayerSource = driver.Open(indexFile, 0) # import fiona #shape = fiona.open("my_shapefile.shp")0 means read-only. 1 means writeable. # indexLayer=indexLayerSource.GetLayer() intersects = 0 for feature in fiona.open(indexFile): #first = shape_f.next() polygon = shape(feature['geometry']) point = Point(lon, lat) #polygon = Polygon(pts) #vertices=list(polygon.exterior.coords) if (point.intersects(polygon)): intersects = 1 raster_file = feature['properties']['fileName'] #print(raster_file) dataset = gdal.Open(raster_file) cols = dataset.RasterXSize rows = dataset.RasterYSize transform = dataset.GetGeoTransform() xOrigin = transform[0] yOrigin = transform[3] pixelWidth = transform[1] pixelHeight = -transform[5] band = dataset.GetRasterBand(1) data = band.ReadAsArray(0, 0, cols, rows) col = int((point.x - xOrigin) / pixelWidth) row = int((yOrigin - point.y) / pixelHeight) max_height = 0 mh_index = (col, row) for x in range(col - scale[0], col + scale[0]): for y in range(row - scale[1], row + scale[1]): if not (x < 0 or x >= cols or y < 0 or y >= rows): if (data[y][x] > max_height): max_height = data[y][x] mh_index = (x, y) mh_lon = mh_index[0] * pixelWidth + xOrigin mh_lat = mh_index[1] * pixelHeight + yOrigin from scipy.ndimage import maximum_filter maxs = maximum_filter(data, size=(20, 20)) height = maxs[row][col] return ((1, (mh_lat, mh_lon, max_height))) if (intersects == 0): #print('NO INTERSECTION') return ((0, (lat, lon, nom_height)))
def generateLocationDicts(locations, ODATA_URL_VARS, ODATA_ID_W, ODATA_ID_Q, searchpolygon): """Generates location dictionaries for water level (W) and discharge (Q). Dictionary key is location number (Paikka_Id) and values are dictionaries with coordinates and location names. Parameters ---------- locations: list of dictionaries Site locations and their metadata from OData API. ODATA_URL_VARS: ODATA url for variable metadata. ODATA_ID_W: string OData water level name. ODATA_ID_Q: string OData discharge name. searchpolygon: polygon Area of interest. Returns W and Q location dictionaries. """ varlist = getVars(ODATA_URL_VARS) locdb_W = {} locdb_Q = {} vardict = {} for var in varlist: # Build vartable where equivalent keys are the var id and it's code string vardict[var["Suure_Id"]] = var vardict[var["Suurekoodi"]] = var for loc in locations: d = {} d['x'] = loc['KoordErTmIta'] d['y'] = loc['KoordErTmPohj'] d['name'] = loc['Nimi'] # Remove points too far outside lake polygon p = Point(d['x'], d['y']) if not p.intersects(searchpolygon): print( "Skipped", d['name'], "(" + vardict[loc['Suure_Id']]['NimiEng'] + ") at coordinates", d['x'], d['y'], "- outside search area.") continue # Distribute elevation and discharge data to separate location dicts if loc['Suure_Id'] == ODATA_ID_W: # Might want to change id lookup to var code lookup from vardict locdb_W[loc['Paikka_Id']] = d mtype = "elevation" elif loc['Suure_Id'] == ODATA_ID_Q: locdb_Q[loc['Paikka_Id']] = d mtype = "discharge" else: print( "Skipped", d['name'], "(" + vardict[loc['Suure_Id']]['NimiEng'] + ") at coordinates", d['x'], d['y']) continue print("Found", mtype, "measurement site at", d['name'], "with coordinates", d['x'], d['y']) return locdb_W, locdb_Q
def inpoly(atz,ply): atm = Point(atz[0], atz[1]) check1 = atm.within(ply) check2 = atm.intersects(ply) if check1 == True: return check1 if check2 == True: return check2
def point_in_cell(point, vertices): from shapely.geometry import Point, Polygon p = Point(point) poly = Polygon(vertices) if p.intersects(poly): return True else: return False
def bnw(self, inside=False): poly = Polygon(self.lines) for y in xrange(self.size): for x in xrange(self.size): p = Point(x, y) if p.intersects(poly): self.array[x, y] = 1 else: self.array[x, y] = -1
def distance(self, position: Point2D) -> float: """ returns:: (distance, nearest position in state) """ p = Point(position) if not self.duality: return math.inf polygon = self.duality.geometry if p.intersects(polygon): return 0 return p.distance(polygon.exterior)
def bnw(self, inside=False): poly = Polygon(self.lines) for y in xrange(self.size): for x in xrange(self.size): p = Point(x,y) if p.intersects(poly): self.array[x,y] = 1 else: self.array[x,y] = -1
def grid_id(lat, long): point = Point(lat, long) for i in grid_poly: try: if point.within(grid_poly[i]) or point.intersects(grid_poly[i]): return i except: print("Invalid point") return None return None
def intersect_point(objshape, lat, lon, t_srs=config.TARGET_PROJECTION, s_srs=config.SOURCE_PROJECTION, transform=True): '''Transform point checks if Point intersects objshape ''' if transform == True: x, y = transform_point(lat, lon, t_srs, s_srs) else: x, y = lat, lon point = Point(x, y) if point.intersects(shape(objshape)): return True return False
def nearest_point(self, position: Point2D) -> Optional[Point2D]: """ returns:: (distance, nearest position in state) """ p = Point(position) if not self.duality: return None polygon = self.duality.geometry if p.intersects(polygon): return position ring = polygon.exterior n_point = ring.interpolate(ring.project(p)) return n_point.coords[0]
def plan(self, waypoints): point = Point(waypoints.n, waypoints.e).buffer(self.radius+self.clearance) safe_point = True if point.within(self.bound_poly): for obs in self.obstacles: if point.intersects(Point(obs.n, obs.e).buffer(obs.r)): safe_point = False if safe_point: if waypoints.d > -45.0: waypoints.d = -45.0 return([waypoints]) north = waypoints.n east = waypoints.e std = 50 count = 0 safe_point = False while safe_point == False: count = count + 1 n = np.random.normal(loc=north, scale=std) e = np.random.normal(loc=east, scale=std) print(n,e) point = Point(n, e).buffer(self.radius+self.clearance) safe_point = True if point.within(self.bound_poly): for obs in self.obstacles: if point.intersects(Point(obs.n, obs.e).buffer(obs.r)): safe_point = False if np.mod(count,5) == 0: std = std + 25 return([msg_ned(n,e,-45.0)])
def calc_store_load(req): CarFormSet = formset_factory(CarForm) formset = CarFormSet(req.POST) if formset.is_valid(): data = formset.cleaned_data store = OreStore.objects.first() store_coords = json.loads(store.wkt_coords) poly = Polygon(store_coords) store_fe_weight = (store.current_volume * store.fe) / 100 store_sio2_weight = (store.current_volume * store.sio2) / 100 new_store_fe = store.fe new_store_sio2 = store.sio2 volume_after_unload = store.current_volume cars_ids = list() for row in data: point = Point([int(coord) for coord in row['coords'].split(" ")]) #if point.within(poly): if point.intersects(poly): cars_ids.append(int(row['car'])) cars = Car.objects.in_bulk(cars_ids) for car in cars.values(): volume_after_unload += car.current_load car_fe_weight = (car.current_load * car.fe) / 100 car_sio2_weight = (car.current_load * car.sio2) / 100 new_store_fe = ((store_fe_weight + car_fe_weight) / volume_after_unload) * 100 new_store_sio2 = ((store_sio2_weight + car_sio2_weight) / volume_after_unload) * 100 store_fe_weight += car_fe_weight store_sio2_weight += car_sio2_weight return render(req, "carspanel/store.html", {'store': store, 'volume': store.current_volume, 'volume_after_unload': volume_after_unload, 'new_store_fe': int(new_store_fe), 'new_store_sio2': int(new_store_sio2)}) else: return redirect(reverse('index'))
def get_subset_index_of_point(self, point: Point) -> Optional[int]: """ Args: point (Point): some point in the phase space of the dynamic system Returns: (int): unique index of the subset of the Markov partition that contains the given point """ n = len(self.markov_partition) for k in range(n): if point.intersects(self.markov_partition[k]): return k return None
def median_of_circle(fp, point, pt_crs, r_circ, line=None): ''' Returns median value of a geotiff within a circle with radius r_circ around point P. If line is given, circle will be intersected with line and merged if line cuts circle, and the median taken from the merged polygon. Arguments: fp (str): filepath to GeoTiff point (array): x,y coordinates in pt_crs of point P. pt_crs (str): EPSG-code of point coordinate reference system l_side (int): radius in meters around point to derive median value from. Optional argument: line (array): ndarray with coordinates of a polyline. Returns: float: median value of all points within defined circle array: coordinates of polygon around point. ''' # define window in meters regardless of coordinate system of raster circ_poly = Point(point).buffer(r_circ) if line is not None: print('Line overlaps circle:') # see if circle overlaps with terminus. check = circ_poly.intersects(LineString(line)) print(check) if check == True: # split circle with terminus if it overlaps cut_circ = shapely.ops.split(circ_poly, LineString(line)) #print(cut_circ[0].length) #print(cut_circ[1].length) if cut_circ[0].length > cut_circ[1].length: circ_poly = cut_circ[0] else: circ_poly = cut_circ[1] #reproject circle to crs of image circ_poly_coords = np.array(circ_poly.exterior.coords) circ_reproj = np.zeros((len(circ_poly_coords), 2)) with rasterio.open(fp) as src: img_crs = pyproj.Proj(src.crs) # Pass CRS of image from rasterio pt_crs = pyproj.Proj(init=pt_crs) for i in range(0, len(circ_reproj)): circ_reproj[i] = pyproj.transform(pt_crs, img_crs, circ_poly_coords[i, 0], circ_poly_coords[i, 1]) circ_reproj_poly = Polygon(circ_reproj) circ = [mapping(circ_reproj_poly)] out_image, out_transform = mask(src, circ, crop=True) out_meta = src.meta.copy() median_value = np.median(out_image) return (median_value, circ_poly_coords)
def perform_geo_class_nyc(): """ since the slowest part of this is figuring out which borough things are in, we are going to classify them and store the result in SQL where a JOIN will hopefully be quicker. :return: """ connect() boroughs = fiona.open('./shp/nybb_wgs84.shp', 'r') geoms = list() for item in boroughs: shply_shape = shape(item['geometry']) adder = [shply_shape, item['properties']['BoroName']] geoms.append(adder) curs = CONNECTION.cursor(cursor_factory=psycopg2.extras.DictCursor) notes = "nyc_class" sql = "SELECT fd.latitude, fd.longitude, fd.internal_id, gc.geo_notes " \ "FROM flickr_data fd " \ "LEFT JOIN geo_class gc ON (fd.internal_id=gc.internal_id) " \ "WHERE gc.internal_id IS NULL" # Warning: doens't take into account non-NYC classifications curs.execute(sql) res = curs.fetchall() print("Results to categorize: ", len(res)) for numcoord, line in enumerate(res): if (numcoord % 10000) == 0: print(time.asctime(), "Geo categorized: ", numcoord) tgt_point = Point(line['longitude'], line['latitude']) # if not tgt_point.intersects(not_nyc_shp): tval = -1 for index, geo in enumerate(geoms): if tgt_point.intersects(geo[0]): tval = index break geo_code = tval if tval > -1: geo_text = geoms[tval][1] else: geo_text = "none" sql = "INSERT INTO geo_class" \ " (geo_code, geo_text, internal_id, geo_notes) " \ "VALUES (%s, %s, %s, %s)" data = (geo_code, geo_text, line['internal_id'], notes) curs.execute(sql, data) CONNECTION.commit()
def inside_limits(self, point): """Checks if point is inside coords limits or possible region.""" if not self.regions: # Use rectangle check lat, lon = point.latitude, point.longitude if (lon > self.limits[0] and lat > self.limits[1] and lon < self.limits[2] and lat < self.limits[3]): return True else: return False else: # Check inside all possible regions p = Point((point.longitude, point.latitude)) for name, poly in self.regions.items(): # if poly.contains(p): if p.intersects(poly): return name return False
def game_loop(): while 1: for event in pygame.event.get(): if event.type == QUIT: return elif event.type == KEYDOWN and event.key == K_ESCAPE: return elif event.type == MOUSEBUTTONDOWN: pt = Point(pygame.mouse.get_pos()) #this list comprehension gets the clicked region pt_match = ([[key for key,val in regns.iteritems() if pt.intersects(Polygon(val))] for regns in all_regions]) if pt_match: #player clicked a region, coords are in nested list #clear out the empty lists so that pt_match #only contains the region int pt_match = [match for match in pt_match if match] try: pt_int = pt_match[0][0] print "state is " print pt_int except IndexError: pt_int = 14 else: #player clicked outside of a region pt_int = 15 #send click info to the world object global world world = _world.process_action(world, pt_int) world = _world.refresh(world) color_regions(world) ''' if world['end'] == 1: print "Game over." return ''' background.blit(foreground, (0, 0)) screen.blit(background, (0, 0)) pygame.display.flip() pygame.display.update()
def blocks(self, location): """ If the location is blocked by some other location in the index, return the blocker's name or False. """ coord = self.locationCoordinate(location).zoomTo(self.zpixel) point = Point(coord.column, coord.row) # figure out which quad the point is in key = quadkey(coord.zoomTo(self.zgroup)) # first try the easy hash check if key not in self.quads: return False # then do the expensive shape check for (name, area) in self.quads[key]: if point.intersects(area): # ensure name evals to true return name or True return False
def get_borough_item(json_line, borough_geoms, stopwords, findwords): """ given a line of JSON data, return the borough and JSON line, if applicable. If no borough, return a -1 code . :param json_line: JSON flickr object :param borough_geoms: Geometry objects for boroughs :return: (borough_key, JSON flickr object) """ geoms = borough_geoms lat = json_line['latitude'] lon = json_line['longitude'] # It pains me to do the below two lines, but sometimes flickr # sends back geos that match NYC but the original data is bad # I think it has to do with their indexing being diffrent from # actual values? But anyways, we need the data and the tags # make sense for NYC. if lon > 0: lon *= -1 tgt_point = Point(lon, lat) # silly shapely - Point(x,y,z) ret_val = list() # if not tgt_point.intersects(not_nyc_shp): for index, geo in enumerate(geoms): if tgt_point.intersects(geo[0]): ret_val.append(index) break if len(ret_val) > 0: tags = json_line['tags'] + " " + json_line['title'] tags = clean_tags(tags, stopwords, findwords) if len(tags) > 20: # more than 20 chars ret_val.append(tags) return ret_val else: return False
grid_polygons.append(Polygon(s.points)) xpts = np.linspace(xmin+pt_deltax,xmax-pt_deltax,5) ypts = np.linspace(ymin+pt_deltay,ymax-pt_deltay,10) #--find valid points - those that intersect a model cell with an appropriate ibound valid_points = [] valid_Points = [] for x in xpts: for y in ypts: pt = [x,y] point = Point(pt) #--make sure this point is in an active model cell for poly,rec in zip(grid_polygons,grid_records): ibnd = rec[ibnd_idx] if ibnd != 0 and ibnd != 2 and point.intersects(poly): valid_points.append([x,y]) valid_Points.append(point) pass #--write a valid points shapefile wr = shapefile.Writer() wr.field('x',fieldType='N',size=20,decimal=3) wr.field('y',fieldType='N',size=20,decimal=3) for [x,y] in valid_points: wr.poly([[[x,y]]],shapeType=shapefile.POINT) wr.record([x,y]) wr.save('shapes\\nexrad_points')
def ISR_figures_demo(): import mywfc3.orient #### Pixel scale ps = {'x':0.1355, 'y':0.1211} #### dispersion offsets disp_box = {'G102': [53, 210], 'G141': [36, 168]} #### Apertures refpix_aper = {'GRISM1024':[497, 562], 'IR':[562, 562], 'IR-FIX': [512,512]} grism = 'G102' aper = 'GRISM1024' #### Demo fig = unicorn.plotting.plot_init(xs=11, aspect=1./4, square=True, left=0.01, bottom=0.01, right=0.01, top=0.01, use_tex=True, NO_GUI=True) theta_pairs = [[-20], [50], [160], [-20,50,160]] dx = 0.99/len(theta_pairs) for i in range(len(theta_pairs)): ax = fig.add_axes((0.005+i*(dx+0*0.005),0.01, dx, 0.97)) if i < 3: ax.text(0.5, 0.05, r'PA($y$) = %d' %(-theta_pairs[i][0]), transform=ax.transAxes, ha='center', va='bottom') else: ax.text(0.5, 0.05, r'PA($y$) = %d $\times$ %d $\times$ %d' %(-theta_pairs[i][0], -theta_pairs[i][1], -theta_pairs[i][2]), transform=ax.transAxes, ha='center', va='bottom') ax.scatter(refpix_aper[aper][0]*ps['x'],refpix_aper[aper][1]*ps['y'], color='purple', marker='x', s=60, label=aper) plt_range = np.array([-72, 180])*0.95 ax.set_xlim(plt_range+10); ax.set_ylim(plt_range+10) ax.set_xticklabels([]); ax.set_yticklabels([]) ax.set_xticks(ax.get_xlim()); ax.set_yticks(ax.get_ylim()) ax.plot([-1000,-1100],[-1110,-1100], color='green', alpha=0.4, linewidth=2, label=grism) if i == 0: ax.legend(scatterpoints=1, loc='upper left', fontsize=10) #theta = 70 pair = theta_pairs[i] results = [] for theta in pair: result = mywfc3.orient.overlap(theta=theta, grism=grism, have_mosaic=True, plot=False, aper='GRISM1024', f_spec=0.5, recenter_target=False) results.append(result) overlap_region = results[0][1].intersection(results[0][1]) for result in results: overlap_region = overlap_region.intersection(result[1]) for j in range(len(pair)): full_poly, sub_poly = results[j][0], results[j][1] full_patch = PolygonPatch(full_poly, fc='None', ec='black', alpha=0.9, zorder=-2) sub_patch = PolygonPatch(sub_poly, fc='black', ec='black', alpha=0.2, zorder=-2) ax.add_patch(full_patch) ax.add_patch(sub_patch) ### Death star xds, yds = threedhst.utils.xyrot(np.array([357,357])*ps['x'], np.array([55,55])*ps['y'], pair[j], x0=refpix_aper[aper][0]*ps['x'], y0=refpix_aper[aper][1]*ps['y']) ds = Point(xds[0], yds[0]).buffer(20*ps['x']) ds_patch = PolygonPatch(ds, fc='None', ec='black', alpha=0.9, zorder=-2) ax.add_patch(ds_patch) if i == 3: isect_patch = PolygonPatch(overlap_region, fc='green', ec='green', alpha=0.2, zorder=1) ax.add_patch(isect_patch) else: for xi in np.arange(-15, 180, 33): for yi in np.arange(15, 180, 30): pi = Point((xi, yi)) if pi.intersects(overlap_region): ax.scatter(xi, yi, color='green', alpha=0.8, zorder=1) trace_x = xi+np.array(disp_box[grism])*ps['x'] trace_y = np.array([yi,yi]) trace_x, trace_y = threedhst.utils.xyrot(trace_x, trace_y, pair[j], x0=xi, y0=yi) #ax.plot(trace_x, trace_y, color='green', alpha=0.4, linewidth=2, zorder=1) trace_line = LineString([(trace_x[0], trace_y[0]), (trace_x[1], trace_y[1])]) trace_in_full = trace_line.buffer(1, resolution=8) ax.add_patch(PolygonPatch(trace_in_full, fc='black', ec='None', alpha=0.15, zorder=1)) trace_in_full = trace_line.buffer(1, resolution=8).intersection(full_poly) ax.add_patch(PolygonPatch(trace_in_full, fc='green', ec='None', alpha=0.5, zorder=1)) unicorn.plotting.savefig(fig, 'orient_demo.pdf') #### Testing if False: line = LineString([(-2,0.5), (0.5, 0.5)]) full_box_x = np.array([0, 1, 1, 0, 0]) full_box_y = np.array([0, 0, 1, 1, 0]) box = Polygon(np.array([full_box_x, full_box_y]).T) line_within = line.buffer(0.02, resolution=8).intersection(box) plt.figure() ax = plt.gca() ax.add_patch(PolygonPatch(box, fc='black', ec='black', alpha=0.2, zorder=-2)) ax.add_patch(PolygonPatch(line_within, fc='green', ec='None', alpha=0.2, zorder=-1)) plt.xlim(-2,2) plt.ylim(-2,2)
def test_intersects(): circle = Point([1,2]).buffer(2) line = LineString([[0,0],[2,0]]) c = line.buffer(1) circle.intersects(c)
def fix_duplicated_lines(lines, MINIMALDIST): new_lines=[] while len(lines)>0: print len(lines),len(new_lines) l1=lines[0] if l1.length<MINIMALDIST: print 'Null-length' lines.pop(0) continue if len(lines)==0: break elif len(lines)==1: new_lines.append(l1) lines.pop(0) break else: for i in range(1, len(lines)+1): if i==len(lines): new_lines.append(l1) lines.pop(0) break l2=lines[i] if l2.length<MINIMALDIST: print 'Null-length' lines.pop(i) break a1=angle_of_line(l1) a2=angle_of_line(l2) if math.fabs(a1-a2)<=2: bff=l1.buffer(MINIMALDIST, resolution=16, cap_style=2) if bff.intersects(l2)==True: if bff.exterior.intersection(l2).geom_type=='GeometryCollection': # contains lines.pop(i) print 'Contains' break elif bff.exterior.intersection(l2).geom_type=='Point': # overlapped or consecutive pt11=Point(list(l1.coords)[0]) pt12=Point(list(l1.coords)[1]) pt21=Point(list(l2.coords)[0]) pt22=Point(list(l2.coords)[1]) if pt21.intersects(bff)==True: if pt22.distance(pt11)>=pt22.distance(pt12): nl=LineString([(pt11.x, pt11.y), (pt22.x, pt22.y)]) else: nl=LineString([(pt12.x, pt12.y), (pt22.x, pt22.y)]) else: if pt21.distance(pt11)>=pt21.distance(pt12): nl=LineString([(pt11.x, pt11.y), (pt21.x, pt21.y)]) else: nl=LineString([(pt12.x, pt12.y), (pt21.x, pt21.y)]) lines.pop(i) lines.pop(0) lines.append(nl) print 'Overlapped or Consecutive' break elif bff.exterior.intersection(l2).geom_type=='MultiPoint': # contained lines.pop(0) print 'Contained' break return new_lines
def intersection(self, lon, lat): if self.index_zupc is None: self.__init_zupc() p = Point(lon, lat) return [i.id for i in self.index_zupc.intersection((lon, lat, lon, lat), objects=True) if p.intersects(i.object)]