Skip to content

Commit

Permalink
Add COG to format extensions list.
Browse files Browse the repository at this point in the history
Add createInfo concept, this will contain driver, parentPath, currentPath, etc.
Make gdal programs no-failure operations.
Capture errors and warnings of gdal programs in the raster tile metadata.
Add RST_Transform expression.
Add ReadAsPath reading strategy.
  • Loading branch information
milos.colic committed Feb 26, 2024
1 parent a4815d6 commit 8356b6a
Show file tree
Hide file tree
Showing 42 changed files with 744 additions and 362 deletions.
27 changes: 27 additions & 0 deletions python/mosaic/api/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"rst_subdivide",
"rst_summary",
"rst_tessellate",
"rst_transform",
"rst_to_overlapping_tiles",
"rst_tryopen",
"rst_upperleftx",
Expand Down Expand Up @@ -997,6 +998,32 @@ def rst_tessellate(raster_tile: ColumnOrName, resolution: ColumnOrName) -> Colum
)


def rst_transform(raster_tile: ColumnOrName, srid: ColumnOrName) -> Column:
"""
Transforms the raster to the given SRID.
The result is a Mosaic raster tile struct of the transformed raster.
The result is stored in the checkpoint directory.
Parameters
----------
raster_tile : Column (RasterTileType)
Mosaic raster tile struct column.
srid : Column (IntegerType)
EPSG authority code for the file's projection.
Returns
-------
Column (RasterTileType)
Mosaic raster tile struct column.
"""
return config.mosaic_context.invoke_function(
"rst_transform",
pyspark_to_java_column(raster_tile),
pyspark_to_java_column(srid),
)


def rst_fromcontent(
raster_bin: ColumnOrName, driver: ColumnOrName, size_in_mb: Any = -1
) -> Column:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ object FormatLookup {
"CAD" -> "dwg",
"CEOS" -> "ceos",
"COASP" -> "coasp",
"COG" -> "tif",
"COSAR" -> "cosar",
"CPG" -> "cpg",
"CSW" -> "csw",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,26 @@ object GDAL {
*/
def readRaster(
inputRaster: Any,
parentPath: String,
shortDriverName: String,
createInfo: Map[String, String],
inputDT: DataType
): MosaicRasterGDAL = {
inputDT match {
case StringType =>
val path = inputRaster.asInstanceOf[UTF8String].toString
MosaicRasterGDAL.readRaster(path, parentPath)
MosaicRasterGDAL.readRaster(createInfo)
case BinaryType =>
val bytes = inputRaster.asInstanceOf[Array[Byte]]
val raster = MosaicRasterGDAL.readRaster(bytes, parentPath, shortDriverName)
val raster = MosaicRasterGDAL.readRaster(bytes, createInfo)
// If the raster is coming as a byte array, we can't check for zip condition.
// We first try to read the raster directly, if it fails, we read it as a zip.
if (raster == null) {
val parentPath = createInfo("parentPath")
val zippedPath = s"/vsizip/$parentPath"
MosaicRasterGDAL.readRaster(bytes, zippedPath, shortDriverName)
MosaicRasterGDAL.readRaster(bytes, createInfo + ("path" -> zippedPath))
} else {
raster
}
case _ =>
throw new IllegalArgumentException(s"Unsupported data type: $inputDT")
case _ => throw new IllegalArgumentException(s"Unsupported data type: $inputDT")
}
}

Expand Down Expand Up @@ -160,7 +159,10 @@ object GDAL {
* @return
* Returns a Raster object.
*/
def raster(path: String, parentPath: String): MosaicRasterGDAL = MosaicRasterGDAL.readRaster(path, parentPath)
def raster(path: String, parentPath: String): MosaicRasterGDAL = {
val createInfo = Map("path" -> path, "parentPath" -> parentPath)
MosaicRasterGDAL.readRaster(createInfo)
}

/**
* Reads a raster from the given byte array. If the byte array is a zip
Expand All @@ -171,8 +173,10 @@ object GDAL {
* @return
* Returns a Raster object.
*/
def raster(content: Array[Byte], parentPath: String, driverShortName: String): MosaicRasterGDAL =
MosaicRasterGDAL.readRaster(content, parentPath, driverShortName)
def raster(content: Array[Byte], parentPath: String, driverShortName: String): MosaicRasterGDAL = {
val createInfo = Map("parentPath" -> parentPath, "driver" -> driverShortName)
MosaicRasterGDAL.readRaster(content, createInfo)
}

/**
* Reads a raster from the given path. It extracts the specified band from
Expand All @@ -186,8 +190,10 @@ object GDAL {
* @return
* Returns a Raster band object.
*/
def band(path: String, bandIndex: Int, parentPath: String): MosaicRasterBandGDAL =
MosaicRasterGDAL.readBand(path, bandIndex, parentPath)
def band(path: String, bandIndex: Int, parentPath: String): MosaicRasterBandGDAL = {
val createInfo = Map("path" -> path, "parentPath" -> parentPath)
MosaicRasterGDAL.readBand(bandIndex, createInfo)
}

/**
* Converts raster x, y coordinates to lat, lon coordinates.
Expand Down
Loading

0 comments on commit 8356b6a

Please sign in to comment.