| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
"""Test the kml overlay classes.""" |
| 18 |
|
| 19 |
from pygeoif.geometry import Point |
| 20 |
|
| 21 |
import fastkml.links |
| 22 |
import fastkml.model |
| 23 |
from fastkml.enums import AltitudeMode |
| 24 |
from fastkml.enums import Verbosity |
| 25 |
from tests.base import Lxml |
| 26 |
from tests.base import StdLibrary |
| 27 |
|
| 28 |
|
| 29 |
class TestModel(StdLibrary): |
| 30 |
def test_from_string(self) -> None: |
| 31 |
doc = ( |
| 32 |
'<Model xmlns="http://www.opengis.net/kml/2.2">' |
| 33 |
"<altitudeMode>absolute</altitudeMode>" |
| 34 |
"<Location>" |
| 35 |
"<longitude>-123.115776547816</longitude>" |
| 36 |
"<latitude>49.279804095564</latitude>" |
| 37 |
"<altitude>21.614010375743</altitude>" |
| 38 |
"</Location>" |
| 39 |
"<Scale><x>1</x><y>1</y><z>1</z></Scale>" |
| 40 |
"<Link><href>http://barcelona.galdos.local/files/PublicLibrary.dae</href></Link>" |
| 41 |
'<ResourceMap id="map01">' |
| 42 |
"<Alias>" |
| 43 |
"<targetHref>http://barcelona.galdos.local/images/Concrete2.jpg</targetHref>" |
| 44 |
"<sourceHref>../images/Concrete.jpg</sourceHref>" |
| 45 |
"</Alias>" |
| 46 |
"</ResourceMap>" |
| 47 |
"</Model>" |
| 48 |
) |
| 49 |
|
| 50 |
model = fastkml.model.Model.from_string(doc) |
| 51 |
|
| 52 |
assert model.altitude_mode == AltitudeMode.absolute |
| 53 |
assert model.geometry == Point( |
| 54 |
-123.115776547816, |
| 55 |
49.279804095564, |
| 56 |
21.614010375743, |
| 57 |
) |
| 58 |
assert model == fastkml.model.Model( |
| 59 |
altitude_mode=AltitudeMode.absolute, |
| 60 |
location=fastkml.model.Location( |
| 61 |
altitude=21.614010375743, |
| 62 |
latitude=49.279804095564, |
| 63 |
longitude=-123.115776547816, |
| 64 |
), |
| 65 |
orientation=None, |
| 66 |
scale=fastkml.model.Scale( |
| 67 |
x=1.0, |
| 68 |
y=1.0, |
| 69 |
z=1.0, |
| 70 |
), |
| 71 |
link=fastkml.links.Link( |
| 72 |
href="http://barcelona.galdos.local/files/PublicLibrary.dae", |
| 73 |
), |
| 74 |
resource_map=fastkml.model.ResourceMap( |
| 75 |
id="map01", |
| 76 |
aliases=[ |
| 77 |
fastkml.model.Alias( |
| 78 |
target_href="http://barcelona.galdos.local/images/Concrete2.jpg", |
| 79 |
source_href="../images/Concrete.jpg", |
| 80 |
), |
| 81 |
], |
| 82 |
), |
| 83 |
) |
| 84 |
|
| 85 |
def test_from_string_no_location(self) -> None: |
| 86 |
doc = ( |
| 87 |
'<Model xmlns="http://www.opengis.net/kml/2.2">' |
| 88 |
"<altitudeMode>absolute</altitudeMode>" |
| 89 |
"<Scale><x>1</x><y>1</y><z>1</z></Scale>" |
| 90 |
"<Link><href>http://barcelona.galdos.local/files/PublicLibrary.dae</href></Link>" |
| 91 |
'<ResourceMap id="map01">' |
| 92 |
"<Alias>" |
| 93 |
"<targetHref>http://barcelona.galdos.local/images/Concrete2.jpg</targetHref>" |
| 94 |
"<sourceHref>../images/Concrete.jpg</sourceHref>" |
| 95 |
"</Alias>" |
| 96 |
"</ResourceMap>" |
| 97 |
"</Model>" |
| 98 |
) |
| 99 |
|
| 100 |
model = fastkml.model.Model.from_string(doc) |
| 101 |
|
| 102 |
assert model.altitude_mode == AltitudeMode.absolute |
| 103 |
assert model.geometry is None |
| 104 |
assert not model |
| 105 |
|
| 106 |
def test_from_string_invalid_location(self) -> None: |
| 107 |
doc = ( |
| 108 |
'<Model xmlns="http://www.opengis.net/kml/2.2">' |
| 109 |
"<altitudeMode>absolute</altitudeMode>" |
| 110 |
"<Location>" |
| 111 |
"<longitude></longitude>" |
| 112 |
"<latitude>49.279804095564</latitude>" |
| 113 |
"<altitude>21.614010375743</altitude>" |
| 114 |
"</Location>" |
| 115 |
"<Scale><x>1</x><y>1</y><z>1</z></Scale>" |
| 116 |
"<Link><href>http://barcelona.galdos.local/files/PublicLibrary.dae</href></Link>" |
| 117 |
'<ResourceMap id="map01">' |
| 118 |
"<Alias>" |
| 119 |
"<targetHref>http://barcelona.galdos.local/images/Concrete2.jpg</targetHref>" |
| 120 |
"<sourceHref>../images/Concrete.jpg</sourceHref>" |
| 121 |
"</Alias>" |
| 122 |
"</ResourceMap>" |
| 123 |
"</Model>" |
| 124 |
) |
| 125 |
|
| 126 |
model = fastkml.model.Model.from_string(doc) |
| 127 |
|
| 128 |
assert model.altitude_mode == AltitudeMode.absolute |
| 129 |
assert model.geometry is None |
| 130 |
assert not model |
| 131 |
|
| 132 |
def test_location_from_string_invalid_location(self) -> None: |
| 133 |
doc = ( |
| 134 |
'<Location xmlns="http://www.opengis.net/kml/2.2">' |
| 135 |
"<longitude></longitude>" |
| 136 |
"<latitude>49.279804095564</latitude>" |
| 137 |
"<altitude>21.614010375743</altitude>" |
| 138 |
"</Location>" |
| 139 |
) |
| 140 |
|
| 141 |
location = fastkml.model.Location.from_string(doc) |
| 142 |
|
| 143 |
assert location.altitude == 21.614010375743 |
| 144 |
assert location.latitude == 49.279804095564 |
| 145 |
assert location.geometry is None |
| 146 |
assert not location |
| 147 |
|
| 148 |
def test_scale_to_string_terse_default(self) -> None: |
| 149 |
scale = fastkml.model.Scale(x=1.0, y=1.0, z=1.0) |
| 150 |
xml = scale.to_string(verbosity=Verbosity.terse) |
| 151 |
|
| 152 |
assert "x>1.0</" not in xml |
| 153 |
assert "y>1.0</" not in xml |
| 154 |
assert "z>1.0</" not in xml |
| 155 |
assert not fastkml.model.Scale.from_string(xml) |
| 156 |
|
| 157 |
|
| 158 |
class TestModelLxml(TestModel, Lxml): |
| 159 |
pass |