site-intelligence-studio / tests /test_kml_parser.py
Eishaan's picture
Add KML terrain and soil analysis layers
2f91d7e
Raw
History Blame Contribute Delete
2.1 kB
import tempfile
import unittest
import zipfile
from pathlib import Path
from src.kml_parser import parse_kml_file
KML_TEXT = """<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<name>Site boundary</name>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
70.2450,21.0020,0 70.2460,21.0020,0 70.2460,21.0030,0 70.2450,21.0030,0 70.2450,21.0020,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>
"""
class KmlParserTests(unittest.TestCase):
def test_kml_polygon_creates_wgs84_selection(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "site.kml"
path.write_text(KML_TEXT, encoding="utf-8")
selection = parse_kml_file(path)
self.assertEqual(selection.selection_type, "kml_boundary")
self.assertEqual(selection.coordinate_mode, "wgs84")
self.assertGreater(selection.area_sqm or 0, 0)
self.assertAlmostEqual(selection.anchor_lat or 0, 21.0024, places=3)
self.assertAlmostEqual(selection.anchor_lon or 0, 70.2454, places=3)
self.assertIn("legal/cadastral", " ".join(selection.limitations))
def test_kmz_polygon_creates_selection(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "site.kmz"
with zipfile.ZipFile(path, "w") as archive:
archive.writestr("doc.kml", KML_TEXT)
selection = parse_kml_file(path)
self.assertEqual(selection.selection_type, "kml_boundary")
self.assertEqual(selection.source_files, ["site.kmz"])
def test_kml_without_polygon_is_rejected(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "empty.kml"
path.write_text("<kml><Document /></kml>", encoding="utf-8")
with self.assertRaises(ValueError):
parse_kml_file(path)
if __name__ == "__main__":
unittest.main()