Compare commits

...

10 Commits

7 changed files with 237 additions and 37 deletions

3
.gitmodules vendored Normal file
View File

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

View File

@ -1,15 +1,22 @@
cmake_minimum_required(VERSION 3.20)
project( project(
contour-creator contour-creator
LANGUAGES CXX) LANGUAGES CXX)
find_package(GDAL CONFIG REQUIRED)
add_executable(${PROJECT_NAME} add_executable(${PROJECT_NAME}
src/HeightMap.cpp src/CellMap.cpp src/main.cpp 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) find_package(OpenMP)
if(OpenMP_CXX_FOUND) if(OpenMP_CXX_FOUND)
target_link_libraries(${PROJECT_NAME} PUBLIC OpenMP::OpenMP_CXX) 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 mkdir build
cmake -DCMAKE_BUILD_TYPE=Debug -B build cmake -DCMAKE_BUILD_TYPE=Debug -B build
cmake --build build --parallel cmake --build build --parallel
build/contour-creator 'example_files/crop.tif' build/contour-creator --interval=1 --blur example_files/crop.tif

1
extern/argh vendored Submodule

@ -0,0 +1 @@
Subproject commit 431bf323acd3bb805483491ff277dac5d85772ad

View File

@ -1,3 +1,4 @@
#include <cstddef>
#include <gdal/cpl_conv.h> #include <gdal/cpl_conv.h>
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
@ -5,7 +6,7 @@
#include <gdal/gdal.h> #include <gdal/gdal.h>
#include "gdal/gdal_priv.h" #include "gdal/gdal_priv.h"
#include <gdal/gdal_frmts.h> #include <gdal/gdal_frmts.h>
//#include "matplotlibcpp.h"
#include "contour_creator.hh" #include "contour_creator.hh"
@ -32,6 +33,7 @@ HeightMap::HeightMap(const char* filepath)
//https://gdal.org/api/gdaldataset_cpp.html#_CPPv4N11GDALDataset15GetGeoTransformEPd //https://gdal.org/api/gdaldataset_cpp.html#_CPPv4N11GDALDataset15GetGeoTransformEPd
this->geotransform = (double *) CPLMalloc(sizeof(double)*6); this->geotransform = (double *) CPLMalloc(sizeof(double)*6);
this->reference_system = *(file->GetSpatialRef()); this->reference_system = *(file->GetSpatialRef());
this->filepath = filepath;
file->GetGeoTransform(this->geotransform); file->GetGeoTransform(this->geotransform);
@ -41,6 +43,7 @@ HeightMap::HeightMap(const char* filepath)
0, 0 ); 0, 0 );
if (error) { throw std::runtime_error("Could not read tif file!"); } if (error) { throw std::runtime_error("Could not read tif file!"); }
band->FlushCache(); band->FlushCache();
const int size = this->width*this->height;
} }
float HeightMap::get_pixel(int x, int y) float HeightMap::get_pixel(int x, int y)
{ {
@ -62,7 +65,7 @@ void HeightMap::blur(float standard_deviation)
*(kernel + i) = 1.0; *(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 parallel
{ {
#pragma omp for #pragma omp for
@ -91,3 +94,100 @@ void HeightMap::blur(float standard_deviation)
this->data = blurred; this->data = blurred;
blurred = nullptr; 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 min; //!< Minimum value in image
float max; //!< Maximum value in image float max; //!< Maximum value in image
OGRSpatialReference reference_system; OGRSpatialReference reference_system;
HeightMap(const char* filepath); HeightMap(const char* filepath);
const char* filepath;
float get_pixel(int x,int y); float get_pixel(int x,int y);
void blur(float standard_deviation); void blur(float standard_deviation);
void statistics();
void calculate_steepness();
}; };
/** /**

View File

@ -1,4 +1,6 @@
#include "contour_creator.hh" #include "contour_creator.hh"
#include <algorithm>
#include <array>
#include <gdal/ogr_api.h> #include <gdal/ogr_api.h>
#include "ogrsf_frmts.h" #include "ogrsf_frmts.h"
#include <gdal/ogr_core.h> #include <gdal/ogr_core.h>
@ -6,12 +8,15 @@
#include <gdal/ogr_geometry.h> #include <gdal/ogr_geometry.h>
#include <iostream> #include <iostream>
#include <ostream> #include <ostream>
#include <sys/types.h>
#include <tuple>
#include <vector> #include <vector>
#include <cstdint> #include <cstdint>
#include <gdal/gdal.h> #include <gdal/gdal.h>
#include "gdal/gdal_priv.h" #include "gdal/gdal_priv.h"
#include <gdal/gdal_frmts.h> #include <gdal/gdal_frmts.h>
#include <omp.h> #include <omp.h>
#include "argh.h"
std::vector<Point> produce_cellmap(HeightMap* heightmap, float z) std::vector<Point> produce_cellmap(HeightMap* heightmap, float z)
{ {
@ -32,11 +37,21 @@ std::vector<Point> produce_cellmap(HeightMap* heightmap, float z)
return points; return points;
} }
bool is_in(int value, std::array<int, 8> array)
{
return std::binary_search(array.begin(), array.end(), value);
}
std::vector<std::vector<Point>> create_lines(HeightMap* heightmap, int interval) std::vector<std::vector<Point>> create_lines(HeightMap* heightmap, int interval)
{ {
int num_contours = (heightmap->max - heightmap->min)/interval; int num_contours = (heightmap->max - heightmap->min)/interval;
std::vector<std::vector<Point>> vector_contours; std::vector<std::vector<Point>> vector_contours;
std::array<int,8> north{4, 5,6,7,8,9,10,11};
std::array<int,8> south{1,2,5,6,9,10,13,14};
std::array<int,8> east{2,3,4,5,10,11,12,13};
std::array<int,8> west{1,3,5,7,8,10,12,14};
#pragma omp parallel #pragma omp parallel
{ {
std::vector<std::vector<Point>> vec_private; std::vector<std::vector<Point>> vec_private;
@ -50,39 +65,44 @@ std::vector<std::vector<Point>> create_lines(HeightMap* heightmap, int interval)
int points_allocated = 0; int points_allocated = 0;
x = points[0].x; x = points[0].x;
y = points[0].y; y = points[0].y;
int current_case = points[0].mscase;
points[0].allocated = true; points[0].allocated = true;
line.push_back(points[0]); line.push_back(points[0]);
for (int j = 0; j < points.size(); j++){ for (int j = 0; j < points.size(); j++){
for (int k = 0; k < points.size(); k++){ for (int k = 0; k < points.size(); k++){
Point* candidate = &points[k]; Point* candidate = &points[k];
if (!candidate->allocated) { if (!candidate->allocated) {
if (candidate->x == x +1 && candidate->y == y) { if (candidate->x == x +1 && candidate->y == y /*&& is_in(current_case, east)*/) {
candidate->allocated = true; candidate->allocated = true;
x = candidate->x; x = candidate->x;
y = candidate->y; y = candidate->y;
current_case = candidate->mscase;
line.push_back(*candidate); line.push_back(*candidate);
points_allocated++; points_allocated++;
break; break;
} }
else if (candidate->x == x -1 && candidate->y == y) { else if (candidate->x == x -1 && candidate->y == y /*&& is_in(current_case, west)*/) {
x = candidate->x; x = candidate->x;
y = candidate->y; y = candidate->y;
current_case = candidate->mscase;
candidate->allocated = true; candidate->allocated = true;
line.push_back(*candidate); line.push_back(*candidate);
points_allocated++; points_allocated++;
break; break;
} }
else if (candidate->x == x && candidate->y == y + 1) { else if (candidate->x == x && candidate->y == y + 1 /*&& is_in(current_case, north)*/) {
x = candidate->x; x = candidate->x;
y = candidate->y; y = candidate->y;
current_case = candidate->mscase;
candidate->allocated = true; candidate->allocated = true;
line.push_back(*candidate); line.push_back(*candidate);
points_allocated++; points_allocated++;
break; break;
} }
else if (candidate->x == x && candidate->y == y - 1) { else if (candidate->x == x && candidate->y == y - 1 /*&& is_in(current_case, south)*/) {
x = candidate->x; x = candidate->x;
y = candidate->y; y = candidate->y;
current_case = candidate->mscase;
candidate->allocated = true; candidate->allocated = true;
line.push_back(*candidate); line.push_back(*candidate);
points_allocated++; points_allocated++;
@ -100,8 +120,10 @@ std::vector<std::vector<Point>> create_lines(HeightMap* heightmap, int interval)
if (!candidate->allocated) if (!candidate->allocated)
{ {
line.push_back(*candidate); line.push_back(*candidate);
candidate->allocated = true;
x = candidate->x; x = candidate->x;
y = candidate->y; y = candidate->y;
current_case = candidate->mscase;
points_allocated++; points_allocated++;
break; break;
} }
@ -129,20 +151,35 @@ std::tuple<double, double> local_to_projected(double* geotransform, int x, int y
constexpr std::tuple<double, double, double, double> marching_squares_lookup(int mcase) constexpr std::tuple<double, double, double, double> marching_squares_lookup(int mcase)
{ {
switch (mcase) { switch (mcase) {
case 1: return {0, 0.5, 0.5, 0}; case 1:
case 2: return {1, 0.5, 0.5, 0}; return {0, 0.5, 0.5, 0};
case 3: return {0, 0.5, 1, 0.5}; case 14:
case 4: return {0.5, 1, 1, 0.5}; return {0.5, 0, 0, 0.5};
case 5: return {0, 0, 0, 0}; //FIXME case 2:
case 6: return {0.5, 1, 0.5, 0}; return {0.5, 0, 1, 0.5};
case 7: return {0, 0.5, 0.5, 1}; case 13:
case 8: return {0, 0.5, 0.5, 1}; return {1, 0.5, 0.5, 0};
case 9: return {0.5, 1, 0.5, 0}; case 3:
case 10: return {0, 0, 0, 0}; //FIXME return {0, 0.5, 1, 0.5};
case 11: return {0.5, 1, 1, 0.5}; case 12:
case 12: return {0, 0.5, 1, 0.5}; return {1, 0.5, 0, 0.5};
case 13: return {0.5, 0, 1, 0.5}; case 4:
case 14: return {0, 0.5, 0.5, 0}; return {0.5, 1, 1, 0.5};
case 11:
return {1, 0.5, 0.5, 1};
case 5:
case 10:
return {0, 0, 0, 0}; //FIXME
case 6:
return {0.5, 1, 0.5, 0};
case 9:
return {0.5, 0, 0.5, 1};
case 7:
return {0, 0.5, 0.5, 1};
case 8:
return {0.5, 0.5, 0, 0.5};
}; };
} }
void write_output_file(std::vector<std::vector<Point>> all_points, const char *filepath, HeightMap* heightmap) void write_output_file(std::vector<std::vector<Point>> all_points, const char *filepath, HeightMap* heightmap)
@ -200,13 +237,40 @@ void write_output_file(std::vector<std::vector<Point>> all_points, const char *f
std::vector<Point> points = all_points[j]; std::vector<Point> points = all_points[j];
bool left_to_right = true;
if ((points[0].y - points[1].y) < 0)
left_to_right = true;
else
left_to_right = false;
for (int k = 0; k < points.size(); k++) for (int k = 0; k < points.size(); k++)
{ {
bool left_to_right = true;
if ((points[k].x - points[k-1].x) < 0)
left_to_right = false;
bool top_to_bottom = true;
if ((points[k].y - points[k-1].y) < 0)
left_to_right = false;
int x_raw = points[k].x; int x_raw = points[k].x;
int y_raw = points[k].y; int y_raw = points[k].y;
auto [x, y] = local_to_projected(heightmap->geotransform, x_raw, y_raw); auto [x, y] = local_to_projected(heightmap->geotransform, x_raw, y_raw);
//auto [x_bl, y_bl, x_tr, y_tr] = marching_squares_lookup(*(cellmap->cells + i) );
geometry->setPoint(geometry->getNumPoints(),x ,y ); auto [x1, y1, x2, y2] = marching_squares_lookup(points[k].mscase);
if (left_to_right or !top_to_bottom)
{
geometry->setPoint(geometry->getNumPoints(),x + (x1/6) ,y + (y1/6));
geometry->setPoint(geometry->getNumPoints(),x + (x2/6) ,y + (y2/6));
}
else {
geometry->setPoint(geometry->getNumPoints(),x + (x2/6) ,y + (y2/6));
geometry->setPoint(geometry->getNumPoints(),x + (x1/6) ,y + (y1/6));
}
} }
if ( feature->SetGeometry(geometry) != OGRERR_NONE) if ( feature->SetGeometry(geometry) != OGRERR_NONE)
@ -227,14 +291,37 @@ void write_output_file(std::vector<std::vector<Point>> all_points, const char *f
int main(int argc, const char* argv[]) int main(int argc, const char* argv[])
{ {
const char* filepath = argv[1]; argh::parser cmdl(argv);
HeightMap map(filepath);
std::cout << "x: " << map.width << " y: " << map.height << "\n"; if (cmdl[{ "-h", "--help" }])
std::cout << "max: " << map.max << " min: " << map.min << "\n"; {
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";
std::string output_file;
cmdl({"-o", "--output"}, "contours.geojson") >> output_file;
std::string input_file;
cmdl(1) >> input_file;
HeightMap map(input_file.c_str());
if (cmdl[{"-b", "--blur"}])
map.blur(0.8); map.blur(0.8);
auto lines = create_lines(&map, 5); if (cmdl[{"--stats"}])
write_output_file(lines, "out.geojson", &map); map.statistics();
auto lines = create_lines(&map, interval);
write_output_file(lines, output_file.c_str(), &map);
std::cout << "Contours written to " << output_file << " 🗺️\n";
} }