| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
"""Test the kml classes.""" |
| 18 |
|
| 19 |
import datetime |
| 20 |
|
| 21 |
import pytest |
| 22 |
from dateutil.tz import tzutc |
| 23 |
from pygeoif import geometry as geo |
| 24 |
|
| 25 |
from fastkml import atom |
| 26 |
from fastkml import features |
| 27 |
from fastkml import geometry |
| 28 |
from fastkml import links |
| 29 |
from fastkml import styles |
| 30 |
from fastkml import times |
| 31 |
from fastkml import views |
| 32 |
from tests.base import Lxml |
| 33 |
from tests.base import StdLibrary |
| 34 |
|
| 35 |
|
| 36 |
class TestStdLibrary(StdLibrary): |
| 37 |
"""Test with the standard library.""" |
| 38 |
|
| 39 |
def test_feature_base(self) -> None: |
| 40 |
f = features._Feature(name="A Feature") |
| 41 |
assert f.name == "A Feature" |
| 42 |
assert f.visibility is None |
| 43 |
assert f.isopen is None |
| 44 |
assert f.atom_author is None |
| 45 |
assert f.atom_link is None |
| 46 |
assert f.address is None |
| 47 |
assert f.snippet is None |
| 48 |
assert f.description is None |
| 49 |
assert f.styles == [] |
| 50 |
assert f.times is None |
| 51 |
assert "_Feature>" in str(f.to_string()) |
| 52 |
|
| 53 |
def test_placemark_empty_str_roundtrip(self) -> None: |
| 54 |
pm = features.Placemark() |
| 55 |
|
| 56 |
new_pm = features.Placemark.from_string(str(pm.to_string())) |
| 57 |
|
| 58 |
assert new_pm == pm |
| 59 |
|
| 60 |
def test_placemark_camera_str_roundtrip(self) -> None: |
| 61 |
camera = views.Camera( |
| 62 |
latitude=37.0, |
| 63 |
longitude=-122.0, |
| 64 |
altitude=0.0, |
| 65 |
roll=0.0, |
| 66 |
tilt=0.0, |
| 67 |
heading=0.0, |
| 68 |
) |
| 69 |
pm = features.Placemark(view=camera) |
| 70 |
|
| 71 |
new_pm = features.Placemark.from_string(str(pm.to_string())) |
| 72 |
|
| 73 |
assert new_pm == pm |
| 74 |
|
| 75 |
def test_placemark_timespan_str_roundtrip(self) -> None: |
| 76 |
time_span = times.TimeSpan( |
| 77 |
begin=times.KmlDateTime( |
| 78 |
dt=datetime.datetime( |
| 79 |
2012, |
| 80 |
3, |
| 81 |
5, |
| 82 |
0, |
| 83 |
48, |
| 84 |
32, |
| 85 |
tzinfo=tzutc(), |
| 86 |
), |
| 87 |
), |
| 88 |
end=times.KmlDateTime(dt=datetime.date(2012, 4, 5)), |
| 89 |
) |
| 90 |
pm = features.Placemark(times=time_span) |
| 91 |
|
| 92 |
new_pm = features.Placemark.from_string(str(pm.to_string())) |
| 93 |
|
| 94 |
assert new_pm == pm |
| 95 |
|
| 96 |
def test_placemark_geometry_parameter_set(self) -> None: |
| 97 |
"""Placemark object can be created with geometry parameter.""" |
| 98 |
geometry = geo.Point(10, 20) |
| 99 |
|
| 100 |
placemark = features.Placemark(geometry=geometry) |
| 101 |
|
| 102 |
assert placemark.name is None |
| 103 |
assert placemark.description is None |
| 104 |
assert placemark.visibility is None |
| 105 |
assert placemark.geometry == geometry |
| 106 |
assert placemark.style_url is None |
| 107 |
assert "Point" in placemark.to_string() |
| 108 |
|
| 109 |
def test_placemark_kml_geometry_parameter_set(self) -> None: |
| 110 |
pt = geo.Point(10, 20) |
| 111 |
point = geometry.Point(geometry=pt) |
| 112 |
|
| 113 |
placemark = features.Placemark(kml_geometry=point) |
| 114 |
|
| 115 |
assert placemark.geometry == pt |
| 116 |
|
| 117 |
def test_placemark_geometry_and_kml_geometry_parameter_set(self) -> None: |
| 118 |
pt = geo.Point(10, 20) |
| 119 |
point = geometry.Point(geometry=pt) |
| 120 |
|
| 121 |
with pytest.raises( |
| 122 |
ValueError, |
| 123 |
match=r"^You can only specify one of kml_geometry or geometry$", |
| 124 |
): |
| 125 |
features.Placemark(geometry=pt, kml_geometry=point) |
| 126 |
|
| 127 |
def test_network_link_with_link_parameter_only(self) -> None: |
| 128 |
"""Test NetworkLink object with Link parameter only.""" |
| 129 |
network_link = features.NetworkLink( |
| 130 |
link=links.Link(href="http://example.com/kml_file.kml"), |
| 131 |
) |
| 132 |
|
| 133 |
assert network_link.link.href == "http://example.com/kml_file.kml" |
| 134 |
assert bool(network_link) |
| 135 |
|
| 136 |
def test_network_link_with_no_link_parameter_only(self) -> None: |
| 137 |
"""NetworkLink object with no Link.""" |
| 138 |
network_link = features.NetworkLink(link=None) |
| 139 |
|
| 140 |
assert not bool(network_link) |
| 141 |
|
| 142 |
def test_network_link_with_optional_parameters(self) -> None: |
| 143 |
"""NetworkLink object with optional parameters.""" |
| 144 |
network_link = features.NetworkLink( |
| 145 |
name="My NetworkLink", |
| 146 |
visibility=True, |
| 147 |
isopen=False, |
| 148 |
atom_link=atom.Link(href="http://example.com/kml_file.kml"), |
| 149 |
refresh_visibility=True, |
| 150 |
fly_to_view=True, |
| 151 |
link=links.Link(href="http://example.com/kml_file.kml"), |
| 152 |
id="networklink1", |
| 153 |
target_id="target1", |
| 154 |
atom_author=atom.Author(name="John Doe"), |
| 155 |
address="123 Main St", |
| 156 |
phone_number="555-1234", |
| 157 |
snippet=features.Snippet(text="This is a snippet"), |
| 158 |
description="This is a description", |
| 159 |
view=views.LookAt(latitude=37.0, longitude=-122.0, altitude=0.0), |
| 160 |
style_url=styles.StyleUrl(url="#style1"), |
| 161 |
styles=[styles.Style(id="style1")], |
| 162 |
) |
| 163 |
|
| 164 |
assert network_link.name == "My NetworkLink" |
| 165 |
assert network_link.visibility |
| 166 |
assert not network_link.isopen |
| 167 |
assert network_link.atom_link.href == "http://example.com/kml_file.kml" |
| 168 |
assert network_link.refresh_visibility |
| 169 |
assert network_link.fly_to_view |
| 170 |
assert network_link.link.href == "http://example.com/kml_file.kml" |
| 171 |
assert network_link.id == "networklink1" |
| 172 |
assert network_link.target_id == "target1" |
| 173 |
assert network_link.atom_author.name == "John Doe" |
| 174 |
assert network_link.address == "123 Main St" |
| 175 |
assert network_link.phone_number == "555-1234" |
| 176 |
assert network_link.snippet.text == "This is a snippet" |
| 177 |
assert network_link.description == "This is a description" |
| 178 |
assert network_link.view.latitude == 37.0 |
| 179 |
assert network_link.view.longitude == -122.0 |
| 180 |
assert network_link.view.altitude == 0.0 |
| 181 |
assert network_link.style_url.url == "#style1" |
| 182 |
assert len(network_link.styles) == 1 |
| 183 |
assert network_link.styles[0].id == "style1" |
| 184 |
assert bool(network_link) |
| 185 |
|
| 186 |
def test_network_link_read(self) -> None: |
| 187 |
doc = ( |
| 188 |
'<kml:NetworkLink xmlns:atom="http://www.w3.org/2005/Atom" ' |
| 189 |
'xmlns:kml="http://www.opengis.net/kml/2.2" ' |
| 190 |
'id="networklink1" targetId="target1"><kml:name>My NetworkLink</kml:name>' |
| 191 |
"<kml:visibility>1</kml:visibility><kml:open>0</kml:open>" |
| 192 |
'<atom:link href="http://example.com/kml_file.kml" /><atom:author>' |
| 193 |
"<atom:name>John Doe</atom:name></atom:author>" |
| 194 |
"<kml:address>123 Main St</kml:address>" |
| 195 |
"<kml:phoneNumber>555-1234</kml:phoneNumber>" |
| 196 |
"<kml:Snippet>This is a snippet</kml:Snippet>" |
| 197 |
"<kml:description>This is a description</kml:description>" |
| 198 |
"<kml:LookAt><kml:longitude>-122.0</kml:longitude>" |
| 199 |
"<kml:latitude>37.0</kml:latitude><kml:altitude>0.0</kml:altitude>" |
| 200 |
"<kml:altitudeMode>relativeToGround</kml:altitudeMode></kml:LookAt>" |
| 201 |
'<kml:styleUrl>#style1</kml:styleUrl><kml:Style id="style1" />' |
| 202 |
"<kml:refreshVisibility>1</kml:refreshVisibility>" |
| 203 |
"<kml:flyToView>1</kml:flyToView>" |
| 204 |
"<kml:Link><kml:href>http://example.com/kml_file.kml</kml:href></kml:Link>" |
| 205 |
"</kml:NetworkLink>" |
| 206 |
) |
| 207 |
|
| 208 |
network_link = features.NetworkLink.from_string(doc) |
| 209 |
|
| 210 |
assert network_link.name == "My NetworkLink" |
| 211 |
assert network_link.visibility |
| 212 |
assert not network_link.isopen |
| 213 |
assert network_link.atom_link.href == "http://example.com/kml_file.kml" |
| 214 |
assert network_link.refresh_visibility |
| 215 |
assert network_link.fly_to_view |
| 216 |
assert network_link.link.href == "http://example.com/kml_file.kml" |
| 217 |
assert network_link.id == "networklink1" |
| 218 |
assert network_link.target_id == "target1" |
| 219 |
assert network_link.atom_author.name == "John Doe" |
| 220 |
assert network_link.address == "123 Main St" |
| 221 |
assert network_link.phone_number == "555-1234" |
| 222 |
assert network_link.snippet.text == "This is a snippet" |
| 223 |
assert bool(network_link.snippet) |
| 224 |
assert network_link.description == "This is a description" |
| 225 |
assert network_link.view.latitude == 37.0 |
| 226 |
assert network_link.view.longitude == -122.0 |
| 227 |
assert network_link.view.altitude == 0.0 |
| 228 |
assert network_link.style_url.url == "#style1" |
| 229 |
assert len(network_link.styles) == 1 |
| 230 |
assert network_link.styles[0].id == "style1" |
| 231 |
assert bool(network_link) |
| 232 |
|
| 233 |
|
| 234 |
class TestLxml(Lxml, TestStdLibrary): |
| 235 |
"""Test with lxml.""" |