File: test_subtypes.py

package info (click to toggle)
fiona 1.10.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,632 kB
  • sloc: python: 12,616; makefile: 214; sh: 45
file content (83 lines) | stat: -rw-r--r-- 2,098 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Tests of schema sub-types."""

import os

import fiona
from fiona.model import Feature


def test_read_bool_subtype(tmp_path):
    test_data = """{"type": "FeatureCollection", "features": [{"type": "Feature", "properties": {"bool": true, "not_bool": 1, "float": 42.5}, "geometry": null}]}"""
    path = tmp_path.joinpath("test_read_bool_subtype.geojson")

    with open(os.fspath(path), "w") as f:
        f.write(test_data)

    with fiona.open(path, "r") as src:
        feature = next(iter(src))

    assert type(feature["properties"]["bool"]) is bool
    assert isinstance(feature["properties"]["not_bool"], int)
    assert type(feature["properties"]["float"]) is float


def test_write_bool_subtype(tmp_path):
    path = tmp_path.joinpath("test_write_bool_subtype.geojson")

    schema = {
        "geometry": "Point",
        "properties": {
            "bool": "bool",
            "not_bool": "int",
            "float": "float",
        },
    }

    feature = Feature.from_dict(
        **{
            "geometry": None,
            "properties": {
                "bool": True,
                "not_bool": 1,
                "float": 42.5,
            },
        }
    )

    with fiona.open(path, "w", driver="GeoJSON", schema=schema) as dst:
        dst.write(feature)

    with open(os.fspath(path)) as f:
        data = f.read()

    assert """"bool": true""" in data
    assert """"not_bool": 1""" in data


def test_write_int16_subtype(tmp_path):
    path = tmp_path.joinpath("test_write_bool_subtype.gpkg")

    schema = {
        "geometry": "Point",
        "properties": {
            "a": "int",
            "b": "int16",
        },
    }

    feature = Feature.from_dict(
        **{
            "geometry": None,
            "properties": {
                "a": 1,
                "b": 2,
            },
        }
    )

    with fiona.open(path, "w", driver="GPKG", schema=schema) as colxn:
        colxn.write(feature)

    with fiona.open(path) as colxn:
        assert colxn.schema["properties"]["a"] == "int"
        assert colxn.schema["properties"]["b"] == "int16"