mirror of
https://gitlab.com/Trygve/contour-creator.git
synced 2026-03-15 00:14:04 +00:00
Compare commits
2 Commits
Statistics
...
cli-argume
| Author | SHA1 | Date | |
|---|---|---|---|
| 43f8b78830 | |||
| 08255875c1 |
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "extern/argh"]
|
||||||
|
path = extern/argh
|
||||||
|
url = https://github.com/adishavit/argh.git
|
||||||
@@ -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)
|
|
||||||
@@ -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
1
extern/argh
vendored
Submodule
Submodule extern/argh added at 431bf323ac
@@ -1,4 +1,3 @@
|
|||||||
#include <cstddef>
|
|
||||||
#include <gdal/cpl_conv.h>
|
#include <gdal/cpl_conv.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
@@ -6,7 +5,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"
|
||||||
|
|
||||||
|
|
||||||
@@ -33,7 +32,6 @@ 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);
|
||||||
|
|
||||||
@@ -43,7 +41,6 @@ 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)
|
||||||
{
|
{
|
||||||
@@ -65,7 +62,7 @@ void HeightMap::blur(float standard_deviation)
|
|||||||
*(kernel + i) = 1.0;
|
*(kernel + i) = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
float* blurred = new float[this->width*this->height];
|
float* blurred = (float *) CPLMalloc(sizeof(float)*this->width*this->height);
|
||||||
#pragma omp parallel
|
#pragma omp parallel
|
||||||
{
|
{
|
||||||
#pragma omp for
|
#pragma omp for
|
||||||
@@ -94,100 +91,3 @@ 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);
|
|
||||||
}
|
|
||||||
@@ -16,12 +16,10 @@ 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();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
44
src/main.cpp
44
src/main.cpp
@@ -12,8 +12,7 @@
|
|||||||
#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 "matplotlibcpp.h"
|
#include "argh.h"
|
||||||
//namespace plt = matplotlibcpp;
|
|
||||||
|
|
||||||
std::vector<Point> produce_cellmap(HeightMap* heightmap, float z)
|
std::vector<Point> produce_cellmap(HeightMap* heightmap, float z)
|
||||||
{
|
{
|
||||||
@@ -229,25 +228,34 @@ 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";
|
||||||
|
|
||||||
map.blur(0.8);
|
std::string output_file;
|
||||||
|
cmdl({"-o", "--output"}, "contours.geojson") >> output_file;
|
||||||
|
|
||||||
map.statistics();
|
std::string input_file;
|
||||||
|
cmdl(1) >> input_file;
|
||||||
map.calculate_steepness();
|
|
||||||
|
|
||||||
auto lines = create_lines(&map, 5);
|
|
||||||
write_output_file(lines, "out.geojson", &map);
|
|
||||||
|
|
||||||
|
|
||||||
std::vector<int> y = {1, 2, 3};
|
HeightMap map(input_file.c_str());
|
||||||
//plt::plot(y, "bo-");
|
if (cmdl[{"-b", "--blur"}])
|
||||||
//plt::show();
|
map.blur(0.8);
|
||||||
|
auto lines = create_lines(&map, interval);
|
||||||
return 0;
|
write_output_file(lines, output_file.c_str(), &map);
|
||||||
|
std::cout << "Contours written to " << output_file << " 🗺️\n";
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user