Edit on GitLab
Granules resource examples
Quickly get a project started with any of our examples related to the granules resource.
Simple example
The following example shows how to query the OpenSearch service with the granules
result, and how to efficently parse the results. Don’t forget to add the logic regarding the paging.
# Import XML Parser, urllib, and shapely geometry
import xml.etree.ElementTree as ET
import urllib.request
import shapely.geometry as SG
# Helper to parse coordinates from raw string to array of points
def parse_coordinates(raw_str):
ret = []
coords = raw_str.split()
n = len(coords)
for i in range(0, n, 2):
ret.append([float(coords[i]), float(coords[i+1])])
return ret
# Consts
datasetId = "scatsat1_l2b_knmi" # s1a_wv_ocn, argo_cycles, jason3_igdr ...
timeStart = "1000-01-01T00:00:00Z"
timeEnd = "2200-01-01T23:59:59Z"
geoBox = "-180.0,-90.0,180.0,90.0"
startPage = "0"
count = "2"
server = "opensearch.ifremer.fr"
protocol = "https"
resource = "granules"
output_format = "atom"
# Get URL associated contents as string, and print HTTP status code
uri = protocol + "://" + server + "/" + resource + "." + output_format + "?datasetId=" + datasetId\
+ "&startPage=" + startPage + "&count=" + count\
+ "&timeStart=" + timeStart + "&timeEnd=" + timeEnd + "&geoBox=" + geoBox
print(uri)
response = urllib.request.urlopen(uri)
status_code = response.getcode()
print("HTTP STATUS CODE = " + str(status_code))
contents = response.read()
# Sep
print("")
# Parse XML as ElementTree elements
root = ET.fromstring(contents)
# Foreach Atom entry..
for entry in root.findall('./{http://www.w3.org/2005/Atom}entry'):
# Print the Atom title
title = entry.find('{http://www.w3.org/2005/Atom}title')
print(r'GRANULE : ' + title.text)
# Parse the Geometry as shapely.geometry
shapely = None
complex_geometry = entry.find('{http://www.georss.org/georss/10}where')
if complex_geometry is not None:
multi_polygon = complex_geometry.find('./{http://www.opengis.net/gml}MultiPolygon')
if multi_polygon is not None:
polygons = []
# Build complex MultiPolygon
for gmlPosList in complex_geometry.findall(
'./{http://www.opengis.net/gml}MultiPolygon/{http://www.opengis.net/gml}polygonMember/{http://www.opengis.net/gml}Polygon/{http://www.opengis.net/gml}outerBoundaryIs/{http://www.opengis.net/gml}LinearRing/{http://www.opengis.net/gml}posList'):
polygons.append(SG.Polygon(parse_coordinates(gmlPosList.text)))
shapely = SG.MultiPolygon(polygons)
else:
# Build complex MultiLineString
multi_curve = complex_geometry.find('./{http://www.opengis.net/gml}MultiCurve')
if multi_curve is not None:
line_strings = []
for gmlPosList in complex_geometry.findall(
'./{http://www.opengis.net/gml}MultiCurve/{http://www.opengis.net/gml}curveMember/{http://www.opengis.net/gml}LineString/{http://www.opengis.net/gml}posList'):
line_strings.append(SG.LineString(parse_coordinates(gmlPosList.text)))
shapely = SG.MultiLineString(line_strings)
else:
raise ValueError('A geometry value was incorrect (1).')
else:
# Build simple Point
geoPoint = entry.find('{http://www.georss.org/georss/10}point')
if geoPoint is not None :
point = geoPoint.text.split()
shapely = SG.Point(float(point[0]), float(point[1]))
else:
# Build simple Polygon
geoPolygon = entry.find('{http://www.georss.org/georss/10}polygon')
if geoPolygon is not None :
shapely = SG.Polygon(parse_coordinates(geoPolygon.text))
else:
# Build simple Line
geoLine = entry.find('{http://www.georss.org/georss/10}line')
if geoLine is not None :
shapely = SG.LineString(parse_coordinates(geoLine.text))
else:
raise ValueError('A geometry value was incorrect (2).')
# Print the resulting Geometry object
print(shapely)
# Print granules URIs
for link in entry.findall('./{http://www.w3.org/2005/Atom}link'):
attr = link.attrib
print(r'PROTOCOL("' + attr.get(r'title') + '") -> ' + attr.get(r'href'))
# Sep
print("")