mirror of
https://gitlab.com/Trygve/contour-creator.git
synced 2024-11-09 16:20:15 +00:00
Compare commits
5 Commits
0485253893
...
29a7ddb083
Author | SHA1 | Date | |
---|---|---|---|
29a7ddb083 | |||
98a312094e | |||
882764a13d | |||
f962a40ade | |||
ee0b87c2b3 |
@ -1,7 +1,7 @@
|
|||||||
#include <gdal/cpl_conv.h>
|
#include <gdal/cpl_conv.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <omp.h>
|
||||||
#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>
|
||||||
@ -49,3 +49,45 @@ float HeightMap::get_pixel(int x, int y)
|
|||||||
//std::cout << " offset: " << offset << " " << x << ","<< y;
|
//std::cout << " offset: " << offset << " " << x << ","<< y;
|
||||||
return *(this->data + offset);
|
return *(this->data + offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HeightMap::blur(float standard_deviation)
|
||||||
|
{
|
||||||
|
// standard_deviation does not do anything yet. This is currently a simple box blur
|
||||||
|
int kernel_height = 5;
|
||||||
|
int kernel_width = 5;
|
||||||
|
int kernel_size = kernel_height*kernel_width;
|
||||||
|
float* kernel = (float*) CPLMalloc(sizeof(float)*kernel_size);
|
||||||
|
for (int i=0; i<kernel_size; i++)
|
||||||
|
{
|
||||||
|
*(kernel + i) = 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
float* blurred = (float *) CPLMalloc(sizeof(float)*this->width*this->height);
|
||||||
|
#pragma omp parallel
|
||||||
|
{
|
||||||
|
#pragma omp for
|
||||||
|
for (int i = 0; i < this->height * this->width; i++)
|
||||||
|
{
|
||||||
|
float blurred_pixel = 0;
|
||||||
|
for (int j = 0; j < kernel_size; j++)
|
||||||
|
{
|
||||||
|
int x = j%kernel_width - kernel_width/2 + i%this->width;
|
||||||
|
int y = j/kernel_width - kernel_height/2 + i/this->width;
|
||||||
|
if (x<0 || x>=this->width)
|
||||||
|
{
|
||||||
|
x = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (y<0 || y>=this->height)
|
||||||
|
{
|
||||||
|
y = 0;
|
||||||
|
}
|
||||||
|
blurred_pixel += this->get_pixel(x, y);
|
||||||
|
}
|
||||||
|
*(blurred + i) = blurred_pixel/float(kernel_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(this->data);
|
||||||
|
this->data = blurred;
|
||||||
|
blurred = nullptr;
|
||||||
|
}
|
@ -19,6 +19,7 @@ class HeightMap
|
|||||||
|
|
||||||
HeightMap(const char* filepath);
|
HeightMap(const char* filepath);
|
||||||
float get_pixel(int x,int y);
|
float get_pixel(int x,int y);
|
||||||
|
void blur(float standard_deviation);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
105
src/main.cpp
105
src/main.cpp
@ -1,6 +1,9 @@
|
|||||||
#include "contour_creator.hh"
|
#include "contour_creator.hh"
|
||||||
#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_feature.h>
|
||||||
|
#include <gdal/ogr_geometry.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -53,17 +56,36 @@ std::vector<CellMap> vector_cellmap(HeightMap* heightmap, int interval)
|
|||||||
std::tuple<double, double> local_to_projected(double* geotransform, int x, int y)
|
std::tuple<double, double> local_to_projected(double* geotransform, int x, int y)
|
||||||
{
|
{
|
||||||
return {
|
return {
|
||||||
geotransform[1] * x + geotransform[2] * y +
|
geotransform[1] * x + geotransform[2] * y +
|
||||||
geotransform[1] * 0.5 + geotransform[2] * 0.5 + geotransform[0],
|
geotransform[1] * 0.5 + geotransform[2] * 0.5 + geotransform[0],
|
||||||
geotransform[4] * x + geotransform[5] * y +
|
geotransform[4] * x + geotransform[5] * y +
|
||||||
geotransform[4] * 0.5 + geotransform[5] * 0.5 + geotransform[3]
|
geotransform[4] * 0.5 + geotransform[5] * 0.5 + geotransform[3]
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constexpr std::tuple<double, double, double, double> marching_squares_lookup(int mcase)
|
||||||
|
{
|
||||||
|
switch (mcase) {
|
||||||
|
case 1: return {0, 0.5, 0.5, 0};
|
||||||
|
case 2: return {1, 0.5, 0.5, 0};
|
||||||
|
case 3: return {0, 0.5, 1, 0};
|
||||||
|
case 4: return {0.5, 1, 1, 0.5};
|
||||||
|
case 5: return {0, 0, 0, 0}; //FIXME
|
||||||
|
case 6: return {0.5, 1, 0.5, 0};
|
||||||
|
case 7: return {0, 0.5, 0.5, 1};
|
||||||
|
case 8: return {0, 0.5, 0.5, 1};
|
||||||
|
case 9: return {0.5, 1, 0.5, 0};
|
||||||
|
case 10: return {0, 0, 0, 0}; //FIXME
|
||||||
|
case 11: return {0.5, 1, 1, 0.5};
|
||||||
|
case 12: return {0, 0.5, 1, 0.5};
|
||||||
|
case 13: return {0.5, 0, 1, 0.5};
|
||||||
|
case 14: return {0, 0.5, 0.5, 0};
|
||||||
|
};
|
||||||
|
}
|
||||||
void write_output_file(std::vector<CellMap> cellmaps, const char *filepath)
|
void write_output_file(std::vector<CellMap> cellmaps, const char *filepath)
|
||||||
{
|
{
|
||||||
|
|
||||||
const char *pszDriverName = "ESRI Shapefile";
|
const char *pszDriverName = "GeoJSON";
|
||||||
GDALDriver *poDriver;
|
GDALDriver *poDriver;
|
||||||
|
|
||||||
GDALAllRegister();
|
GDALAllRegister();
|
||||||
@ -86,7 +108,7 @@ void write_output_file(std::vector<CellMap> cellmaps, const char *filepath)
|
|||||||
|
|
||||||
OGRLayer *poLayer;
|
OGRLayer *poLayer;
|
||||||
|
|
||||||
poLayer = poDS->CreateLayer( "contours", &(cellmaps[0]).reference_system, wkbPoint, NULL );
|
poLayer = poDS->CreateLayer( "contours", &(cellmaps[0]).reference_system, wkbLineString, NULL );
|
||||||
if( poLayer == NULL )
|
if( poLayer == NULL )
|
||||||
{
|
{
|
||||||
printf( "Layer creation failed.\n" );
|
printf( "Layer creation failed.\n" );
|
||||||
@ -105,53 +127,44 @@ void write_output_file(std::vector<CellMap> cellmaps, const char *filepath)
|
|||||||
for (int j = 0; j < cellmaps.size(); j++)
|
for (int j = 0; j < cellmaps.size(); j++)
|
||||||
{
|
{
|
||||||
CellMap* cellmap = &cellmaps[j];
|
CellMap* cellmap = &cellmaps[j];
|
||||||
for (int i = 0; i<cellmap->height*cellmap->width; i++) {
|
|
||||||
|
for (int i = 0; i < cellmap->height*cellmap->width; i++)
|
||||||
|
{
|
||||||
if (*(cellmap->cells + i) != 0 && *(cellmap->cells + i) != 15)
|
if (*(cellmap->cells + i) != 0 && *(cellmap->cells + i) != 15)
|
||||||
{
|
{
|
||||||
OGRFeature *poFeature;
|
OGRFeature *feature;
|
||||||
poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
|
OGRLineString *geometry = new OGRLineString();
|
||||||
poFeature->SetField( "Name", *(cellmap->cells + i) );
|
feature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
|
||||||
|
feature->SetField( "Name", j );
|
||||||
//OGRSpatialReference local;
|
|
||||||
|
|
||||||
//auto poCT = OGRCreateCoordinateTransformation( &local, &cellmap->reference_system );
|
|
||||||
|
|
||||||
/*
|
|
||||||
if( poCT == NULL || !poCT->Transform( 1, &x, &y ) )
|
|
||||||
printf( "Transformation failed.\n" );
|
|
||||||
*/
|
|
||||||
OGRPoint pt;
|
|
||||||
|
|
||||||
int x_raw = i%cellmap->width;
|
int x_raw = i%cellmap->width;
|
||||||
int y_raw = i/cellmap->width;
|
int y_raw = i/cellmap->width;
|
||||||
auto [x, y] = local_to_projected(cellmap->geotransform, x_raw, y_raw);
|
auto [x, y] = local_to_projected(cellmap->geotransform, x_raw, y_raw);
|
||||||
|
auto [x_bl, y_bl, x_tr, y_tr] = marching_squares_lookup(*(cellmap->cells + i) );
|
||||||
|
//std::cout << x << ", " << y << " ";
|
||||||
|
geometry->setPoint( geometry->getNumPoints(), x+x_tr*0.25, y+y_tr*0.25 );
|
||||||
|
geometry->setPoint( geometry->getNumPoints(), x+x_bl*0.25, y+y_bl*0.25 );
|
||||||
|
|
||||||
pt.setX( x );
|
|
||||||
pt.setY( y );
|
|
||||||
|
|
||||||
poFeature->SetGeometry( &pt );
|
if ( feature->SetGeometry(geometry) != OGRERR_NONE)
|
||||||
|
{
|
||||||
if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE )
|
printf( "Failed to set geometry.\n" );
|
||||||
|
exit( 1 );
|
||||||
|
}
|
||||||
|
OGRGeometryFactory::destroyGeometry(geometry);
|
||||||
|
if( poLayer->CreateFeature( feature ) != OGRERR_NONE )
|
||||||
{
|
{
|
||||||
printf( "Failed to create feature in shapefile.\n" );
|
printf( "Failed to create feature in shapefile.\n" );
|
||||||
exit( 1 );
|
exit( 1 );
|
||||||
}
|
}
|
||||||
|
OGRFeature::DestroyFeature( feature );
|
||||||
|
}
|
||||||
|
|
||||||
OGRFeature::DestroyFeature( poFeature );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GDALClose( poDS );
|
GDALClose( poDS );
|
||||||
/*
|
|
||||||
OGRDataSourceH datasource = OGR_Dr_CreateDataSource(OGRGetDriverByName("GeoJSON"), "contour.geojson", new char*);
|
|
||||||
OGRSpatialReference reference_system = cellmap->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)));
|
|
||||||
|
|
||||||
OGRGeometryH geometry = OGR_G_CreateGeometry(wkbLineString25D);
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char* argv[])
|
int main(int argc, const char* argv[])
|
||||||
@ -162,20 +175,10 @@ int main(int argc, const char* argv[])
|
|||||||
std::cout << "x: " << map.width << " y: " << map.height << "\n";
|
std::cout << "x: " << map.width << " y: " << map.height << "\n";
|
||||||
std::cout << "max: " << map.max << " min: " << map.min << "\n";
|
std::cout << "max: " << map.max << " min: " << map.min << "\n";
|
||||||
|
|
||||||
|
map.blur(0.8);
|
||||||
|
|
||||||
auto cellmap = produce_cellmap(&map, 40);
|
auto cellmap = produce_cellmap(&map, 40);
|
||||||
/*
|
|
||||||
for (int y = 0; y < cellmap.height; y++)
|
|
||||||
{
|
|
||||||
for (int x = 0; x < cellmap.width; x++)
|
|
||||||
{
|
|
||||||
if (cellmap.get_cell(x, y) && cellmap.get_cell(x, y)!=15) {
|
|
||||||
std::cout << x << ","<< y << "=" << cellmap.get_cell(x, y) << " ";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//std::cout << "\n";
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
auto cellmaps = vector_cellmap(&map, 5);
|
auto cellmaps = vector_cellmap(&map, 5);
|
||||||
write_output_file(cellmaps, "out.shp");
|
write_output_file(cellmaps, "out.geojson");
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user