| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
"""Test the styles classes.""" |
| 18 |
|
| 19 |
import pytest |
| 20 |
|
| 21 |
from fastkml import links |
| 22 |
from fastkml import styles |
| 23 |
from fastkml.containers import Document |
| 24 |
from fastkml.enums import ColorMode |
| 25 |
from fastkml.enums import DisplayMode |
| 26 |
from fastkml.enums import PairKey |
| 27 |
from fastkml.enums import Units |
| 28 |
from fastkml.enums import Verbosity |
| 29 |
from fastkml.exceptions import KMLParseError |
| 30 |
from fastkml.features import Placemark |
| 31 |
from tests.base import Lxml |
| 32 |
from tests.base import StdLibrary |
| 33 |
|
| 34 |
|
| 35 |
class TestStdLibrary(StdLibrary): |
| 36 |
"""Test with the standard library.""" |
| 37 |
|
| 38 |
def test_style_url(self) -> None: |
| 39 |
url = styles.StyleUrl(url="#style-0") |
| 40 |
|
| 41 |
serialized = url.to_string() |
| 42 |
|
| 43 |
assert '<kml:styleUrl xmlns:kml="http://www.opengis.net/kml/2.2"' in serialized |
| 44 |
assert ">#style-0</kml:styleUrl>" in serialized |
| 45 |
|
| 46 |
def test_style_url_read(self) -> None: |
| 47 |
url = styles.StyleUrl.from_string( |
| 48 |
'<kml:styleUrl xmlns:kml="http://www.opengis.net/kml/2.2"' |
| 49 |
' id="id-0" targetId="target-0">#style-0</kml:styleUrl>', |
| 50 |
) |
| 51 |
|
| 52 |
assert url.url == "#style-0" |
| 53 |
|
| 54 |
def test_icon_style(self) -> None: |
| 55 |
icons = styles.IconStyle( |
| 56 |
id="id-0", |
| 57 |
target_id="target-0", |
| 58 |
color="ff0000ff", |
| 59 |
color_mode=ColorMode("random"), |
| 60 |
scale=1.0, |
| 61 |
heading=0, |
| 62 |
icon=links.Icon(href="http://example.com/icon.png"), |
| 63 |
) |
| 64 |
|
| 65 |
serialized = icons.to_string() |
| 66 |
|
| 67 |
assert icons.icon_href == "http://example.com/icon.png" |
| 68 |
assert '<kml:IconStyle xmlns:kml="http://www.opengis.net/kml/2.2"' in serialized |
| 69 |
assert 'id="id-0"' in serialized |
| 70 |
assert 'targetId="target-0"' in serialized |
| 71 |
assert "<kml:color>ff0000ff</kml:color>" in serialized |
| 72 |
assert "<kml:colorMode>random</kml:colorMode>" in serialized |
| 73 |
assert "<kml:scale>1.0</kml:scale>" in serialized |
| 74 |
assert "<kml:heading>0</kml:heading>" in serialized |
| 75 |
assert "<kml:Icon>" in serialized |
| 76 |
assert "<kml:href>http://example.com/icon.png</kml:href>" in serialized |
| 77 |
assert "</kml:Icon>" in serialized |
| 78 |
assert "</kml:IconStyle>" in serialized |
| 79 |
|
| 80 |
def test_icon_style_icon_href(self) -> None: |
| 81 |
icons = styles.IconStyle( |
| 82 |
icon_href="http://example.com/icon.png", |
| 83 |
) |
| 84 |
|
| 85 |
assert icons.icon |
| 86 |
assert icons.icon.href == "http://example.com/icon.png" |
| 87 |
|
| 88 |
def test_icon_style_icon_and_href(self) -> None: |
| 89 |
icons = styles.IconStyle( |
| 90 |
icon=links.Icon(href="http://example1.com/icon.png"), |
| 91 |
icon_href="http://example2.com/icon.png", |
| 92 |
) |
| 93 |
|
| 94 |
assert icons.icon |
| 95 |
assert icons.icon.href == "http://example1.com/icon.png" |
| 96 |
|
| 97 |
def test_icon_style_with_hot_spot(self) -> None: |
| 98 |
icon_style = styles.IconStyle( |
| 99 |
ns="{http://www.opengis.net/kml/2.2}", |
| 100 |
id="id-1", |
| 101 |
target_id="target-1", |
| 102 |
color="ff2200ff", |
| 103 |
color_mode=ColorMode.random, |
| 104 |
scale=5.0, |
| 105 |
heading=20.0, |
| 106 |
icon=links.Icon( |
| 107 |
ns="{http://www.opengis.net/kml/2.2}", |
| 108 |
id="icon-id", |
| 109 |
href="", |
| 110 |
), |
| 111 |
hot_spot=styles.HotSpot( |
| 112 |
ns="{http://www.opengis.net/kml/2.2}", |
| 113 |
x=0.5, |
| 114 |
y=0.7, |
| 115 |
xunits=Units.fraction, |
| 116 |
yunits=Units.inset_pixels, |
| 117 |
), |
| 118 |
) |
| 119 |
|
| 120 |
serialized = icon_style.to_string() |
| 121 |
|
| 122 |
assert "icon-id" not in serialized |
| 123 |
assert "hotSpot" in serialized |
| 124 |
assert 'x="0.5"' in serialized |
| 125 |
assert 'y="0.7"' in serialized |
| 126 |
assert 'xunits="fraction"' in serialized |
| 127 |
assert 'yunits="insetPixels"' in serialized |
| 128 |
assert "Icon>" not in serialized |
| 129 |
assert "href" not in serialized |
| 130 |
|
| 131 |
def test_icon_style_read(self) -> None: |
| 132 |
icons = styles.IconStyle.from_string( |
| 133 |
'<kml:IconStyle xmlns:kml="http://www.opengis.net/kml/2.2" ' |
| 134 |
'id="id-1" targetId="target-1">' |
| 135 |
"<kml:color>ff2200ff</kml:color><kml:colorMode>random</kml:colorMode>" |
| 136 |
"<kml:scale>5</kml:scale><kml:heading>20</kml:heading><kml:Icon>" |
| 137 |
"<kml:href>http://example.com/icon.png</kml:href></kml:Icon>" |
| 138 |
'<kml:hotSpot x="0.5" y="0.7" xunits="fraction" yunits="insetPixels"/>' |
| 139 |
"</kml:IconStyle>", |
| 140 |
) |
| 141 |
|
| 142 |
assert icons.id == "id-1" |
| 143 |
assert icons.target_id == "target-1" |
| 144 |
assert icons.color == "ff2200ff" |
| 145 |
assert icons.color_mode == ColorMode("random") |
| 146 |
assert icons.scale == 5.0 |
| 147 |
assert icons.icon |
| 148 |
assert icons.icon.href == "http://example.com/icon.png" |
| 149 |
assert icons.heading == 20.0 |
| 150 |
assert icons.hot_spot |
| 151 |
assert icons.hot_spot.x == 0.5 |
| 152 |
assert icons.hot_spot.y == 0.7 |
| 153 |
assert icons.hot_spot.xunits |
| 154 |
assert icons.hot_spot.yunits |
| 155 |
assert icons.hot_spot.xunits.value == "fraction" |
| 156 |
assert icons.hot_spot.yunits.value == "insetPixels" |
| 157 |
|
| 158 |
def test_icon_style_with_hot_spot_enum_relaxed(self) -> None: |
| 159 |
icons = styles.IconStyle.from_string( |
| 160 |
'<kml:IconStyle xmlns:kml="http://www.opengis.net/kml/2.2" ' |
| 161 |
'id="id-1" targetId="target-1">' |
| 162 |
"<kml:color>ff2200ff</kml:color><kml:colorMode>random</kml:colorMode>" |
| 163 |
"<kml:scale>5</kml:scale><kml:heading>20</kml:heading><kml:Icon>" |
| 164 |
"<kml:href>http://example.com/icon.png</kml:href></kml:Icon>" |
| 165 |
'<kml:hotSpot x="0.5" y="0.7" xunits="Fraction" yunits="Insetpixels"/>' |
| 166 |
"</kml:IconStyle>", |
| 167 |
strict=False, |
| 168 |
) |
| 169 |
|
| 170 |
assert icons.hot_spot |
| 171 |
assert icons.hot_spot.xunits |
| 172 |
assert icons.hot_spot.yunits |
| 173 |
assert icons.hot_spot.xunits.value == "fraction" |
| 174 |
assert icons.hot_spot.yunits.value == "insetPixels" |
| 175 |
|
| 176 |
def test_icon_style_with_hot_spot_enum_strict(self) -> None: |
| 177 |
with pytest.raises(KMLParseError): |
| 178 |
styles.IconStyle.from_string( |
| 179 |
'<kml:IconStyle xmlns:kml="http://www.opengis.net/kml/2.2" ' |
| 180 |
'id="id-1" targetId="target-1">' |
| 181 |
"<kml:color>ff2200ff</kml:color><kml:colorMode>random</kml:colorMode>" |
| 182 |
"<kml:scale>5</kml:scale><kml:heading>20</kml:heading><kml:Icon>" |
| 183 |
"<kml:href>http://example.com/icon.png</kml:href></kml:Icon>" |
| 184 |
'<kml:hotSpot x="0.5" y="0.7" xunits="Fraction" yunits="Insetpixels"/>' |
| 185 |
"</kml:IconStyle>", |
| 186 |
) |
| 187 |
|
| 188 |
def test_line_style(self) -> None: |
| 189 |
lines = styles.LineStyle( |
| 190 |
id="id-0", |
| 191 |
target_id="target-0", |
| 192 |
color="ff0000ff", |
| 193 |
color_mode=ColorMode.normal, |
| 194 |
width=1.2, |
| 195 |
) |
| 196 |
|
| 197 |
serialized = lines.to_string() |
| 198 |
|
| 199 |
assert '<kml:LineStyle xmlns:kml="http://www.opengis.net/kml/2.2"' in serialized |
| 200 |
assert 'id="id-0"' in serialized |
| 201 |
assert 'targetId="target-0"' in serialized |
| 202 |
assert "<kml:color>ff0000ff</kml:color>" in serialized |
| 203 |
assert "<kml:colorMode>normal</kml:colorMode>" in serialized |
| 204 |
assert "<kml:width>1.2</kml:width>" in serialized |
| 205 |
assert "</kml:LineStyle>" in serialized |
| 206 |
|
| 207 |
def test_line_style_read(self) -> None: |
| 208 |
lines = styles.LineStyle.from_string( |
| 209 |
'<kml:LineStyle xmlns:kml="http://www.opengis.net/kml/2.2" ' |
| 210 |
'id="id-l0" targetId="target-line">\n' |
| 211 |
" <kml:color>ffaa00ff</kml:color>\n" |
| 212 |
" <kml:colorMode>normal</kml:colorMode>\n" |
| 213 |
" <kml:width>3.0</kml:width>\n" |
| 214 |
"</kml:LineStyle>\n", |
| 215 |
) |
| 216 |
|
| 217 |
assert lines.id == "id-l0" |
| 218 |
assert lines.target_id == "target-line" |
| 219 |
assert lines.color == "ffaa00ff" |
| 220 |
assert lines.color_mode == ColorMode.normal |
| 221 |
assert lines.width == 3.0 |
| 222 |
|
| 223 |
def test_line_style_to_string_terse_default(self) -> None: |
| 224 |
lines = styles.LineStyle( |
| 225 |
color="ffffffff", |
| 226 |
color_mode=ColorMode.normal, |
| 227 |
width=1.0, |
| 228 |
) |
| 229 |
|
| 230 |
serialized = lines.to_string(verbosity=Verbosity.terse) |
| 231 |
|
| 232 |
assert '<kml:LineStyle xmlns:kml="http://www.opengis.net/kml/2.2"' in serialized |
| 233 |
assert "<kml:color>ffffffff</kml:color>" not in serialized |
| 234 |
assert "<kml:colorMode>normal</kml:colorMode>" not in serialized |
| 235 |
assert "<kml:width>1.0</kml:width>" not in serialized |
| 236 |
assert not styles.LineStyle.from_string(serialized) |
| 237 |
|
| 238 |
def test_poly_style(self) -> None: |
| 239 |
ps = styles.PolyStyle( |
| 240 |
id="id-0", |
| 241 |
target_id="target-0", |
| 242 |
color="ff0000ff", |
| 243 |
color_mode=ColorMode.random, |
| 244 |
fill=False, |
| 245 |
outline=True, |
| 246 |
) |
| 247 |
|
| 248 |
serialized = ps.to_string() |
| 249 |
|
| 250 |
assert '<kml:PolyStyle xmlns:kml="http://www.opengis.net/kml/2.2"' in serialized |
| 251 |
assert 'id="id-0"' in serialized |
| 252 |
assert 'targetId="target-0"' in serialized |
| 253 |
assert "<kml:color>ff0000ff</kml:color>" in serialized |
| 254 |
assert "<kml:colorMode>random</kml:colorMode>" in serialized |
| 255 |
assert "<kml:fill>0</kml:fill>" in serialized |
| 256 |
assert "<kml:outline>1</kml:outline>" in serialized |
| 257 |
assert "</kml:PolyStyle>" in serialized |
| 258 |
|
| 259 |
def test_poly_style_read(self) -> None: |
| 260 |
ps = styles.PolyStyle.from_string( |
| 261 |
'<kml:PolyStyle xmlns:kml="http://www.opengis.net/kml/2.2" ' |
| 262 |
'id="id-1" targetId="target-1">' |
| 263 |
"<kml:color>ffaabbff</kml:color>" |
| 264 |
"<kml:colorMode>normal</kml:colorMode>" |
| 265 |
"<kml:fill>1</kml:fill>" |
| 266 |
"<kml:outline>0</kml:outline>" |
| 267 |
"</kml:PolyStyle>", |
| 268 |
) |
| 269 |
|
| 270 |
assert ps.id == "id-1" |
| 271 |
assert ps.target_id == "target-1" |
| 272 |
assert ps.color == "ffaabbff" |
| 273 |
assert ps.color_mode == ColorMode.normal |
| 274 |
assert ps.fill == 1 |
| 275 |
assert ps.outline == 0 |
| 276 |
|
| 277 |
def test_poly_style_to_string_terse_default(self) -> None: |
| 278 |
ps = styles.PolyStyle( |
| 279 |
color="ffffffff", |
| 280 |
color_mode=ColorMode.normal, |
| 281 |
fill=True, |
| 282 |
outline=True, |
| 283 |
) |
| 284 |
|
| 285 |
serialized = ps.to_string(verbosity=Verbosity.terse) |
| 286 |
|
| 287 |
assert '<kml:PolyStyle xmlns:kml="http://www.opengis.net/kml/2.2"' in serialized |
| 288 |
assert "<kml:color>ffffffff</kml:color>" not in serialized |
| 289 |
assert "<kml:colorMode>normal</kml:colorMode>" not in serialized |
| 290 |
assert "<kml:fill>1</kml:fill>" not in serialized |
| 291 |
assert "<kml:outline>1</kml:outline>" not in serialized |
| 292 |
assert not styles.PolyStyle.from_string(serialized) |
| 293 |
|
| 294 |
def test_label_style(self) -> None: |
| 295 |
ls = styles.LabelStyle( |
| 296 |
id="id-0", |
| 297 |
target_id="target-0", |
| 298 |
color="ff0000ff", |
| 299 |
color_mode=ColorMode.random, |
| 300 |
scale=1.434, |
| 301 |
) |
| 302 |
|
| 303 |
serialized = ls.to_string() |
| 304 |
|
| 305 |
assert ( |
| 306 |
'<kml:LabelStyle xmlns:kml="http://www.opengis.net/kml/2.2"' in serialized |
| 307 |
) |
| 308 |
assert 'id="id-0"' in serialized |
| 309 |
assert 'targetId="target-0"' in serialized |
| 310 |
assert "<kml:color>ff0000ff</kml:color>" in serialized |
| 311 |
assert "<kml:colorMode>random</kml:colorMode>" in serialized |
| 312 |
assert "<kml:scale>1.434</kml:scale>" in serialized |
| 313 |
assert "</kml:LabelStyle>" in serialized |
| 314 |
|
| 315 |
def test_label_style_read(self) -> None: |
| 316 |
ls = styles.LabelStyle.from_string( |
| 317 |
'<kml:LabelStyle xmlns:kml="http://www.opengis.net/kml/2.2" ' |
| 318 |
'id="id-1" targetId="target-1">' |
| 319 |
"<kml:color>ff001122</kml:color>" |
| 320 |
"<kml:colorMode>normal</kml:colorMode>" |
| 321 |
"<kml:scale>2.2</kml:scale>" |
| 322 |
"</kml:LabelStyle>", |
| 323 |
) |
| 324 |
|
| 325 |
assert ls.id == "id-1" |
| 326 |
assert ls.target_id == "target-1" |
| 327 |
assert ls.color == "ff001122" |
| 328 |
assert ls.color_mode == ColorMode.normal |
| 329 |
assert ls.scale == 2.2 |
| 330 |
|
| 331 |
def test_label_style_to_string_terse_default(self) -> None: |
| 332 |
ls = styles.LabelStyle( |
| 333 |
color="ffffffff", |
| 334 |
color_mode=ColorMode.normal, |
| 335 |
scale=1.0, |
| 336 |
) |
| 337 |
|
| 338 |
serialized = ls.to_string(verbosity=Verbosity.terse) |
| 339 |
|
| 340 |
assert ( |
| 341 |
'<kml:LabelStyle xmlns:kml="http://www.opengis.net/kml/2.2"' in serialized |
| 342 |
) |
| 343 |
assert "<kml:color>ffffffff</kml:color>" not in serialized |
| 344 |
assert "<kml:colorMode>normal</kml:colorMode>" not in serialized |
| 345 |
assert "<kml:scale>1.0</kml:scale>" not in serialized |
| 346 |
assert not styles.LabelStyle.from_string(serialized) |
| 347 |
|
| 348 |
def test_balloon_style(self) -> None: |
| 349 |
bs = styles.BalloonStyle( |
| 350 |
id="id-0", |
| 351 |
target_id="target-0", |
| 352 |
bg_color="7fff0000", |
| 353 |
text_color="ff00ff00", |
| 354 |
text="<b>Hello</b>", |
| 355 |
display_mode=DisplayMode.hide, |
| 356 |
) |
| 357 |
|
| 358 |
serialized = bs.to_string() |
| 359 |
|
| 360 |
assert ( |
| 361 |
'<kml:BalloonStyle xmlns:kml="http://www.opengis.net/kml/2.2"' in serialized |
| 362 |
) |
| 363 |
assert 'id="id-0"' in serialized |
| 364 |
assert 'targetId="target-0">' in serialized |
| 365 |
assert "<kml:bgColor>7fff0000</kml:bgColor>" in serialized |
| 366 |
assert "<kml:textColor>ff00ff00</kml:textColor>" in serialized |
| 367 |
assert "<kml:text><b>Hello</b></kml:text>" in serialized |
| 368 |
assert "<kml:displayMode>hide</kml:displayMode>" in serialized |
| 369 |
assert "</kml:BalloonStyle>" in serialized |
| 370 |
|
| 371 |
def test_balloon_style_read(self) -> None: |
| 372 |
bs = styles.BalloonStyle.from_string( |
| 373 |
'<kml:BalloonStyle xmlns:kml="http://www.opengis.net/kml/2.2" ' |
| 374 |
'id="id-7" targetId="target-6">' |
| 375 |
"<kml:bgColor>7fff1144</kml:bgColor>" |
| 376 |
"<kml:textColor>ff11ff22</kml:textColor>" |
| 377 |
"<kml:text><b>World</b></kml:text>" |
| 378 |
"<kml:displayMode>default</kml:displayMode>" |
| 379 |
"</kml:BalloonStyle>", |
| 380 |
) |
| 381 |
|
| 382 |
assert bs.id == "id-7" |
| 383 |
assert bs.target_id == "target-6" |
| 384 |
assert bs.bg_color == "7fff1144" |
| 385 |
assert bs.text_color == "ff11ff22" |
| 386 |
assert bs.text == "<b>World</b>" |
| 387 |
assert bs.display_mode == DisplayMode.default |
| 388 |
|
| 389 |
def test_style(self) -> None: |
| 390 |
icons = styles.IconStyle( |
| 391 |
id="id-i0", |
| 392 |
target_id="target-i0", |
| 393 |
color="ff0000ff", |
| 394 |
color_mode=ColorMode.random, |
| 395 |
scale=1.0, |
| 396 |
heading=0, |
| 397 |
icon=links.Icon(href="http://example.com/icon.png"), |
| 398 |
) |
| 399 |
lines = styles.LineStyle( |
| 400 |
id="id-l0", |
| 401 |
target_id="target-l0", |
| 402 |
color="ff0000ff", |
| 403 |
color_mode=ColorMode.normal, |
| 404 |
width=1.0, |
| 405 |
) |
| 406 |
bs = styles.BalloonStyle( |
| 407 |
id="id-b0", |
| 408 |
target_id="target-b0", |
| 409 |
bg_color="7fff0000", |
| 410 |
text_color="ff00ff00", |
| 411 |
text="<b>Hello</b>", |
| 412 |
display_mode=DisplayMode.hide, |
| 413 |
) |
| 414 |
ls = styles.LabelStyle( |
| 415 |
id="id-a0", |
| 416 |
target_id="target-a0", |
| 417 |
color="ff0000ff", |
| 418 |
color_mode=ColorMode.random, |
| 419 |
scale=1.0, |
| 420 |
) |
| 421 |
ps = styles.PolyStyle( |
| 422 |
id="id-p0", |
| 423 |
target_id="target-p0", |
| 424 |
color="ff0000ff", |
| 425 |
color_mode=ColorMode.random, |
| 426 |
fill=False, |
| 427 |
outline=True, |
| 428 |
) |
| 429 |
style = styles.Style( |
| 430 |
id="id-0", |
| 431 |
target_id="target-0", |
| 432 |
styles=[icons, lines, bs, ls, ps], |
| 433 |
) |
| 434 |
|
| 435 |
serialized = style.to_string() |
| 436 |
|
| 437 |
assert '<kml:Style xmlns:kml="http://www.opengis.net/kml/2.2" ' in serialized |
| 438 |
assert 'id="id-0" ' in serialized |
| 439 |
assert 'targetId="target-0">' in serialized |
| 440 |
assert "<kml:IconStyle" in serialized |
| 441 |
assert "<kml:Icon>" in serialized |
| 442 |
assert "</kml:Icon>" in serialized |
| 443 |
assert "</kml:IconStyle>" in serialized |
| 444 |
assert "<kml:LineStyle" in serialized |
| 445 |
assert "<kml:BalloonStyle " in serialized |
| 446 |
assert "<kml:LabelStyle " in serialized |
| 447 |
assert "<kml:PolyStyle " in serialized |
| 448 |
assert "</kml:PolyStyle>" in serialized |
| 449 |
assert "</kml:Style>" in serialized |
| 450 |
|
| 451 |
def test_style_read(self) -> None: |
| 452 |
style = styles.Style.from_string( |
| 453 |
'<kml:Style xmlns:kml="http://www.opengis.net/kml/2.2" ' |
| 454 |
'id="id-0" targetId="target-0">' |
| 455 |
'<kml:IconStyle id="id-i0" targetId="target-i0">' |
| 456 |
"<kml:color>ff0000ff</kml:color>" |
| 457 |
"<kml:colorMode>random</kml:colorMode>" |
| 458 |
"<kml:scale>1.0</kml:scale>" |
| 459 |
"<kml:heading>0</kml:heading>" |
| 460 |
"<kml:Icon>" |
| 461 |
"<kml:href>http://example.com/icon.png</kml:href>" |
| 462 |
"</kml:Icon>" |
| 463 |
"</kml:IconStyle>" |
| 464 |
'<kml:LineStyle id="id-l0" targetId="target-l0">' |
| 465 |
"<kml:color>ff0000ff</kml:color>" |
| 466 |
"<kml:colorMode>normal</kml:colorMode>" |
| 467 |
"<kml:width>1.0</kml:width>" |
| 468 |
"</kml:LineStyle>" |
| 469 |
'<kml:BalloonStyle id="id-b0" targetId="target-b0">' |
| 470 |
"<kml:bgColor>7fff0000</kml:bgColor>" |
| 471 |
"<kml:textColor>ff00ff00</kml:textColor>" |
| 472 |
"<kml:text><b>Hello</b></kml:text>" |
| 473 |
"<kml:displayMode>hide</kml:displayMode>" |
| 474 |
"</kml:BalloonStyle>" |
| 475 |
'<kml:LabelStyle id="id-a0" targetId="target-a0">' |
| 476 |
"<kml:color>ff0000ff</kml:color>" |
| 477 |
"<kml:colorMode>random</kml:colorMode>" |
| 478 |
"<kml:scale>1.0</kml:scale>" |
| 479 |
"</kml:LabelStyle>" |
| 480 |
'<kml:PolyStyle id="id-p0" targetId="target-p0">' |
| 481 |
"<kml:color>ff0000ff</kml:color>" |
| 482 |
"<kml:colorMode>random</kml:colorMode>" |
| 483 |
"<kml:fill>0</kml:fill>" |
| 484 |
"<kml:outline>1</kml:outline>" |
| 485 |
"</kml:PolyStyle>" |
| 486 |
"</kml:Style>", |
| 487 |
) |
| 488 |
|
| 489 |
assert style.id == "id-0" |
| 490 |
assert style.target_id == "target-0" |
| 491 |
|
| 492 |
assert isinstance(style.styles[0], styles.IconStyle) |
| 493 |
assert style.styles[0].id == "id-i0" |
| 494 |
assert style.styles[0].target_id == "target-i0" |
| 495 |
assert style.styles[0].color == "ff0000ff" |
| 496 |
assert style.styles[0].color_mode == ColorMode.random |
| 497 |
assert style.styles[0].scale == 1.0 |
| 498 |
assert style.styles[0].heading == 0 |
| 499 |
assert style.styles[0].icon |
| 500 |
assert style.styles[0].icon.href == "http://example.com/icon.png" |
| 501 |
|
| 502 |
assert isinstance(style.styles[1], styles.LabelStyle) |
| 503 |
assert style.styles[1].id == "id-a0" |
| 504 |
assert style.styles[1].target_id == "target-a0" |
| 505 |
assert style.styles[1].color == "ff0000ff" |
| 506 |
assert style.styles[1].color_mode == ColorMode.random |
| 507 |
assert style.styles[1].scale == 1.0 |
| 508 |
|
| 509 |
assert isinstance(style.styles[2], styles.LineStyle) |
| 510 |
assert style.styles[2].id == "id-l0" |
| 511 |
assert style.styles[2].target_id == "target-l0" |
| 512 |
assert style.styles[2].color == "ff0000ff" |
| 513 |
assert style.styles[2].color_mode == ColorMode.normal |
| 514 |
assert style.styles[2].width == 1.0 |
| 515 |
|
| 516 |
assert isinstance(style.styles[3], styles.PolyStyle) |
| 517 |
assert style.styles[3].id == "id-p0" |
| 518 |
assert style.styles[3].target_id == "target-p0" |
| 519 |
assert style.styles[3].color == "ff0000ff" |
| 520 |
assert style.styles[3].color_mode == ColorMode.random |
| 521 |
assert style.styles[3].fill == 0 |
| 522 |
assert style.styles[3].outline == 1 |
| 523 |
|
| 524 |
assert isinstance(style.styles[4], styles.BalloonStyle) |
| 525 |
assert style.styles[4].id == "id-b0" |
| 526 |
assert style.styles[4].target_id == "target-b0" |
| 527 |
assert style.styles[4].bg_color == "7fff0000" |
| 528 |
assert style.styles[4].text_color == "ff00ff00" |
| 529 |
assert style.styles[4].text == "<b>Hello</b>" |
| 530 |
assert style.styles[4].display_mode == DisplayMode.hide |
| 531 |
|
| 532 |
def test_stylemap(self) -> None: |
| 533 |
url = styles.StyleUrl(url="#style-0") |
| 534 |
icons = styles.IconStyle( |
| 535 |
id="id-i0", |
| 536 |
target_id="target-i0", |
| 537 |
color="ff0000ff", |
| 538 |
color_mode=ColorMode.random, |
| 539 |
scale=1.0, |
| 540 |
heading=0, |
| 541 |
icon=links.Icon(href="http://example.com/icon.png"), |
| 542 |
) |
| 543 |
lines = styles.LineStyle( |
| 544 |
id="id-l0", |
| 545 |
target_id="target-l0", |
| 546 |
color="ff0000ff", |
| 547 |
color_mode=ColorMode.normal, |
| 548 |
width=1.0, |
| 549 |
) |
| 550 |
bs = styles.BalloonStyle( |
| 551 |
id="id-b0", |
| 552 |
target_id="target-b0", |
| 553 |
bg_color="7fff0000", |
| 554 |
text_color="ff00ff00", |
| 555 |
text="<b>Hello</b>", |
| 556 |
display_mode=DisplayMode.hide, |
| 557 |
) |
| 558 |
ls = styles.LabelStyle( |
| 559 |
id="id-a0", |
| 560 |
target_id="target-a0", |
| 561 |
color="ff0000ff", |
| 562 |
color_mode=ColorMode.random, |
| 563 |
scale=1.0, |
| 564 |
) |
| 565 |
ps = styles.PolyStyle( |
| 566 |
id="id-p0", |
| 567 |
target_id="target-p0", |
| 568 |
color="ff0000ff", |
| 569 |
color_mode=ColorMode.random, |
| 570 |
fill=False, |
| 571 |
outline=True, |
| 572 |
) |
| 573 |
style = styles.Style( |
| 574 |
id="id-0", |
| 575 |
target_id="target-0", |
| 576 |
styles=[icons, lines, bs, ls, ps], |
| 577 |
) |
| 578 |
sm = styles.StyleMap( |
| 579 |
id="id-sm-0", |
| 580 |
target_id="target-sm-0", |
| 581 |
pairs=[ |
| 582 |
styles.Pair(key=PairKey.normal, style=url), |
| 583 |
styles.Pair(key=PairKey.highlight, style=style), |
| 584 |
], |
| 585 |
) |
| 586 |
|
| 587 |
serialized = sm.to_string() |
| 588 |
|
| 589 |
assert '<kml:StyleMap xmlns:kml="http://www.opengis.net/kml/2.2" ' in serialized |
| 590 |
assert "<kml:Pair>" in serialized |
| 591 |
assert "<kml:key>normal</kml:key>" in serialized |
| 592 |
assert "<kml:styleUrl" in serialized |
| 593 |
assert "</kml:Pair>" in serialized |
| 594 |
assert "<kml:Pair>" in serialized |
| 595 |
assert "<kml:key>highlight</kml:key>" in serialized |
| 596 |
assert "<kml:Style " in serialized |
| 597 |
assert "<kml:IconStyle " in serialized |
| 598 |
assert "<kml:color>ff0000ff</kml:color>" in serialized |
| 599 |
assert "<kml:colorMode>random</kml:colorMode>" in serialized |
| 600 |
assert "<kml:scale>1.0</kml:scale>" in serialized |
| 601 |
assert "<kml:heading>0</kml:heading>" in serialized |
| 602 |
assert "<kml:Icon>" in serialized |
| 603 |
assert "<kml:href>http://example.com/icon.png</kml:href>" in serialized |
| 604 |
assert "</kml:Icon>" in serialized |
| 605 |
assert "</kml:IconStyle>" in serialized |
| 606 |
assert "<kml:LineStyle " in serialized |
| 607 |
assert "<kml:color>ff0000ff</kml:color>" in serialized |
| 608 |
assert "<kml:colorMode>normal</kml:colorMode>" in serialized |
| 609 |
assert "<kml:width>1.0</kml:width>" in serialized |
| 610 |
assert "</kml:LineStyle>" in serialized |
| 611 |
assert "<kml:BalloonStyle " in serialized |
| 612 |
assert "<kml:bgColor>7fff0000</kml:bgColor>" in serialized |
| 613 |
assert "<kml:textColor>ff00ff00</kml:textColor>" in serialized |
| 614 |
assert "<kml:text><b>Hello</b></kml:text>" in serialized |
| 615 |
assert "<kml:displayMode>hide</kml:displayMode>" in serialized |
| 616 |
assert "</kml:BalloonStyle>" in serialized |
| 617 |
assert "<kml:LabelStyle " in serialized |
| 618 |
assert "<kml:color>ff0000ff</kml:color>" in serialized |
| 619 |
assert "<kml:colorMode>random</kml:colorMode>" in serialized |
| 620 |
assert "<kml:scale>1.0</kml:scale>" in serialized |
| 621 |
assert "</kml:LabelStyle>" in serialized |
| 622 |
assert "<kml:PolyStyle " in serialized |
| 623 |
assert "<kml:color>ff0000ff</kml:color>" in serialized |
| 624 |
assert "<kml:colorMode>random</kml:colorMode>" in serialized |
| 625 |
assert "<kml:fill>0</kml:fill>" in serialized |
| 626 |
assert "<kml:outline>1</kml:outline>" in serialized |
| 627 |
assert "</kml:PolyStyle>" in serialized |
| 628 |
assert "</kml:Style>" in serialized |
| 629 |
assert "</kml:Pair>" in serialized |
| 630 |
assert "</kml:StyleMap>" in serialized |
| 631 |
|
| 632 |
def test_stylemap_read(self) -> None: |
| 633 |
sm = styles.StyleMap.from_string( |
| 634 |
""" |
| 635 |
<kml:StyleMap xmlns:kml="http://www.opengis.net/kml/2.2" |
| 636 |
id="id-sm-0" targetId="target-sm-0"> |
| 637 |
<kml:Pair> |
| 638 |
<kml:key>highlight</kml:key> |
| 639 |
<kml:styleUrl id="id-u0" targetId="target-u0">#style-0</kml:styleUrl> |
| 640 |
</kml:Pair> |
| 641 |
<kml:Pair> |
| 642 |
<kml:key>normal</kml:key> |
| 643 |
<kml:Style id="id-0" targetId="target-0"> |
| 644 |
<kml:IconStyle id="id-i0" targetId="target-i0"> |
| 645 |
<kml:color>ff0000ff</kml:color> |
| 646 |
<kml:colorMode>random</kml:colorMode> |
| 647 |
<kml:scale>1.0</kml:scale> |
| 648 |
<kml:heading>0</kml:heading> |
| 649 |
<kml:Icon> |
| 650 |
<kml:href>http://example.com/icon.png</kml:href> |
| 651 |
</kml:Icon> |
| 652 |
</kml:IconStyle> |
| 653 |
<kml:LineStyle id="id-l0" targetId="target-l0"> |
| 654 |
<kml:color>ff0000ff</kml:color> |
| 655 |
<kml:colorMode>normal</kml:colorMode> |
| 656 |
<kml:width>1.0</kml:width> |
| 657 |
</kml:LineStyle> |
| 658 |
<kml:BalloonStyle id="id-b0" targetId="target-b0"> |
| 659 |
<kml:bgColor>7fff0000</kml:bgColor> |
| 660 |
<kml:textColor>ff00ff00</kml:textColor> |
| 661 |
<kml:text><b>Hello</b></kml:text> |
| 662 |
<kml:displayMode>hide</kml:displayMode> |
| 663 |
</kml:BalloonStyle> |
| 664 |
<kml:LabelStyle id="id-a0" targetId="target-a0"> |
| 665 |
<kml:color>ff0000ff</kml:color> |
| 666 |
<kml:colorMode>random</kml:colorMode> |
| 667 |
<kml:scale>1.0</kml:scale> |
| 668 |
</kml:LabelStyle> |
| 669 |
<kml:PolyStyle id="id-p0" targetId="target-p0"> |
| 670 |
<kml:color>ff0000ff</kml:color> |
| 671 |
<kml:colorMode>random</kml:colorMode> |
| 672 |
<kml:fill>0</kml:fill> |
| 673 |
<kml:outline>1</kml:outline> |
| 674 |
</kml:PolyStyle> |
| 675 |
</kml:Style> |
| 676 |
</kml:Pair> |
| 677 |
</kml:StyleMap> |
| 678 |
""", |
| 679 |
) |
| 680 |
|
| 681 |
assert sm.id == "id-sm-0" |
| 682 |
assert sm.target_id == "target-sm-0" |
| 683 |
assert sm.normal.id == "id-0" |
| 684 |
assert sm.normal.target_id == "target-0" |
| 685 |
|
| 686 |
def test_style_map_none_case(self) -> None: |
| 687 |
sm = styles.StyleMap() |
| 688 |
|
| 689 |
assert sm.normal is None |
| 690 |
assert sm.highlight is None |
| 691 |
|
| 692 |
|
| 693 |
class TestStyleUsage: |
| 694 |
def test_create_document_style(self) -> None: |
| 695 |
style = styles.Style( |
| 696 |
styles=[ |
| 697 |
styles.PolyStyle( |
| 698 |
color="7f000000", |
| 699 |
fill=True, |
| 700 |
outline=True, |
| 701 |
), |
| 702 |
], |
| 703 |
) |
| 704 |
|
| 705 |
doc = Document(styles=[style]) |
| 706 |
|
| 707 |
doc2 = Document() |
| 708 |
doc2.styles.append(style) |
| 709 |
|
| 710 |
expected = """ |
| 711 |
<kml:Document xmlns:kml="http://www.opengis.net/kml/2.2"> |
| 712 |
<kml:visibility/> |
| 713 |
<kml:Style> |
| 714 |
<kml:PolyStyle> |
| 715 |
<kml:color>7f000000</kml:color> |
| 716 |
<kml:fill>1</kml:fill> |
| 717 |
<kml:outline>1</kml:outline> |
| 718 |
</kml:PolyStyle> |
| 719 |
</kml:Style> |
| 720 |
</kml:Document> |
| 721 |
""" |
| 722 |
|
| 723 |
doc3 = Document.from_string(expected) |
| 724 |
|
| 725 |
assert doc.to_string() == doc2.to_string() |
| 726 |
assert doc2.to_string() == doc3.to_string() |
| 727 |
assert doc.to_string() == doc3.to_string() |
| 728 |
|
| 729 |
def test_create_placemark_style(self) -> None: |
| 730 |
style = styles.Style( |
| 731 |
styles=[ |
| 732 |
styles.PolyStyle( |
| 733 |
color="7f000000", |
| 734 |
fill=True, |
| 735 |
outline=True, |
| 736 |
), |
| 737 |
], |
| 738 |
) |
| 739 |
|
| 740 |
place = Placemark(styles=[style]) |
| 741 |
|
| 742 |
place2 = Placemark() |
| 743 |
place2.styles.append(style) |
| 744 |
|
| 745 |
expected = """ |
| 746 |
<kml:Placemark xmlns:kml="http://www.opengis.net/kml/2.2"> |
| 747 |
<kml:Style> |
| 748 |
<kml:PolyStyle> |
| 749 |
<kml:color>7f000000</kml:color> |
| 750 |
<kml:fill>1</kml:fill> |
| 751 |
<kml:outline>1</kml:outline> |
| 752 |
</kml:PolyStyle> |
| 753 |
</kml:Style> |
| 754 |
</kml:Placemark> |
| 755 |
""" |
| 756 |
|
| 757 |
place3 = Placemark.from_string(expected) |
| 758 |
assert place.to_string() == place2.to_string() |
| 759 |
assert place2.to_string() == place3.to_string() |
| 760 |
assert place.to_string() == place3.to_string() |
| 761 |
|
| 762 |
|
| 763 |
class TestLxml(Lxml, TestStdLibrary): |
| 764 |
"""Test with lxml.""" |