Pawel Netzel - software
Repositories
Help
Report an Issue
plClump4p
Code
Commits
Branches
Tags
Search
Tree:
38f25a5
Branches
Tags
master
plClump4p
data.c
Initial commit
netzel
commited
38f25a5
at 2024-02-21 22:59:53
data.c
Blame
History
Raw
/**************************************************************************** * * PROGRAM: clump4p * * AUTHOR(S): Pawel Netzel - SIL UC * Jarek Jasiewicz - SIL UC * (clumping algorithm for 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 free software under the GNU General Public * License (>=v2). * ***************************************************************************/ #include <stdio.h> #include <ezgdal.h> #include "data.h" int totalIndex=0; int nRegions=0; int nRegionCols=0; int nRegionRows=0; Region *regions; int nDataCols=0; int nDataRows=0; int *borderDataBuffer, *borderResultBuffer; int **dataBuffer; int **resultBuffer; int **calcBuffer; void createRegions() { int i,j,rMin,rMax,c,nr; nRegionRows=(nDataRows-1)/REGION_SIZE+1; nRegionCols=(nDataCols-1)/REGION_SIZE+1; nRegions=nRegionRows*nRegionCols; regions=(Region *)malloc(nRegions*sizeof(Region)); rMin=0; nr=0; c=0; for(i=0; i<nRegionRows; i++) { rMax=rMin+REGION_SIZE-1; if(rMax>=nDataRows) rMax=nDataRows-1; for(j=0; j<nRegionCols; j++) { regions[nr].rowMin=rMin; regions[nr].rowMax=rMax; regions[nr].colMin=c; c+=REGION_SIZE-1; if(c>=nDataCols) c=nDataCols-1; regions[nr].colMax=c; regions[nr].nLabels=0; regions[nr].labels=(int *)malloc(INCR*sizeof(int)); regions[nr].labels[0]=0; if(i==0) regions[nr].regionUp=-1; else regions[nr].regionUp=(i-1)*nRegionCols+j; nr++; c+=1; } c=0; rMin=rMax+1; } } void dumpArray(int *arr, int num) { int i; for(i=0; i<num; i++) { if(i>0) printf(", "); printf("[%d]=%d",i,arr[i]); } } void dumpRegions(char *Comment) { int i; printf("%s\n",Comment); for(i=0; i<nRegions; i++) { printf("%d: c(%d,%d), r(%d,%d) nLab: %d, Labs: ", i, regions[i].colMin,regions[i].colMax, regions[i].rowMin,regions[i].rowMax, regions[i].nLabels); dumpArray(regions[i].labels,regions[i].nLabels+1); printf("\n"); } } void destroyRegions() { int i; for(i=0; i<nRegions; i++) free(regions[i].labels); free(regions); } void createBuffers() { dataBuffer=(int **)malloc(REGION_SIZE*sizeof(int*)); resultBuffer=(int **)malloc(REGION_SIZE*sizeof(int*)); int i,n; n=nDataCols*sizeof(int); for(i=0; i<REGION_SIZE; i++) { dataBuffer[i]=(int *)malloc(n); resultBuffer[i]=(int *)malloc(n); } borderDataBuffer=(int *)calloc(n,1); borderResultBuffer=(int *)calloc(n,1); } void destroyBuffers() { int i; free(borderDataBuffer); free(borderResultBuffer); for(i=0; i<REGION_SIZE; i++) { free(dataBuffer[i]); free(resultBuffer[i]); } free(dataBuffer); free(resultBuffer); } void loadDataBuffer(EZGDAL_LAYER *layer, int minRow, int maxRow) { int i,n,j; n=maxRow-minRow+1; for(i=0; i<n; i++) { ezgdal_read_buffer(layer,i+minRow); for(j=0; j<nDataCols; j++) dataBuffer[i][j]=(int)layer->buffer[j]; } } void saveResultBuffer(EZGDAL_LAYER *layer, int minRow, int maxRow) { int i,j,n; n=maxRow-minRow+1; for(i=0; i<n; i++) { for(j=0; j<nDataCols; j++) if(resultBuffer[i][j] == 0) ezgdal_set_null(layer, &(layer->buffer[j])); else layer->buffer[j] = (double)resultBuffer[i][j]; ezgdal_write_buffer(layer, i+minRow); } } void initVariables(EZGDAL_LAYER *layer) { nDataRows = layer->rows; nDataCols = layer->cols; totalIndex=0; createRegions(); createBuffers(); } void freeVariables() { destroyRegions(); destroyBuffers(); }