PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ corner_to_subfield_sb()

static int corner_to_subfield_sb ( stringbuffer_t sb,
double  decimal_degrees,
const char *  format,
char  subfield 
)
static

decimal degrees

decimal minutes

degrees/minutes/seconds (dddmmss)

Definition at line 231 of file lwgeom_out_marc21.c.

231  {
232 
233  char cardinal_direction;
234  char decimal_separator;
235 
236  int degrees = (int) decimal_degrees;
237  double minutes = fabs((decimal_degrees-degrees)*60);
238  double seconds = fabs((minutes-(int)minutes) *60);
239 
240  int has_cardinal_direction = 0;
241  int num_decimals = 0;
242  char* res = palloc(sizeof(char)*strlen(format)+2);
243 
244  /* size of the buffer for the output snprintf calls.
245  * the output strings must have the same length as the format.
246  * +1 to make room for the null character '\0' */
247  size_t buffer_size = strlen(format)+1;
248 
249  /* +1 one digit to the buffer size in case of negative
250  * numbers to account for the "-" sign */
251  if(degrees < 0) buffer_size = buffer_size+1;
252 
253 
254  POSTGIS_DEBUGF(2,"corner_to_subfield_sb called with coordinates: %f and format: %s",decimal_degrees,format);
255 
256  if((int)(seconds + 0.5)>=60) {
257 
258  seconds = seconds-60;
259  minutes = minutes +1;
260 
261  }
262 
263 
264  if(strchr(format, '.')) {
265  num_decimals = strlen(strchr(format, '.'))-1;
266  decimal_separator = '.';
267  }
268 
269  if(strchr(format, ',')) {
270  num_decimals = strlen(strchr(format, ','))-1;
271  decimal_separator = ',';
272  }
273 
274  if(format[0]=='h'){
275 
276  has_cardinal_direction = 1;
277 
278  if(subfield=='d'||subfield=='e'){
279 
280  if(decimal_degrees>0){
281 
282  cardinal_direction='E';
283 
284  } else {
285 
286  cardinal_direction='W';
287  degrees=abs(degrees);
288  decimal_degrees= fabs(decimal_degrees);
289 
290  }
291  }
292 
293  if(subfield=='f'||subfield=='g'){
294 
295  if(decimal_degrees>0){
296 
297  cardinal_direction='N';
298 
299  } else {
300 
301  cardinal_direction='S';
302  degrees=abs(degrees);
303  decimal_degrees= fabs(decimal_degrees);
304 
305  }
306  }
307 
308 
309  }
310 
311  if(format[3+has_cardinal_direction]=='.' || format[3+has_cardinal_direction]==',' ) {
312 
317  int pad_degrees = (int)strlen(format);
318 
319  if(decimal_degrees <0 && decimal_degrees>-100) pad_degrees=strlen(format)+1;
320 
321  if(has_cardinal_direction) pad_degrees=pad_degrees-1;
322 
323  snprintf(res,buffer_size,"%0*.*f",pad_degrees,num_decimals,decimal_degrees);
324 
325 
326  } else if(format[5+has_cardinal_direction]=='.' || format[5+has_cardinal_direction]==',' ) {
327 
331  int pad_minutes = 0;
332 
333  if(minutes<10) pad_minutes = (int)strlen(format)-has_cardinal_direction-3;
334 
335  snprintf(res,buffer_size,"%.3d%0*.*f",degrees,pad_minutes,num_decimals,fabs(minutes));
336 
337  }
338 
339  else if(format[7+has_cardinal_direction]=='.' || format[7+has_cardinal_direction]==',') {
340 
341  /*
342  * decimal seconds
343  */
344 
345  int pad_seconds = 0;
346 
347  if(seconds<10) pad_seconds = (int) strlen(format)-has_cardinal_direction-5;
348 
349  snprintf(res,buffer_size,"%.3d%.2d%0*.*f",degrees,(int)minutes,pad_seconds,num_decimals,fabs(seconds));
350 
351  } else {
352 
357  snprintf(res,buffer_size,"%.3d%.2d%.2d",degrees,(int)minutes,(int)(seconds + 0.5));
358 
359  }
360 
361 
362  if(decimal_separator==','){
363 
364  res[strlen(res)-num_decimals-1] = ',';
365 
366  }
367 
368  if(has_cardinal_direction){
369 
370  if (stringbuffer_aprintf(sb, "<subfield code=\"%c\">%c%s</subfield>", subfield, cardinal_direction, res) < 0) return LW_FAILURE;
371 
372  } else {
373 
374  if (stringbuffer_aprintf(sb, "<subfield code=\"%c\">%s</subfield>", subfield, res) < 0) return LW_FAILURE;
375 
376  }
377 
378 
379  pfree(res);
380  return LW_SUCCESS;
381 
382 
383 
384 }
#define LW_FAILURE
Definition: liblwgeom.h:96
#define LW_SUCCESS
Definition: liblwgeom.h:97
#define buffer_size
Definition: lwout_wkt.c:32
tuple res
Definition: window.py:79
int stringbuffer_aprintf(stringbuffer_t *s, const char *fmt,...)
Appends a formatted string to the current string buffer, using the format and argument list provided.
Definition: stringbuffer.c:254

References buffer_size, LW_FAILURE, LW_SUCCESS, window::res, and stringbuffer_aprintf().

Referenced by gbox_to_marc21_sb().

Here is the call graph for this function:
Here is the caller graph for this function: