Exemple #1
0
 def test_convert_to_point(self):
     expected_point = shapely.geometry.Point(12.8, -34.4)
     self.assertIs(expected_point, convert_geometry(expected_point))
     self.assertEqual(expected_point, convert_geometry([12.8, -34.4]))
     self.assertEqual(expected_point,
                      convert_geometry(np.array([12.8, -34.4])))
     self.assertEqual(expected_point, convert_geometry(expected_point.wkt))
     self.assertEqual(expected_point,
                      convert_geometry(expected_point.__geo_interface__))
Exemple #2
0
    def test_invalid(self):
        from xcube.util.geom import _INVALID_GEOMETRY_MSG

        with self.assertRaises(ValueError) as cm:
            convert_geometry(dict(coordinates=[12.8, -34.4]))
        self.assertEqual(_INVALID_GEOMETRY_MSG, f'{cm.exception}')

        with self.assertRaises(ValueError) as cm:
            convert_geometry([12.8, -34.4, '?'])
        self.assertEqual(_INVALID_GEOMETRY_MSG, f'{cm.exception}')
Exemple #3
0
 def test_convert_to_box(self):
     expected_box = shapely.geometry.box(12.8, -34.4, 14.2, 20.6)
     self.assertIs(expected_box, convert_geometry(expected_box))
     self.assertEqual(expected_box,
                      convert_geometry([12.8, -34.4, 14.2, 20.6]))
     self.assertEqual(expected_box,
                      convert_geometry(np.array([12.8, -34.4, 14.2, 20.6])))
     self.assertEqual(expected_box, convert_geometry(expected_box.wkt))
     self.assertEqual(expected_box,
                      convert_geometry(expected_box.__geo_interface__))
Exemple #4
0
 def test_convert_to_point(self):
     expected_point = shapely.geometry.Point(12.8, -34.4)
     self.assertIs(expected_point,
                   convert_geometry(expected_point))
     self.assertEqual(expected_point,
                      convert_geometry([12.8, -34.4]))
     self.assertEqual(expected_point,
                      convert_geometry('POINT (12.8 -34.4)'))
     self.assertEqual(expected_point,
                      convert_geometry(dict(type='Point', coordinates=[12.8, -34.4])))
Exemple #5
0
    def test_invalid(self):
        expected_msg = ('geometry must be either a (shapely) geometry object, '
                        'a valid GeoJSON object, a valid WKT string, '
                        'box coordinates (x1, y1, x2, y2), '
                        'or point coordinates (x, y)')

        with self.assertRaises(ValueError) as cm:
            convert_geometry(dict(coordinates=[12.8, -34.4]))
        self.assertEqual(expected_msg, f'{cm.exception}')

        with self.assertRaises(ValueError) as cm:
            convert_geometry([12.8, -34.4, '?'])
        self.assertEqual(expected_msg, f'{cm.exception}')
Exemple #6
0
 def test_convert_to_box(self):
     expected_box = shapely.geometry.Polygon(
         [(12.8, -34.4), (14.2, -34.4), (14.2, 20.6), (12.8, 20.6), (12.8, -34.4)])
     self.assertIs(expected_box,
                   convert_geometry(expected_box))
     self.assertEqual(expected_box,
                      convert_geometry([12.8, -34.4, 14.2, 20.6]))
     self.assertEqual(expected_box,
                      convert_geometry('POLYGON ((12.8 -34.4, 14.2 -34.4, 14.2 20.6, 12.8 20.6, 12.8 -34.4))'))
     self.assertEqual(expected_box,
                      convert_geometry(dict(type='Polygon', coordinates=[
                          [[12.8, -34.4], [14.2, -34.4], [14.2, 20.6], [12.8, 20.6], [12.8, -34.4]]
                      ])))
Exemple #7
0
    def test_convert_from_geojson_feature_dict(self):
        expected_box1 = shapely.geometry.box(-10, -20, 20, 10)
        expected_box2 = shapely.geometry.box(30, 20, 50, 40)
        feature1 = dict(type='Feature',
                        geometry=expected_box1.__geo_interface__)
        feature2 = dict(type='Feature',
                        geometry=expected_box2.__geo_interface__)
        feature_collection = dict(type='FeatureCollection',
                                  features=(feature1, feature2))

        actual_geom = convert_geometry(feature1)
        self.assertEqual(expected_box1, actual_geom)

        actual_geom = convert_geometry(feature2)
        self.assertEqual(expected_box2, actual_geom)

        expected_geom = shapely.geometry.GeometryCollection(
            geoms=[expected_box1, expected_box2])
        actual_geom = convert_geometry(feature_collection)
        self.assertEqual(expected_geom, actual_geom)
Exemple #8
0
 def test_convert_to_split_box(self):
     expected_split_box = shapely.geometry.MultiPolygon(polygons=[
         shapely.geometry.Polygon(((180.0, -34.4), (180.0, 20.6),
                                   (172.1, 20.6), (172.1, -34.4), (180.0,
                                                                   -34.4))),
         shapely.geometry.Polygon(((-165.7, -34.4), (-165.7, 20.6), (-180.0,
                                                                     20.6),
                                   (-180.0, -34.4), (-165.7, -34.4)))
     ])
     self.assertEqual(expected_split_box,
                      convert_geometry([172.1, -34.4, -165.7, 20.6]))
Exemple #9
0
    def test_convert_invalid_box(self):
        from xcube.util.geom import _INVALID_BOX_COORDS_MSG

        with self.assertRaises(ValueError) as cm:
            convert_geometry([12.8, 20.6, 14.2, -34.4])
        self.assertEqual(_INVALID_BOX_COORDS_MSG, f'{cm.exception}')
        with self.assertRaises(ValueError) as cm:
            convert_geometry([12.8, -34.4, 12.8, 20.6])
        self.assertEqual(_INVALID_BOX_COORDS_MSG, f'{cm.exception}')
        with self.assertRaises(ValueError) as cm:
            convert_geometry([12.8, -34.4, 12.8, 20.6])
        self.assertEqual(_INVALID_BOX_COORDS_MSG, f'{cm.exception}')
Exemple #10
0
 def test_convert_box_as_point(self):
     expected_point = shapely.geometry.Point(12.8, -34.4)
     self.assertEqual(expected_point,
                      convert_geometry([12.8, -34.4, 12.8, -34.4]))
Exemple #11
0
 def test_convert_null(self):
     self.assertIs(None, convert_geometry(None))