void output_geotiff ( rgb_image & out ) { int i, r, c; int val; char s[2]; OGRSpatialReference ref; GDALDataset *df; char *wkt = NULL; GDALRasterBand *bd; double trans[6]; GDALDriver *gt; char **options = NULL; int ov[] = { 2, 4, 8, 16, 32 }; int nov; int n; int bands[] = { 1, 2, 3 }; char file[1000]; int block, ir, rows; options = CSLSetNameValue ( options, "TILED", "NO" ); options = CSLSetNameValue ( options, "BLOCKXSIZE", "256" ); options = CSLSetNameValue ( options, "BLOCKYSIZE", "256" ); options = CSLSetNameValue ( options, "COMPRESS", "LZW" ); gt = GetGDALDriverManager()->GetDriverByName("GTiff"); if ( !gt ) { fprintf(stderr,"Could not get GTiff driver\n"); exit(1); } strcpy ( file, p.value("output_file").c_str() ); df = gt->Create( file, out.cols, out.rows, 3, GDT_Byte, options ); if( df == NULL ) { fprintf(stderr,"Could not create %s\n", file ); exit(1); } trans[0] = p.dvalue("easting_left"); trans[1] = p.dvalue("output_cell_size"); trans[2] = 0.0; trans[3] = p.dvalue("northing_top"); trans[4] = 0.0; trans[5] = -p.dvalue("output_cell_size"); df->SetGeoTransform ( trans ); ref.SetUTM ( p.ivalue("utm_zone") ); ref.SetWellKnownGeogCS ( "NAD27" ); ref.exportToWkt ( &wkt ); df->SetProjection(wkt); CPLFree ( wkt ); for ( ir = 0; ir < out.rows; ir += bs ) { rows = out.rows - ir; if ( rows > bs ) rows = bs; //printf("Writer waiting for %d\n",ir ); pe.wait_for("data",ir); for ( i = 0; i < 3; i++ ) { bd = df->GetRasterBand(i+1); bd->RasterIO( GF_Write, 0, ir, out.cols, rows, out.img[i].data+ir*out.cols, out.cols, rows, GDT_Byte, 0, 0 ); } } delete df; df = (GDALDataset *)GDALOpen ( file, GA_Update ); if( df == NULL ) { fprintf(stderr,"Could not open for update %s\n", file ); exit(1); } nov = p.ivalue("overviews"); if ( nov > 5 ) nov = 5; if ( nov > 0 ) { df->BuildOverviews("NEAREST", nov, ov, 3, bands, NULL, NULL ); } }
int main ( int argc, char **argv ) { int i, j, n, pid, t; double pix; double ir, ic; int req, task, value; char task_name[16]; log_start(); if ( argc < 2 ) { fprintf(stderr,"Usage: %s param_file\n", argv[0]); exit(1); } pe.mq = msgget ( getpid(), 0777|IPC_CREAT ); set_defaults(); p.read_file(argv[1]); GDALAllRegister(); east1 = p.dvalue("easting_left"); east2 = p.dvalue("easting_right"); north1 = p.dvalue("northing_bottom"); north2 = p.dvalue("northing_top"); wid = p.dvalue("output_cell_size"); pix = p.dvalue("input_cell_size"); ir = p.dvalue("input_rows"); ic = p.dvalue("input_columns"); input_rows = (int)ir; input_cols = (int)ic; max_dist = 1.2 * pix * sqrt(ir*ir+ic*ic) / wid; max_dist_2 = max_dist/2; max_dist = max_dist * max_dist; readers = p.ivalue("readers"); mappers = p.ivalue("mappers"); writers = p.ivalue("writers"); rows = int((north2 - north1) / wid); cols = int((east2 - east1) / wid); cindex = new int[cols]; dist.create(rows,cols); img.create(rows,cols); srand(time(NULL)); read_image_file(img); fill ( img, dist, 0, rows ); //spread_dist ( img, dist ); //if ( p.value("dist_file") != "" ) output_dist_geotiff ( dist, v ); //if ( p.value("blob_file") != "" ) output_pcolor_geotiff ( img, dist, v ); sort ( img_indices, img_indices+img_ct, comp_row ); valid_img = new int[max_img_id]; for ( i = 0; i < img_ct; i++ ) { j = img_indices[i]; valid_img[j] = 0; if ( xy[j][0]+max_dist_2 >= 0 && xy[j][0]-max_dist_2 < rows ) { pe.add_task ( "image", j ); } } read_dem_header(dem); pe.launch ( dem_reader ); out.create_image(rows,cols); for ( i = 0; i < out.rows; i += bs ) pe.add_task ( "mapper", i ); for ( i = 0; i < readers; i++ ) { pe.launch ( reader ); } for ( i = 0; i < mappers; i++ ) { pe.launch ( mapper ); } for ( i = 0; i < writers; i++ ) { pe.launch ( writer ); } while ( pe.children > 0 ) { t = msgrcv ( pe.mq, &pe.msg, MSG_SIZE, 1, 0 ); if ( t < 0 ) perror(""); sscanf(pe.msg.s,"%d %d %s %d", &req, &pid, task_name, &value ); switch ( req ) { case EXIT: log("exit %d",pid); pe.children--; break; case WAIT: if ( pe.ready(task_name,value) ) { sprintf(pe.msg.s,"1"); pe.msg.t = pid; //printf("Sending %d task %d\n",pid,task); log("wait %d %s %d",pid,task_name,value); t = msgsnd ( pe.mq, &pe.msg, MSG_SIZE, 0 ); } else { log("sleep %d %s %d",pid,task_name,value); pe.enqueue ( task_name, value, pid ); } break; case TASK: task = pe.fetch_task(task_name); log("task %d %s %d",pid,task_name,task); sprintf(pe.msg.s,"%d",task); pe.msg.t = pid; //printf("Sending %d task %d\n",pid,task); t = msgsnd ( pe.mq, &pe.msg, MSG_SIZE, 0 ); break; case REPORT: log("complete %d %s %d",pid,task_name,value); pe.complete ( task_name, value ); break; } } msgctl(pe.mq,IPC_RMID,NULL); dump_log(); return 0; }
void output_dist_geotiff ( image<float> & dist, image<unsigned char> & v ) { int r, c; int val; OGRSpatialReference ref; GDALDataset *df; char *wkt = NULL; GDALRasterBand *bd; double trans[6]; GDALDriver *gt; char **options = NULL; int ov[] = { 2, 4, 8, 16, 32 }; int nov; int n; char file[1000]; options = CSLSetNameValue ( options, "TILED", "NO" ); options = CSLSetNameValue ( options, "COMPRESS", "LZW" ); gt = GetGDALDriverManager()->GetDriverByName("GTiff"); if ( !gt ) { fprintf(stderr,"Could not get GTiff driver\n"); exit(1); } strcpy ( file, p.value("dist_file").c_str() ); df = gt->Create( file, dist.cols, dist.rows, 1, GDT_Byte, options ); if( df == NULL ) { fprintf(stderr,"Could not create %s\n", file ); exit(1); } trans[0] = p.dvalue("easting_left"); trans[1] = p.dvalue("output_cell_size"); trans[2] = 0.0; trans[3] = p.dvalue("northing_top"); trans[4] = 0.0; trans[5] = -p.dvalue("output_cell_size"); df->SetGeoTransform ( trans ); ref.SetUTM ( p.ivalue("utm_zone") ); ref.SetWellKnownGeogCS ( "NAD27" ); ref.exportToWkt ( &wkt ); df->SetProjection(wkt); CPLFree ( wkt ); for ( r=0; r < dist.rows; r++ ) { for ( c=0; c < dist.cols; c++ ) { val = int(sqrt(dist[r][c])+0.5); if ( val > 255 ) val = 255; v[r][c] = val; } } bd = df->GetRasterBand(1); bd->RasterIO( GF_Write, 0, 0, v.cols, v.rows, v.data, v.cols, v.rows, GDT_Byte, 0, 0 ); delete df; df = (GDALDataset *)GDALOpen ( file, GA_Update ); if( df == NULL ) { fprintf(stderr,"Could not open for update %s\n", file ); exit(1); } nov = p.ivalue("overviews"); if ( nov > 5 ) nov = 5; if ( nov > 0 ) { n = 1; df->BuildOverviews("NEAREST", nov, ov, 1, &n, NULL, NULL ); } }