fastkml
fastkml copied to clipboard
empty coordinates
I have a KML looking like the following:
<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.opengis.net/kml/2.2">
<Folder>
<Placemark>
<name/>
<description/>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates></coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Folder>
</kml>
I know this is stupid but I was sent one looking like that by a customer and it's parsed with no exception in OpenLayers. It however raises an error in faskml. Can something be done to handle geometries with empty coordinates or at least prevent the code from failing?
which error? trace back?
Here's what I get when trying a from_string
on the document.
Traceback (most recent call last):
File "read.py", line 11, in <module>
k.from_string(doc)
File "/tmp/env/local/lib/python2.7/site-packages/fastkml/kml.py", line 106, in from_string
feature.from_element(folder)
File "/tmp/env/local/lib/python2.7/site-packages/fastkml/kml.py", line 1035, in from_element
feature.from_element(placemark)
File "/tmp/env/local/lib/python2.7/site-packages/fastkml/kml.py", line 1083, in from_element
geom.from_element(polygon)
File "/tmp/env/local/lib/python2.7/site-packages/fastkml/geometry.py", line 413, in from_element
geom = self._get_geometry(element)
File "/tmp/env/local/lib/python2.7/site-packages/fastkml/geometry.py", line 351, in _get_geometry
ob = self._get_linear_ring(outer_boundary)
File "/tmp/env/local/lib/python2.7/site-packages/fastkml/geometry.py", line 334, in _get_linear_ring
coords = self._get_coordinates(lr)
File "/tmp/env/local/lib/python2.7/site-packages/fastkml/geometry.py", line 324, in _get_coordinates
latlons = re.sub(r', +', ',', coordinates.text.strip()).split()
AttributeError: 'NoneType' object has no attribute 'strip'
Also if I put a line break between coordinates
opening and closing tag, I then get the following error:
File "read.py", line 11, in <module>
k.from_string(doc)
File "/tmp/env/local/lib/python2.7/site-packages/fastkml/kml.py", line 106, in from_string
feature.from_element(folder)
File "/tmp/env/local/lib/python2.7/site-packages/fastkml/kml.py", line 1035, in from_element
feature.from_element(placemark)
File "/tmp/env/local/lib/python2.7/site-packages/fastkml/kml.py", line 1083, in from_element
geom.from_element(polygon)
File "/tmp/env/local/lib/python2.7/site-packages/fastkml/geometry.py", line 413, in from_element
geom = self._get_geometry(element)
File "/tmp/env/local/lib/python2.7/site-packages/fastkml/geometry.py", line 351, in _get_geometry
ob = self._get_linear_ring(outer_boundary)
File "/tmp/env/local/lib/python2.7/site-packages/fastkml/geometry.py", line 335, in _get_linear_ring
return LinearRing(coords)
File "/tmp/env/local/lib/python2.7/site-packages/shapely/geometry/polygon.py", line 53, in __init__
self._set_coords(coordinates)
File "/tmp/env/local/lib/python2.7/site-packages/shapely/geometry/polygon.py", line 68, in _set_coords
self._geom, self._ndim = geos_linearring_from_py(coordinates)
File "/tmp/env/local/lib/python2.7/site-packages/shapely/geometry/polygon.py", line 445, in geos_linearring_from_py
n = len(ob[0])
What would be a disireable test for this?
What about creating one (in test_main.py
), with the original OP sample code, and make it give the desired result (be it an exception or something else)?
-
The KML schema only mentions
coordinatesType
as being of base typestring
, without any specific mask or facet. -
Google KML Reference mentions the need to have a minimum amount of coordinate tuples, and as such, an empty coordinate string should be considered an Exception.
-
The OGC KML Standard requires
coordinates
to be:String representing one or more coordinate tuples
...and as such, an empty string would be also an error.
As a final remark, there are some other parsers that save "non-recognized elements" in a separate list, so that when you save back to disk (possibly after editing some recognized elements), the original, unrecognized file content is not lost. That is, IMO, a very desireable feature.
Location history files downloaded from Google include numerous empty tags, all of which result in "AttributeError: 'NoneType' object has no attribute 'strip'". If Google can't be bothered to maintain compliance with their nominal standards, I think that it makes sense to handle the common errors and return a usable result. I'm fairly new to coding, but would be willing to help implement if desired.
Related work in the underlying pygeoif