def tiff_to_stac(item_file_name, dates, products, baseurl): def link_to(link): return os.path.join(baseurl, link) identifier = "CLMS_{}".format(dates) title = "Copernicus Land productss - {}".format(dates) assets = { product_name: { 'href': url, 'title': product_name.upper(), 'type': 'image/vnd.stac.geotiff; cloud-optimized=true' } for product_name, url in products.items() } if len(dates) > 8: time = datetime.strptime(dates.split('_')[0], '%Y') time = datetime.strftime(time, '%Y-%m-%dT%H:%M:%S.%fZ') time_end = datetime.strptime(dates.split('_')[1], '%Y') time_end = datetime.strftime(time_end, '%Y-%m-%dT%H:%M:%S.%fZ') else: time = datetime.strptime(dates, '%Y') time = datetime.strftime(time, '%Y-%m-%dT%H:%M:%S.%fZ') time_end = datetime.strptime(dates, '%Y') time_end = datetime.strftime(time_end, '%Y-%m-%dT%H:%M:%S.%fZ') ds = gdal.Open("/vsicurl/" + list(products.values())[0]) geom, bbox = datasetUtils.get_geom_and_bbox_from_ds(ds) ret = { 'id': identifier, 'type': 'Feature', 'geometry': geom, 'bbox': bbox, 'properties': { 'datetime': time, 'title': title, 'dtr:start_datetime': time, 'dtr:end_datetime': time_end }, 'links': [{ 'href': link_to(item_file_name), 'rel': 'self' }], 'assets': assets } return ret
def dim2stac(inputfile, input_uri, baseurl, args): def linkTo(link): return urljoin(baseurl, link) def httpLinkToS3(link): tmp = args.h_url if tmp[-1] != '/': tmp += '/' tmp += link[5:] return tmp itemSpecFileName = os.path.basename(inputfile) previous_cwd = os.getcwd() try: temp = tempfile.NamedTemporaryFile(suffix=".dim") urlretrieve(input_uri, temp.name) with open(temp.name) as fp: soup = BeautifulSoup(fp, "lxml") identifier = soup.select( "Dimap_Document > Dataset_Id > DATASET_NAME")[0].get_text() # title = soup.select("Dimap_Document > Dataset_Id > DATASET_SERIES")[0].get_text() title = soup.select("Dimap_Document > Dataset_Id > DATASET_NAME")[0].get_text() + ' ' + \ soup.select("Dimap_Document > Production > PRODUCT_TYPE")[0].get_text() datafile_format = soup.select( "Dimap_Document > Data_Access > DATA_FILE_FORMAT")[0].get_text() assets = {} with open(temp.name, 'r') as file: dim_xml = file.read() # Create STAC assets and modify DIM asset hrefs to HTTPS links for data_file in soup.select( "Dimap_Document > Data_Access > Data_File"): idx = data_file.select("BAND_INDEX")[0].get_text() href = data_file.select("DATA_FILE_PATH")[0]['href'] datafile_uri = httpLinkToS3(href) assets['band-' + idx] = { 'href': datafile_uri, 'title': 'Band ' + idx, 'type': 'image/vnd.stac.geotiff; cloud-optimized=true' } # Replace href in the XML (dirty, but works in this case as the DIMs are homogenous in this sense) if '/vsicurl' in href: dim_xml = dim_xml.replace('"{}"'.format(href), '"{}"'.format(datafile_uri)) elif 'http' in href: dim_xml = dim_xml.replace('"{}"'.format(href), '"/vsicurl/{}"'.format(datafile_uri)) elif '../sen1' in href: dim_xml = dim_xml.replace( '"../{}"'.format(href), '"/vsicurl/https://pta.data.lit.fmi.fi/{}"'.format( datafile_uri)) elif '/sen1' in href: dim_xml = dim_xml.replace( '"{}"'.format(href), '"/vsicurl/https://pta.data.lit.fmi.fi/{}"'.format( datafile_uri)) # Write the modified file with open(temp.name, 'w') as file: file.write(dim_xml) os.chdir(os.path.dirname(temp.name)) ds = gdal.Open(os.path.basename(temp.name)) metadata = ds.GetMetadata_Dict() time = metadata['PRODUCT_SCENE_RASTER_START_TIME'] time = datetime.strptime(time, '%d-%b-%Y %H:%M:%S.%f') time = datetime.strftime(time, '%Y-%m-%dT%H:%M:%S.%fZ') timeEnd = metadata['PRODUCT_SCENE_RASTER_STOP_TIME'] timeEnd = datetime.strptime(timeEnd, '%d-%b-%Y %H:%M:%S.%f') timeEnd = datetime.strftime(timeEnd, '%Y-%m-%dT%H:%M:%S.%fZ') geom, bbox = datasetUtils.get_geom_and_bbox_from_ds(ds) ret = { 'id': identifier, 'type': 'Feature', 'geometry': geom, 'bbox': bbox, 'properties': { 'datetime': time, 'title': title, 'dtr:start_datetime': time, 'dtr:end_datetime': timeEnd }, 'links': [{ 'href': linkTo(itemSpecFileName), 'rel': 'self' }], 'assets': assets } finally: temp.close() os.chdir(previous_cwd) return ret