2 Commits

7 changed files with 130 additions and 47 deletions

3
.gitmodules vendored
View File

@@ -1,3 +0,0 @@
[submodule "extern/argh"]
path = extern/argh
url = https://github.com/adishavit/argh.git

View File

@@ -1,22 +1,15 @@
cmake_minimum_required(VERSION 3.20)
project(
contour-creator
LANGUAGES CXX)
find_package(GDAL CONFIG REQUIRED)
add_executable(${PROJECT_NAME}
src/HeightMap.cpp src/CellMap.cpp src/main.cpp
)
# Argh is a simple argrument parser
add_subdirectory(extern/argh)
target_link_libraries(${PROJECT_NAME} PRIVATE argh)
# Gdal is used for geodata IO
find_package(GDAL CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE GDAL::GDAL)
find_package(OpenMP)
if(OpenMP_CXX_FOUND)
target_link_libraries(${PROJECT_NAME} PUBLIC OpenMP::OpenMP_CXX)
endif()
endif()
target_link_libraries(${PROJECT_NAME} GDAL::GDAL)

View File

@@ -3,4 +3,4 @@ rm -rf build
mkdir build
cmake -DCMAKE_BUILD_TYPE=Debug -B build
cmake --build build --parallel
build/contour-creator --interval=1 --blur example_files/crop.tif
build/contour-creator 'example_files/crop.tif'

1
extern/argh vendored

Submodule extern/argh deleted from 431bf323ac

View File

@@ -1,3 +1,4 @@
#include <cstddef>
#include <gdal/cpl_conv.h>
#include <iostream>
#include <stdexcept>
@@ -5,7 +6,7 @@
#include <gdal/gdal.h>
#include "gdal/gdal_priv.h"
#include <gdal/gdal_frmts.h>
//#include "matplotlibcpp.h"
#include "contour_creator.hh"
@@ -32,6 +33,7 @@ HeightMap::HeightMap(const char* filepath)
//https://gdal.org/api/gdaldataset_cpp.html#_CPPv4N11GDALDataset15GetGeoTransformEPd
this->geotransform = (double *) CPLMalloc(sizeof(double)*6);
this->reference_system = *(file->GetSpatialRef());
this->filepath = filepath;
file->GetGeoTransform(this->geotransform);
@@ -41,6 +43,7 @@ HeightMap::HeightMap(const char* filepath)
0, 0 );
if (error) { throw std::runtime_error("Could not read tif file!"); }
band->FlushCache();
const int size = this->width*this->height;
}
float HeightMap::get_pixel(int x, int y)
{
@@ -62,7 +65,7 @@ void HeightMap::blur(float standard_deviation)
*(kernel + i) = 1.0;
}
float* blurred = (float *) CPLMalloc(sizeof(float)*this->width*this->height);
float* blurred = new float[this->width*this->height];
#pragma omp parallel
{
#pragma omp for
@@ -90,4 +93,101 @@ void HeightMap::blur(float standard_deviation)
free(this->data);
this->data = blurred;
blurred = nullptr;
}
void HeightMap::statistics()
{
float avg = 0;
float var = 0;
float std = 0;
for (int i = 0; i < this->width*this->height; i++)
{int x = i%this->width;
int y = i/this->width;
if (x<0 || x>=this->width)
{
x = 0;
}
if (y<0 || y>=this->height)
{
y = 0;
}
avg += this->get_pixel(x, y);}
//std::cout << this->get_pixel(x, y);}
avg = avg/(this->width*this->height);
std::cout << "Average value: " << avg <<"\n";
for (int i = 0; i < this->width*this->height; i++)
{
int x = i%this->width;
int y = i/this->width;
var += (avg - this->get_pixel(x, y))*(avg - this->get_pixel(x, y));
}
std = sqrt(var)/(this->width*this->height-1);
var = var / (this->width*this->height-1);
std::cout << "std: " << std << "\n";
std::cout << "var: " << var << "\n";
};
void HeightMap::calculate_steepness()
{
int kernel_height = 5;
int kernel_width = 5;
int kernel_size = kernel_height*kernel_width;
float* kernel = new float[sizeof(float)*kernel_size];
float* steepness = new float[this->width*this->height];
#pragma omp parallel
{
#pragma omp for
for (int i = 0; i < this->height * this->width; i++)
{
int x = i%this->width;
int y = i/this->width;
float max = 0;
float min = 0;
for (int j = 0; j < kernel_size; j++)
{
if (this->get_pixel(x, y) > max)
{
max = get_pixel(x, y);
}
if (this->get_pixel(x, y) < min)
{
min = get_pixel(x, y);
}
if (x<0 || x>=this->width)
{
x = 0;
}
if (y<0 || y>=this->height)
{
y = 0;
}
}
steepness[i] = max - min;
}
}
GDALAllRegister();
GDALDataset *original_file = (GDALDataset *) GDALOpen( filepath, GA_ReadOnly );
const char * filename = "steepness.tif";
auto driver = GetGDALDriverManager()->GetDriverByName("GeoRaster");
GDALDataset *file;
file = driver->CreateCopy("steepness.tif", original_file, FALSE, NULL, NULL, NULL);
file->AddBand(GDT_Float32, NULL);
GDALRasterBand *band = file->GetRasterBand(0);
CPLErr error = band->RasterIO( GF_Write, 0, 0, this->width, this->height,
steepness, this->width, this->height, GDT_Float32,
0, 0 );
GDALClose(file);
}

