PostGIS  3.4.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 246 of file lwgeom_out_marc21.c.

246  {
247 
248  char cardinal_direction;
249  char decimal_separator;
250 
251  int degrees = (int) decimal_degrees;
252  double minutes = fabs((decimal_degrees-degrees)*60);
253  double seconds = fabs((minutes-(int)minutes) *60);
254 
255  int has_cardinal_direction = 0;
256  int num_decimals = 0;
257  char* res = palloc(sizeof(char)*strlen(format)+2);
258 
259  /* size of the buffer for the output snprintf calls.
260  * the output strings must have the same length as the format.
261  * +1 to make room for the null character '\0' */
262  size_t buffer_size = strlen(format)+1;
263 
264  /* +1 one digit to the buffer size in case of negative
265  * numbers to account for the "-" sign */
266  if(degrees < 0) buffer_size = buffer_size+1;
267 
268 
269  POSTGIS_DEBUGF(2,"corner_to_subfield_sb called with coordinates: %f and format: %s",decimal_degrees,format);
270 
271  if((int)(seconds + 0.5)>=60) {
272 
273  seconds = seconds-60;
274  minutes = minutes +1;
275 
276  }
277 
278 
279  if(strchr(format, '.')) {
280  num_decimals = strlen(strchr(format, '.'))-1;
281  decimal_separator = '.';
282  }
283 
284  if(strchr(format, ',')) {
285  num_decimals = strlen(strchr(format, ','))-1;
286  decimal_separator = ',';
287  }
288 
289  if(format[0]=='h'){
290 
291  has_cardinal_direction = 1;
292 
293  if(subfield=='d'||subfield=='e'){
294 
295  if(decimal_degrees>0){
296 
297  cardinal_direction='E';
298 
299  } else {
300 
301  cardinal_direction='W';
302  degrees=abs(degrees);
303  decimal_degrees= fabs(decimal_degrees);
304 
305  }
306  }
307 
308  if(subfield=='f'||subfield=='g'){
309 
310  if(decimal_degrees>0){
311 
312  cardinal_direction='N';
313 
314  } else {
315 
316  cardinal_direction='S';
317  degrees=abs(degrees);
318  decimal_degrees= fabs(decimal_degrees);
319 
320  }
321  }
322 
323 
324  }
325 
326  if(format[3+has_cardinal_direction]=='.' || format[3+has_cardinal_direction]==',' ) {
327 
332  int pad_degrees = (int)strlen(format);
333 
334  if(decimal_degrees <0 && decimal_degrees>-100) pad_degrees=strlen(format)+1;
335 
336  if(has_cardinal_direction) pad_degrees=pad_degrees-1;
337 
338  snprintf(res,buffer_size,"%0*.*f",pad_degrees,num_decimals,decimal_degrees);
339 
340 
341  } else if(format[5+has_cardinal_direction]=='.' || format[5+has_cardinal_direction]==',' ) {
342 
346  int pad_minutes = 0;
347 
348  if(minutes<10) pad_minutes = (int)strlen(format)-has_cardinal_direction-3;
349 
350  snprintf(res,buffer_size,"%.3d%0*.*f",degrees,pad_minutes,num_decimals,fabs(minutes));
351 
352  }
353 
354  else if(format[7+has_cardinal_direction]=='.' || format[7+has_cardinal_direction]==',') {
355 
356  /*
357  * decimal seconds
358  */
359 
360  int pad_seconds = 0;
361 
362  if(seconds<10) pad_seconds = (int) strlen(format)-has_cardinal_direction-5;
363 
364  snprintf(res,buffer_size,"%.3d%.2d%0*.*f",degrees,(int)minutes,pad_seconds,num_decimals,fabs(seconds));
365 
366  } else {
367 
372  snprintf(res,buffer_size,"%.3d%.2d%.2d",degrees,(int)minutes,(int)(seconds + 0.5));
373 
374  }
375 
376 
377  if(decimal_separator==','){
378 
379  res[strlen(res)-num_decimals-1] = ',';
380 
381  }
382 
383  if(has_cardinal_direction){
384 
385  if (stringbuffer_aprintf(sb, "<subfield code=\"%c\">%c%s</subfield>", subfield, cardinal_direction, res) < 0) return LW_FAILURE;
386 
387  } else {
388 
389  if (stringbuffer_aprintf(sb, "<subfield code=\"%c\">%s</subfield>", subfield, res) < 0) return LW_FAILURE;
390 
391  }
392 
393 
394  pfree(res);
395  return LW_SUCCESS;
396 
397 
398 
399 }
#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:247

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: