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 create_voxmap_in_area(data, area_center, horizontal_delta, vertical_delta, voxel_size=5): """ Returns a grid representation of a 3D configuration space based on given obstacle data. The `voxel_size` argument sets the resolution of the voxel map. """ # minimum and maximum north coordinates north_min = np.floor(np.amin(data[:, 0] - data[:, 3])) north_max = np.ceil(np.amax(data[:, 0] + data[:, 3])) # minimum and maximum east coordinates east_min = np.floor(np.amin(data[:, 1] - data[:, 4])) east_max = np.ceil(np.amax(data[:, 1] + data[:, 4])) # maximum altitude alt_max = np.ceil(np.amax(data[:, 2] + data[:, 5])) # given the minimum and maximum coordinates we can # calculate the size of the grid. north_size = int(2*horizontal_delta) // voxel_size east_size = int(2*horizontal_delta) // voxel_size alt_size = int(2*vertical_delta) // voxel_size area_coordinates = [ (area_center[0]-horizontal_delta, area_center[1]-horizontal_delta), (area_center[0]-horizontal_delta, area_center[1]+horizontal_delta), (area_center[0]+horizontal_delta, area_center[1]+horizontal_delta), (area_center[0]+horizontal_delta, area_center[1]-horizontal_delta), ] area_polygon = Polygon(area_coordinates) print("polygon area", area_polygon.area) # Create an empty grid voxmap = np.zeros((north_size, east_size, alt_size), dtype=np.bool) for i in range(data.shape[0]): # TODO: fill in the voxels that are part of an obstacle with `True` # # i.e. grid[0:5, 20:26, 2:7] = True north, east, alt, d_north, d_east, d_alt = data[i, :] obstacle = [ int(north - d_north - north_min), int(north + d_north - north_min), int(east - d_east - east_min), int(east + d_east - east_min), ] obstacle_polygon = Polygon([ (obstacle[0], obstacle[2]), (obstacle[0], obstacle[3]), (obstacle[1], obstacle[3]), (obstacle[1], obstacle[2]), ]) # check if obstacle in area of interest if not area_polygon.contains(obstacle_polygon) and not area_polygon.crosses(obstacle_polygon): continue if not((area_center[2] - vertical_delta) < alt + d_alt < (area_center[2] + vertical_delta)): continue obstacle_coordinates = [ limit_value_int(north - d_north - north_min - area_center[0] - horizontal_delta, 0, 2*horizontal_delta) // voxel_size, limit_value_int(north + d_north - north_min - area_center[0] - horizontal_delta, 0, 2*horizontal_delta) // voxel_size, limit_value_int(east - d_east - east_min - area_center[1] - horizontal_delta, 0, 2*horizontal_delta) // voxel_size, limit_value_int(east + d_east - east_min - area_center[1] - horizontal_delta, 0, 2*horizontal_delta) // voxel_size, ] height = limit_value_int(alt + d_alt - vertical_delta, 0, 2*vertical_delta) // voxel_size voxmap[ obstacle_coordinates[0]:obstacle_coordinates[1], obstacle_coordinates[2]:obstacle_coordinates[3], 0:height ] = True return voxmap