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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
import json
import os.path
import tempfile
import pytest
import fiona
from fiona import prop_type, prop_width
from fiona.model import Feature
from .conftest import gdal_version
def test_width_str():
assert prop_width("str:254") == 254
assert prop_width("str") == 80
def test_width_other():
assert prop_width("int") == None
assert prop_width("float") == None
assert prop_width("date") == None
def test_types():
assert prop_type("str:254") == str
assert prop_type("str") == str
assert isinstance(0, prop_type("int"))
assert isinstance(0.0, prop_type("float"))
assert prop_type("date") == str
@pytest.mark.xfail(not gdal_version.at_least("3.5"), reason="Requires at least GDAL 3.5.0")
def test_read_json_object_properties():
"""JSON object properties are properly serialized"""
data = """
{
"type": "FeatureCollection",
"features": [
{
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
87.33588,
43.53139
],
[
87.33588,
45.66894
],
[
90.27542,
45.66894
],
[
90.27542,
43.53139
],
[
87.33588,
43.53139
]
]
]
},
"type": "Feature",
"properties": {
"upperLeftCoordinate": {
"latitude": 45.66894,
"longitude": 87.91166
},
"tricky": "{gotcha"
}
}
]
}
"""
tmpdir = tempfile.mkdtemp()
filename = os.path.join(tmpdir, "test.json")
with open(filename, "w") as f:
f.write(data)
with fiona.open(filename) as src:
ftr = next(iter(src))
props = ftr["properties"]
assert props["upperLeftCoordinate"]["latitude"] == 45.66894
assert props["upperLeftCoordinate"]["longitude"] == 87.91166
assert props["tricky"] == "{gotcha"
@pytest.mark.xfail(not gdal_version.at_least("3.5"), reason="Requires at least GDAL 3.5.0")
def test_write_json_object_properties():
"""Python object properties are properly serialized"""
data = """
{
"type": "FeatureCollection",
"features": [
{
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
87.33588,
43.53139
],
[
87.33588,
45.66894
],
[
90.27542,
45.66894
],
[
90.27542,
43.53139
],
[
87.33588,
43.53139
]
]
]
},
"type": "Feature",
"properties": {
"upperLeftCoordinate": {
"latitude": 45.66894,
"longitude": 87.91166
},
"tricky": "{gotcha"
}
}
]
}
"""
data = Feature.from_dict(**json.loads(data)["features"][0])
tmpdir = tempfile.mkdtemp()
filename = os.path.join(tmpdir, "test.json")
with fiona.open(
filename,
"w",
driver="GeoJSON",
schema={
"geometry": "Polygon",
"properties": {"upperLeftCoordinate": "str", "tricky": "str"},
},
) as dst:
dst.write(data)
with fiona.open(filename) as src:
ftr = next(iter(src))
props = ftr["properties"]
assert props["upperLeftCoordinate"]["latitude"] == 45.66894
assert props["upperLeftCoordinate"]["longitude"] == 87.91166
assert props["tricky"] == "{gotcha"
def test_json_prop_decode_non_geojson_driver():
feature = Feature.from_dict(
**{
"type": "Feature",
"properties": {
"ulc": {"latitude": 45.66894, "longitude": 87.91166},
"tricky": "{gotcha",
},
"geometry": {"type": "Point", "coordinates": [10, 15]},
}
)
meta = {
"crs": "EPSG:4326",
"driver": "ESRI Shapefile",
"schema": {
"geometry": "Point",
"properties": {"ulc": "str:255", "tricky": "str:255"},
},
}
tmpdir = tempfile.mkdtemp()
filename = os.path.join(tmpdir, "test.json")
with fiona.open(filename, "w", **meta) as dst:
dst.write(feature)
with fiona.open(filename) as src:
actual = next(iter(src))
assert isinstance(actual["properties"]["ulc"], str)
a = json.loads(actual["properties"]["ulc"])
e = json.loads(actual["properties"]["ulc"])
assert e == a
assert actual["properties"]["tricky"].startswith("{")
|