def test_set_precision_intersection(): """Operations should use the most precise presision grid size of the inputs""" box1 = shapely.normalize(shapely.box(0, 0, 0.9, 0.9)) box2 = shapely.normalize(shapely.box(0.75, 0, 1.75, 0.75)) assert shapely.get_precision(shapely.intersection(box1, box2)) == 0 # GEOS will use and keep the most precise precision grid size box1 = shapely.set_precision(box1, 0.5) box2 = shapely.set_precision(box2, 1) out = shapely.intersection(box1, box2) assert shapely.get_precision(out) == 0.5 assert_geometries_equal(out, shapely.Geometry("LINESTRING (1 1, 1 0)"))
def test_relate_pattern(): g = shapely.linestrings([(0, 0), (1, 0), (1, 1)]) polygon = shapely.box(0, 0, 2, 2) assert shapely.relate(g, polygon) == "11F00F212" assert shapely.relate_pattern(g, polygon, "11F00F212") assert shapely.relate_pattern(g, polygon, "*********") assert not shapely.relate_pattern(g, polygon, "F********")
def generateParticles(buildingMap): mapBuilder = BuildingMapProto.BuildingMap.MergeFrom(buildingMap) mapBuilder.clearFloors() for floor in buildingMap.floors: floorBuilder = FloorProto.Floor.MergeFrom(floor) floorBuilder.clearLandmarks() navigableArea = createAccessibleArea(floor.navigableSpaces) for landmark in floor.landmarks: radius = 0.5 while radius < 5.0: rect = box( landmark.location.x - radius, landmark.location.y - radius, landmark.location.x + (2 * radius), landmark.location.y + (2 * radius)) intersection = Polygon(navigableArea) if rect.intersects(navigableArea): break radius += 0.5 landmarkBuilder = Landmark.MergeFrom(landmark) if radius > 5.0: pass else: for i in range(0,NUM_OF_PARTICLES_PER_LANDMARK): x,y = 0.0 while !navigableArea.contains(Point(x, y)): x = landmark.location.x + 2 * (random.random() - 0.5) * radius x = landmark.location.y + 2 * (random.random() - 0.5) * radius coordinatesBuilder = Coordinates(x, y) landmarkBuilder.addParticles(coordinatesBuilder) floorBuilder.addLandmarks(landmarkBuilder) mapBuilder.addFloors(floorBuilder) return mapBuilder
def test_coverage_union_reduce_1dim(n): """ This is tested seperately from other set operations as it differs in two ways: 1. It expects only non-overlapping polygons 2. It expects GEOS 3.8.0+ """ test_data = [ shapely.box(0, 0, 1, 1), shapely.box(1, 0, 2, 1), shapely.box(2, 0, 3, 1), ] actual = shapely.coverage_union_all(test_data[:n]) # perform the reduction in a python loop and compare expected = test_data[0] for i in range(1, n): expected = shapely.coverage_union(expected, test_data[i]) assert_geometries_equal(actual, expected, normalize=True)
def test_destroy_prepared(): arr = np.array([shapely.points(1, 1), None, shapely.box(0, 0, 1, 1)]) shapely.prepare(arr) assert arr[0]._ptr_prepared != 0 assert arr[2]._ptr_prepared != 0 shapely.destroy_prepared(arr) assert arr[0]._ptr_prepared == 0 assert arr[1] is None assert arr[2]._ptr_prepared == 0 shapely.destroy_prepared(arr) # does not error
def setup(self): # create irregular polygons by merging overlapping point buffers self.polygon = shapely.union_all( shapely.buffer(shapely.points(np.random.random((1000, 2)) * 500), 10)) xmin = np.random.random(100) * 100 xmax = xmin + 100 ymin = np.random.random(100) * 100 ymax = ymin + 100 self.bounds = np.array([xmin, ymin, xmax, ymax]).T self.boxes = shapely.box(xmin, ymin, xmax, ymax)
def test_coverage_union_reduce_axis(): # shape = (3, 2), all polygons - none of them overlapping data = [[shapely.box(i, j, i + 1, j + 1) for i in range(2)] for j in range(3)] actual = shapely.coverage_union_all(data, axis=None) # default assert isinstance(actual, Geometry) actual = shapely.coverage_union_all(data, axis=0) assert actual.shape == (2, ) actual = shapely.coverage_union_all(data, axis=1) assert actual.shape == (3, ) actual = shapely.coverage_union_all(data, axis=-1) assert actual.shape == (3, )
def test_prepare(): arr = np.array([shapely.points(1, 1), None, shapely.box(0, 0, 1, 1)]) assert arr[0]._ptr_prepared == 0 assert arr[2]._ptr_prepared == 0 shapely.prepare(arr) assert arr[0]._ptr_prepared != 0 assert arr[1] is None assert arr[2]._ptr_prepared != 0 # preparing again actually does nothing original = arr[0]._ptr_prepared shapely.prepare(arr) assert arr[0]._ptr_prepared == original
def generateMinimap(buildingMap, tileSize): mapBuilder = BuildingMapProto.BuildingMap.MergeFrom(buildingMap) mapBuilder.clearFloors() for floor in buildingMap.floors: minX = sys.float_info.max minY = sys.float_info.max maxX = float('-Infinity') maxY = float('-Infinity') for space in floor.navigableSpaces: for vertex in space.outerBoundary: if vertex.x > maxX: maxX = vertex.x if vertex.x < minX: minX = vertex.x if vertex.y > maxY: maxY = vertex.y if vertex.y < minY: minY = vertex.y rows = max(math.ceil((maxX - minY) / tileSize), 0) columns = max(math.ceil(maxX - minX) / tileSize), 0) landmarks = floor.landmarks minimapBuilder = Minimap() minimapBuilder.columns = columns minimapBuilder.rows = rows minimapBuilder.sideSize = tileSize minimapBuilder.minCoordinates = Coordinates(minX, minY) navigableArea = createAccessibleArea(floor.navigableSpaces) currY = minY for i in range(0,rows): currX = minX for column in range(0,columns) tile = box(currX, currY, ) if navigableArea.intersects(tile): tileBuilder = Tile() tileBuilder.row = row tileBuilder.column = column for ty in LandmarkType: tileBuilder.addAllLandmarks( findClosestLandmark( NUM_OF_MINIMAP_TILE_LANDMARKS, landmarks, navigableArea, ty, tile) ) minimapBuilder.addTiles(tileBuilder) currX += tileSize currY += tileSize nfloor = Floor() nlfoor.miniMap = minimapBuilder mapBuilder.addFloors(nfloor)
import sys from contextlib import contextmanager import numpy as np import pytest import shapely shapely20_todo = pytest.mark.xfail( strict=False, reason="Not yet implemented for Shapely 2.0" ) point_polygon_testdata = ( shapely.points(np.arange(6), np.arange(6)), shapely.box(2, 2, 4, 4), ) point = shapely.points(2, 3) line_string = shapely.linestrings([(0, 0), (1, 0), (1, 1)]) linear_ring = shapely.linearrings([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]) polygon = shapely.polygons([(0, 0), (2, 0), (2, 2), (0, 2), (0, 0)]) multi_point = shapely.multipoints([(0, 0), (1, 2)]) multi_line_string = shapely.multilinestrings([[(0, 0), (1, 2)]]) multi_polygon = shapely.multipolygons( [ [(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)], [(2.1, 2.1), (2.2, 2.1), (2.2, 2.2), (2.1, 2.2), (2.1, 2.1)], ] ) geometry_collection = shapely.geometrycollections( [shapely.points(51, -1), shapely.linestrings([(52, -1), (49, 2)])] )
def time_box(self): shapely.box(*np.hstack([self.coords, self.coords + 100]).T)
shapely.union, # shapely.coverage_union is tested seperately ) REDUCE_SET_OPERATIONS = ( (shapely.intersection_all, shapely.intersection), (shapely.symmetric_difference_all, shapely.symmetric_difference), (shapely.union_all, shapely.union), # shapely.coverage_union_all, shapely.coverage_union) is tested seperately ) # operations that support fixed precision REDUCE_SET_OPERATIONS_PREC = ((shapely.union_all, shapely.union), ) reduce_test_data = [ shapely.box(0, 0, 5, 5), shapely.box(2, 2, 7, 7), shapely.box(4, 4, 9, 9), shapely.box(5, 5, 10, 10), ] non_polygon_types = [ geom for geom in all_types if (not shapely.is_empty(geom) and geom not in (polygon, multi_polygon)) ] @pytest.mark.parametrize("a", all_types) @pytest.mark.parametrize("func", SET_OPERATIONS) def test_set_operation_array(a, func): actual = func([a, a], point)
def test_box_nan(coords): assert shapely.box(*coords) is None
def test_box_array(coords, ccw, expected): actual = shapely.box(*coords, ccw=ccw) assert_geometries_equal(actual, expected)