From ee0b87c2b3494f5c9d1b330445369eb064429f06 Mon Sep 17 00:00:00 2001 From: Trygve Date: Fri, 26 Apr 2024 15:40:09 +0200 Subject: [PATCH] Start to implement line drawing --- src/main.cpp | 59 +++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 23826ca..486af9e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,9 @@ #include "contour_creator.hh" #include #include "ogrsf_frmts.h" +#include +#include +#include #include #include #include @@ -86,7 +89,7 @@ void write_output_file(std::vector cellmaps, const char *filepath) 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 ) { printf( "Layer creation failed.\n" ); @@ -105,41 +108,41 @@ void write_output_file(std::vector cellmaps, const char *filepath) for (int j = 0; j < cellmaps.size(); j++) { CellMap* cellmap = &cellmaps[j]; - for (int i = 0; iheight*cellmap->width; i++) { + OGRFeature *feature; + OGRLineString *geometry; + feature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() ); + feature->SetField( "Name", j ); + for (int i = 0; i < cellmap->height*cellmap->width; i++) + { if (*(cellmap->cells + i) != 0 && *(cellmap->cells + i) != 15) { - OGRFeature *poFeature; - poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() ); - poFeature->SetField( "Name", *(cellmap->cells + i) ); - - //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 y_raw = i/cellmap->width; + + std::cout << "🤬"; std::cout.flush(); + + auto [x, y] = local_to_projected(cellmap->geotransform, x_raw, y_raw); + - pt.setX( x ); - pt.setY( y ); - - poFeature->SetGeometry( &pt ); - - if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE ) - { - printf( "Failed to create feature in shapefile.\n" ); - exit( 1 ); - } - - OGRFeature::DestroyFeature( poFeature ); + geometry->setPoint( i, x, y ); + } + } + + if ( feature->SetGeometry(geometry) != 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" ); + exit( 1 ); + } + OGRFeature::DestroyFeature( feature ); } GDALClose( poDS );