def _download_parks(file_path, geofence_file, nest_parks=False): parks = [] geofences = parse_geofence_file(geofence_file) for geofence in geofences: log.info('Downloading parks in geofence %s...', geofence['name']) box = get_geofence_box(geofence) parks_in_box = _query_overpass_api(box['sw'], box['ne'], nest_parks) coords = geofence['polygon'] coords.append(coords[0]) path = matplotlib.path.Path(coords) parks_in_geofence = [] for park in parks_in_box: for coord in park: coord_tuple = (coord[0], coord[1]) if path.contains_point(coord_tuple): parks_in_geofence.append(park) break log.info('%d parks found in geofence %s.', len(parks_in_geofence), geofence['name']) parks.extend(parks_in_geofence) output = {"date": str(datetime.now()), "parks": parks} if len(output['parks']) > 0: with open(file_path, 'w') as file: json.dump(output, file, separators=(',', ':')) log.info('%d parks downloaded to %s.', len(output['parks']), file_path) else: log.info('0 parks downloaded. Skipping saving to %s', file_path)
def flood_area(polygon): path = matplotlib.path.Path(polygon) # Calculated center. lat, lon = [sum(y) / len(y) for y in zip(*polygon)] starting_cell = s2sphere.CellId.from_lat_lng(s2sphere.LatLng.from_degrees( lat, lon)).parent(16) seen = set() cells = set() candidates = [starting_cell] # Add all cells that are strictly within the polygon. while candidates: cell = candidates.pop() if not path.contains_point(lat_lng(cell)): continue cells.add(cell) for neighbour in cell.get_edge_neighbors(): if neighbour not in seen: seen.add(neighbour) candidates.append(neighbour) # Then add all cells on the border to make sure we cover the area. for cell in list(cells): for neighbour in cell.get_edge_neighbors(): if neighbour not in cells: cells.add(neighbour) return cells
def find_containing_building(latlon): """Find the building that contains item""" import matplotlib.path for building in locations: if building.data['type'] != 'building': continue path = matplotlib.path.Path(building.outline) if path.contains_point(latlon): return building
def point_in_polygon(x: float, y: float, polygon: typing.List[typing.List[float]]) -> bool: """ Checks if a point is in a given polygon. :param x: x :param y: y :param polygon: The polygon to check for inclusion :return: Whether or not the given point is in the provided polygon """ path = matplotlib.path.Path(vertices=numpy.array(polygon), closed=True) return path.contains_point([x, y])
def count_by_position(regions, data, key=None): paths = { name: matplotlib.path.Path( np.array(polygon), ([matplotlib.path.Path.MOVETO] + [matplotlib.path.Path.LINETO for _ in range(len(polygon) - 2)] + [matplotlib.path.Path.CLOSEPOLY] ) ) for name, polygons in regions.iteritems() for polygon in polygons } count = collections.defaultdict(int) for entry in data: for name, path in paths.iteritems(): pos = entry['position'][1], entry['position'][0] if path.contains_point(pos): count[name] += int(entry.get(key, 1)) return count
def is_point_within_polygon(point, polygonVertexCoords): path = matplotlib.path.Path( polygonVertexCoords) return path.contains_point(point[0], point[1])
def test_path(): """Create a fake path and test it for anomalies using KDEClassifier.""" with open("points.pickle", "r") as f: points = pickle.load(f) # all points outside of poly are ignored """ poly = np.array([ [115.707, 7.9], [116.509, 7.727], [118.295, 10.3057], [118.915, 13.4038], [118.21, 13.4038], [117.652, 10.667], [115.707, 7.9] ]) """ poly = np.array([[106.146, 4.0428], [114.046, 12.1145], [111.592, 14.1508], [105.508, 4.06733], [106.146, 4.0428]]) """ poly = np.array([ [113.531, 11.356], [113.531, 9.2452], [116.328, 9.2452], [116.328, 11.356], [113.531, 11.356] ]) """ raw_training_data = np.array([[ p[0], p[1], p[3] * np.sin(p[2] / 10.0 * np.pi / 180) / 600, p[3] * np.cos(p[2] / 10.0 * np.pi / 180) / 600 ] for p in points]) path = matplotlib.path.Path(poly[:-1], closed=True) training_data = raw_training_data # crop using poly training_data = training_data[np.apply_along_axis( lambda x: False if path.contains_point([x[0], x[1]]) == 0 else True, 1, raw_training_data)] # select stationary points # training_data = training_data[np.where(training_data[:,2]**2+training_data[:,3]**2 < 10**-2)] kde = KDEClassifier(training_data) minx = min(training_data[:, 0]) miny = min(training_data[:, 1]) maxx = max(training_data[:, 0]) maxy = max(training_data[:, 1]) """ start = np.array([116.103, 7.87]) middle = np.array([117.97, 10.47]) anom1 = np.array([117.67, 11.423]) anom2 = np.array([119.221, 10.5673]) anom3 = np.array([116.402, 10.8365]) normal1 = np.array([117.786, 10.0526]) normal2 = np.array([118.224, 11.09]) anom4 = np.array([118.627, 9.99]) end = np.array([118.604, 13.234]) testing_data = np.array(create_long_path([start, normal1, anom4, normal2, end], 0.15, 0.03, 0.06)) """ # create a fake path start = np.array([106.085, 4.458]) end = np.array([112.875, 12.623]) normal1 = np.array([109.655, 8.7621]) normal2 = np.array([111.436, 10.8496]) normal3 = np.array([108.243, 7.10426]) anom1 = np.array([108.888, 11.248]) anom2 = np.array([108.12, 4.0956]) anom3 = np.array([106.505, 9.48133]) testing_data = np.array(create_long_path([end, start], 0.15, 0.03, 0.06)) # classify! fixed_anom_bools = kde.classify(testing_data, fixed=True, approx=True) # plot results m = get_basemap(minx - 1, maxx + 1, miny - 1, maxy + 1) xpts, ypts = m(training_data[:, 0], training_data[:, 1]) us, vs = training_data[:, 2], training_data[:, 3] m.scatter(xpts, ypts, c="k", marker=".", alpha=0.3) # m.quiver(xpts, ypts, us, vs, alpha=0.3) # m.plot(poly[:,0], poly[:,1]) xs, ys = m(testing_data[:, 0], testing_data[:, 1]) # us, vs = testing_data[:,2], testing_data[:,3] colors = np.where(fixed_anom_bools, "r", "g") m.scatter(xs, ys, c=colors, marker="8", s=100) # m.quiver(xs, ys, us, vs, color=colors, scale=3) # m.hexbin(training_data[:,0], training_data[:,1]) plt.show()
#sys.exit(0) # test_path() points = load_points() print "processed file" poly = np.array([[91.0695, -6.4], [91.0695, -19.542], [120.211, -19.542], [120.211, -6.4], [91.0695, -6.4]]) path = matplotlib.path.Path(poly, closed=True) # points = points[-200000:,:2] points = points[np.apply_along_axis( lambda x: False if path.contains_point([x[0], x[1]]) == 0 else True, 1, points)] points = points[np.where(points[:, 2]**2 + points[:, 3]**2 == 0)][:, :2] print points.shape invcov = np.linalg.inv(np.cov(points.T)) # metric = sklearn.neighbors.DistanceMetric.get_metric("mahalanobis", VI=invcov) def metric(x, y): # why is this needed if len(x) == 10: return 0 return np.sqrt(np.einsum("i,ij,j", x - y, invcov, x - y)) # metric = lambda foo, bar: np.sqrt((foo-bar).dot(foo-bar)) labels = sklearn.cluster.DBSCAN(eps=0.3, metric="euclidean").fit_predict( points[:, :2])
def test_path(): """Create a fake path and test it for anomalies using KDEClassifier.""" with open("points.pickle", "r") as f: points = pickle.load(f) # all points outside of poly are ignored """ poly = np.array([ [115.707, 7.9], [116.509, 7.727], [118.295, 10.3057], [118.915, 13.4038], [118.21, 13.4038], [117.652, 10.667], [115.707, 7.9] ]) """ poly = np.array([ [106.146, 4.0428], [114.046, 12.1145], [111.592, 14.1508], [105.508, 4.06733], [106.146, 4.0428] ]) """ poly = np.array([ [113.531, 11.356], [113.531, 9.2452], [116.328, 9.2452], [116.328, 11.356], [113.531, 11.356] ]) """ raw_training_data = np.array([[p[0], p[1], p[3]*np.sin(p[2]/10.0*np.pi/180)/600, p[3]*np.cos(p[2]/10.0*np.pi/180)/600] for p in points]) path = matplotlib.path.Path(poly[:-1], closed=True) training_data = raw_training_data # crop using poly training_data = training_data[np.apply_along_axis(lambda x: False if path.contains_point([x[0], x[1]])==0 else True, 1, raw_training_data)] # select stationary points # training_data = training_data[np.where(training_data[:,2]**2+training_data[:,3]**2 < 10**-2)] kde = KDEClassifier(training_data) minx = min(training_data[:,0]) miny = min(training_data[:,1]) maxx = max(training_data[:,0]) maxy = max(training_data[:,1]) """ start = np.array([116.103, 7.87]) middle = np.array([117.97, 10.47]) anom1 = np.array([117.67, 11.423]) anom2 = np.array([119.221, 10.5673]) anom3 = np.array([116.402, 10.8365]) normal1 = np.array([117.786, 10.0526]) normal2 = np.array([118.224, 11.09]) anom4 = np.array([118.627, 9.99]) end = np.array([118.604, 13.234]) testing_data = np.array(create_long_path([start, normal1, anom4, normal2, end], 0.15, 0.03, 0.06)) """ # create a fake path start = np.array([106.085, 4.458]) end = np.array([112.875, 12.623]) normal1 = np.array([109.655, 8.7621]) normal2 = np.array([111.436, 10.8496]) normal3 = np.array([108.243, 7.10426]) anom1 = np.array([108.888, 11.248]) anom2 = np.array([108.12, 4.0956]) anom3 = np.array([106.505, 9.48133]) testing_data = np.array(create_long_path([end, start], 0.15, 0.03, 0.06)) # classify! fixed_anom_bools = kde.classify(testing_data, fixed=True, approx=True) # plot results m = get_basemap(minx-1, maxx+1, miny-1, maxy+1) xpts, ypts = m(training_data[:,0], training_data[:,1]) us, vs = training_data[:,2], training_data[:,3] m.scatter(xpts, ypts, c="k", marker=".", alpha=0.3) # m.quiver(xpts, ypts, us, vs, alpha=0.3) # m.plot(poly[:,0], poly[:,1]) xs, ys = m(testing_data[:,0], testing_data[:,1]) # us, vs = testing_data[:,2], testing_data[:,3] colors = np.where(fixed_anom_bools, "r", "g") m.scatter(xs, ys, c=colors, marker="8", s=100) # m.quiver(xs, ys, us, vs, color=colors, scale=3) # m.hexbin(training_data[:,0], training_data[:,1]) plt.show()
points = load_points() print "processed file" poly = np.array([ [91.0695, -6.4], [91.0695, -19.542], [120.211, -19.542], [120.211, -6.4], [91.0695, -6.4] ]) path = matplotlib.path.Path(poly, closed=True) # points = points[-200000:,:2] points = points[np.apply_along_axis(lambda x: False if path.contains_point([x[0], x[1]])==0 else True, 1, points)] points = points[np.where(points[:,2]**2+points[:,3]**2 == 0)][:,:2] print points.shape invcov = np.linalg.inv(np.cov(points.T)) # metric = sklearn.neighbors.DistanceMetric.get_metric("mahalanobis", VI=invcov) def metric(x, y): # why is this needed if len(x) == 10: return 0 return np.sqrt(np.einsum("i,ij,j", x-y, invcov, x-y)) # metric = lambda foo, bar: np.sqrt((foo-bar).dot(foo-bar)) labels = sklearn.cluster.DBSCAN(eps=0.3, metric="euclidean").fit_predict(points[:,:2]) print labels unique_labels = list(set(labels))