[gdal-dev] Create tile index by time? (Solution)

Stefan Gofferje lists at home.gofferje.net
Sat Oct 15 00:33:04 PDT 2022


On 10/14/22 10:48, Stefan Gofferje wrote:
> I have a little hobby project where I pull Sentinel imagery of my town's 
> area and create various composites. I run that every morning, so I have 
> a bunch of raster images which represent the same geographic area at 
> different times. I recently also started playing with mapserver and I 
> would like to set up a mapserver to serve those images. I know mapserver 
> basics, have my first mapserver up and running.
> 
> What I'm looking for now is how to create a tile index for a bunch of 
> images which includes the times of those images, so I can server them 
> from mapserver as a layer with a time dimension.

In case it is of use for anybody, here is the solution I came up with 
last night:

1.) Add a time index metadatum to the output file 
(gdaltranslate/gdal_edit -mo TIMEINDEX="yyyy-mm-ddThhmmZ"
2.) Hack together a Python script:

#!/usr/bin/python3

import os, sys
from osgeo import gdal
import geopandas as gpd
from shapely.geometry import box

import warnings
warnings.filterwarnings("ignore")

StartDir = str(sys.argv[1])

def getBounds(path):
     raster = gdal.Open(path)
     ulx, xres, xskew, uly, yskew, yres = raster.GetGeoTransform()
     lrx = ulx + (raster.RasterXSize * xres)
     lry = uly + (raster.RasterYSize * yres)
     return box(lrx, lry, ulx, uly)


df = gpd.GeoDataFrame(columns=['location', 'geometry','timestamp'])
for dir, subdir, files in os.walk(StartDir):
     for fname in files:
         if fname.endswith(".tif"):
             fullname = os.path.join(dir+"/", fname)
             print (fullname)
             ds=gdal.Open(fullname)
             metadata=ds.GetMetadata()
             ds=None
             print(metadata)
             df = df.append({'location': fname, 'geometry': 
getBounds(fullname),'timestamp': metadata['TIMESTAMP']}, ignore_index=True)
#            df = gpd.pd.concat(df,{'location': fname, 'geometry': 
getBounds(os.path.join(dir+"/", fname))}, ignore_index=True)

df.to_file("tile-index.shp")


NOTE: suppressing all warnings is temporary until I figured out how to 
move from df.append to pd.concat.

-Stefan


-- 
  (o_   Stefan Gofferje            | SCLT, MCP, CCSA
  //\   Reg'd Linux User #247167   | VCP #2263
  V_/_  https://www.gofferje.net   | https://www.saakeskus.fi



More information about the gdal-dev mailing list