Skip to content

Commit

Permalink
Merge branch 'master' into loop_consolidation
Browse files Browse the repository at this point in the history
  • Loading branch information
dericke authored Dec 15, 2020
2 parents da750f9 + 5d2ef77 commit ceb9ba9
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 7 deletions.
55 changes: 55 additions & 0 deletions fastkml/gx.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,60 @@
located at http://developers.google.com/kml/schema/kml22gx.xsd.
"""

try:
from shapely.geometry.linestring import LineString
from shapely.geometry.multilinestring import MultiLineString

except ImportError:
from pygeoif.geometry import LineString, MultiLineString

from pygeoif.geometry import GeometryCollection

from .config import GXNS as NS
from .geometry import Geometry

import logging
logger = logging.getLogger('fastkml.gx')


class GxGeometry(Geometry):

def __init__(
self, ns=None, id=None,
):
"""
gxgeometry: a read-only subclass of geometry supporting gx: features,
like gx:Track
"""
super(GxGeometry, self).__init__(ns, id)
self.ns = NS if ns is None else ns

def _get_geometry(self, element):
# Track
if element.tag == ('%sTrack' % self.ns):
coords = self._get_coordinates(element)
self._get_geometry_spec(element)
return LineString(coords)

def _get_multigeometry(self, element):
# MultiTrack
geoms = []
if element.tag == ('%sMultiTrack' % self.ns):
tracks = element.findall("%sTrack" % self.ns)
for track in tracks:
self._get_geometry_spec(track)
geoms.append(LineString(self._get_coordinates(track)))

geom_types = {geom.geom_type for geom in geoms}
if len(geom_types) > 1:
return GeometryCollection(geoms)
if 'LineString' in geom_types:
return MultiLineString(geoms)

def _get_coordinates(self, element):
coordinates = element.findall('%scoord' % self.ns)
if coordinates is not None:
return [
[float(c) for c in coord.text.strip().split()]
for coord in coordinates
]
20 changes: 14 additions & 6 deletions fastkml/kml.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
from .styles import StyleUrl, Style, StyleMap, _StyleSelector

import fastkml.atom as atom
# import fastkml.gx as gx
import fastkml.gx as gx
import fastkml.config as config

try:
Expand Down Expand Up @@ -1083,6 +1083,18 @@ def from_element(self, element):
geom.from_element(multigeometry)
self._geometry = geom
return
track = element.find("%sTrack" % gx.NS)
if track is not None:
geom = gx.GxGeometry(ns=gx.NS)
geom.from_element(track)
self._geometry = geom
return
multitrack = element.find("%sMultiTrack" % gx.NS)
if line is not None:
geom = gx.GxGeometry(ns=gx.NS)
geom.from_element(multitrack)
self._geometry = geom
return

logger.warn('No geometries found')
logger.debug(u'Problem with element: {}'.format(etree.tostring(element)))
Expand Down Expand Up @@ -1340,11 +1352,7 @@ def from_element(self, element):
sfname = simple_field.get('name')
sftype = simple_field.get('type')
display_name = simple_field.find('%sdisplayName' % self.ns)
sfdisplay_name = (
display_name.text
if display_name is not None
else None
)
sfdisplay_name = display_name.text if display_name is not None else None
self.append(sftype, sfname, sfdisplay_name)

def etree_element(self):
Expand Down
45 changes: 44 additions & 1 deletion fastkml/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from fastkml import base
from fastkml import atom
from fastkml import config
from fastkml import gx # NOQA

import datetime
from dateutil.tz import tzutc, tzoffset
Expand All @@ -37,6 +36,8 @@
from fastkml.geometry import LinearRing, GeometryCollection
from fastkml.geometry import Geometry

from fastkml.gx import GxGeometry


class BaseClassesTestCase(unittest.TestCase):
""" BaseClasses must raise a NotImplementedError on etree_element
Expand Down Expand Up @@ -2124,6 +2125,47 @@ def test_geometrycollection(self):
self.assertEqual(g.geometry.geom_type, 'GeometryCollection')


class GetGxGeometryTestCase(unittest.TestCase):

def test_track(self):
doc = """<gx:Track xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
<when>2020-01-01T00:00:00Z</when>
<when>2020-01-01T00:10:00Z</when>
<gx:coord>0.000000 0.000000</gx:coord>
<gx:coord>1.000000 1.000000</gx:coord>
</gx:Track>"""

g = GxGeometry()
g.from_string(doc)
self.assertEqual(
g.geometry.__geo_interface__,
{
'type': 'LineString',
'coordinates': ((0.0, 0.0), (1.0, 1.0))})

def test_multitrack(self):
doc = """
<gx:MultiTrack xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
<gx:Track>
<when>2020-01-01T00:00:00Z</when>
<when>2020-01-01T00:10:00Z</when>
<gx:coord>0.000000 0.000000</gx:coord>
<gx:coord>1.000000 0.000000</gx:coord>
</gx:Track>
<gx:Track>
<when>2020-01-01T00:10:00Z</when>
<when>2020-01-01T00:20:00Z</when>
<gx:coord>0.000000 1.000000</gx:coord>
<gx:coord>1.000000 1.000000</gx:coord>
</gx:Track>
</gx:MultiTrack>
"""

g = GxGeometry()
g.from_string(doc)
self.assertEqual(len(g.geometry), 2)


class Force3DTestCase(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -2588,6 +2630,7 @@ def test_suite():
suite.addTest(unittest.makeSuite(AtomTestCase))
suite.addTest(unittest.makeSuite(SetGeometryTestCase))
suite.addTest(unittest.makeSuite(GetGeometryTestCase))
suite.addTest(unittest.makeSuite(GetGxGeometryTestCase))
suite.addTest(unittest.makeSuite(Force3DTestCase))
suite.addTest(unittest.makeSuite(BaseOverlayTestCase))
suite.addTest(unittest.makeSuite(GroundOverlayTestCase))
Expand Down

0 comments on commit ceb9ba9

Please sign in to comment.