def test_wkb(self): p = Point(0.0, 0.0) wkb_big_endian = wkb.dumps(p, big_endian=True) wkb_little_endian = wkb.dumps(p, big_endian=False) # Regardless of byte order, loads ought to correctly recover the # geometry self.assertTrue(p.equals(wkb.loads(wkb_big_endian))) self.assertTrue(p.equals(wkb.loads(wkb_little_endian)))
def test_binary_predicates(self): point = Point(0.0, 0.0) self.assertTrue(point.disjoint(Point(-1.0, -1.0))) self.assertFalse(point.touches(Point(-1.0, -1.0))) self.assertFalse(point.crosses(Point(-1.0, -1.0))) self.assertFalse(point.within(Point(-1.0, -1.0))) self.assertFalse(point.contains(Point(-1.0, -1.0))) self.assertFalse(point.equals(Point(-1.0, -1.0))) self.assertFalse(point.touches(Point(-1.0, -1.0))) self.assertTrue(point.equals(Point(0.0, 0.0)))
class TestTools: def setup_method(self): self.p1 = Point(0, 0) self.p2 = Point(1, 1) self.p3 = Point(2, 2) self.mpc = MultiPoint([self.p1, self.p2, self.p3]) self.mp1 = MultiPoint([self.p1, self.p2]) self.line1 = LineString([(3, 3), (4, 4)]) def test_collect_single(self): result = collect(self.p1) assert self.p1.equals(result) def test_collect_single_force_multi(self): result = collect(self.p1, multi=True) expected = MultiPoint([self.p1]) assert expected.equals(result) def test_collect_multi(self): result = collect(self.mp1) assert self.mp1.equals(result) def test_collect_multi_force_multi(self): result = collect(self.mp1) assert self.mp1.equals(result) def test_collect_list(self): result = collect([self.p1, self.p2, self.p3]) assert self.mpc.equals(result) def test_collect_GeoSeries(self): s = GeoSeries([self.p1, self.p2, self.p3]) result = collect(s) assert self.mpc.equals(result) def test_collect_mixed_types(self): with pytest.raises(ValueError): collect([self.p1, self.line1]) def test_collect_mixed_multi(self): with pytest.raises(ValueError): collect([self.mpc, self.mp1]) def test_epsg_from_crs(self): assert epsg_from_crs({'init': 'epsg:4326'}) == 4326 assert epsg_from_crs({'init': 'EPSG:4326'}) == 4326 assert epsg_from_crs('+init=epsg:4326') == 4326 @pytest.mark.skipif( LooseVersion(pyproj.__version__) >= LooseVersion('2.0.0'), reason="explicit_crs_from_epsg depends on parsing data files of " "proj.4 < 6 / pyproj < 2 ") def test_explicit_crs_from_epsg(self): expected = {'no_defs': True, 'proj': 'longlat', 'datum': 'WGS84', 'init': 'epsg:4326'} assert explicit_crs_from_epsg(epsg=4326) == expected assert explicit_crs_from_epsg(epsg='4326') == expected assert explicit_crs_from_epsg(crs={'init': 'epsg:4326'}) == expected assert explicit_crs_from_epsg(crs="+init=epsg:4326") == expected
def test_point(self): point = Point(0, 0) point2 = Point(-1, 1) self.assertTrue(point.union(point2).equals(point | point2)) self.assertTrue((point & point2).is_empty) self.assertTrue(point.equals(point - point2)) self.assertTrue( point.symmetric_difference(point2).equals(point ^ point2))
def test_from_shape(): p = Point(1, 2) e = from_shape(p) assert isinstance(e, WKBElement) assert isinstance(e.data, buffer) s = shapely.wkb.loads(bytes(e.data)) assert isinstance(s, Point) assert p.equals(p)
def _find_exact_insert_position(entry_point, way_nodes): """ try to find a point in the way_nodes that overlaps the entry point exactly """ for i, node in enumerate(way_nodes): way_point = Point(node['coords']) if way_point.equals(entry_point): return i return None
class TestTools: def setup_method(self): self.p1 = Point(0, 0) self.p2 = Point(1, 1) self.p3 = Point(2, 2) self.mpc = MultiPoint([self.p1, self.p2, self.p3]) self.mp1 = MultiPoint([self.p1, self.p2]) self.line1 = LineString([(3, 3), (4, 4)]) def test_collect_single(self): result = collect(self.p1) assert self.p1.equals(result) def test_collect_single_force_multi(self): result = collect(self.p1, multi=True) expected = MultiPoint([self.p1]) assert expected.equals(result) def test_collect_multi(self): result = collect(self.mp1) assert self.mp1.equals(result) def test_collect_multi_force_multi(self): result = collect(self.mp1) assert self.mp1.equals(result) def test_collect_list(self): result = collect([self.p1, self.p2, self.p3]) assert self.mpc.equals(result) def test_collect_GeoSeries(self): s = GeoSeries([self.p1, self.p2, self.p3]) result = collect(s) assert self.mpc.equals(result) def test_collect_mixed_types(self): with pytest.raises(ValueError): collect([self.p1, self.line1]) def test_collect_mixed_multi(self): with pytest.raises(ValueError): collect([self.mpc, self.mp1]) def test_epsg_from_crs(self): assert epsg_from_crs({'init': 'epsg:4326'}) == 4326 assert epsg_from_crs({'init': 'EPSG:4326'}) == 4326 assert epsg_from_crs('+init=epsg:4326') == 4326 def test_explicit_crs_from_epsg(self): expected = {'no_defs': True, 'proj': 'longlat', 'datum': 'WGS84', 'init': 'epsg:4326'} assert explicit_crs_from_epsg(epsg=4326) == expected assert explicit_crs_from_epsg(epsg='4326') == expected assert explicit_crs_from_epsg(crs={'init': 'epsg:4326'}) == expected assert explicit_crs_from_epsg(crs="+init=epsg:4326") == expected
def test_from_shape(): import shapely.wkb from shapely.geometry import Point from geoalchemy2.shape import from_shape from geoalchemy2.elements import WKBElement p = Point(1, 2) e = from_shape(p) ok_(isinstance(e, WKBElement)) ok_(isinstance(e.data, buffer)) s = shapely.wkb.loads(str(e.data)) ok_(isinstance(s, Point)) ok_(p.equals(p))
def test_point_equal(): p1 = Point(0,0.001234567890123456786) p2 = Point(0,0.001234567890123456987) eq = p1.equals(p2) ok_(eq==False, eq) aeq = p1.almost_equals(p2,18) ok_(aeq==True, aeq) assert_aae(p1.coords[:][0], p2.coords[:][0],18) assert_ape(p1.coords[:][0][1], p2.coords[:][0][1],16)
def test_from_shape(): from geoalchemy2.elements import WKBElement try: from geoalchemy2.shape import from_shape import shapely.wkb from shapely.geometry import Point except ImportError: raise SkipTest p = Point(1, 2) e = from_shape(p) ok_(isinstance(e, WKBElement)) ok_(isinstance(e.data, buffer)) # flake8: noqa s = shapely.wkb.loads(str(e.data)) ok_(isinstance(s, Point)) ok_(p.equals(p))
def SpatialToplogy (Spatial_A,Spatial_B): if Spatial_A[4] == 'Point' and Spatial_B[4]== 'Point': Point_0=Point(Spatial_A[0],Spatial_A[1]) Point_1=Point(Spatial_B[0],Spatial_B[1]) #Point to point relationships if Point_0.equals(Point_1): return 'Point1 equals Point2' if Point_0.within(Point_1.buffer(2)): return 'Point1 lies within a buffer of 2 m from Point2' if Point_0.overlaps(Point_1): return 'Point1 overlaps Point2' #if Point_0.disjoint(Point_1): return 'Point1 disjoint Point2' #Point to line relationships if Spatial_A[4] == 'Point' and Spatial_B[4]== 'Line': Point_0=Point(Spatial_A[0],Spatial_A[1]) Line_0=LineString([(Spatial_B[0],Spatial_B[1]),(Spatial_B[2],Spatial_B[3])]) if Point_0.touches(Line_0):return 'Point1 touches Line1' if Point_0.within(Line_0.buffer(2)):return 'Point1 lies within a buffer of 2 m from L1' #Point to polygon relationships if Spatial_A[4] == 'Point' and Spatial_B[4]== 'Polygon': Point_0=Point(Spatial_A[0],Spatial_A[1]) Polygon_0=Polygon([(Spatial_B[0],Spatial_B[1]),(Spatial_B[2],Spatial_B[1]),(Spatial_B[2],Spatial_B[3]),(Spatial_B[0],Spatial_B[3])]) if Point_0.touches(Polygon_0):return 'Point1 touches Polygon1' if Point_0.within(Polygon_0):return'Point1 lies within Polygon1' if Point_0.overlaps(Polygon_0):return 'Point1 lies overlaps Polygon1' #Line to line relationships if Spatial_A[4]=='Line' and Spatial_B[4]=='Line': Line_0=LineString([(Spatial_A[0],Spatial_A[1]),(Spatial_A[2],Spatial_A[3])]) Line_1=LineString([(Spatial_B[0],Spatial_B[1]),(Spatial_B[2],Spatial_B[3])]) if Line_0.equals(Line_1):return 'Line0 equals Line1' if Line_0.touches(Line_1):return 'Line0 touches Line1' if Line_0.crosses(Line_1):return 'Line0 crosses Line1' if Line_0.within(Line_1.buffer(2)):return 'Line0 lies within a buffer of 2 m Line1' if Line_0.overlaps(Line_1):return 'Line0 overlaps Line1' #Line to polygon relationships if Spatial_A[4]=='Line' and Spatial_B[4]=='Polygon': Line_0=LineString([(Spatial_A[0],Spatial_A[1]),(Spatial_A[2],Spatial_A[3])]) Polygon_0=Polygon([(Spatial_B[0],Spatial_B[1]),(Spatial_B[2],Spatial_B[1]),(Spatial_B[2],Spatial_B[3]),(Spatial_B[0],Spatial_B[3])]) if Line_0.touches(Polygon_0):return 'Line0 touches Polygon1' if Line_0.crosses(Polygon_0):return 'Line0 crosses Polygon1' if Line_0.within(Polygon_0):return 'Line0 lies within Polygon1' #Polygon to Polygon relationships if Spatial_A[4]=='Polygon' and Spatial_B[4]=='Polygon': Polygon_0=Polygon([(Spatial_A[0],Spatial_A[1]),(Spatial_A[2],Spatial_A[1]),(Spatial_A[2],Spatial_A[3]),(Spatial_A[0],Spatial_A[3])]) Polygon_1=Polygon([(Spatial_B[0],Spatial_B[1]),(Spatial_B[2],Spatial_B[1]),(Spatial_B[2],Spatial_B[3]),(Spatial_B[0],Spatial_B[3])]) if Polygon_0.touches(Polygon_1):return 'Polygon touches Polygon1' if Polygon_0.equals(Polygon_1):return 'Polygon0 equals Polygon1' if Polygon_0.within(Polygon_1):return 'Polygon lies within Polygon1' if Polygon_0.within(Polygon_1.buffer(2)):return 'Polygon lies within a buffer of 2m Polygon1'
class TestTools(unittest.TestCase): def setUp(self): self.p1 = Point(0,0) self.p2 = Point(1,1) self.p3 = Point(2,2) self.mpc = MultiPoint([self.p1, self.p2, self.p3]) self.mp1 = MultiPoint([self.p1, self.p2]) self.line1 = LineString([(3,3), (4,4)]) def test_collect_single(self): result = collect(self.p1) self.assert_(self.p1.equals(result)) def test_collect_single_force_multi(self): result = collect(self.p1, multi=True) expected = MultiPoint([self.p1]) self.assert_(expected.equals(result)) def test_collect_multi(self): result = collect(self.mp1) self.assert_(self.mp1.equals(result)) def test_collect_multi_force_multi(self): result = collect(self.mp1) self.assert_(self.mp1.equals(result)) def test_collect_list(self): result = collect([self.p1, self.p2, self.p3]) self.assert_(self.mpc.equals(result)) def test_collect_GeoSeries(self): s = GeoSeries([self.p1, self.p2, self.p3]) result = collect(s) self.assert_(self.mpc.equals(result)) def test_collect_mixed_types(self): with self.assertRaises(ValueError): collect([self.p1, self.line1]) def test_collect_mixed_multi(self): with self.assertRaises(ValueError): collect([self.mpc, self.mp1])
class TestTools: def setup_method(self): self.p1 = Point(0, 0) self.p2 = Point(1, 1) self.p3 = Point(2, 2) self.mpc = MultiPoint([self.p1, self.p2, self.p3]) self.mp1 = MultiPoint([self.p1, self.p2]) self.line1 = LineString([(3, 3), (4, 4)]) def test_collect_single(self): result = collect(self.p1) assert self.p1.equals(result) def test_collect_single_force_multi(self): result = collect(self.p1, multi=True) expected = MultiPoint([self.p1]) assert expected.equals(result) def test_collect_multi(self): result = collect(self.mp1) assert self.mp1.equals(result) def test_collect_multi_force_multi(self): result = collect(self.mp1) assert self.mp1.equals(result) def test_collect_list(self): result = collect([self.p1, self.p2, self.p3]) assert self.mpc.equals(result) def test_collect_GeoSeries(self): s = GeoSeries([self.p1, self.p2, self.p3]) result = collect(s) assert self.mpc.equals(result) def test_collect_mixed_types(self): with pytest.raises(ValueError): collect([self.p1, self.line1]) def test_collect_mixed_multi(self): with pytest.raises(ValueError): collect([self.mpc, self.mp1])
class TestTools: def setup_method(self): self.p1 = Point(0, 0) self.p2 = Point(1, 1) self.p3 = Point(2, 2) self.mpc = MultiPoint([self.p1, self.p2, self.p3]) self.mp1 = MultiPoint([self.p1, self.p2]) self.line1 = LineString([(3, 3), (4, 4)]) def test_collect_single(self): result = collect(self.p1) assert self.p1.equals(result) def test_collect_single_force_multi(self): result = collect(self.p1, multi=True) expected = MultiPoint([self.p1]) assert expected.equals(result) def test_collect_multi(self): result = collect(self.mp1) assert self.mp1.equals(result) def test_collect_multi_force_multi(self): result = collect(self.mp1) assert self.mp1.equals(result) def test_collect_list(self): result = collect([self.p1, self.p2, self.p3]) assert self.mpc.equals(result) def test_collect_GeoSeries(self): s = GeoSeries([self.p1, self.p2, self.p3]) result = collect(s) assert self.mpc.equals(result) def test_collect_mixed_types(self): with pytest.raises(ValueError): collect([self.p1, self.line1]) def test_collect_mixed_multi(self): with pytest.raises(ValueError): collect([self.mpc, self.mp1]) @pytest.mark.skipif(PYPROJ_LT_231, reason="segfault") def test_epsg_from_crs(self): with pytest.warns(FutureWarning): assert epsg_from_crs({"init": "epsg:4326"}) == 4326 assert epsg_from_crs({"init": "EPSG:4326"}) == 4326 assert epsg_from_crs("+init=epsg:4326") == 4326 @pytest.mark.skipif(PYPROJ_LT_231, reason="segfault") def test_explicit_crs_from_epsg(self): with pytest.warns(FutureWarning): assert explicit_crs_from_epsg(epsg=4326) == CRS.from_epsg(4326) assert explicit_crs_from_epsg(epsg="4326") == CRS.from_epsg(4326) assert explicit_crs_from_epsg( crs={"init": "epsg:4326"}) == CRS.from_dict( {"init": "epsg:4326"}) assert explicit_crs_from_epsg( crs="+init=epsg:4326") == CRS.from_proj4("+init=epsg:4326") @pytest.mark.filterwarnings("ignore:explicit_crs_from_epsg:FutureWarning") def test_explicit_crs_from_epsg__missing_input(self): with pytest.raises(ValueError): explicit_crs_from_epsg()
def skip_origin_point(point: geometry.Point) -> bool: """Simple filter to trip out any 0, 0 intersection points""" return not point.equals(geometry.Point(0, 0))
class TestTools: def setup_method(self): self.p1 = Point(0, 0) self.p2 = Point(1, 1) self.p3 = Point(2, 2) self.mpc = MultiPoint([self.p1, self.p2, self.p3]) self.mp1 = MultiPoint([self.p1, self.p2]) self.line1 = LineString([(3, 3), (4, 4)]) def test_collect_single(self): result = collect(self.p1) assert self.p1.equals(result) def test_collect_single_force_multi(self): result = collect(self.p1, multi=True) expected = MultiPoint([self.p1]) assert expected.equals(result) def test_collect_multi(self): result = collect(self.mp1) assert self.mp1.equals(result) def test_collect_multi_force_multi(self): result = collect(self.mp1) assert self.mp1.equals(result) def test_collect_list(self): result = collect([self.p1, self.p2, self.p3]) assert self.mpc.equals(result) def test_collect_GeoSeries(self): s = GeoSeries([self.p1, self.p2, self.p3]) result = collect(s) assert self.mpc.equals(result) def test_collect_mixed_types(self): with pytest.raises(ValueError): collect([self.p1, self.line1]) def test_collect_mixed_multi(self): with pytest.raises(ValueError): collect([self.mpc, self.mp1]) def test_epsg_from_crs(self): assert epsg_from_crs({"init": "epsg:4326"}) == 4326 assert epsg_from_crs({"init": "EPSG:4326"}) == 4326 assert epsg_from_crs("+init=epsg:4326") == 4326 @pytest.mark.skipif( LooseVersion(pyproj.__version__) >= LooseVersion("2.0.0"), reason="explicit_crs_from_epsg depends on parsing data files of " "proj.4 < 6 / pyproj < 2 ", ) def test_explicit_crs_from_epsg(self): expected = { "no_defs": True, "proj": "longlat", "datum": "WGS84", "init": "epsg:4326", } assert explicit_crs_from_epsg(epsg=4326) == expected assert explicit_crs_from_epsg(epsg="4326") == expected assert explicit_crs_from_epsg(crs={"init": "epsg:4326"}) == expected assert explicit_crs_from_epsg(crs="+init=epsg:4326") == expected
class DeterministicMazeRemaster(object): def __init__(self, setting): maze = setting.get('maze') self.maze = box(maze[0], maze[1], maze[2], maze[3]) obstacles = setting.get('obstacles') self.obstacles = [] if obstacles: for coord in obstacles: self.obstacles.append(Polygon(coord)) muds = setting.get('muds') self.muds = [] if muds: for coord in muds: self.muds.append(Polygon(coord)) start_state = setting.get('start_state') self.start_state = Point(start_state) self.current_state = self.start_state goal_states = setting.get('goal_states') self.goal_states = [] for coord in goal_states: self.goal_states.append(Point(coord)) self.action_range = setting.get('action_range') self.deadend_toleration = setting.get('deadend_toleration') self.injail = False self.inmud = False self.backup = { k: v for k, v in self.__dict__.items() if not (k.startswith('__') and k.endswith('__')) } def PlotMaze(self): fig = plt.figure(dpi=90) ax = fig.add_subplot(111) x, y = self.maze.exterior.xy ax.plot(x, y, 'o', color='#999999', zorder=1) patch = PolygonPatch(self.maze, facecolor='#6699cc', edgecolor='#6699cc', alpha=0.5, zorder=2) ax.add_patch(patch) if self.muds: for mud in self.muds: patch = PolygonPatch(mud, facecolor='#ff3333', edgecolor='#ff3333', alpha=0.5, hatch='\\', zorder=2) ax.add_patch(patch) if self.obstacles: for obstacles in self.obstacles: patch = PolygonPatch(obstacles, facecolor='#ffaabb', edgecolor='#ffaabb', alpha=0.5, hatch='\\', zorder=2) ax.add_patch(patch) plt.show() def Reset(self): backup = self.backup self.__dict__.update(backup) self.backup = backup def UpdateState(self, new_state): self.current_state = new_state def InJail(self): if self.injail == True: return 1 else: return -1 def InMud(self): for mud in self.muds: if mud.intersects(self.current_state): self.Inmud = True return 1 else: self.Inmud = False return -1 def GetCurrentState(self): return self.current_state.x, self.current_state.y, self.InJail( ), self.InMud() def TakeAction(self, action): if not self.injail: proposal = Point((self.current_state.x + action[0], self.current_state.y + action[1])) #print proposal.x, proposal.y path = LineString([self.current_state, proposal]) if self.obstacles: for obstacle in self.obstacles: if obstacle.intersects(path): self.injail = True self.current_state = Point((-1, -1)) return if self.muds: for mud in self.muds: if mud.intersects(path): # print 'we are here' path_inmud = mud.intersection(path) coords = [path.coords[0], path.coords[1]] for loc in path_inmud.coords: if loc not in coords: coords.append(loc) coords.sort(key=lambda tup: tup[1]) p_in_mud = proposal.intersects(mud) s_in_mud = self.current_state.intersects(mud) if p_in_mud and not s_in_mud: # print 'current not in mud' if coords.index((self.current_state.x, self.current_state.y)) == 0: x = coords[1][0] - coords[0][0] + 0.5 * ( coords[-1][0] - coords[1][0]) y = coords[1][1] - coords[0][1] + 0.5 * ( coords[-1][1] - coords[1][1]) proposal = Point( (coords[0][0] + x, coords[0][1] + y)) else: x = coords[1][0] - coords[-1][0] + 0.5 * ( coords[0][0] - coords[1][0]) y = coords[1][1] - coords[-1][1] + 0.5 * ( coords[0][1] - coords[1][1]) proposal = Point( (coords[-1][0] + x, coords[-1][1] + y)) elif s_in_mud and not p_in_mud: # print 'proposal not in mud' if coords.index((self.current_state.x, self.current_state.y)) == 0: x = 0.5 * (coords[1][0] - coords[0][0]) + ( coords[-1][0] - coords[1][0]) y = 0.5 * (coords[1][1] - coords[0][1]) + ( coords[-1][1] - coords[1][1]) proposal = Point( (coords[0][0] + x, coords[0][1] + y)) else: x = 0.5 * (coords[1][0] - coords[-1][0]) + ( coords[0][0] - coords[1][0]) y = 0.5 * (coords[1][1] - coords[-1][1]) + ( coords[0][1] - coords[1][1]) proposal = Point( (coords[-1][0] + x, coords[-1][1] + y)) else: proposal = Point( (self.current_state.x + action[0] * 0.5, self.current_state.y + action[1] * 0.5)) path = LineString([self.current_state, proposal]) bounds = LinearRing(self.maze.exterior.coords) if bounds.intersects(path): onedge = bounds.intersection(path) if type(onedge) is MultiPoint: for point in onedge: if not point.equals(self.current_state): proposal = point elif type(onedge) is Point: if not onedge.equals(self.current_state): proposal = onedge elif not self.maze.contains(proposal): proposal = bounds.interpolate(bounds.project(proposal)) self.current_state = proposal else: self.deadend_toleration = self.deadend_toleration - 1 return self.GetCurrentState() def IfGameEnd(self): for goal in self.goal_states: if self.current_state.equals(goal): self.Reset() if (self.deadend_toleration == 0): self.Reset() def DeltaDistance(self, new_state, old_state): delta = [] delta.append(new_state[0] - old_state[0]) delta.append(new_state[1] - old_state[1]) if (new_state[2] == 1): delta.append(1) else: delta.append(-1) if (new_state[3] == 1): delta.append(1) else: delta.append(-1) return delta