def load_geofences(tr_cfg): nest_config_path = tr_spy_config["nest_config_path"] with open(nest_config_path) as f: nests = eval(f.read()) if "address" not in nests[0]: gmaps = googlemaps.Client(key=tr_cfg["maps_api_key"]) for nest in nests: polygon_pts = nest["path"] polygon = Polygon(polygon_pts) coords = eval(polygon.representative_point().wkt[6:].replace( " ", ", ")) address = gmaps.reverse_geocode(coords)[0]["formatted_address"] str_split = address.split(", ") if len(str_split) == 4: address = ", ".join(str_split[1:]) nest["address"] = address nest["center"] = coords with open(nest_config_path, "w+") as f: f.write(json.dumps(nests, indent=1)) for nest in nests: polygon_pts = nest["path"] polygon = Polygon(polygon_pts) nest["polygon"] = polygon return nests
def operation_example(): patch = Point(0.0, 0.0).buffer(10.0) print('patch = {}.'.format(patch)) print('patch.area = {}.'.format(patch.area)) line = LineString([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)]) dilated = line.buffer(0.5) eroded = dilated.buffer(-0.3) ring = LinearRing([(0, 0), (1, 1), (1, 0)]) point = Point(0.5, 0.5) polygon = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) print('polygon.contains(point) = {}.'.format(polygon.contains(point))) #box = shapely.geometry.box(minx, miny, maxx, maxy, ccw=True) pa = asPoint(np.array([0.0, 0.0])) la = asLineString(np.array([[1.0, 2.0], [3.0, 4.0]])) ma = asMultiPoint(np.array([[1.1, 2.2], [3.3, 4.4], [5.5, 6.6]])) data = {'type': 'Point', 'coordinates': (0.0, 0.0)} geom = shapely.geometry.shape(data) #geom = shapely.geometry.asShape(data) #-------------------- #geom = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) geom = LineString([(0, 0), (10, 0), (10, 5), (20, 5)]) print('polygon.has_z = {}.'.format(geom.has_z)) if isinstance(geom, LinearRing): print('polygon.is_ccw = {}.'.format(geom.is_ccw)) # LinearRing only. print('polygon.is_empty = {}.'.format(geom.is_empty)) print('polygon.is_ring = {}.'.format(geom.is_ring)) print('polygon.is_simple = {}.'.format(geom.is_simple)) print('polygon.is_valid = {}.'.format(geom.is_valid)) print('polygon.coords = {}.'.format(geom.coords)) print('polygon.area = {}.'.format(geom.area)) print('polygon.bounds = {}.'.format(geom.bounds)) print('polygon.length = {}.'.format(geom.length)) print('polygon.minimum_clearance = {}.'.format(geom.minimum_clearance)) print('polygon.geom_type = {}.'.format(geom.geom_type)) print('polygon.boundary = {}.'.format(geom.boundary)) print('polygon.centroid = {}.'.format(geom.centroid)) print('polygon.convex_hull.exterior.coords.xy = {}.'.format(list((x, y) for x, y in zip(*geom.convex_hull.exterior.coords.xy)))) print('polygon.envelope = {}.'.format(geom.envelope)) print('polygon.minimum_rotated_rectangle = {}.'.format(geom.minimum_rotated_rectangle)) #polygon.parallel_offset(distance, side, resolution=16, join_style=1, mitre_limit=5.0) #polygon.simplify(tolerance, preserve_topology=True) #-------------------- poly1 = Polygon([(3, 3), (5, 3), (5, 5), (3, 5)]) poly2 = Polygon([(1, 1), (4, 1), (4, 3.5), (1, 3.5)]) print('poly1.intersection(poly2) = {}.'.format(poly1.intersection(poly2))) print('poly1.difference(poly2) = {}.'.format(poly1.difference(poly2))) print('poly1.symmetric_difference(poly2) = {}.'.format(poly1.symmetric_difference(poly2))) print('poly1.union(poly2) = {}.'.format(poly1.union(poly2))) print('poly1.equals(poly2) = {}.'.format(poly1.equals(poly2))) print('poly1.almost_equals(poly2, decimal=6) = {}.'.format(poly1.almost_equals(poly2, decimal=6))) print('poly1.contains(poly2) = {}.'.format(poly1.contains(poly2))) print('poly1.covers(poly2) = {}.'.format(poly1.covers(poly2))) print('poly1.crosses(poly2) = {}.'.format(poly1.crosses(poly2))) print('poly1.disjoint(poly2) = {}.'.format(poly1.disjoint(poly2))) print('poly1.intersects(poly2) = {}.'.format(poly1.intersects(poly2))) print('poly1.overlaps(poly2) = {}.'.format(poly1.overlaps(poly2))) print('poly1.touches(poly2) = {}.'.format(poly1.touches(poly2))) print('poly1.within(poly2) = {}.'.format(poly1.within(poly2))) #-------------------- poly1 = Polygon([(40, 50), (60, 50), (60, 90), (40, 90)]) poly2 = Polygon([(10, 100), (100, 100), (100, 150), (10, 150)]) print('The distance between two convex polygons = {}.'.format(poly1.distance(poly2))) print('The Hausdorff distance between two convex polygons = {}.'.format(poly1.hausdorff_distance(poly2))) print('The representative point of a polygon = {}.'.format(poly1.representative_point())) #-------------------- lines = [ ((0, 0), (1, 1)), ((0, 0), (0, 1)), ((0, 1), (1, 1)), ((1, 1), (1, 0)), ((1, 0), (0, 0)), ((1, 1), (2, 0)), ((2, 0), (1, 0)), ((5, 5), (6, 6)), ((1, 1), (100, 100)), ] polys = shapely.ops.polygonize(lines) result, dangles, cuts, invalids = shapely.ops.polygonize_full(lines) merged_line = shapely.ops.linemerge(lines) # Efficient unions. polygons = [Point(i, 0).buffer(0.7) for i in range(5)] shapely.ops.unary_union(polygons) shapely.ops.cascaded_union(polygons) # Delaunay triangulation. points = MultiPoint([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)]) triangles = shapely.ops.triangulate(points) # Voronoi diagram. #points = MultiPoint([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)]) #regions = shapely.ops.voronoi_diagram(points) # Nearest points. triangle = Polygon([(0, 0), (1, 0), (0.5, 1), (0, 0)]) square = Polygon([(0, 2), (1, 2), (1, 3), (0, 3), (0, 2)]) nearest_points = shapely.ops.nearest_points(triangle, square) square = Polygon([(1,1), (2, 1), (2, 2), (1, 2), (1, 1)]) line = LineString([(0,0), (0.8, 0.8), (1.8, 0.95), (2.6, 0.5)]) result = shapely.ops.snap(line, square, 0.5) g1 = LineString([(0, 0), (10, 0), (10, 5), (20, 5)]) g2 = LineString([(5, 0), (30, 0), (30, 5), (0, 5)]) forward, backward = shapely.ops.shared_paths(g1, g2) pt = Point((1, 1)) line = LineString([(0, 0), (2, 2)]) result = shapely.ops.split(line, pt) ls = LineString((i, 0) for i in range(6)) shapely.ops.substring(ls, start_dist=1, end_dist=3) shapely.ops.substring(ls, start_dist=3, end_dist=1) shapely.ops.substring(ls, start_dist=1, end_dist=-3) shapely.ops.substring(ls, start_dist=0.2, end_dist=-0.6, normalized=True)
def extract(access, geocode, layer='twitter', **kwargs): if 'consumer_key' in access.keys() and 'consumer_secret' in access.keys( ) and 'access_token' in access.keys() and 'access_secret' in access.keys(): auth = OAuthHandler(access['consumer_key'], access['consumer_secret']) auth.set_access_token(access['access_token'], access['access_secret']) if 'items_per_request' not in kwargs.keys(): kwargs['items_per_request'] = 100 if 'wait' not in kwargs.keys(): kwargs['wait'] = 60 if 'max_area' not in kwargs.keys(): kwargs['max_area'] = 200 api = tweepy.API(auth) count = 0 total = 0 data = [] while True: try: collection = tweepy.Cursor(api.search, geocode=geocode).items( kwargs['items_per_request']) for status in collection: if status.coordinates: data.append( (status.user.screen_name + ':' + str(status.user.id), int(time.mktime(status.created_at.timetuple())), status.coordinates['coordinates'][1], status.coordinates['coordinates'][0], layer)) total = total + 1 elif status.place: polygon = ShapelyPolygon( status.place.bounding_box.coordinates[0]) geom_area = ops.transform( partial( pyproj.transform, pyproj.Proj(init='EPSG:4326'), pyproj.Proj(proj='aea', lat1=polygon.bounds[1], lat2=polygon.bounds[3])), polygon) if geom_area.area < kwargs['max_area']: point = polygon.representative_point().xy data.append( (status.user.screen_name + ':' + str(status.user.id), int(time.mktime(status.created_at.timetuple())), point[1][0], point[0][0], layer)) # except tweepy.TweepError as e: except: logpath = 'data/logs/' if not os.path.exists(logpath): os.makedirs(logpath) filename = datetime.datetime.fromtimestamp( time.time()).strftime(logpath + 'error_%Y%m%d.log') logging.basicConfig(filename=filename, filemode='a+') logging.exception("message") if 'force' in kwargs.keys() and kwargs['force']: time.sleep(5 * kwargs['wait']) continue if len(data) > 0: frame = pd.DataFrame(data) frame.columns = ['uid', 'timestamp', 'lat', 'lon', 'layer'] data = [] if 'filename' not in kwargs.keys(): kwargs['filename'] = 'data/entries/twitter.csv' if not os.path.exists('data/entries/'): os.makedirs('data/entries/') header = not os.path.exists(kwargs['filename']) with open(kwargs['filename'], 'a+') as file: frame.to_csv(file, header=header, index=False) count = count + 1 if 'limit' in kwargs.keys() and count >= kwargs['limit']: break time.sleep(kwargs['wait'])