Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SEDONA-449] Add two raster column support to RS_MapAlgebra #1150

Merged
merged 11 commits into from
Dec 21, 2023

Conversation

furqaankhan
Copy link
Contributor

@furqaankhan furqaankhan commented Dec 19, 2023

Did you read the Contributor Guide?

Is this PR related to a JIRA ticket?

What changes were proposed in this PR?

  • Add support for two raster inputs in RS_MapAlgebra

How was this patch tested?

  • passed new and existing tests

Did this PR include necessary documentation updates?

  • Yes, I have updated the documentation update.

@furqaankhan furqaankhan changed the title [SEDONA-449] Add 2 raster column support to RS_MapAlgebra [SEDONA-449] Add two raster column support to RS_MapAlgebra Dec 20, 2023
@furqaankhan furqaankhan marked this pull request as ready for review December 20, 2023 16:59
previousRuntime.set(runtime);
}

// TODO is it worth to deduplicate code from 236-246 in this function
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix TODOs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I thought I removed it.

Assert.assertEquals((int) expected, (int) actual);
break;
default:
Assert.assertEquals(expected, actual, 1e-3);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we already define a variable in the TestBase as the allowed precision

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixing

val df = df1.join(df2, df1("path") === df2("path1"), "inner").select("rast0", "rast1", "path")
Seq(null, "b", "s", "i", "f", "d").foreach { pixelType =>
val pixelTypeExpr = if (pixelType == null) null else s"'$pixelType'"
val dfResult = df.withColumn("rast_2", expr(s"RS_MapAlgebra(rast0, rast1, $pixelTypeExpr, 'out[0] = rast0[0] * 0.5 + rast1[0] * 0.5;', null)"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the script have out[0]? What is the difference between out and out[0]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and it doesn't matter as we are return a single band raster.

.option("pathGlobFilter", "*.tif*")
.load(resourceFolder + "raster")
.selectExpr("path", "RS_FromGeoTiff(content) as rast0")
val df2 = sparkSession.read.format("binaryFile")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to read the same dataset twice. Please use self-join: https://www.sparkcodehub.com/spark-self-join-in-spark-sql-and-dataframe


// If pixelType does not match with the data type of the result image (should be double since Jiffle only supports
// double destination image), we need to convert the resultImage to the specified pixel type.
if (rasterDataType != resultImage.getSampleModel().getDataType()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the band data type after the map algebra? If the map algebra result is a double, and the original data type is Int, how will the final value be? Will it round the values?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will truncate them. Will have to look into it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about this? @Kontinuation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

convertedRaster.setSamples casts double values to the data type of the pixel type. It does not round the values.

PostGIS does something more complicated: it clamps the value according to the range of data type. It does not round the values.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@furqaankhan can you describe the behavior of it in the doc?

Comment on lines 207 to 209
if (!cmRast0.equals(cmRast1)) {
throw new IllegalArgumentException("Color Model did not match. Provide rasters that has the same properties.");
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jiayuasu @Kontinuation Is this required to check the color models of the two input rasters?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this check is needed. If the input rasters do have the same color model, simply let the resulting raster have a fallback color model.

@jiayuasu jiayuasu merged commit 73741be into apache:master Dec 21, 2023
39 checks passed
@furqaankhan furqaankhan deleted the rs-map-algegra-2 branch July 16, 2024 01:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants