def test_linestring(self): l = geometry.LineString([(0, 0), (1, 1)]) self.assertEqual(l.coords, ((0.0, 0.0), (1.0, 1.0))) self.assertEqual(l.coords[:1], ((0.0, 0.0), )) self.assertEqual(l.bounds, (0.0, 0.0, 1.0, 1.0)) self.assertEqual( l.__geo_interface__, { 'type': 'LineString', 'bbox': (0.0, 0.0, 1.0, 1.0), 'coordinates': ((0.0, 0.0), (1.0, 1.0)) }) self.assertEqual(l.to_wkt(), 'LINESTRING (0.0 0.0, 1.0 1.0)') p = geometry.Point(0, 0) p1 = geometry.Point(1, 1) p2 = geometry.Point(2, 2) l1 = geometry.LineString([p, p1, p2]) self.assertEqual(l1.coords, ((0.0, 0.0), (1.0, 1.0), (2.0, 2.0))) l2 = geometry.LineString(l1) self.assertEqual(l2.coords, ((0.0, 0.0), (1.0, 1.0), (2.0, 2.0))) l.coords = l2.coords self.assertEqual(l.__geo_interface__, l2.__geo_interface__) self.assertRaises(ValueError, geometry.LineString, [(0, 0, 0), (1, 1)]) ext = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)] int_1 = [(0.5, 0.25), (1.5, 0.25), (1.5, 1.25), (0.5, 1.25), (0.5, 0.25)] int_2 = [(0.5, 1.25), (1, 1.25), (1, 1.75), (0.5, 1.75), (0.5, 1.25)] po = geometry.Polygon(ext, [int_1, int_2]) self.assertRaises(TypeError, geometry.LineString, po) pt = geometry.Point(0, 1) self.assertRaises(TypeError, geometry.LineString, pt) self.assertRaises(TypeError, geometry.LineString, 0) self.assertRaises(ValueError, setattr, l2, 'coords', ((0, 0), (1, 1, 1))) self.assertRaises(ValueError, setattr, l2, 'coords', 0)
def test_overlaps_attr_multilinestring(): result = parse( { "op": "s_overlaps", "args": [ {"property": "geometry"}, { "type": "MultiLineString", "coordinates": [[[1, 1], [2, 2]], [[0, 3], [1, 1]]], "bbox": [0.0, 1.0, 2.0, 3.0], }, ], } ) assert result == ast.GeometryOverlaps( ast.Attribute("geometry"), values.Geometry( normalize_geom( geometry.MultiLineString( [ geometry.LineString([(1, 1), (2, 2)]), geometry.LineString([(0, 3), (1, 1)]), ] ).__geo_interface__ ), ), )
def test_overlaps_attr_multilinestring(): result = parse('OVERLAPS(geometry, MULTILINESTRING((1 1,2 2),(0 3,1 1)))') assert result == ast.GeometryOverlaps( ast.Attribute('geometry'), values.Geometry( geometry.MultiLineString([ geometry.LineString([(1, 1), (2, 2)]), geometry.LineString([(0, 3), (1, 1)]), ]).__geo_interface__, ), )
def test_geometrycollection(self): self.assertRaises(TypeError, geometry.GeometryCollection) self.assertRaises(TypeError, geometry.GeometryCollection, None) p = geometry.Polygon([(0, 0), (1, 1), (1, 0), (0, 0)]) e = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)] i = [(1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5), (1, 0)] ph = geometry.Polygon(e, [i]) p0 = geometry.Point(0, 0) p1 = geometry.Point(-1, -1) r = geometry.LinearRing([(0, 0), (1, 1), (1, 0), (0, 0)]) l = geometry.LineString([(0, 0), (1, 1)]) gc = geometry.GeometryCollection([p, ph, p0, p1, r, l]) self.assertEqual(len(list(gc.geoms)), 6) self.assertEqual(len(gc), 6) self.assertEqual(gc.bounds, (-1.0, -1.0, 2.0, 2.0)) self.assertEqual(gc.__geo_interface__, geometry.as_shape(gc).__geo_interface__) self.assertEqual( gc.__geo_interface__, geometry.as_shape(gc.__geo_interface__).__geo_interface__) f = geometry._Geometry() gc1 = geometry.GeometryCollection( [p.__geo_interface__, ph, p0, p1, r, l.__geo_interface__]) self.assertEqual(gc.__geo_interface__, gc1.__geo_interface__) self.assertRaises(NotImplementedError, geometry.GeometryCollection, [p, f]) mp1 = geometry.MultiPoint([p0, p1]) self.assertRaises(ValueError, geometry.GeometryCollection, [p, mp1]) self.assertEqual([p, ph, p0, p1, r, l], [geom for geom in gc])
def test_disjoint_linestring_attr(): result = parse('DISJOINT(LINESTRING(1 1,2 2), geometry)') assert result == ast.GeometryDisjoint( values.Geometry( geometry.LineString([(1, 1), (2, 2)]).__geo_interface__, ), ast.Attribute('geometry'), )
def testVertexPositions(self): v = czml.VertexPositions() l = geometry.LineString([(0, 0), (1, 1)]) r = geometry.LinearRing([(0, 0), (1, 1), (1, 0), (0, 0)]) ext = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)] int_1 = [(0.5, 0.25), (1.5, 0.25), (1.5, 1.25), (0.5, 1.25), (0.5, 0.25)] int_2 = [(0.5, 1.25), (1, 1.25), (1, 1.75), (0.5, 1.75), (0.5, 1.25)] p = geometry.Polygon(ext, [int_1, int_2]) v.cartesian = l v.cartographicDegrees = r v.cartographicRadians = p self.assertEqual(v.data(), {'cartesian': [0.0, 0.0, 0, 1.0, 1.0, 0], 'cartographicRadians': [0.0, 0.0, 0, 0.0, 2.0, 0, 2.0, 2.0, 0, 2.0, 0.0, 0, 0.0, 0.0, 0], 'cartographicDegrees': [0.0, 0.0, 0, 1.0, 1.0, 0, 1.0, 0.0, 0, 0.0, 0.0, 0]}) v2 = czml.VertexPositions() v2.loads(v.dumps()) self.assertEqual(v.data(), v2.data()) v.cartesian = None v.cartographicDegrees = None v.cartographicRadians = [0.0, 0.0, .0, 1.0, 1.0, 1.0] self.assertEqual(v.data(), {'cartographicRadians': [0.0, 0.0, 0.0, 1.0, 1.0, 1.0]})
def test_multilinestring(self): ml = geometry.MultiLineString([[[0.0, 0.0], [1.0, 2.0]]]) ml1 = geometry.MultiLineString(ml) self.assertEqual(ml.geoms[0].coords, ((0.0, 0.0), (1.0, 2.0))) self.assertEqual(ml.bounds, (0.0, 0.0, 1.0, 2.0)) l = geometry.LineString([(0, 0), (1, 1)]) l1 = geometry.LineString([(0.0, 0.0), (1.0, 1.0), (2.0, 2.0)]) ml2 = geometry.MultiLineString([l, l1]) self.assertEqual(ml2.geoms[0].coords, ((0.0, 0.0), (1.0, 1.0))) self.assertEqual(ml2.geoms[1].coords, ((0.0, 0.0), (1.0, 1.0), (2.0, 2.0))) self.assertEqual(len(ml2), 2) ml3 = geometry.MultiLineString(l) self.assertEqual(ml3.geoms[0].coords, ((0.0, 0.0), (1.0, 1.0))) pt = geometry.Point(0, 1) self.assertRaises(TypeError, geometry.MultiLineString, pt) self.assertRaises(TypeError, geometry.MultiLineString, 0)
def test_geometrycollection(self): p = geometry.Point(0, 1) l = geometry.LineString([(0, 0), (1, 1)]) f = geometry.GeometryCollection([p, l]) s = geometry.as_shape(f) self.assertEqual(f.__geo_interface__, s.__geo_interface__) self.assertEqual(f.__geo_interface__['geometries'][0], p.__geo_interface__) self.assertEqual(f.__geo_interface__['geometries'][1], l.__geo_interface__)
def test_crosses_attr_multilinestring(): result = parse({ "crosses": [ {"property": "geometry"}, { "type": "MultiLineString", "coordinates": [[[1, 1], [2, 2]], [[0, 3], [1, 1]]], "bbox": [0.0, 1.0, 2.0, 3.0] }, ] }) assert result == ast.GeometryCrosses( ast.Attribute('geometry'), values.Geometry( normalize_geom( geometry.MultiLineString([ geometry.LineString([(1, 1), (2, 2)]), geometry.LineString([(0, 3), (1, 1)]), ]).__geo_interface__ ) ), )
def test_intersects_attr_geometrycollection(): result = parse('INTERSECTS(geometry, GEOMETRYCOLLECTION(POINT(1 1),' 'LINESTRING(1 1,2 2),' 'POLYGON((1 1,2 2,0 3,1 1))' '))') assert result == ast.GeometryIntersects( ast.Attribute('geometry'), values.Geometry( geometry.GeometryCollection([ geometry.Point(1, 1), geometry.LineString([(1, 1), (2, 2)]), geometry.Polygon([(1, 1), (2, 2), (0, 3), (1, 1)]) ]).__geo_interface__), )
def test_multipoint(self): p0 = geometry.Point(0, 0) p1 = geometry.Point(1, 1) p2 = geometry.Point(2, 2) p3 = geometry.Point(3, 3) mp = geometry.MultiPoint(p0) self.assertEqual(len(mp.geoms), 1) self.assertEqual(mp.geoms[0].x, 0) mp1 = geometry.MultiPoint([p0, p1, p2]) self.assertEqual(len(mp1.geoms), 3) self.assertEqual(len(mp1), 3) self.assertEqual(mp1.geoms[0].x, 0) self.assertEqual(mp1.geoms[1].x, 1) self.assertEqual(mp1.geoms[2].x, 2) self.assertEqual(mp1.bounds, (0.0, 0.0, 2.0, 2.0)) l1 = geometry.LineString([p0, p1, p2]) mp2 = geometry.MultiPoint(l1) self.assertEqual(len(mp2.geoms), 3) self.assertEqual(mp2.geoms[2].x, 2) mp3 = geometry.MultiPoint([l1, p3]) self.assertEqual(mp3.geoms[3].x, 3) self.assertRaises(TypeError, geometry.MultiPoint, [mp1, mp3]) mp4 = geometry.MultiPoint([p0, p1, p0, p1, p2]) self.assertEqual(len(mp4.geoms), 5) mp4.unique() self.assertEqual(len(mp4.geoms), 3) mp5 = geometry.MultiPoint([[0.0, 0.0], [1.0, 2.0]]) self.assertEqual(len(mp5.geoms), 2) p = geometry.Polygon( (((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)), ((0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.2, 0.1)))) mp6 = geometry.MultiPoint(p) self.assertEqual(mp6.bounds, (0.0, 0.0, 1.0, 1.0)) self.assertRaises(TypeError, geometry.MultiPoint, [0, 0]) self.assertRaises( TypeError, geometry.MultiPoint, 0, ) self.assertEqual( mp1.__geo_interface__, { 'type': 'MultiPoint', 'bbox': (0.0, 0.0, 2.0, 2.0), 'coordinates': ((0.0, 0.0), (1.0, 1.0), (2.0, 2.0)) })
def setUp(self): self.geometry = geometry._Geometry() self.point = geometry.Point(0, 1) self.linestring = geometry.LineString([[0, 0], [1, 0], [1, 1]]) self.linearring = geometry.LinearRing([[0, 0], [1, 0], [1, 1], [0, 0]]) self.coords_1 = ((0., 0.), (0., 1.), (1., 1.), (1., 0.), (0., 0.)) self.polygon = geometry.Polygon(self.coords_1) self.multipoint = geometry.MultiPoint([[0.0, 0.0], [1.0, 2.0]]) self.multiline = geometry.MultiLineString([[[0.0, 0.0], [1.0, 2.0]]]) self.multipoly = geometry.MultiPolygon([ (((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)), [((0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.2, 0.1))]) ]) self.geo_collect = geometry.GeometryCollection( [self.point, self.linestring, self.linearring]) self.feature = geometry.Feature(self.point, {'a': 1, 'b': 2}) self.feature_list = [self.feature, self.feature] self.fc = geometry.FeatureCollection(self.feature_list)
def test_disjoint_linestring_attr(): result = parse({ "disjoint": [ { "type": "LineString", "coordinates": [[1, 1], [2, 2]], "bbox": [1.0, 1.0, 2.0, 2.0] }, {"property": "geometry"}, ] }) assert result == ast.GeometryDisjoint( values.Geometry( normalize_geom( geometry.LineString([(1, 1), (2, 2)]).__geo_interface__ ), ), ast.Attribute('geometry'), )
def test_linearring(self): r = geometry.LinearRing([(0, 0), (1, 1), (1, 0), (0, 0)]) self.assertEqual(r.coords, ((0, 0), (1, 1), (1, 0), (0, 0))) self.assertEqual(r.bounds, (0.0, 0.0, 1.0, 1.0)) l = geometry.LineString(r) self.assertEqual(l.coords, ((0, 0), (1, 1), (1, 0), (0, 0))) self.assertEqual( r.__geo_interface__, { 'type': 'LinearRing', 'coordinates': ((0.0, 0.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)) }) ext = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)] int_1 = [(0.5, 0.25), (1.5, 0.25), (1.5, 1.25), (0.5, 1.25), (0.5, 0.25)] int_2 = [(0.5, 1.25), (1, 1.25), (1, 1.75), (0.5, 1.75), (0.5, 1.25)] p = geometry.Polygon(ext, [int_1, int_2]) self.assertRaises(TypeError, geometry.LinearRing, p) # A LinearRing is self closing r2 = geometry.LinearRing([(0, 0), (1, 1), (1, 0)]) self.assertEqual(r.__geo_interface__, r2.__geo_interface__) r3 = geometry.LinearRing(int_1) r3.coords = [(0, 0), (1, 1), (1, 0)] self.assertEqual(r.__geo_interface__, r3.__geo_interface__)
def testPolyline(self): # Create a new polyline sc = czml.SolidColor(color={'rgba': [0, 255, 127, 55]}) m1 = czml.Material(solidColor=sc) c1 = geometry.LineString([(-162, 41, 0), (-151, 43, 0), (-140, 45, 0)]) v1 = czml.Positions(cartographicDegrees=c1) p1 = czml.Polyline(show=True, width=5, followSurface=False, material=m1, positions=v1) self.assertEqual( p1.data(), { 'show': True, 'width': 5, 'followSurface': False, 'material': { 'solidColor': { 'color': { 'rgba': [0, 255, 127, 55] } }, }, 'positions': { 'cartographicDegrees': [-162, 41, 0, -151, 43, 0, -140, 45, 0] }, }) # Create a new polyline pg = czml.PolylineGlow(color={'rgba': [0, 255, 127, 55]}, glowPower=0.25) m2 = czml.Material(polylineGlow=pg) c2 = geometry.LineString([(1.6, 5.3, 10), (2.4, 4.2, 20), (3.8, 3.1, 30)]) v2 = czml.Positions(cartographicRadians=c2) p2 = czml.Polyline(show=False, width=7, followSurface=True, material=m2, positions=v2) self.assertEqual( p2.data(), { 'show': False, 'width': 7, 'followSurface': True, 'material': { 'polylineGlow': { 'color': { 'rgba': [0, 255, 127, 55] }, 'glowPower': 0.25 }, }, 'positions': { 'cartographicRadians': [1.6, 5.3, 10, 2.4, 4.2, 20, 3.8, 3.1, 30] }, }) # Create a polyline from an existing polyline p3 = czml.Polyline() p3.loads(p2.dumps()) self.assertEqual(p3.data(), p2.data()) # Modify an existing polyline po = czml.PolylineOutline(color={'rgba': [0, 255, 127, 55]}, outlineColor={'rgba': [0, 55, 127, 255]}, outlineWidth=4) m3 = czml.Material(polylineOutline=po) c3 = geometry.LineString([(1000, 7500, 90), (2000, 6500, 50), (3000, 5500, 20)]) v3 = czml.Positions(cartesian=c3) p3.material = m3 p3.positions = v3 self.assertEqual( p3.data(), { 'show': False, 'width': 7, 'followSurface': True, 'material': { 'polylineOutline': { 'color': { 'rgba': [0, 255, 127, 55] }, 'outlineColor': { 'rgba': [0, 55, 127, 255] }, 'outlineWidth': 4 }, }, 'positions': { 'cartesian': [1000, 7500, 90, 2000, 6500, 50, 3000, 5500, 20] }, }) # Add a polyline to a CZML packet packet = czml.CZMLPacket(id='abc') packet.polyline = p3 self.assertEqual( packet.data(), { 'id': 'abc', 'polyline': { 'show': False, 'width': 7, 'followSurface': True, 'material': { 'polylineOutline': { 'color': { 'rgba': [0, 255, 127, 55] }, 'outlineColor': { 'rgba': [0, 55, 127, 255] }, 'outlineWidth': 4 }, }, 'positions': { 'cartesian': [1000, 7500, 90, 2000, 6500, 50, 3000, 5500, 20] }, }, })
def testPolygon(self): # Create a new polygon img = czml.Image(image='http://localhost/img.png', repeat=2) mat = czml.Material(image=img) pts = geometry.LineString([(50, 20, 2), (60, 30, 3), (50, 30, 4), (60, 20, 5)]) pos = czml.Positions(cartographicDegrees=pts) col = {'rgba': [0, 255, 127, 55]} pol = czml.Polygon(show=True, material=mat, positions=pos, perPositionHeight=True, fill=True, outline=True, outlineColor=col) self.assertEqual( pol.data(), { 'show': True, 'fill': True, 'outline': True, 'perPositionHeight': True, 'outlineColor': { 'rgba': [0, 255, 127, 55] }, 'material': { 'image': { 'image': 'http://localhost/img.png', 'repeat': 2 }, }, 'positions': { 'cartographicDegrees': [50, 20, 2, 60, 30, 3, 50, 30, 4, 60, 20, 5] }, }) # Create a new polygon from an existing polygon pol2 = czml.Polygon() pol2.loads(pol.dumps()) self.assertEqual(pol2.data(), pol.data()) # Modify an existing polygon grid = czml.Grid(color={'rgba': [0, 55, 127, 255]}, cellAlpha=0.4, lineCount=5, lineThickness=2, lineOffset=0.3) mat2 = czml.Material(grid=grid) pts2 = geometry.LineString([(1.5, 1.2, 0), (1.6, 1.3, 0), (1.5, 1.3, 0), (1.6, 1.2, 0)]) pos2 = czml.Positions(cartographicRadians=pts2) pol2.material = mat2 pol2.positions = pos2 pol2.perPositionHeight = False pol2.height = 7 pol2.extrudedHeight = 30 self.assertEqual( pol2.data(), { 'show': True, 'fill': True, 'outline': True, 'perPositionHeight': False, 'height': 7, 'extrudedHeight': 30, 'outlineColor': { 'rgba': [0, 255, 127, 55] }, 'material': { 'grid': { 'color': { 'rgba': [0, 55, 127, 255] }, 'cellAlpha': 0.4, 'lineCount': 5, 'lineThickness': 2, 'lineOffset': 0.3 }, }, 'positions': { 'cartographicRadians': [1.5, 1.2, 0, 1.6, 1.3, 0, 1.5, 1.3, 0, 1.6, 1.2, 0] }, }) # Add a polygon to a CZML packet packet = czml.CZMLPacket(id='abc') packet.polygon = pol2 self.assertEqual( packet.data(), { 'id': 'abc', 'polygon': { 'show': True, 'fill': True, 'outline': True, 'perPositionHeight': False, 'height': 7, 'extrudedHeight': 30, 'outlineColor': { 'rgba': [0, 255, 127, 55] }, 'material': { 'grid': { 'color': { 'rgba': [0, 55, 127, 255] }, 'cellAlpha': 0.4, 'lineCount': 5, 'lineThickness': 2, 'lineOffset': 0.3 }, }, 'positions': { 'cartographicRadians': [1.5, 1.2, 0, 1.6, 1.3, 0, 1.5, 1.3, 0, 1.6, 1.2, 0] }, }, })
def test_linestring(self): f = geometry.LineString([(0, 0), (1, 1)]) s = geometry.as_shape(f) self.assertEqual(f.__geo_interface__, s.__geo_interface__)