From 92cf3e88df48993c0e6830b1d74a0349d4f6b7bf Mon Sep 17 00:00:00 2001 From: Fabian Schindler Date: Fri, 12 Dec 2014 17:26:44 +0100 Subject: [PATCH] Improving reading of metadata formats by using the better suited parse_iso8601 function. --- .../coverages/metadata/formats/dimap_general.py | 6 +++--- .../resources/coverages/metadata/formats/eoom.py | 9 +++------ .../resources/coverages/metadata/formats/native.py | 13 +++++-------- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/eoxserver/resources/coverages/metadata/formats/dimap_general.py b/eoxserver/resources/coverages/metadata/formats/dimap_general.py index 335d71845..c0b6f683d 100644 --- a/eoxserver/resources/coverages/metadata/formats/dimap_general.py +++ b/eoxserver/resources/coverages/metadata/formats/dimap_general.py @@ -26,7 +26,7 @@ #------------------------------------------------------------------------------- -from django.utils.dateparse import parse_date, parse_datetime +from eoxserver.core.util.timetools import parse_iso8601 from django.contrib.gis.geos import Polygon, MultiPolygon from eoxserver.core import Component, implements @@ -102,7 +102,7 @@ def cond_set(dct, key, value): def parse_date_or_datetime_11(string): - value = parse_date(string) or parse_datetime(string) + value = parse_iso8601(string) if not value: raise Exception("Could not parse date or datetime from '%s'." % string) return value @@ -129,7 +129,7 @@ def parse_size_11(elem): def parse_geotransform_11(elem): values = ( - elem.findtext("ULXMAP"), elem.findtext("XDIM"), 0, + elem.findtext("ULXMAP"), elem.findtext("XDIM"), 0, elem.findtext("ULYMAP"), 0, elem.findtext("YDIM") ) return map(float, values) diff --git a/eoxserver/resources/coverages/metadata/formats/eoom.py b/eoxserver/resources/coverages/metadata/formats/eoom.py index 1fd471663..31bdbc106 100644 --- a/eoxserver/resources/coverages/metadata/formats/eoom.py +++ b/eoxserver/resources/coverages/metadata/formats/eoom.py @@ -25,12 +25,9 @@ # THE SOFTWARE. #------------------------------------------------------------------------------- - -from lxml import etree - -from django.utils.dateparse import parse_datetime from django.contrib.gis.geos import Polygon, MultiPolygon +from eoxserver.core.util.timetools import parse_iso8601 from eoxserver.core.util.xmltools import parse, NameSpace, NameSpaceMap from eoxserver.core.util.iteratortools import pairwise from eoxserver.core import Component, implements @@ -81,8 +78,8 @@ def parse_ring(string): class EOOMFormatDecoder(xml.Decoder): identifier = xml.Parameter("eop:metaDataProperty/eop:EarthObservationMetaData/eop:identifier/text()", type=str, num=1) - begin_time = xml.Parameter("om:phenomenonTime/gml:TimePeriod/gml:beginPosition/text()", type=parse_datetime, num=1) - end_time = xml.Parameter("om:phenomenonTime/gml:TimePeriod/gml:endPosition/text()", type=parse_datetime, num=1) + begin_time = xml.Parameter("om:phenomenonTime/gml:TimePeriod/gml:beginPosition/text()", type=parse_iso8601, num=1) + end_time = xml.Parameter("om:phenomenonTime/gml:TimePeriod/gml:endPosition/text()", type=parse_iso8601, num=1) polygons = xml.Parameter("om:featureOfInterest/eop:Footprint/eop:multiExtentOf/gml:MultiSurface/gml:surfaceMember/gml:Polygon", type=parse_polygon_xml, num="+") namespaces = nsmap diff --git a/eoxserver/resources/coverages/metadata/formats/native.py b/eoxserver/resources/coverages/metadata/formats/native.py index e59e1e33c..7bb61fe52 100644 --- a/eoxserver/resources/coverages/metadata/formats/native.py +++ b/eoxserver/resources/coverages/metadata/formats/native.py @@ -29,12 +29,12 @@ from lxml import etree from lxml.builder import E -from django.utils.dateparse import parse_datetime from django.contrib.gis.geos import Polygon, MultiPolygon from eoxserver.core.util.xmltools import parse from eoxserver.core.util.timetools import isoformat from eoxserver.core.util.iteratortools import pairwise +from eoxserver.core.util.timetools import parse_iso8601 from eoxserver.core import Component, implements from eoxserver.core.decoders import xml from eoxserver.resources.coverages.metadata.interfaces import ( @@ -52,10 +52,8 @@ def test(self, obj): xml = parse(obj) return xml is not None and xml.tag == "Metadata" - def get_format_name(self, obj): return "native" - def read(self, obj): tree = parse(obj) @@ -70,7 +68,6 @@ def read(self, obj): } raise Exception("Could not parse from obj '%s'." % repr(obj)) - def write(self, values, file_obj, format=None, encoding=None, pretty=False): def flip(point): return point[1], point[0] @@ -81,7 +78,7 @@ def flip(point): E.BeginTime(isoformat(values["begin_time"])), E.EndTime(isoformat(values["end_time"])), E.Footprint( - *map(lambda polygon: + *map(lambda polygon: E.Polygon( E.Exterior( " ".join([ @@ -112,14 +109,14 @@ def parse_polygon_xml(elem): *map(lambda e: parse_ring(e.text), elem.findall("Interior")) ) + def parse_ring(string): - points = [] raw_coords = map(float, string.split(" ")) return [(lon, lat) for lat, lon in pairwise(raw_coords)] class NativeFormatDecoder(xml.Decoder): identifier = xml.Parameter("EOID/text()") - begin_time = xml.Parameter("BeginTime/text()", type=parse_datetime) - end_time = xml.Parameter("EndTime/text()", type=parse_datetime) + begin_time = xml.Parameter("BeginTime/text()", type=parse_iso8601) + end_time = xml.Parameter("EndTime/text()", type=parse_iso8601) polygons = xml.Parameter("Footprint/Polygon", type=parse_polygon_xml, num="+")