예제 #1
0
파일: main.c 프로젝트: rforge/cart
void main(int argc, char *argv[])
{
  int xsize,ysize;
  double *gridx,*gridy;  // Array for grid points
  double **rho;          // Initial population density
  FILE *infp;
  FILE *outfp;

  /* Read the command-line parameters and open the input and output files */

  if (argc<5) {
    fprintf(stderr,"Usage: %s xsize ysize inputfile outputfile\n",argv[0]);
    exit(1);
  }
  xsize = atoi(argv[1]);
  if (xsize<1) {
    fprintf(stderr,"%s: xsize botched\n",argv[0]);
    exit(2);
  }
  ysize = atoi(argv[2]);
  if (ysize<1) {
    fprintf(stderr,"%s: ysize botched\n",argv[0]);
    exit(3);
  }
  infp = fopen(argv[3],"r");
  if (infp==NULL) {
    fprintf(stderr,"%s: unable to open file `%s'\n",argv[0],argv[3]);
    exit(4);
  }
  outfp = fopen(argv[4],"w");
  if (outfp==NULL) {
    fprintf(stderr,"%s: unable to open file `%s'\n",argv[0],argv[4]);
    exit(5);
  }

  /* Allocate space for the cartogram code to use */

  cart_makews(xsize,ysize);

  /* Read in the population data, transform it, then destroy it again */

  rho = cart_dmalloc(xsize,ysize);
  if (readpop(infp,rho,xsize,ysize)) {
    fprintf(stderr,"%s: density file contains too few or incorrect data\n",
	    argv[0]);
    exit(6);
  }
  cart_transform(rho,xsize,ysize);
  cart_dfree(rho);

  /* Create the grid of points */

  gridx = malloc((xsize+1)*(ysize+1)*sizeof(double));
  gridy = malloc((xsize+1)*(ysize+1)*sizeof(double));
  creategrid(gridx,gridy,xsize,ysize);

  /* Make the cartogram */

  cart_makecart(gridx,gridy,(xsize+1)*(ysize+1),xsize,ysize,0.0);

  /* Write out the final positions of the grid points */

  writepoints(outfp,gridx,gridy,(xsize+1)*(ysize+1));

  /* Free up the allocated space */

  cart_freews(xsize,ysize);
  free(gridx);
  free(gridy);

  /* Close the input and output files */

  fclose(infp);
  fclose(outfp);
}
int equalize_density(char *infile, char *outfile, int fast, int accurate) {
	
	int xsize, ysize;				// Size of the density grid.
	double *gridx, *gridy;			// Array for grid	
	double **rho;					// Initial population density
	GDALDatasetH hDataset;			// The input density raster file.
	GDALRasterBandH hBand;			// The raster band we are going to use.
	FILE *outfp;					// The morphing file (a text file).
	double adfGeoTransform[6];		// For the georeference of the raster.
	
	
	// Register all GDAL drivers.
    GDALAllRegister();
	
	
#if defined (_OPENMP)
	omp_set_num_threads(omp_get_num_procs());
#endif
	
	
	hDataset = GDALOpen(infile, GA_ReadOnly);
    if (hDataset == NULL) {
		fprintf(stderr,"Error. Unable to open file `%s'\n", infile);
		exit(1);
	}
	
	outfp = fopen(outfile, "w");
	if (outfp == NULL) {
		fprintf(stderr,"Error. Unable to open file `%s'\n", outfile);
		exit(1);
	}
	
	
	// Get the raster band for the dataset; we are using the first band.
	hBand = GDALGetRasterBand(hDataset, 1);
	if (hBand == NULL) {
		fprintf(stderr, "Error. Unable to read band 1 in file `%s'\n", infile);
		exit(1);
	}
	
	// Determine the raster size
	xsize = GDALGetRasterBandXSize(hBand);
	ysize = GDALGetRasterBandYSize(hBand);
	
	
	
	// Allocate space for the cartogram code to use
	cart_makews(xsize, ysize);
	
	
	// Read in the population data, transform it, then destroy it again
	rho = cart_dmalloc(xsize, ysize);
	if (readpop(hBand, rho, xsize, ysize)) {
		fprintf(stderr,"Error. Density file contains too few or incorrect data\n");
		exit(1);
	}
	cart_transform(rho, xsize, ysize);
	cart_dfree(rho);
	
	
	// Create the grid of points
	gridx = malloc((xsize+1)*(ysize+1)*sizeof(double));
	gridy = malloc((xsize+1)*(ysize+1)*sizeof(double));
	creategrid(gridx, gridy, xsize, ysize);
	
	
	// Compute the cartogram
	cart_makecart(gridx, gridy, (xsize+1)*(ysize+1), xsize, ysize, 0.0);
	
	
	// Write out the final positions of the grid points
	GDALGetGeoTransform(hDataset, adfGeoTransform);
	writepoints(outfp, gridx, gridy, xsize, ysize, adfGeoTransform);
	//writepoints(outfp, gridx, gridy, (xsize+1)*(ysize+1));
	
	
	// Free up the allocated memory
	cart_freews(xsize, ysize);
	free(gridx);
	free(gridy);
	
	
	// Close the input and output files
	GDALClose(hDataset);
	fclose(outfp);
	
	return 0;
}