Skip to content

Commit

Permalink
Expose GDAL resampling methods to Geodot
Browse files Browse the repository at this point in the history
  • Loading branch information
kb173 committed Mar 6, 2020
1 parent 481be05 commit 03df752
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 20 deletions.
18 changes: 9 additions & 9 deletions demo/World.gd
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
extends Spatial


# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var instanced_image_count = 0


# Called when the node enters the scene tree for the first time.
Expand All @@ -13,11 +11,11 @@ func _ready():

var img = Geodot.save_tile_from_heightmap(
"/home/retour/LandscapeLab/testdata/webm.tif",
"/home/retour/LandscapeLab/testdata/tile.tif",
1546670.0,
5918250.0,
500.0,
256
256,
1
)

get_node("MeshInstance").mesh.surface_get_material(0).set_shader_param("heightmap", img)
Expand All @@ -26,13 +24,15 @@ func _ready():
func _process(delta):
var img = Geodot.save_tile_from_heightmap(
"/home/retour/LandscapeLab/testdata/webm.tif",
"/home/retour/LandscapeLab/testdata/tile.tif",
1546670.0 + Geodot.get_time_passed() * 1000,
1546670.0,
5918250.0,
500.0,
256
256,
1
)

instanced_image_count += 1

get_node("MeshInstance").mesh.surface_get_material(0).set_shader_param("heightmap", img)

print(Geodot.get_time_passed())
print(instanced_image_count / Geodot.get_time_passed())
Binary file modified src/gdlibrary.os
Binary file not shown.
16 changes: 13 additions & 3 deletions src/geodot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ union {
int8_t bval[4];
} floatAsBytes;

Ref<ImageTexture> Geodot::save_tile_from_heightmap(String infile, String outfile, float new_top_left_x, float new_top_left_y, float new_size, int img_size) {
Ref<ImageTexture> Geodot::save_tile_from_heightmap(String infile, float new_top_left_x, float new_top_left_y, float new_size, int img_size, int interpolation) {
// We need to lock a mutex because the RasterTileExtractor seems not to be thread-safe (due to gdal?)
load_mutex->lock();

RasterTileExtractor rte;

float *data = new float[sizeof(float) * img_size * img_size];
rte.clip(infile.utf8().get_data(), outfile.utf8().get_data(), new_top_left_x, new_top_left_y, new_size, img_size, data);
rte.clip(infile.utf8().get_data(), new_top_left_x, new_top_left_y, new_size, img_size, interpolation, data);

PoolByteArray pba;

Expand Down Expand Up @@ -85,7 +85,17 @@ Ref<ImageTexture> Geodot::save_tile_from_heightmap(String infile, String outfile
// Create an ImageTexture wrapping the Image
ImageTexture *imgTex = ImageTexture::_new();
imgTex->set_storage(ImageTexture::STORAGE_RAW);
imgTex->create_from_image(Ref<Image>(img), ImageTexture::FLAG_FILTER);

// By default, the returned texture has the FILTER flag. Only if the interpolation method
// is nearest neighbor or one of the modal types, it is disabled, since we most likely
// want crisp textures then.
int flag = ImageTexture::FLAG_FILTER;
if (interpolation == INTERPOLATION::NEAREST
|| interpolation > 5) {
flag = 0;
}

imgTex->create_from_image(Ref<Image>(img), flag);

return Ref<ImageTexture>(imgTex);
}
18 changes: 17 additions & 1 deletion src/geodot.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ class Geodot : public Node {
Ref<Mutex> load_mutex;

public:
// TODO: Not exportable? https://github.com/godotengine/godot/issues/15922
enum INTERPOLATION {
NEAREST,
BILINEAR,
CUBIC,
CUBICSPLINE,
LANCZOS,
AVG,
MODE,
MAX,
MIN,
MED,
Q1,
Q2,
};

static void _register_methods();

Geodot();
Expand All @@ -31,7 +47,7 @@ class Geodot : public Node {

void reproject_to_webmercator(String infile, String outfile);

Ref<ImageTexture> save_tile_from_heightmap(String infile, String outfile, float new_top_left_x, float new_top_left_y, float new_size, int img_size);
Ref<ImageTexture> save_tile_from_heightmap(String infile, float new_top_left_x, float new_top_left_y, float new_size, int img_size, int interpolation);
};

}
Expand Down
Binary file modified src/geodot.os
Binary file not shown.
12 changes: 8 additions & 4 deletions src/raster-tile-extractor/RasterTileExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ void RasterTileExtractor::reproject_to_webmercator(const char *infile, const cha
}

void
RasterTileExtractor::clip(const char *infile, const char *outfile, double top_left_x, double top_left_y,
double size_meters, int img_size, float *result_target) {
RasterTileExtractor::clip(const char *infile, double top_left_x, double top_left_y, double size_meters, int img_size,
int interpolation_type, float *result_target) {
GDALDatasetH source, dest;
GDALDriverH pDriver;

Expand All @@ -120,6 +120,9 @@ RasterTileExtractor::clip(const char *infile, const char *outfile, double top_le

source = GDALOpen(infile, GA_ReadOnly);

// TODO: Use this datatype instead of Float32 only! (Requires changes in Geodot itself as well)
GDALDataType datatype = GDALGetRasterDataType(GDALGetRasterBand(source, 1));

// Get the current Transform of the source image
double transform[6];
GDALGetGeoTransform(source, transform);
Expand All @@ -137,7 +140,8 @@ RasterTileExtractor::clip(const char *infile, const char *outfile, double top_le
transform[5] = -new_pixel_size;

// Create a new geoimage at the given path with our img_size
dest = GDALCreate(pDriver, outfile, img_size, img_size,
// The outfile path is empty since it's only in RAM
dest = GDALCreate(pDriver, "", img_size, img_size,
GDALGetRasterCount(source), GDT_Float32, nullptr);

// Get Source coordinate system.
Expand Down Expand Up @@ -174,7 +178,7 @@ RasterTileExtractor::clip(const char *infile, const char *outfile, double top_le

// If we are going beyond the available resolution, use bilinear scaling
if (new_pixel_size < previous_pixel_size) {
psWarpOptions->eResampleAlg = GRA_Bilinear;
psWarpOptions->eResampleAlg = static_cast<GDALResampleAlg>(interpolation_type);
} else {
psWarpOptions->eResampleAlg = GRA_NearestNeighbour;
}
Expand Down
4 changes: 2 additions & 2 deletions src/raster-tile-extractor/RasterTileExtractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class RasterTileExtractor {

/// Clip the infile to an image starting at top_left_x, top_left_y with a given size (in meters).
/// The resulting image has the resolution img_size x img_size (pixels).
void clip(const char *infile, const char *outfile, double top_left_x, double top_left_y,
double size_meters, int img_size, float *result_target);
void clip(const char *infile, double top_left_x, double top_left_y, double size_meters, int img_size,
int interpolation_type, float *result_target);
};


Expand Down
2 changes: 1 addition & 1 deletion src/raster-tile-extractor/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main() {
int img_size = 256;

auto *result = new float[sizeof(float) * img_size * img_size];
rte.clip("/home/retour/LandscapeLab/testdata/DGM_K_5m.tif", "/home/retour/LandscapeLab/testdata/tile.tif", new_top_left_x, new_top_left_y, new_size, img_size, result);
rte.clip("/home/retour/LandscapeLab/testdata/DGM_K_5m.tif", new_top_left_x, new_top_left_y, new_size, img_size, 0, result);

std::cout << result[0] << std::endl;

Expand Down

0 comments on commit 03df752

Please sign in to comment.