mirror of
https://gitlab.com/Trygve/contour-creator.git
synced 2024-12-04 21:10:17 +00:00
Compare commits
4 Commits
01ddd4e914
...
040e011a66
Author | SHA1 | Date | |
---|---|---|---|
040e011a66 | |||
|
cf72fbfefa | ||
40d41fc065 | |||
2ee2936cf1 |
@ -1,13 +1,15 @@
|
|||||||
project(
|
project(
|
||||||
contour-creator
|
contour-creator
|
||||||
LANGUAGES CXX)
|
LANGUAGES CXX)
|
||||||
cmake_minimum_required(VERSION 3.0)
|
|
||||||
|
|
||||||
find_package(GDAL CONFIG REQUIRED)
|
find_package(GDAL CONFIG REQUIRED)
|
||||||
|
|
||||||
add_executable(${PROJECT_NAME}
|
add_executable(${PROJECT_NAME}
|
||||||
src/HeightMap.cpp src/CaseMap.cpp src/main.cpp
|
src/HeightMap.cpp src/CaseMap.cpp src/main.cpp
|
||||||
)
|
)
|
||||||
|
find_package(OpenMP)
|
||||||
|
if(OpenMP_CXX_FOUND)
|
||||||
|
target_link_libraries(${PROJECT_NAME} PUBLIC OpenMP::OpenMP_CXX)
|
||||||
|
endif()
|
||||||
|
|
||||||
target_link_libraries(${PROJECT_NAME} GDAL::GDAL)
|
target_link_libraries(${PROJECT_NAME} GDAL::GDAL)
|
@ -40,6 +40,6 @@ float HeightMap::get_pixel(int x, int y)
|
|||||||
{
|
{
|
||||||
// all the pixels are in an array of floats from left to right, top to bottom
|
// all the pixels are in an array of floats from left to right, top to bottom
|
||||||
int offset = ((this->width * y) + x);
|
int offset = ((this->width * y) + x);
|
||||||
std::cout << " " << x << ","<< y;
|
//std::cout << " offset: " << offset << " " << x << ","<< y;
|
||||||
return *(this->data + offset);
|
return *(this->data + offset);
|
||||||
}
|
}
|
||||||
|
318
src/main.cpp
318
src/main.cpp
@ -1,146 +1,174 @@
|
|||||||
#include "HeightMap.hh"
|
#include "HeightMap.hh"
|
||||||
#include "CaseMap.hh"
|
#include "CaseMap.hh"
|
||||||
#include <gdal/ogr_api.h>
|
#include <gdal/ogr_api.h>
|
||||||
#include "ogrsf_frmts.h"
|
#include "ogrsf_frmts.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#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>
|
||||||
CaseMap produce_casemap(HeightMap* heightmap, float z)
|
|
||||||
{
|
CaseMap produce_casemap(HeightMap* heightmap, float z)
|
||||||
int length = (heightmap->width-1)*(heightmap->height-1);
|
{
|
||||||
uint8_t *cases = (uint8_t *) CPLMalloc(sizeof(uint8_t)*length);
|
int length = (heightmap->width-1)*(heightmap->height-1);
|
||||||
for (int i = 0; i<length; i++) {
|
uint8_t *cases = (uint8_t *) CPLMalloc(sizeof(uint8_t)*length);
|
||||||
int y = i/(heightmap->width-1);
|
for (int i = 0; i<length; i++) {
|
||||||
int x = i%(heightmap->width-1);
|
int y = i/(heightmap->width-1);
|
||||||
uint8_t result = (heightmap->get_pixel(x,y)>z) +
|
int x = i%(heightmap->width-1);
|
||||||
(heightmap->get_pixel(x+1,y)>z)*2 +
|
uint8_t result = (heightmap->get_pixel(x,y)>z) +
|
||||||
(heightmap->get_pixel(x+1,y+1)>z)*4 +
|
(heightmap->get_pixel(x+1,y)>z)*2 +
|
||||||
(heightmap->get_pixel(x,y+1)>z)*8;
|
(heightmap->get_pixel(x+1,y+1)>z)*4 +
|
||||||
*(cases + i) = result;
|
(heightmap->get_pixel(x,y+1)>z)*8;
|
||||||
}
|
*(cases + i) = result;
|
||||||
|
}
|
||||||
return CaseMap(heightmap->width-1, heightmap->height-1, cases, heightmap->reference_system);
|
|
||||||
|
return CaseMap(heightmap->width-1, heightmap->height-1, cases, heightmap->reference_system);
|
||||||
|
|
||||||
}
|
|
||||||
|
}
|
||||||
void write_output_file(CaseMap* casemap, const char *filepath)
|
|
||||||
{
|
std::vector<CaseMap> vector_casemap(HeightMap* heightmap, int interval)
|
||||||
|
{
|
||||||
const char *pszDriverName = "ESRI Shapefile";
|
int num_contours = (heightmap->max - heightmap->min)/interval;
|
||||||
GDALDriver *poDriver;
|
std::vector<CaseMap> vector_contours;
|
||||||
|
omp_set_num_threads(12);
|
||||||
GDALAllRegister();
|
|
||||||
|
#pragma omp parallel
|
||||||
poDriver = GetGDALDriverManager()->GetDriverByName(pszDriverName );
|
{
|
||||||
if( poDriver == NULL )
|
std::vector<CaseMap> vec_private;
|
||||||
{
|
#pragma omp for
|
||||||
printf( "%s driver not available.\n", pszDriverName );
|
for (int i = 1; i <= num_contours; i++)
|
||||||
exit( 1 );
|
{
|
||||||
}
|
vec_private.push_back(produce_casemap(heightmap, heightmap->min + interval*i));
|
||||||
|
std::cout << "Execute thread " << omp_get_num_threads() << " ";
|
||||||
GDALDataset *poDS;
|
}
|
||||||
|
#pragma omp critical
|
||||||
poDS = poDriver->Create( filepath, 0, 0, 0, GDT_Unknown, NULL );
|
vector_contours.insert(vector_contours.end(), vec_private.begin(), vec_private.end());
|
||||||
if( poDS == NULL )
|
|
||||||
{
|
}
|
||||||
printf( "Creation of output file failed.\n" );
|
return vector_contours;
|
||||||
exit( 1 );
|
}
|
||||||
}
|
|
||||||
|
void write_output_file(std::vector<CaseMap> casemaps, const char *filepath)
|
||||||
OGRLayer *poLayer;
|
{
|
||||||
|
|
||||||
poLayer = poDS->CreateLayer( "contours", &casemap->reference_system, wkbPoint, NULL );
|
const char *pszDriverName = "ESRI Shapefile";
|
||||||
if( poLayer == NULL )
|
GDALDriver *poDriver;
|
||||||
{
|
|
||||||
printf( "Layer creation failed.\n" );
|
GDALAllRegister();
|
||||||
exit( 1 );
|
|
||||||
}
|
poDriver = GetGDALDriverManager()->GetDriverByName(pszDriverName );
|
||||||
|
if( poDriver == NULL )
|
||||||
OGRFieldDefn oField( "Name", OFTString );
|
{
|
||||||
|
printf( "%s driver not available.\n", pszDriverName );
|
||||||
oField.SetWidth(32);
|
exit( 1 );
|
||||||
|
}
|
||||||
if( poLayer->CreateField( &oField ) != OGRERR_NONE )
|
|
||||||
{
|
GDALDataset *poDS;
|
||||||
printf( "Creating Name field failed.\n" );
|
|
||||||
exit( 1 );
|
poDS = poDriver->Create( filepath, 0, 0, 0, GDT_Unknown, NULL );
|
||||||
}
|
if( poDS == NULL )
|
||||||
|
{
|
||||||
for (int i = 0; i<casemap->height*casemap->width; i++) {
|
printf( "Creation of output file failed.\n" );
|
||||||
if (*(casemap->cases + i) != 0 && *(casemap->cases + i) != 15)
|
exit( 1 );
|
||||||
{
|
}
|
||||||
int x_int = i%casemap->width;
|
|
||||||
int y_int = casemap->height*casemap->width - i/casemap->width;
|
OGRLayer *poLayer;
|
||||||
double x = double(x_int);
|
|
||||||
double y = double(y_int);
|
poLayer = poDS->CreateLayer( "contours", &(casemaps[0]).reference_system, wkbPoint, NULL );
|
||||||
|
if( poLayer == NULL )
|
||||||
OGRFeature *poFeature;
|
{
|
||||||
|
printf( "Layer creation failed.\n" );
|
||||||
poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
|
exit( 1 );
|
||||||
poFeature->SetField( "Name", *(casemap->cases + i) );
|
}
|
||||||
|
|
||||||
//OGRSpatialReference local;
|
OGRFieldDefn oField( "Name", OFTString );
|
||||||
|
|
||||||
//auto poCT = OGRCreateCoordinateTransformation( &local, &casemap->reference_system );
|
oField.SetWidth(32);
|
||||||
|
|
||||||
/*
|
if( poLayer->CreateField( &oField ) != OGRERR_NONE )
|
||||||
if( poCT == NULL || !poCT->Transform( 1, &x, &y ) )
|
{
|
||||||
printf( "Transformation failed.\n" );
|
printf( "Creating Name field failed.\n" );
|
||||||
*/
|
exit( 1 );
|
||||||
OGRPoint pt;
|
}
|
||||||
pt.setX( x );
|
for (int j = 0; j < casemaps.size(); j++)
|
||||||
pt.setY( y );
|
{
|
||||||
|
CaseMap* casemap = &casemaps[j];
|
||||||
poFeature->SetGeometry( &pt );
|
for (int i = 0; i<casemap->height*casemap->width; i++) {
|
||||||
|
if (*(casemap->cases + i) != 0 && *(casemap->cases + i) != 15)
|
||||||
if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE )
|
{
|
||||||
{
|
int x_int = i%casemap->width;
|
||||||
printf( "Failed to create feature in shapefile.\n" );
|
int y_int = casemap->height*casemap->width - i/casemap->width;
|
||||||
exit( 1 );
|
double x = double(x_int);
|
||||||
}
|
double y = double(y_int);
|
||||||
|
|
||||||
OGRFeature::DestroyFeature( poFeature );
|
OGRFeature *poFeature;
|
||||||
}
|
|
||||||
}
|
poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
|
||||||
|
poFeature->SetField( "Name", *(casemap->cases + i) );
|
||||||
GDALClose( poDS );
|
|
||||||
/*
|
//OGRSpatialReference local;
|
||||||
OGRDataSourceH datasource = OGR_Dr_CreateDataSource(OGRGetDriverByName("GeoJSON"), "contour.geojson", new char*);
|
|
||||||
OGRSpatialReference reference_system = casemap->reference_system;
|
//auto poCT = OGRCreateCoordinateTransformation( &local, &casemap->reference_system );
|
||||||
OGRLayerH layer = OGR_DS_CreateLayer(datasource, "contour", &reference_system, wkbLineString25D, new char*);
|
|
||||||
|
/*
|
||||||
auto feature = OGR_F_Create(OGR_L_GetLayerDefn(static_cast<OGRLayerH>(layer)));
|
if( poCT == NULL || !poCT->Transform( 1, &x, &y ) )
|
||||||
|
printf( "Transformation failed.\n" );
|
||||||
OGRGeometryH geometry = OGR_G_CreateGeometry(wkbLineString25D);
|
*/
|
||||||
*/
|
OGRPoint pt;
|
||||||
}
|
pt.setX( x );
|
||||||
|
pt.setY( y );
|
||||||
int main(int argc, const char* argv[])
|
|
||||||
{
|
poFeature->SetGeometry( &pt );
|
||||||
const char* filepath = argv[1];
|
|
||||||
HeightMap map(filepath);
|
if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE )
|
||||||
|
{
|
||||||
std::cout << "x: " << map.width << " y: " << map.height << "\n";
|
printf( "Failed to create feature in shapefile.\n" );
|
||||||
std::cout << "max: " << map.max << " min: " << map.min << "\n";
|
exit( 1 );
|
||||||
|
}
|
||||||
auto casemap = produce_casemap(&map, 40);
|
|
||||||
/*
|
OGRFeature::DestroyFeature( poFeature );
|
||||||
for (int y = 0; y < casemap.height; y++)
|
}
|
||||||
{
|
}
|
||||||
for (int x = 0; x < casemap.width; x++)
|
}
|
||||||
{
|
|
||||||
if (casemap.get_case(x, y) && casemap.get_case(x, y)!=15) {
|
GDALClose( poDS );
|
||||||
std::cout << x << ","<< y << "=" << casemap.get_case(x, y) << " ";
|
/*
|
||||||
}
|
OGRDataSourceH datasource = OGR_Dr_CreateDataSource(OGRGetDriverByName("GeoJSON"), "contour.geojson", new char*);
|
||||||
}
|
OGRSpatialReference reference_system = casemap->reference_system;
|
||||||
//std::cout << "\n";
|
OGRLayerH layer = OGR_DS_CreateLayer(datasource, "contour", &reference_system, wkbLineString25D, new char*);
|
||||||
}
|
|
||||||
*/
|
auto feature = OGR_F_Create(OGR_L_GetLayerDefn(static_cast<OGRLayerH>(layer)));
|
||||||
write_output_file(&casemap, "out.shp");
|
|
||||||
|
OGRGeometryH geometry = OGR_G_CreateGeometry(wkbLineString25D);
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, const char* argv[])
|
||||||
|
{
|
||||||
|
const char* filepath = argv[1];
|
||||||
|
HeightMap map(filepath);
|
||||||
|
|
||||||
|
std::cout << "x: " << map.width << " y: " << map.height << "\n";
|
||||||
|
std::cout << "max: " << map.max << " min: " << map.min << "\n";
|
||||||
|
|
||||||
|
auto casemap = produce_casemap(&map, 40);
|
||||||
|
/*
|
||||||
|
for (int y = 0; y < casemap.height; y++)
|
||||||
|
{
|
||||||
|
for (int x = 0; x < casemap.width; x++)
|
||||||
|
{
|
||||||
|
if (casemap.get_case(x, y) && casemap.get_case(x, y)!=15) {
|
||||||
|
std::cout << x << ","<< y << "=" << casemap.get_case(x, y) << " ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//std::cout << "\n";
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
auto casemaps = vector_casemap(&map, 5);
|
||||||
|
write_output_file(casemaps, "out.shp");
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user