Pawel Netzel - software
Repositories
Help
Report an Issue
plClump4p
Code
Commits
Branches
Tags
Search
Tree:
38f25a5
Branches
Tags
master
plClump4p
main.c
Initial commit
netzel
commited
38f25a5
at 2024-02-21 22:59:53
main.c
Blame
History
Raw
/**************************************************************************** * * PROGRAM: clump4p * * AUTHOR(S): Pawel Netzel - SIL UC * Jarek Jasiewicz - SIL UC * (the clumping algorithm for a region) * * PURPOSE: Recategorizes data in a raster map layer by grouping cells * that form physically discrete areas into unique categories. * * COPYRIGHT: (C) 2012 by Pawel Netzel * * This program is a free software under the GNU General Public * License (>=v2). * ***************************************************************************/ #include <stdlib.h> #include <string.h> #include <unistd.h> #include <omp.h> #include <gdal.h> #include <ezgdal.h> #include <argtable3.h> #include "data.h" #include "main.h" void usage(char *progname, void *argtable) { printf("\nclump4p recategorizes data in a raster map by grouping cells \n"); printf("that form physically discrete areas into unique categories.\n"); printf("\nclump4p uses \"divide and conquer\" algorithm to speed up \n"); printf("the clumping process. By default, clumps are generated by the four \n"); printf("directions clumping algorithm. One can change this to the eight directions\n"); printf("clumping algorithm (flag -8).\n"); printf("\nUsage:\n\t%s", progname); arg_print_syntax(stdout,argtable,"\n"); printf("\n"); arg_print_glossary_gnu(stdout,argtable); printf("\n"); exit(0); } int main(int argc, char *argv[]) { EZGDAL_LAYER *in_fd, *tmp_fd, *out_fd; char *OUTPUT; char *INPUT; char TEMPLAYER[1024]; int DIRECTIONS=4; struct arg_str *inp = arg_str1("i","input","<file_name>","name of input files (GeoTIFF)"); struct arg_str *out = arg_str1("o","output","<file_name>","name of output file with clumps (GeoTIFF)"); struct arg_int *th = arg_int0("t",NULL,"<n>","number of threads (default: 1)"); struct arg_lit *dirs = arg_lit0("8",NULL,"8 direction connecting"); struct arg_lit *local = arg_lit0("l",NULL,"temporary files in local directory (default: /tmp)"); struct arg_str *path = arg_str0(NULL,"path","<existing_path>","location for temporary files (default: /tmp)"); struct arg_lit *help = arg_lit0("h","help","print help and exit"); struct arg_end *end = arg_end(20); void* argtable[] = {inp,out,th,dirs,local,path,help,end}; int nerrors = arg_parse(argc,argv,argtable); if (help->count > 0) usage(argv[0],argtable); if (nerrors > 0) { arg_print_errors(stdout,end,argv[0]); usage(argv[0],argtable); } if(th->count > 0) omp_set_num_threads(th->ival[0]); else omp_set_num_threads(1); INPUT = (char *)inp->sval[0]; OUTPUT = (char *)out->sval[0]; if(dirs->count>0) DIRECTIONS=8; int pid = getpid(); memset(TEMPLAYER,0,1024); if(local->count>0) sprintf(TEMPLAYER,"tmp_clump_%06d.tif",pid); else if(path->count>0) sprintf(TEMPLAYER,"%s/tmp_clump_%06d.tif",path->sval[0],pid); else sprintf(TEMPLAYER,"/tmp/tmp_clump_%06d.tif",pid); if(!ezgdal_file_exists(INPUT)) { printf("\nThe file '%s' does not exist!\n\n",INPUT); exit(0); } printf("Opening input file..."); in_fd = ezgdal_open_layer(INPUT); if(in_fd==NULL) { printf("\nThe file '%s' can't be opened!\n\n",INPUT); exit(0); } else printf(" OK\n"); initVariables(in_fd); int nodata = -9999; printf("Creating temporary file..."); tmp_fd = ezgdal_create_layer(TEMPLAYER, ezgdal_layer_get_wkt(in_fd), "Int32", ezgdal_layer_get_at(in_fd), in_fd->rows, in_fd->cols, &nodata, EZGDAL_COMPRESS_NONE ); if(tmp_fd==NULL) { printf("\nThe file '%s' can't be created!\n\n",TEMPLAYER); exit(0); } else printf("OK\n"); printf("Clumping... "); clump(in_fd, tmp_fd, DIRECTIONS); printf(" OK\n"); ezgdal_close_layer(in_fd); ezgdal_close_layer(tmp_fd); printf("Creating output file..."); tmp_fd = ezgdal_open_layer(TEMPLAYER); out_fd = ezgdal_create_layer(OUTPUT, ezgdal_layer_get_wkt(tmp_fd), "Int32", ezgdal_layer_get_at(tmp_fd), tmp_fd->rows, tmp_fd->cols, &nodata, EZGDAL_COMPRESS_NONE ); if(out_fd==NULL) { printf("\nThe file '%s' can't be created!\n\n",OUTPUT); exit(0); } else printf(" OK\n"); replaceLabels(tmp_fd,out_fd); ezgdal_close_layer(out_fd); ezgdal_close_layer(tmp_fd); freeVariables(); unlink(TEMPLAYER); exit(EXIT_SUCCESS); }