View File

@@ -16,10 +16,12 @@ class HeightMap
float min; //!< Minimum value in image
float max; //!< Maximum value in image
OGRSpatialReference reference_system;
HeightMap(const char* filepath);
const char* filepath;
float get_pixel(int x,int y);
void blur(float standard_deviation);
void statistics();
void calculate_steepness();
};
/**

View File

@@ -12,7 +12,8 @@
#include "gdal/gdal_priv.h"
#include <gdal/gdal_frmts.h>
#include <omp.h>
#include "argh.h"
//#include "matplotlibcpp.h"
//namespace plt = matplotlibcpp;
std::vector<Point> produce_cellmap(HeightMap* heightmap, float z)
{
@@ -228,34 +229,25 @@ void write_output_file(std::vector<std::vector<Point>> all_points, const char *f
int main(int argc, const char* argv[])
{
argh::parser cmdl(argv);
if (cmdl[{ "-h", "--help" }])
{
std::cout << "Usage:\n"
<< "contour_creator [OPTIONS] <input_file>\n"
<< "-o; --output <FILENAME.geojson> - File to write output to (Default: contours.geojson)\n"
<< "-i; --interval <int> - Set the interval between contours (Default: 5)\n"
<< "-b; --blur - Blur the image\n"
<< "--stats - Print statistical information about the heightmap\n";
exit(0);
}
int interval;
cmdl({"-i", "--interval"}, 5) >> interval;
if (interval <= 0)
std::cerr << "Interval must be valid positive integer!" << "\n";
const char* filepath = argv[1];
HeightMap map(filepath);
std::string output_file;
cmdl({"-o", "--output"}, "contours.geojson") >> output_file;
std::cout << "x: " << map.width << " y: " << map.height << "\n";
std::cout << "max: " << map.max << " min: " << map.min << "\n";
std::string input_file;
cmdl(1) >> input_file;
map.blur(0.8);
map.statistics();
map.calculate_steepness();
auto lines = create_lines(&map, 5);
write_output_file(lines, "out.geojson", &map);
HeightMap map(input_file.c_str());
if (cmdl[{"-b", "--blur"}])
map.blur(0.8);
auto lines = create_lines(&map, interval);
write_output_file(lines, output_file.c_str(), &map);
std::cout << "Contours written to " << output_file << " 🗺️\n";
std::vector<int> y = {1, 2, 3};
//plt::plot(y, "bo-");
//plt::show();
return 0;
}