Skip to content

Commit

Permalink
Add exception handlers for Polygon and Locale methods (#929)
Browse files Browse the repository at this point in the history
* Add exception handlers or Polygon and Locale methods

* Add null check for interior_ring_elements
  • Loading branch information
vietnguyengit authored May 31, 2024
1 parent 0bee95e commit 13b0050
Showing 1 changed file with 47 additions and 24 deletions.
71 changes: 47 additions & 24 deletions owslib/iso3.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,18 +338,29 @@ def __init__(self, namespaces, md=None):
:param md: PT_Locale etree.Element
"""
self.namespaces = namespaces
if md is None:
self.id = None
self.languagecode = None
self.charset = None
else:
self.id = md.attrib.get('id')
self.languagecode = md.find(
util.nspath_eval('lan:language/lan:LanguageCode', self.namespaces)).attrib.get('codeListValue')
self.charset = md.find(
util.nspath_eval('lan:characterEncoding/lan:MD_CharacterSetCode', self.namespaces)).attrib.get(
'codeListValue')

self.id = None
self.languagecode = None
self.charset = None

if md is not None:
try:
self.id = md.attrib.get('id')
except AttributeError:
pass

try:
self.languagecode = md.find(
util.nspath_eval('lan:language/lan:LanguageCode', self.namespaces)).attrib.get('codeListValue')
except AttributeError:
pass

try:
self.charset = md.find(
util.nspath_eval('lan:characterEncoding/lan:MD_CharacterSetCode', self.namespaces)).attrib.get(
'codeListValue')
except AttributeError:
pass


class CI_Date(printable):
Expand Down Expand Up @@ -1114,19 +1125,31 @@ def __init__(self, namespaces, md=None):
:param md: EX_Polygon etree.Element
"""
self.namespaces = namespaces
if md is None:
self.exterior_ring = None
self.interior_rings = []
else:
linear_ring = md.find(util.nspath_eval('gml32:Polygon/gml32:exterior/gml32:LinearRing', self.namespaces))
if linear_ring is not None:
self.exterior_ring = self._coordinates_for_ring(linear_ring)

interior_ring_elements = md.findall(util.nspath_eval('gml32:Polygon/gml32:interior', self.namespaces))
self.interior_rings = []
for iring_element in interior_ring_elements:
linear_ring = iring_element.find(util.nspath_eval('gml32:LinearRing', self.namespaces))
self.interior_rings.append(self._coordinates_for_ring(linear_ring))

self.exterior_ring = None
self.interior_rings = []

if md is not None:
try:
linear_ring = md.find(util.nspath_eval('gml32:Polygon/gml32:exterior/gml32:LinearRing', self.namespaces))
if linear_ring is not None:
self.exterior_ring = self._coordinates_for_ring(linear_ring)
except KeyError:
pass

try:
interior_ring_elements = md.findall(util.nspath_eval('gml32:Polygon/gml32:interior', self.namespaces))
self.interior_rings = []
if interior_ring_elements is not None:
for iring_element in interior_ring_elements:
try:
linear_ring = iring_element.find(util.nspath_eval('gml32:LinearRing', self.namespaces))
self.interior_rings.append(self._coordinates_for_ring(linear_ring))
except KeyError:
pass
except KeyError:
pass


def _coordinates_for_ring(self, linear_ring):
""" Get coordinates for gml coordinate ring
Expand Down

0 comments on commit 13b0050

Please sign in to comment.