예제 #1
0
    def test_multi_polygon(self):
        with tempfile.TemporaryDirectory("-allfed-spatial-test") as tempdir:
            filename = os.path.join(tempdir, "testfile.file")

            feature1 = Feature(
                MultiPolygon([
                    Polygon([(0, 0), (1, 1), (0, 1)]),
                    Polygon([(0, 0), (1, 1), (1, 0)])
                ]), complete_test_data_1)
            feature2 = Feature(
                MultiPolygon([
                    Polygon([(1, 1), (2, 2), (1, 2)]),
                    Polygon([(1, 1), (2, 2), (2, 1)])
                ]), complete_test_data_2)

            featuresToDisk = [feature1, feature2]

            geoms = [f.geom for f in featuresToDisk]
            data = [f.data for f in featuresToDisk]

            schema = featureIO.get_feature_schema(featuresToDisk[0])

            featureIO.write_shape(geoms, data, schema, filename)
            featuresFromDisk = featureIO.load_features(filename)
            self.FeaturesEqual(featuresFromDisk, featuresToDisk)
예제 #2
0
 def test_int_data(self):
     feature = Feature(LineString([(0, 0), (1, 1)]), {"test": 123})
     self.assertEqual(featureIO.get_feature_schema(feature), {
         "geometry": "LineString",
         "properties": {
             "test": "int:16"
         }
     })
예제 #3
0
 def test_string_data(self):
     feature = Feature(LineString([(0, 0), (1, 1)]), {"test": "data"})
     self.assertEqual(featureIO.get_feature_schema(feature), {
         "geometry": "LineString",
         "properties": {
             "test": "str:250"
         }
     })
예제 #4
0
 def test_polygon_geometry(self):
     feature = Feature(Polygon([(0, 0), (1, 1), (0, 1)]), {"test": "data"})
     self.assertEqual(featureIO.get_feature_schema(feature), {
         "geometry": "Polygon",
         "properties": {
             "test": "str:250"
         }
     })
예제 #5
0
 def test_point_geometry(self):
     feature = Feature(Point(0, 0), {"test": "data"})
     self.assertEqual(featureIO.get_feature_schema(feature), {
         "geometry": "Point",
         "properties": {
             "test": "str:250"
         }
     })
예제 #6
0
 def test_other_geometry(self):
     feature = Feature(LinearRing([(0, 0), (1, 1), (0, 1)]),
                       {"test": "data"})
     # other types will fail later in the process
     self.assertEqual(featureIO.get_feature_schema(feature), {
         "geometry": "LinearRing",
         "properties": {
             "test": "str:250"
         }
     })
예제 #7
0
    def test_data_mismatch(self):
        with tempfile.TemporaryDirectory("-allfed-spatial-test") as tempdir:
            filename = os.path.join(tempdir, "testfile.file")

            feature1 = Feature(Point(0, 0), complete_test_data_1)
            feature2 = Feature(Point(1, 1), diff_schema_test_data)

            featuresToDisk = [feature1, feature2]

            geoms = [f.geom for f in featuresToDisk]
            data = [f.data for f in featuresToDisk]

            schema = featureIO.get_feature_schema(featuresToDisk[0])

            with self.assertRaises(ValueError):
                featureIO.write_shape(geoms, data, schema, filename)
예제 #8
0
    def test_mixed_geom(self):
        with tempfile.TemporaryDirectory("-allfed-spatial-test") as tempdir:
            filename = os.path.join(tempdir, "testfile.file")

            feature1 = Feature(LineString([(0, 0), (1, 1)]),
                               complete_test_data_1)
            feature2 = Feature(Point(20, 20), complete_test_data_2)

            featuresToDisk = [feature1, feature2]

            geoms = [f.geom for f in featuresToDisk]
            data = [f.data for f in featuresToDisk]

            schema = featureIO.get_feature_schema(featuresToDisk[0])

            with self.assertRaises(fiona.errors.GeometryTypeValidationError):
                featureIO.write_shape(geoms, data, schema, filename)
예제 #9
0
    def test_other_geom(self):
        """Other geometry is silently lost, no exceptions stop the process"""
        with tempfile.TemporaryDirectory("-allfed-spatial-test") as tempdir:
            filename = os.path.join(tempdir, "testfile.file")

            feature1 = Feature(LinearRing([(0, 0), (1, 1), (0, 1)]),
                               complete_test_data_1)

            featuresToDisk = [feature1]

            geoms = [f.geom for f in featuresToDisk]
            data = [f.data for f in featuresToDisk]

            schema = featureIO.get_feature_schema(featuresToDisk[0])

            featureIO.write_shape(geoms, data, schema, filename)
            featuresFromDisk = featureIO.load_features(filename)
            self.assertEqual(featuresFromDisk, [])
예제 #10
0
    def test_data_unconvertable(self):
        with tempfile.TemporaryDirectory("-allfed-spatial-test") as tempdir:
            filename = os.path.join(tempdir, "testfile.file")

            feature1 = Feature(Point(0, 0), complete_test_data_1)
            feature2 = Feature(Point(1, 1),
                               diff_schema_unconvertable_float_test_data)

            featuresToDisk = [feature1, feature2]

            geoms = [f.geom for f in featuresToDisk]
            data = [f.data for f in featuresToDisk]

            schema = featureIO.get_feature_schema(featuresToDisk[0])

            featureIO.write_shape(geoms, data, schema, filename)
            featuresFromDisk = featureIO.load_features(filename)

            modifiedFeaturesFromDisk = featureIO.load_features(filename)
            modifiedFeaturesFromDisk[1].data["uniqueKey"] = math.nan

            self.assertFalse(math.isnan(featuresFromDisk[1].data["uniqueKey"]))
            self.assertEqual(featuresFromDisk[1].data["uniqueKey"], -(2**63))
            self.FeaturesEqual(modifiedFeaturesFromDisk, featuresToDisk)
예제 #11
0
    def test_invalid_data_types(self):
        with self.assertRaises(ValueError):
            feature = Feature(LineString([(0, 0), (1, 1)]), {"test": None})
            featureIO.get_feature_schema(feature)

        with self.assertRaises(ValueError):
            feature = Feature(LineString([(0, 0), (1, 1)]),
                              {"test": [0, 1, 2]})
            featureIO.get_feature_schema(feature)

        with self.assertRaises(ValueError):
            feature = Feature(LineString([(0, 0), (1, 1)]),
                              {"test": (0, 1, 2)})
            featureIO.get_feature_schema(feature)

        with self.assertRaises(ValueError):
            feature = Feature(LineString([(0, 0), (1, 1)]),
                              {"test": {
                                  "more": "test",
                                  "data": 123
                              }})
            featureIO.get_feature_schema(feature)

        with self.assertRaises(ValueError):
            feature = Feature(LineString([(0, 0), (1, 1)]), {"test": object()})
            featureIO.get_feature_schema(feature)

        with self.assertRaises(ValueError):
            feature = Feature(LineString([(0, 0), (1, 1)]),
                              {"test": Point(0, 0)})
            featureIO.get_feature_schema(feature)