Пример #1
0
SEXP
R_makecartogram(SEXP r_pop, SEXP gridx, SEXP gridy, SEXP r_dims, SEXP blur)
{
    int *dims;
    double **pop;
    int i, n;
    SEXP ans;

    dims = INTEGER(r_dims);    

    /* Note that if we get an interrupt, this will not be freed! 
       We may change cart.c to use R_alloc().
     */
    cart_makews(dims[0], dims[1]);

    
    pop = (double **) R_alloc(sizeof(double *), dims[0]);
    if(TYPEOF(r_pop) == VECSXP) {
	/* This leads to non-contiguous values. Not certain if this is
	 * allowed in the cart.c code. */
	for(i = 0; i < dims[0]; i++) {
	    pop[i] = REAL(VECTOR_ELT(r_pop, i));
	}
    } else {
	/* Given a numeric matrix, so just pull out the pointers to
	    the beginning of each column. Since we are using the
            columns for the Y values, this gives us the form of a 2D
            array in C with pop[i] giving a vector/array of the y's. 
          */
	double *data = REAL(r_pop);
	for(i = 0; i < dims[0]; i++) {
	    pop[i] = data + i * dims[1];
	}
    }

    INTERRUPT
    cart_transform(pop, dims[0], dims[1]);

    PROTECT(ans = allocVector(VECSXP, 2));
    SET_VECTOR_ELT(ans, 0, gridx = Rf_duplicate(gridx));
    SET_VECTOR_ELT(ans, 1, gridy = Rf_duplicate(gridy));

    INTERRUPT
    cart_makecart(REAL(gridx), REAL(gridy), Rf_length(gridx), dims[0], dims[1], REAL(blur)[0]);

    INTERRUPT
    cart_freews(dims[0], dims[1]); 

    UNPROTECT(1);
    return(ans);
}
Пример #2
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;
}