def test_convexhull(self): _skip_if_no_scipy() _skip_if_no_shapely() import numpy as np import scipy.spatial np.random.seed(1234) points = np.random.rand(15, 2) * 5 + np.array([130, 40]) hull = cesiumpy.spatial.ConvexHull(points) polyline = hull.get_polyline() expected = [130.37690620821488, 41.844120030009876, 132.51541582653905, 40.06884224795341, 133.89987904059402, 41.36296302641321, 134.6657005099126, 43.25689071613289, 134.7906967684185, 44.379663173710476, 133.86413310806188, 44.41320595318058, 131.38232127571547, 44.00936088767509, 130.95759725189447, 43.11054385519916, 130.37690620821488, 41.844120030009876] self.assertEqual(polyline.positions.x, expected) hull = scipy.spatial.ConvexHull(points) hull = cesiumpy.spatial.ConvexHull(hull) polyline = hull.get_polyline() self.assertEqual(polyline.positions.x, expected)
def test_point_to_entity(self): _skip_if_no_shapely() import shapely.geometry p = shapely.geometry.Point(0, 1) res = cesiumpy.extension.shapefile.to_entity(p) exp = """{position : Cesium.Cartesian3.fromDegrees(0.0, 1.0, 0.0), point : {pixelSize : 10.0, color : Cesium.Color.WHITE}}""" self.assertEqual(res.script, exp) p = shapely.geometry.Point(0, 1, 3) res = cesiumpy.extension.shapefile.to_entity(p) exp = """{position : Cesium.Cartesian3.fromDegrees(0.0, 1.0, 3.0), point : {pixelSize : 10.0, color : Cesium.Color.WHITE}}""" self.assertEqual(res.script, exp) # multipoint p = shapely.geometry.MultiPoint([[1, 1], [1, 2], [2, 2], [2, 1]]) res = cesiumpy.extension.shapefile.to_entity(p) self.assertIsInstance(res, list) self.assertEqual(len(res), 4) exp = ['{position : Cesium.Cartesian3.fromDegrees(1.0, 1.0, 0.0), point : {pixelSize : 10.0, color : Cesium.Color.WHITE}}', '{position : Cesium.Cartesian3.fromDegrees(1.0, 2.0, 0.0), point : {pixelSize : 10.0, color : Cesium.Color.WHITE}}', '{position : Cesium.Cartesian3.fromDegrees(2.0, 2.0, 0.0), point : {pixelSize : 10.0, color : Cesium.Color.WHITE}}', '{position : Cesium.Cartesian3.fromDegrees(2.0, 1.0, 0.0), point : {pixelSize : 10.0, color : Cesium.Color.WHITE}}'] self.assertEqual([e.script for e in res], exp)
def test_line_to_entity(self): _skip_if_no_shapely() import shapely.geometry p = shapely.geometry.LineString([(0, 1), (2, 3)]) res = cesiumpy.extension.shapefile.to_entity(p) exp = """{polyline : {positions : Cesium.Cartesian3.fromDegreesArray([0.0, 1.0, 2.0, 3.0])}}""" self.assertEqual(res.script, exp) p = shapely.geometry.LinearRing([(0, 1), (2, 3), (1, 3)]) res = cesiumpy.extension.shapefile.to_entity(p) # last element is being added exp = """{polyline : {positions : Cesium.Cartesian3.fromDegreesArray([0.0, 1.0, 2.0, 3.0, 1.0, 3.0, 0.0, 1.0])}}""" self.assertEqual(res.script, exp) # multilinestring p = shapely.geometry.MultiLineString([[[1, 1], [1, 2]], [[2, 2], [2, 1]]]) res = cesiumpy.extension.shapefile.to_entity(p) self.assertIsInstance(res, list) self.assertEqual(len(res), 2) exp = ['{polyline : {positions : Cesium.Cartesian3.fromDegreesArray([1.0, 1.0, 1.0, 2.0])}}', '{polyline : {positions : Cesium.Cartesian3.fromDegreesArray([2.0, 2.0, 2.0, 1.0])}}'] self.assertEqual([e.script for e in res], exp)
def test_country_jpn(self): _skip_if_no_shapely() jpn = cesiumpy.countries.jpn self.assertIsInstance(jpn, list) self.assertTrue(all([isinstance(e, cesiumpy.Polygon) for e in jpn])) exp = """{polygon : {hierarchy : Cesium.Cartesian3.fromDegreesArray([153.958588, 24.295, 153.953308, 24.292774, 153.946625, 24.293331, 153.942749, 24.296944, 153.939697, 24.300831, 153.938873, 24.306942, 153.940247, 24.312496, 153.947754, 24.319443, 153.952759, 24.321384, 153.960236, 24.321663, 153.96579, 24.31361, 153.96579, 24.309441, 153.963013, 24.29833, 153.958588, 24.295])}}""" self.assertEqual(jpn[0].script, exp) jpn = cesiumpy.countries.JPN self.assertIsInstance(jpn, list) self.assertTrue(all([isinstance(e, cesiumpy.Polygon) for e in jpn])) self.assertEqual(jpn[0].script, exp) # 2 character jpn = cesiumpy.countries.JP self.assertIsInstance(jpn, list) self.assertTrue(all([isinstance(e, cesiumpy.Polygon) for e in jpn])) self.assertEqual(jpn[0].script, exp) # official name jpn = cesiumpy.countries.JAPAN self.assertIsInstance(jpn, list) self.assertTrue(all([isinstance(e, cesiumpy.Polygon) for e in jpn])) self.assertEqual(jpn[0].script, exp)
def test_polygon_to_cartesian_array(self): _skip_if_no_shapely() import shapely.geometry p = shapely.geometry.Polygon([[1, 1], [1, 2], [2, 2], [2, 1]]) res = cartesian.Cartesian3.fromDegreesArray(p) exp = cartesian.Cartesian3.fromDegreesArray([1., 1., 1., 2., 2., 2., 2., 1., 1., 1.]) self.assertIsInstance(res, cartesian.Cartesian3Array) self.assertEqual(res.script, exp.script)
def test_shape(self): _skip_if_no_shapely() path = os.path.join(current_dir, 'data', 'coastl_jpn.shp') self.assertTrue(os.path.exists(path)) res = cesiumpy.io.read_shape(path) exp = """{polyline : {positions : Cesium.Cartesian3.fromDegreesArray([136.06983283646417, 20.425446784039757, 136.06989943430338, 20.4254103630917, 136.06994984324314, 20.42536719269311, 136.06998867703822, 20.425312805816333, 136.07000535502831, 20.425227687745696, 136.06997763989327, 20.42514023834311, 136.06994170803583, 20.425077341289573, 136.06981677798885, 20.425014868114573, 136.06967221910506, 20.425023785866852, 136.06956593971367, 20.425117626053517, 136.06954869111786, 20.425169632689283, 136.06952692658533, 20.425285954741376, 136.06955619050734, 20.425353954640368, 136.06961863107628, 20.42540397230762, 136.06966507838203, 20.42543426331992, 136.06969721163566, 20.425443213678243, 136.06975939135629, 20.42545514748933, 136.06983283646417, 20.425446784039757])}}""" self.assertIsInstance(res, list) self.assertTrue(all([isinstance(e, cesiumpy.Polyline) for e in res])) self.assertEqual(res[0].script, exp)
def test_geojson(self): _skip_if_no_shapely() path = os.path.join(current_dir, 'data', 'jpn.geo.json') self.assertTrue(os.path.exists(path)) res = cesiumpy.io.read_geojson(path) exp = """{polygon : {hierarchy : Cesium.Cartesian3.fromDegreesArray([153.958588, 24.295, 153.953308, 24.292774, 153.946625, 24.293331, 153.942749, 24.296944, 153.939697, 24.300831, 153.938873, 24.306942, 153.940247, 24.312496, 153.947754, 24.319443, 153.952759, 24.321384, 153.960236, 24.321663, 153.96579, 24.31361, 153.96579, 24.309441, 153.963013, 24.29833, 153.958588, 24.295])}}""" self.assertIsInstance(res, list) self.assertTrue(all([isinstance(e, cesiumpy.Polygon) for e in res])) self.assertEqual(res[0].script, exp)
def test_viewer(self): _skip_if_no_shapely() v = cesiumpy.Viewer(divid='viewertest') v.entities.add(cesiumpy.countries.abw) res = v.to_html() exp = """<script src="https://cesiumjs.org/Cesium/Build/Cesium/Cesium.js"></script> <link rel="stylesheet" href="https://cesiumjs.org/Cesium/Build/Cesium/Widgets/widgets.css" type="text/css"> <div id="viewertest" style="width:100%; height:100%;"><div> <script type="text/javascript"> var widget = new Cesium.Viewer("viewertest"); widget.entities.add({polygon : {hierarchy : Cesium.Cartesian3.fromDegreesArray([-69.882233, 12.41111, -69.946945, 12.436666, -70.056122, 12.534443, -70.059448, 12.538055, -70.060287, 12.544167, -70.063339, 12.621666, -70.063065, 12.628611, -70.058899, 12.631109, -70.053345, 12.629721, -70.035278, 12.61972, -70.031113, 12.616943, -69.932236, 12.528055, -69.896957, 12.480833, -69.891403, 12.472221, -69.885559, 12.457777, -69.873901, 12.421944, -69.873337, 12.415833, -69.876114, 12.411665, -69.882233, 12.41111])}}); widget.zoomTo(widget.entities); </script>""" self.assertEqual(res, exp)
def test_viewer(self): _skip_if_no_shapely() v = cesiumpy.Viewer(divid='viewertest') v.entities.add(cesiumpy.countries.abw) res = v.to_html() exp = """<script src="https://cesiumjs.org/Cesium/Build/Cesium/Cesium.js"></script> <link rel="stylesheet" href="http://cesiumjs.org/Cesium/Build/Cesium/Widgets/widgets.css" type="text/css"> <div id="viewertest" style="width:100%; height:100%;"><div> <script type="text/javascript"> var widget = new Cesium.Viewer("viewertest"); widget.entities.add({polygon : {hierarchy : Cesium.Cartesian3.fromDegreesArray([-69.882233, 12.41111, -69.946945, 12.436666, -70.056122, 12.534443, -70.059448, 12.538055, -70.060287, 12.544167, -70.063339, 12.621666, -70.063065, 12.628611, -70.058899, 12.631109, -70.053345, 12.629721, -70.035278, 12.61972, -70.031113, 12.616943, -69.932236, 12.528055, -69.896957, 12.480833, -69.891403, 12.472221, -69.885559, 12.457777, -69.873901, 12.421944, -69.873337, 12.415833, -69.876114, 12.411665, -69.882233, 12.41111])}}); widget.zoomTo(widget.entities); </script>""" self.assertEqual(res, exp)
def test_point_to_cartesian(self): _skip_if_no_shapely() import shapely.geometry p = shapely.geometry.Point(0, 1) res = cartesian.Cartesian2.maybe(p) exp = cesiumpy.Cartesian2(0., 1.) self.assertIsInstance(res, cartesian.Cartesian2) self.assertEqual(res.script, exp.script) p = shapely.geometry.Point(0, 1, 3) res = cartesian.Cartesian3.maybe(p) exp = cesiumpy.Cartesian3(0., 1., 3.) self.assertIsInstance(res, cartesian.Cartesian3) self.assertEqual(res.script, exp.script)
def test_line_to_cartesian_array(self): _skip_if_no_shapely() import shapely.geometry p = shapely.geometry.LineString([(0, 1), (2, 3)]) res = cartesian.Cartesian3.fromDegreesArray(p) exp = cartesian.Cartesian3.fromDegreesArray([0., 1., 2., 3.]) self.assertIsInstance(res, cartesian.Cartesian3Array) self.assertEqual(res.script, exp.script) p = shapely.geometry.LinearRing([(0, 1), (2, 3), (1, 3)]) res = cartesian.Cartesian3.fromDegreesArray(p) # last element is being added exp = cartesian.Cartesian3.fromDegreesArray([0., 1., 2., 3., 1., 3., 0., 1.]) self.assertIsInstance(res, cartesian.Cartesian3Array) self.assertEqual(res.script, exp.script)
def test_polygon_to_entity(self): _skip_if_no_shapely() import shapely.geometry p = shapely.geometry.Polygon([[1, 1], [1, 2], [2, 2], [2, 1]]) res = cesiumpy.extension.shapefile.to_entity(p) exp = """{polygon : {hierarchy : Cesium.Cartesian3.fromDegreesArray([1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 1.0, 1.0, 1.0])}}""" self.assertEqual(res.script, exp) # multipolygon p1 = shapely.geometry.Polygon([[1, 1], [1, 2], [2, 2], [2, 1]]) p2 = shapely.geometry.Polygon([[3, 3], [3, 4], [4, 4], [4, 3]]) p = shapely.geometry.MultiPolygon([p1, p2]) res = cesiumpy.extension.shapefile.to_entity(p) self.assertIsInstance(res, list) self.assertEqual(len(res), 2) exp = ['{polygon : {hierarchy : Cesium.Cartesian3.fromDegreesArray([1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 1.0, 1.0, 1.0])}}', '{polygon : {hierarchy : Cesium.Cartesian3.fromDegreesArray([3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0, 3.0, 3.0, 3.0])}}'] self.assertEqual([e.script for e in res], exp)
def test_point_to_cartesian_degrees(self): _skip_if_no_shapely() import shapely.geometry p = shapely.geometry.Point(0, 1) res = cartesian.Cartesian2.maybe(p, degrees=True) exp = cesiumpy.Cartesian2.fromDegrees(0., 1.) self.assertIsInstance(res, cartesian.Cartesian2) self.assertEqual(res.script, exp.script) # do not convert res = cartesian.Cartesian3.maybe(p) self.assertEqual(res, [0., 1.]) p = shapely.geometry.Point(0, 1, 3) res = cartesian.Cartesian3.maybe(p, degrees=True) exp = cesiumpy.Cartesian3.fromDegrees(0., 1., 3.) self.assertIsInstance(res, cartesian.Cartesian3) self.assertEqual(res.script, exp.script) # do not convert res = cartesian.Cartesian2.maybe(p) self.assertEqual(res, [0., 1., 3.])
def test_voronoi(self): _skip_if_no_scipy() _skip_if_no_shapely() import numpy as np import scipy.spatial np.random.seed(1234) points = np.random.rand(15, 2) * 5 + np.array([130, 40]) vor = cesiumpy.spatial.Voronoi(points) polygons = vor.get_polygons() expected = [[129.53037742006228, 44.33471494590681, 131.40476793036768, 43.448996661779695, 131.39784031783313, 43.270076795621065, 130.99199913910985, 42.328426095963245, 129.53037742006228, 42.99862087958684, 129.53037742006228, 44.33471494590681], [132.5231502900114, 45.90344076159844, 132.97793597508354, 44.336889661521575, 132.64956736534268, 43.22629856885517, 132.04973345010688, 43.48337280292282, 132.25107509065992, 45.90344076159844, 132.5231502900114, 45.90344076159844], [133.77311875584925, 40.110955821295555, 132.56020732493596, 41.408540471782935, 133.05978915987998, 42.33192230184925, 133.81747880349292, 42.498078263756426, 135.4154315003338, 41.85193612810953, 135.4154315003338, 40.110955821295555, 133.77311875584925, 40.110955821295555], [131.89390865691786, 45.90344076159844, 131.63882963366206, 43.55998220705691, 131.40476793036768, 43.448996661779695, 129.53037742006228, 44.33471494590681, 129.53037742006228, 45.90344076159844, 131.89390865691786, 45.90344076159844], [135.4154315003338, 43.741768534167036, 134.30817767731628, 43.86503713071969, 134.38197047363133, 45.90344076159844, 135.4154315003338, 45.90344076159844, 135.4154315003338, 43.741768534167036], [131.03585127801438, 42.275291825551136, 131.69926550801236, 42.680449017412236, 133.0090748373699, 42.409930619839045, 133.05978915987998, 42.33192230184925, 132.56020732493596, 41.408540471782935, 131.57844318202783, 41.115828979808505, 131.03585127801438, 42.275291825551136], [132.64956736534268, 43.22629856885517, 132.97793597508354, 44.336889661521575, 134.12142452823772, 43.73557828446551, 133.81747880349292, 42.498078263756426, 133.05978915987998, 42.33192230184925, 133.0090748373699, 42.409930619839045, 132.7101884373397, 43.027826672381536, 132.64956736534268, 43.22629856885517], [131.7316387448889, 42.93096881919415, 132.7101884373397, 43.027826672381536, 133.0090748373699, 42.409930619839045, 131.69926550801236, 42.680449017412236, 131.7316387448889, 42.93096881919415], [130.7442505383254, 40.110955821295555, 131.57844318202783, 41.115828979808505, 132.56020732493596, 41.408540471782935, 133.77311875584925, 40.110955821295555, 130.7442505383254, 40.110955821295555], [134.38197047363133, 45.90344076159844, 134.30817767731628, 43.86503713071969, 134.12142452823772, 43.73557828446551, 132.97793597508354, 44.336889661521575, 132.5231502900114, 45.90344076159844, 134.38197047363133, 45.90344076159844], [132.04973345010688, 43.48337280292282, 132.64956736534268, 43.22629856885517, 132.7101884373397, 43.027826672381536, 131.7316387448889, 42.93096881919415, 131.39784031783313, 43.270076795621065, 131.40476793036768, 43.448996661779695, 131.63882963366206, 43.55998220705691, 132.04973345010688, 43.48337280292282], [129.53037742006228, 42.99862087958684, 130.99199913910985, 42.328426095963245, 131.03585127801438, 42.275291825551136, 131.57844318202783, 41.115828979808505, 130.7442505383254, 40.110955821295555, 129.53037742006228, 40.110955821295555, 129.53037742006228, 42.99862087958684], [135.4154315003338, 41.85193612810953, 133.81747880349292, 42.498078263756426, 134.12142452823772, 43.73557828446551, 134.30817767731628, 43.86503713071969, 135.4154315003338, 43.741768534167036, 135.4154315003338, 41.85193612810953], [132.25107509065992, 45.90344076159844, 132.04973345010688, 43.48337280292282, 131.63882963366206, 43.55998220705691, 131.89390865691786, 45.90344076159844, 132.25107509065992, 45.90344076159844], [131.39784031783313, 43.270076795621065, 131.7316387448889, 42.93096881919415, 131.69926550801236, 42.680449017412236, 131.03585127801438, 42.275291825551136, 130.99199913910985, 42.328426095963245, 131.39784031783313, 43.270076795621065]] for polygon, exp in zip(polygons, expected): self.assertEqual(polygon.hierarchy.x, exp) # testing scipy.spatial.Voronoi instance vor = scipy.spatial.Voronoi(points) vor = cesiumpy.spatial.Voronoi(vor) polygons = vor.get_polygons() for polygon, exp in zip(polygons, expected): self.assertEqual(polygon.hierarchy.x, exp)
def test_voronoi(self): _skip_if_no_scipy() _skip_if_no_shapely() import numpy as np import scipy.spatial np.random.seed(1234) points = np.random.rand(15, 2) * 5 + np.array([130, 40]) vor = cesiumpy.spatial.Voronoi(points) polygons = vor.get_polygons() expected = [ [ 129.53037742006228, 44.33471494590681, 131.40476793036768, 43.448996661779695, 131.39784031783313, 43.270076795621065, 130.99199913910985, 42.328426095963245, 129.53037742006228, 42.99862087958684, 129.53037742006228, 44.33471494590681 ], [ 132.5231502900114, 45.90344076159844, 132.97793597508354, 44.336889661521575, 132.64956736534268, 43.22629856885517, 132.04973345010688, 43.48337280292282, 132.25107509065992, 45.90344076159844, 132.5231502900114, 45.90344076159844 ], [ 133.77311875584925, 40.110955821295555, 132.56020732493596, 41.408540471782935, 133.05978915987998, 42.33192230184925, 133.81747880349292, 42.498078263756426, 135.4154315003338, 41.85193612810953, 135.4154315003338, 40.110955821295555, 133.77311875584925, 40.110955821295555 ], [ 131.89390865691786, 45.90344076159844, 131.63882963366206, 43.55998220705691, 131.40476793036768, 43.448996661779695, 129.53037742006228, 44.33471494590681, 129.53037742006228, 45.90344076159844, 131.89390865691786, 45.90344076159844 ], [ 135.4154315003338, 43.741768534167036, 134.30817767731628, 43.86503713071969, 134.38197047363133, 45.90344076159844, 135.4154315003338, 45.90344076159844, 135.4154315003338, 43.741768534167036 ], [ 131.03585127801438, 42.275291825551136, 131.69926550801236, 42.680449017412236, 133.0090748373699, 42.409930619839045, 133.05978915987998, 42.33192230184925, 132.56020732493596, 41.408540471782935, 131.57844318202783, 41.115828979808505, 131.03585127801438, 42.275291825551136 ], [ 132.64956736534268, 43.22629856885517, 132.97793597508354, 44.336889661521575, 134.12142452823772, 43.73557828446551, 133.81747880349292, 42.498078263756426, 133.05978915987998, 42.33192230184925, 133.0090748373699, 42.409930619839045, 132.7101884373397, 43.027826672381536, 132.64956736534268, 43.22629856885517 ], [ 131.7316387448889, 42.93096881919415, 132.7101884373397, 43.027826672381536, 133.0090748373699, 42.409930619839045, 131.69926550801236, 42.680449017412236, 131.7316387448889, 42.93096881919415 ], [ 130.7442505383254, 40.110955821295555, 131.57844318202783, 41.115828979808505, 132.56020732493596, 41.408540471782935, 133.77311875584925, 40.110955821295555, 130.7442505383254, 40.110955821295555 ], [ 134.38197047363133, 45.90344076159844, 134.30817767731628, 43.86503713071969, 134.12142452823772, 43.73557828446551, 132.97793597508354, 44.336889661521575, 132.5231502900114, 45.90344076159844, 134.38197047363133, 45.90344076159844 ], [ 132.04973345010688, 43.48337280292282, 132.64956736534268, 43.22629856885517, 132.7101884373397, 43.027826672381536, 131.7316387448889, 42.93096881919415, 131.39784031783313, 43.270076795621065, 131.40476793036768, 43.448996661779695, 131.63882963366206, 43.55998220705691, 132.04973345010688, 43.48337280292282 ], [ 129.53037742006228, 42.99862087958684, 130.99199913910985, 42.328426095963245, 131.03585127801438, 42.275291825551136, 131.57844318202783, 41.115828979808505, 130.7442505383254, 40.110955821295555, 129.53037742006228, 40.110955821295555, 129.53037742006228, 42.99862087958684 ], [ 135.4154315003338, 41.85193612810953, 133.81747880349292, 42.498078263756426, 134.12142452823772, 43.73557828446551, 134.30817767731628, 43.86503713071969, 135.4154315003338, 43.741768534167036, 135.4154315003338, 41.85193612810953 ], [ 132.25107509065992, 45.90344076159844, 132.04973345010688, 43.48337280292282, 131.63882963366206, 43.55998220705691, 131.89390865691786, 45.90344076159844, 132.25107509065992, 45.90344076159844 ], [ 131.39784031783313, 43.270076795621065, 131.7316387448889, 42.93096881919415, 131.69926550801236, 42.680449017412236, 131.03585127801438, 42.275291825551136, 130.99199913910985, 42.328426095963245, 131.39784031783313, 43.270076795621065 ] ] for polygon, exp in zip(polygons, expected): self.assertEqual(polygon.hierarchy.x, exp) # testing scipy.spatial.Voronoi instance vor = scipy.spatial.Voronoi(points) vor = cesiumpy.spatial.Voronoi(vor) polygons = vor.get_polygons() for polygon, exp in zip(polygons, expected): self.assertEqual(polygon.hierarchy.x, exp)