示例#1
0
文件: space.c 项目: ajoykayak/opengdp
Space_t *SetupSpace(Space_def_t *space_def)
/* 
!C******************************************************************************

!Description: 'SetupSpace' sets up the 'space' data structure and initializes 
 the forward and inverse mapping.
 
!Input Parameters:
 space_def      space definition; the following fields are input:
                  pixel_size, ul_corner.x, ul_corner.y, img_size.l,
		  img_size.s, proj_num, zone, sphere, proj_param[*], isin_type

!Output Parameters:
 (returns)      'space' data structure or NULL when error occurs

!Team Unique Header:

 ! Design Notes:
   1. An error status is returned when:
       a. either image size dimension is zero or negative
       b. the pixel size is zero or negative
       c. the project number is less than zero or greater than 'MAX_PROJ'
       d. there is an error allocating memory
       e. an error occurs initializing either the forward or inverse map 
          projection.
   2. Error messages are handled with the 'LOG_RETURN_ERROR' macro.
   3. 'FreeSpace' should be called to deallocate the space structure.
   4. Angular values should be in radians for any calls to GCTP.

!END****************************************************************************
*/
{
  Space_t *this;
  char file27[1024];          /* name of NAD 1927 parameter file */
  char file83[1024];          /* name of NAD 1983 parameter file */
  char mrttables[1024];       /* storage for mrttables */
  char *ptr;                  /* point to mrttables */
  int32 (*for_trans[MAX_PROJ + 1])();
  int32 (*inv_trans[MAX_PROJ + 1])();
  int ip;
  int32 iflag;

  /* Place State Plane directory in file27, file83 */
  ptr = (char *)getenv("MRTSWATH_DATA_DIR");
  if( ptr == NULL ) {
     ptr = (char *)getenv("MRTDATADIR");
     if (ptr == NULL)
        ptr = MRTSWATH_DATA_DIR;
   }

  strcpy(mrttables, ptr);
  sprintf(file27, "%s/nad27sp", mrttables);
  sprintf(file83, "%s/nad83sp", mrttables);

  /* Verify some of the space definition parameters */
  
  if (space_def->img_size.l < 1) 
    LOG_RETURN_ERROR("invalid number of lines", "SetupSpace",  
                 (Space_t *)NULL);
  if (space_def->img_size.s < 1) 
    LOG_RETURN_ERROR("invalid number of samples per line", "SetupSpace",  
                 (Space_t *)NULL);
  if (space_def->pixel_size <= 0.0)
    LOG_RETURN_ERROR("invalid pixel size", "SetupSpace",  
                 (Space_t *)NULL);
  if (space_def->proj_num < 0  ||  space_def->proj_num > MAX_PROJ)
    LOG_RETURN_ERROR("invalid projection number", "SetupSpace",  
                 (Space_t *)NULL);

  /* Create the space data structure */

  this = (Space_t *)malloc(sizeof(Space_t));
  if (this == (Space_t *)NULL) 
    LOG_RETURN_ERROR("allocating Space structure", "SetupSpace", 
                 (Space_t *)NULL);

  /* Copy the space definition */

  this->def.pixel_size = space_def->pixel_size;
  this->def.ul_corner.x = space_def->ul_corner.x;
  this->def.ul_corner.y = space_def->ul_corner.y;
  this->def.lr_corner.x = space_def->lr_corner.x;
  this->def.lr_corner.y = space_def->lr_corner.y;
  this->def.img_size.l = space_def->img_size.l;
  this->def.img_size.s = space_def->img_size.s;
  this->def.proj_num = space_def->proj_num;
  this->def.zone = space_def->zone;
  this->def.sphere = space_def->sphere;
  this->def.isin_type = space_def->isin_type;
  for (ip = 0; ip < NPROJ_PARAM; ip++) {
    this->def.proj_param[ip] = space_def->proj_param[ip];
  }

  /* Setup the forward transform */

  for_init(this->def.proj_num, this->def.zone, this->def.proj_param, 
           this->def.sphere, file27, file83, &iflag, for_trans);
  if (this->def.proj_num != GEO) {
    printf ("iflag=%i\n", iflag);
    if (iflag) {
      free(this);
      LOG_RETURN_ERROR("bad return from for_init", "SetupSpace",
                       (Space_t *)NULL);
    }
    this->for_trans = for_trans[this->def.proj_num];
  }
  else
  {
    /* for_trans is not defined for geographic so call our own */
    this->for_trans = geofor;
  }

  /* Setup the inverse transform */

  inv_init(this->def.proj_num, this->def.zone, this->def.proj_param, 
           this->def.sphere, file27, file83, &iflag, inv_trans);
  if (this->def.proj_num != GEO) {
    if (iflag) {
      free(this);
      LOG_RETURN_ERROR("bad return from inv_init", "SetupSpace",
                       (Space_t *)NULL);
    }
    this->inv_trans = inv_trans[this->def.proj_num];
  }
  else
  {
    /* inv_trans is not defined for geographic so call our own */
    this->inv_trans = geoinv;
  }

  return this;
}
/******************************************************************************
MODULE:  setup_mapping

PURPOSE:  Sets up the geolocation data structure and initializes the forward
and inverse mapping functions via GCTP.

RETURN VALUE:
Type = Geoloc_t *
Value      Description
-----      -----------
NULL       Error occurred in the setup
not-NULL   Successful completion

PROJECT:  Land Satellites Data System Science Research and Development (LSRD)
at the USGS EROS

HISTORY:
Date          Programmer       Reason
----------    ---------------  -------------------------------------
1/23/2014     Gail Schmidt     Original Development (based on input routines
                               from the LEDAPS lndsr application)
4/25/2014     Gail Schmidt     Updated to support additional projections

NOTES:
1. Memory is allocated for the Geoloc_t pointer.  It is up to the calling
   routine to free the memory for this pointer.
2. Make sure the corners Space_def_t are reported as the upper left of the
   the since that's what the other routines are expecting.
******************************************************************************/
Geoloc_t *setup_mapping
(
    Space_def_t *space_def     /* I: space definition structure */
)
{
    char FUNC_NAME[] = "setup_mapping"; /* function name */
    char errmsg[STR_SIZE];              /* error message */
    char file27[] = "FILE27";           /* file for NAD27 (only for State Plane)
                                           so just use something fake for now */
    char file83[] = "FILE83";           /* file for NAD83 (only for State Plane)
                                           so just use something fake for now */
    Geoloc_t *this = NULL;              /* pointer to the space structure */
    double temp1, temp2;                /* temp variables for PS projection */
    int i;                              /* looping variable */
    int iflag;                          /* return status from GCTP */
    int (*for_trans[MAX_PROJ + 1])();   /* forward transformation function */
    int (*inv_trans[MAX_PROJ + 1])();   /* inverse transformation function */
  
    /* Verify some of the space definition parameters */
    if (space_def->img_size.l < 1) 
    {
        sprintf (errmsg, "Invalid number of lines: %d", space_def->img_size.l);
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
    if (space_def->img_size.s < 1) 
    {
        sprintf (errmsg, "Invalid number of samples per line: %d",
            space_def->img_size.s);
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }

    if (space_def->pixel_size[0] <= 0.0 || space_def->pixel_size[1] <= 0.0)
    {
        sprintf (errmsg, "Invalid pixel size: %lf %lf",
            space_def->pixel_size[0], space_def->pixel_size[1]);
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }

    if (space_def->proj_num < 0  ||  space_def->proj_num > MAX_PROJ)
    {
        sprintf (errmsg, "Invalid projection number: %d", space_def->proj_num);
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
  
    /* Create the geolocation data structure */
    this = malloc (sizeof (Geoloc_t));
    if (this == NULL) 
    {
        sprintf (errmsg, "Allocating geolocation data structure");
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
  
    /* Copy the space definition fields */
    this->def.pixel_size[0] = space_def->pixel_size[0];
    this->def.pixel_size[1] = space_def->pixel_size[1];
    this->def.ul_corner.x = space_def->ul_corner.x;
    this->def.ul_corner.y = space_def->ul_corner.y;
    this->def.img_size.l = space_def->img_size.l;
    this->def.img_size.s = space_def->img_size.s;
    this->def.proj_num = space_def->proj_num;
    this->def.zone = space_def->zone;
    this->def.spheroid = space_def->spheroid;
    this->def.orientation_angle = space_def->orientation_angle;
    for (i = 0; i < NPROJ_PARAM; i++) 
        this->def.proj_param[i] = space_def->proj_param[i];
  
    /* Calculate the orientation cosine/sine */
    this->sin_orien = sin (space_def->orientation_angle);
    this->cos_orien = cos (space_def->orientation_angle);
  
    /* Convert angular projection parameters to DMS the necessary projections */
    if (this->def.proj_num == GCTP_PS_PROJ ||
        this->def.proj_num == GCTP_ALBERS_PROJ)
    {
        if (!degdms (&this->def.proj_param[4], &temp1, "DEG", "LON" ) ||
            !degdms (&this->def.proj_param[5], &temp2, "DEG", "LAT" ))
        {
            free (this);
            sprintf (errmsg, "Converting PS or ALBERS angular parameters from "
                "degrees to DMS");
            error_handler (true, FUNC_NAME, errmsg);
            return (NULL);
        }
        this->def.proj_param[4] = temp1;
        this->def.proj_param[5] = temp2;
    }
    else if (this->def.proj_num == GCTP_SIN_PROJ)
    {
        if (!degdms (&this->def.proj_param[4], &temp1, "DEG", "LON" ))
        {
            free (this);
            sprintf (errmsg, "Converting SIN angular parameters from degrees "
                "to DMS");
            error_handler (true, FUNC_NAME, errmsg);
            return (NULL);
        }
        this->def.proj_param[4] = temp1;
    }
     
    /* Setup the forward transform */
    for_init (this->def.proj_num, this->def.zone, this->def.proj_param, 
        this->def.spheroid, file27, file83, &iflag, for_trans);
    if (iflag)
    {
        free (this);
        sprintf (errmsg, "Error returned from for_init");
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
    this->for_trans = for_trans[this->def.proj_num];
  
    /* Setup the inverse transform */
    inv_init (this->def.proj_num, this->def.zone, this->def.proj_param, 
        this->def.spheroid, file27, file83, &iflag, inv_trans);
    if (iflag)
    {
        free (this);
        sprintf (errmsg, "Error returned from for_init");
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
    this->inv_trans = inv_trans[this->def.proj_num];
  
    /* Successful completion */
    return (this);
}
示例#3
0
文件: space.c 项目: GEO-IASS/ledaps
Space_t *SetupSpace(Space_def_t *space_def)
/* 
!C******************************************************************************

!Description: 'SetupSpace' sets up the 'space' data structure and initializes 
 the forward and inverse mapping.
 
!Input Parameters:
 space_def      space definition; the following fields are input:
                  pixel_size, ul_corner.x, ul_corner.y, img_size.l,
		  img_size.s, proj_num, zone, sphere, proj_param[*], isin_type

!Output Parameters:
 (returns)      'space' data structure or NULL when error occurs

!Team Unique Header:

 ! Design Notes:
   1. An error status is returned when:
       a. either image size dimension is zero or negative
       b. the pixel size is zero or negative
       c. the project number is less than zero or greater than 'MAX_PROJ'
       d. there is an error allocating memory
       e. an error occurs initializing either the forward or inverse map 
          projection.
   2. Error messages are handled with the 'RETURN_ERROR' macro.
   3. 'FreeSpace' should be called to deallocate the space structure.

!END****************************************************************************
*/
{
  Space_t *this;
  char file27[28] = "FILE27";
  char file83[28] = "FILE83";
  long (*for_trans[MAX_PROJ + 1])();
  long (*inv_trans[MAX_PROJ + 1])();
  int ip;
  long iflag;

  /* Verify some of the space definition parameters */
  
  if (space_def->img_size.l < 1) 
    RETURN_ERROR("invalid number of lines", "SetupSpace",  
                 (Space_t *)NULL);
  if (space_def->img_size.s < 1) 
    RETURN_ERROR("invalid number of samples per line", "SetupSpace",  
                 (Space_t *)NULL);
  if (space_def->pixel_size <= 0.0)
    RETURN_ERROR("invalid pixel size", "SetupSpace",  
                 (Space_t *)NULL);
  if (space_def->proj_num < 0  ||  space_def->proj_num > MAX_PROJ)
    RETURN_ERROR("invalid projection number", "SetupSpace",  
                 (Space_t *)NULL);

  /* Create the space data structure */

  this = (Space_t *)malloc(sizeof(Space_t));
  if (this == (Space_t *)NULL) 
    RETURN_ERROR("allocating Space structure", "SetupSpace", 
                 (Space_t *)NULL);

  /* Copy the space definition */

  this->def.pixel_size = space_def->pixel_size;
  this->def.ul_corner.x = space_def->ul_corner.x;
  this->def.ul_corner.y = space_def->ul_corner.y;
  this->def.img_size.l = space_def->img_size.l;
  this->def.img_size.s = space_def->img_size.s;
  this->def.proj_num = space_def->proj_num;
  this->def.zone = space_def->zone;
  this->def.sphere = space_def->sphere;
  this->def.isin_type = space_def->isin_type;
  this->def.orientation_angle = space_def->orientation_angle;
  for (ip = 0; ip < NPROJ_PARAM; ip++) 
    this->def.proj_param[ip] = space_def->proj_param[ip];

  /* Calculate the orientation cosine/sine */

  this->sin_orien = sin(space_def->orientation_angle);
  this->cos_orien = cos(space_def->orientation_angle);

  /* Setup the forward transform */

  for_init(this->def.proj_num, this->def.zone, this->def.proj_param, 
           this->def.sphere, file27, file83, &iflag, for_trans);
  if (iflag) {
    free(this);
    RETURN_ERROR("bad return from for_init", "SetupSpace", (Space_t *)NULL);
  }
  this->for_trans = for_trans[this->def.proj_num];

  /* Setup the inverse transform */

  inv_init(this->def.proj_num, this->def.zone, this->def.proj_param, 
           this->def.sphere, file27, file83, &iflag, inv_trans);
  if (iflag) {
    free(this);
    RETURN_ERROR("bad return from inv_init", "SetupSpace", (Space_t *)NULL);
  }
  this->inv_trans = inv_trans[this->def.proj_num];

  return this;
}