Example #1
0
File: main.c Project: 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);
}
Example #2
0
int main(int argc, char **argv) {
    int xsize, ysize;
    double **rho_0;
    FILE *infp;
    int i;
    double t;
    double cumulative_error;

    if (argc != 4) {
        fprintf(stderr, "Usage: %s xsize ysize density-grid\n", argv[0]);
        exit(1);
    }

    xsize = atoi(argv[1]);
    ysize = atoi(argv[2]);

    infp = fopen(argv[3], "r");
    if (infp == NULL) {
        fprintf(stderr, "%s: unable to open file '%s': ", argv[0], argv[3]);
        perror(NULL);
        exit(4);
    }

    /* Read in the density data and transform it */
    rho_0 = cart_dmalloc(xsize * 3, ysize * 3);
    if (readdensity(infp, rho_0, xsize, ysize)) {
        fprintf(stderr, "%s: density file contains too few or incorrect data\n", argv[0]);
        exit(6);
    }
    fclose(infp);

    fftrho = fftw_malloc(xsize*3 * ysize*3 * sizeof(double));
    clt_rhot = malloc(xsize*3 * ysize*3 * sizeof(double));
    cart_forward(rho_0, xsize*3, ysize*3);

    fftexpt = fftw_malloc(xsize*3 * ysize*3 * sizeof(double));
    fft_rhot = fftw_malloc(xsize*3 * ysize*3 * sizeof(double));
    rhotplan = fftw_plan_r2r_2d(xsize*3, ysize*3, fftexpt, fft_rhot,
                                FFTW_REDFT01, FFTW_REDFT01, FFTW_MEASURE);
    expky = malloc(ysize*3 * sizeof(double));
    clt_temp = malloc(xsize*3 * ysize*3 * sizeof(double));

    fftvxt = malloc((xsize*3 + 1) * sizeof(double*));
    cltvxt = malloc((xsize*3 + 1) * sizeof(double*));
    fftvyt = malloc((xsize*3 + 1) * sizeof(double*));
    cltvyt = malloc((xsize*3 + 1) * sizeof(double*));
    for (i=0; i<=xsize*3; i++) {
        fftvxt[i] = malloc((ysize*3 + 1) * sizeof(double));
        cltvxt[i] = malloc((ysize*3 + 1) * sizeof(double));
        fftvyt[i] = malloc((ysize*3 + 1) * sizeof(double));
        cltvyt[i] = malloc((ysize*3 + 1) * sizeof(double));
    }

    t = 0;
    cumulative_error = 0;

    for (i=0; h_values[i] != 0; i++) {
        double h, err;
        
        h = h_values[i];
        t += 2*h;

        cart_density_fft(t, xsize*3, ysize*3);

        if (t < 1) {
            set_clt_rhot(*rho_0, xsize*3, ysize*3);
            cart_density_clt(0.5*t, xsize*3, ysize*3);
            cart_density_clt(0.5*t, xsize*3, ysize*3);
            cart_density_clt(0.5*t, xsize*3, ysize*3);
            cart_density_clt(0.5*t, xsize*3, ysize*3);
            // cart_density_clt(2.0*t, xsize*3, ysize*3);
        } else {
            cart_density_clt(0.5*h, xsize*3, ysize*3);
            cart_density_clt(0.5*h, xsize*3, ysize*3);
            cart_density_clt(0.5*h, xsize*3, ysize*3);
            cart_density_clt(0.5*h, xsize*3, ysize*3);
        }

        err = compare(xsize*3, ysize*3);
        cumulative_error += h*err;
        printf("[%d] t=%g, h=%g, error=%g\n", i, t, h, h*err);
    }
    printf("\nCumulative error = %g\n", cumulative_error);

    cart_dfree(rho_0);
    free(clt_rhot);
    free(clt_temp);

    for (i=0; i<=xsize; i++) {
        free(fftvxt[i]);
        free(cltvxt[i]);
    }
    free(fftvxt);
    free(cltvxt);

    for (i=0; i<=xsize; i++) {
        free(fftvyt[i]);
        free(cltvyt[i]);
    }
    free(fftvyt);
    free(cltvyt);

    free(expky);
    fftw_free(fftrho);
    fftw_free(fftexpt);
    fftw_free(fft_rhot);
    fftw_destroy_plan(rhotplan);
    return 0;
}
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;
}