-> hdddmmss (hemisphere-degrees-minutes-seconds) -> hddd.dddddd (hemisphere-degrees.decimal degrees) -> +-ddd.dddddd (hemisphere[+/-]-degrees.decimal degrees) (“+” for N and E, “-“ for S and W; the plus sign is optional) -> hdddmm.mmmm (hemisphere-degrees-minutes.decimal minutes): -> hdddmmss.sss (hemisphere-degrees-minutes-seconds.decimal seconds)
degrees/minutes/seconds: hdddmmss (hemisphere-degrees-minutes-seconds) Ex. 0793235 +0793235 E0793235 || | |-> seconds (2 digits; left padded with 0) || |-> minutes (2 digits; left padded with 0) ||-> degrees (3 digits; left padded with 0) |-> start_literal (1 digit; optional)
decimal degrees: hddd.dddddd (hemisphere-degrees.decimal degrees) Ex. E079.533265 +079.533265 || |-> indicates decimal degrees ||-> degrees (3 digits; left padded with 0) |-> start_literal (1 digit; optional)
decimal minutes: hdddmm.mmmm (cardinal direction|degrees|minutes.decimal minutes) Ex. E07932.5332 || | |-> indicates decimal minutes || |-> minutes (2 digits; left padded with 0) ||-> degrees (3 digits; left padded with 0) |-> start_literal (1 digit; optional)
decimal seconds: hdddmmss.sss (hemisphere-degrees-minutes-seconds.decimal seconds) Ex. E0793235.575 || | | |-> indicates decimal seconds || | |-> seconds (2 digits; left padded with 0) || |-> minutes (2 digits; left padded with 0) ||-> degrees (3 digits; left padded with 0) |-> start_literal (1 digit; optional)
208 size_t literal_length;
210 char start_character = literal[0];
211 int start_literal = 0;
214 const size_t numdigits_degrees = 3;
215 const size_t numdigits_minutes = 2;
216 const size_t numdigits_seconds = 2;
218 POSTGIS_DEBUGF(2,
"parse_geo_literal called (%s)", literal);
219 POSTGIS_DEBUGF(2,
" start character: %c", start_character);
221 literal_length = strlen(literal);
223 if (!isdigit(start_character)) start_literal = 1;
225 POSTGIS_DEBUGF(2,
" start_literal=%d", start_literal);
227 dgr = palloc(
sizeof(
char)*numdigits_degrees+1);
228 snprintf(dgr, numdigits_degrees+1,
"%s", &literal[start_literal]);
230 if (strchr(literal,
'.') == NULL && strchr(literal,
',') == NULL) {
243 POSTGIS_DEBUG(2,
" lat/lon integer coordinates detected");
244 POSTGIS_DEBUGF(2,
" parsed degrees (lon/lat): %s", dgr);
251 if (literal_length > (start_literal + numdigits_degrees)) {
253 min = palloc(
sizeof(
char)*numdigits_minutes+1);
254 snprintf(min, numdigits_minutes+1,
"%s", &literal[start_literal+numdigits_degrees]);
255 POSTGIS_DEBUGF(2,
" parsed minutes (lon/lat): %s", min);
260 if (literal_length >= (start_literal + numdigits_degrees + numdigits_minutes)) {
262 sec = palloc(
sizeof(
char)*numdigits_seconds+1);
263 snprintf(sec, numdigits_seconds+1,
"%s", &literal[start_literal+numdigits_degrees+numdigits_minutes]);
264 POSTGIS_DEBUGF(2,
" parsed seconds (lon/lat): %s", sec);
277 POSTGIS_DEBUG(2,
" decimal coordinates detected");
279 if (strchr(literal,
',')) {
284 literal[literal_length-strlen(strchr(literal,
','))]=
'.';
285 POSTGIS_DEBUGF(2,
" decimal separator changed to '.': %s",literal);
290 if (literal[start_literal + numdigits_degrees] ==
'.') {
301 char *dec = palloc(
sizeof(
char)*literal_length+1);
302 snprintf(dec, literal_length+1,
"%s", &literal[start_literal]);
305 POSTGIS_DEBUGF(2,
" parsed decimal degrees: %s", dec);
309 }
else if (literal[start_literal + numdigits_degrees + numdigits_minutes] ==
'.') {
320 size_t len_decimal_minutes = literal_length - (start_literal + numdigits_degrees);
322 min = palloc(
sizeof(
char)*len_decimal_minutes+1);
323 snprintf(min, len_decimal_minutes+1,
"%s", &literal[start_literal + numdigits_degrees]);
325 POSTGIS_DEBUGF(2,
" parsed degrees: %s", dgr);
326 POSTGIS_DEBUGF(2,
" parsed decimal minutes: %s", min);
328 result = atof(dgr) + (atof(min) / 60);
333 }
else if (literal[start_literal + numdigits_degrees + numdigits_minutes + numdigits_seconds] ==
'.') {
345 size_t len_decimal_seconds = literal_length - (start_literal + numdigits_degrees + numdigits_minutes);
347 min = palloc(
sizeof(
char)*numdigits_minutes+1);
348 snprintf(min, numdigits_minutes+1,
"%s", &literal[start_literal + numdigits_degrees]);
350 sec = palloc(
sizeof(
char)*len_decimal_seconds+1);
351 snprintf(sec, len_decimal_seconds+1,
"%s", &literal[start_literal + numdigits_degrees + numdigits_minutes]);
353 result = atof(dgr) + (atof(min) / 60) + (atof(sec) / 3600);
355 POSTGIS_DEBUGF(2,
" parsed degrees: %s", dgr);
356 POSTGIS_DEBUGF(2,
" parsed minutes: %s", min);
357 POSTGIS_DEBUGF(2,
" parsed decimal seconds: %s", sec);
372 if (start_character ==
'S' || start_character ==
'W' || start_character ==
'-') {
374 POSTGIS_DEBUGF(2,
" switching sign due to start character: '%c'", start_character);
379 POSTGIS_DEBUGF(2,
"=> parse_geo_literal returns: %.*f (in decimal degrees)", literal_length-(3+start_literal),
result);
char result[OUT_DOUBLE_BUFFER_SIZE]