MISR Toolkit  1.5.1
MtkSetRegionByGenericMapInfo.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkSetRegionByGenericMapInfo =
4 = =
5 =============================================================================
6 
7  Jet Propulsion Laboratory
8  MISR
9  MISR Toolkit
10 
11  Copyright 2008, California Institute of Technology.
12  ALL RIGHTS RESERVED.
13  U.S. Government Sponsorship acknowledged.
14 
15 ============================================================================*/
16 
17 #include "MisrSetRegion.h"
18 #include "MisrUtil.h"
19 #include <stdlib.h>
20 #include <math.h>
21 #include "MisrToolkit.h" /* Definition of inv_init */
22 #include <proj.h> /* Definition of MAXPROJ */
23 
38  const MTKt_GenericMapInfo *Map_info,
39  const MTKt_GCTPProjInfo *Proj_info,
40  int Path,
41  MTKt_Region *Region
42 )
43 {
44  MTKt_status status_code = MTK_FAILURE; /* Return status of this function */
45  MTKt_status status; /* Return status of called routines. */
46  MTKt_Region region_tmp = MTKT_REGION_INIT;
47  /* Region */
48  double corner_lat[4]; /* Latitude coordinates for each corner of the
49  target map. */
50  double corner_lon[4]; /* Longitude coordinates for each corner of the
51  target map. */
52  double min_som_x = 0.0; /* Minimum SOM X coordinate. */
53  double min_som_y = 0.0; /* Minimum SOM Y coordinate. */
54  double max_som_x = 0.0; /* Maximum SOM X coordinate. */
55  double max_som_y = 0.0; /* Maximum SOM Y coordinate. */
56  int i; /* Loop iterator */
57  double rad2deg = 180 / acos(-1); /* For converting Radians to degress */
58 
59  /* ------------------------------------------------------------------ */
60  /* Argument check: Map_info == NULL */
61  /* ------------------------------------------------------------------ */
62 
63  if (Map_info == NULL) {
64  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Map_info == NULL");
65  }
66 
67  /* ------------------------------------------------------------------ */
68  /* Argument check: Proj_info == NULL */
69  /* Proj_info->proj_code > MAXPROJ */
70  /* ------------------------------------------------------------------ */
71 
72  if (Proj_info == NULL) {
73  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Proj_info == NULL");
74  }
75  if (Proj_info->proj_code > MAXPROJ) {
76  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Proj_info->proj_code > MAXPROJ");
77  }
78 
79  /* ------------------------------------------------------------------ */
80  /* Argument check: Region == NULL */
81  /* ------------------------------------------------------------------ */
82 
83  if (Region == NULL) {
84  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Region == NULL");
85  }
86 
87  /* ------------------------------------------------------------------ */
88  /* Calculate lat/lon coordinates for each corner of the target map. */
89  /* ------------------------------------------------------------------ */
90 
91  { int iflg; /* Status flag returned by GCTP. */
92  int (*inv_trans[MAXPROJ+1])(); /* Array of transformation functions
93  returned by GCTP. */
94 
95  inv_init(Proj_info->proj_code, Proj_info->zone_code,
96  (double *)Proj_info->proj_param,
97  Proj_info->sphere_code, 0, 0, &iflg, inv_trans);
98  if (iflg) {
99  MTK_ERR_MSG_JUMP("Trouble with GCTP inv_init routine\n");
100  }
101 
102  inv_trans[Proj_info->proj_code](Map_info->min_x, Map_info->min_y,
103  &corner_lon[0], &corner_lat[0]);
104  inv_trans[Proj_info->proj_code](Map_info->min_x, Map_info->max_y,
105  &corner_lon[1], &corner_lat[1]);
106  inv_trans[Proj_info->proj_code](Map_info->max_x, Map_info->min_y,
107  &corner_lon[2], &corner_lat[2]);
108  inv_trans[Proj_info->proj_code](Map_info->max_x, Map_info->max_y,
109  &corner_lon[3], &corner_lat[3]);
110  }
111 
112  /* ------------------------------------------------------------------ */
113  /* Calculate SOM coordinates for each corner of the target map. */
114  /* Find the minimum and maximum SOM coords */
115  /* ------------------------------------------------------------------ */
116 
117  for (i = 0 ; i < 4 ; i++) {
118  double som_x; /* SOM X coordinate for this corner. */
119  double som_y; /* SOM Y coordinate for each corner of the
120  target map. */
121 
122  status = MtkLatLonToSomXY(Path,
123  corner_lat[i]*rad2deg,
124  corner_lon[i]*rad2deg,
125  &som_x, &som_y);
126  MTK_ERR_COND_JUMP(status);
127 
128  if (i == 0 || som_x < min_som_x) {
129  min_som_x = som_x;
130  }
131  if (i == 0 || som_y < min_som_y) {
132  min_som_y = som_y;
133  }
134  if (i == 0 || som_x > max_som_x) {
135  max_som_x = som_x;
136  }
137  if (i == 0 || som_y > max_som_y) {
138  max_som_y = som_y;
139  }
140  }
141 
142  /* ------------------------------------------------------------------ */
143  /* Create region containing the target map area. */
144  /* ------------------------------------------------------------------ */
145 
146  {
147  double som_x_extent = max_som_x - min_som_x;
148  double som_y_extent = max_som_y - min_som_y;
149 
150  double center_som_x = min_som_x + (max_som_x - min_som_x) / 2.0;
151  double center_som_y = min_som_y + (max_som_y - min_som_y) / 2.0;
152  double center_lat;
153  double center_lon;
154 
155  status = MtkSomXYToLatLon(Path, center_som_x, center_som_y, &center_lat,
156  &center_lon);
157  MTK_ERR_COND_JUMP(status);
158 
159  status = MtkSetRegionByLatLonExtent(center_lat, center_lon,
160  som_x_extent, som_y_extent, "meters",
161  &region_tmp);
162  MTK_ERR_COND_JUMP(status);
163  }
164 
165 
166  /* ------------------------------------------------------------------ */
167  /* Return. */
168  /* ------------------------------------------------------------------ */
169 
170  *Region = region_tmp;
171  return MTK_SUCCESS;
172 
173 ERROR_HANDLE:
174  return status_code;
175 }
#define MAXPROJ
Definition: proj.h:84
Generic map information.
Definition: MisrMapQuery.h:98
MTKt_status MtkSetRegionByGenericMapInfo(const MTKt_GenericMapInfo *Map_info, const MTKt_GCTPProjInfo *Proj_info, int Path, MTKt_Region *Region)
Create an MtkRegion structure that contains the given map.
GCTP projection information.
Definition: MisrMapQuery.h:123
#define MTKT_REGION_INIT
Definition: MisrSetRegion.h:46
MTKt_status MtkSomXYToLatLon(int path, double som_x, double som_y, double *lat_dd, double *lon_dd)
Convert SOM X, SOM Y to decimal degrees latitude and longitude.
double proj_param[15]
Definition: MisrMapQuery.h:127
#define MTK_ERR_CODE_MSG_JUMP(code, msg)
Definition: MisrError.h:181
#define MTK_ERR_MSG_JUMP(msg)
Definition: MisrError.h:169
MTKt_status MtkSetRegionByLatLonExtent(double ctr_lat_dd, double ctr_lon_dd, double lat_extent, double lon_extent, const char *extent_units, MTKt_Region *region)
Select region by latitude, longitude in decimal degrees, and extent in specified units of degrees...
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status
Definition: MisrError.h:11
Region of interest.
Definition: MisrSetRegion.h:41
int inv_init(int insys, int inzone, const double *inparm, int indatum, char *fn27, char *fn83, int *iflg, int(*inv_trans[])(double, double, double *, double *))
MTKt_status MtkLatLonToSomXY(int path, double lat_dd, double lon_dd, double *som_x, double *som_y)
Convert decimal degrees latitude and longitude to SOM X, SOM Y.

MISR Toolkit - Copyright © 2005 - 2020 Jet Propulsion Laboratory
Generated on Fri Jun 19 2020 22:49:51