1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
|
/**********************************************************************
*
* PostGIS - Spatial Types for PostgreSQL
* http://postgis.net
*
* PostGIS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* PostGIS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
*
**********************************************************************
*
* Copyright (C) 2010-2015 Paul Ramsey <pramsey@cleverelephant.ca>
* Copyright (C) 2011 Sandro Santilli <strk@kbt.io>
*
**********************************************************************/
#include "liblwgeom_internal.h"
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "ryu/ryu.h"
/* Ensures the given lat and lon are in the "normal" range:
* -90 to +90 for lat, -180 to +180 for lon. */
static void lwprint_normalize_latlon(double *lat, double *lon)
{
/* First remove all the truly excessive trips around the world via up or down. */
while (*lat > 270)
{
*lat -= 360;
}
while (*lat < -270)
{
*lat += 360;
}
/* Now see if latitude is past the top or bottom of the world.
* Past 90 or -90 puts us on the other side of the earth,
* so wrap latitude and add 180 to longitude to reflect that. */
if (*lat > 90)
{
*lat = 180 - *lat;
*lon += 180;
}
if (*lat < -90)
{
*lat = -180 - *lat;
*lon += 180;
}
/* Now make sure lon is in the normal range. Wrapping longitude
* has no effect on latitude. */
while (*lon > 180)
{
*lon -= 360;
}
while (*lon < -180)
{
*lon += 360;
}
}
/* Converts a single double to DMS given the specified DMS format string.
* Symbols are specified since N/S or E/W are the only differences when printing
* lat vs. lon. They are only used if the "C" (compass dir) token appears in the
* format string.
* NOTE: Format string and symbols are required to be in UTF-8. */
static char * lwdouble_to_dms(double val, const char *pos_dir_symbol, const char *neg_dir_symbol, const char * format)
{
/* 3 numbers, 1 sign or compass dir, and 5 possible strings (degree signs, spaces, misc text, etc) between or around them.*/
# define NUM_PIECES 9
# define WORK_SIZE 1024
char pieces[NUM_PIECES][WORK_SIZE];
int current_piece = 0;
int is_negative = 0;
double degrees = 0.0;
double minutes = 0.0;
double seconds = 0.0;
int compass_dir_piece = -1;
int reading_deg = 0;
int deg_digits = 0;
int deg_has_decpoint = 0;
int deg_dec_digits = 0;
int deg_piece = -1;
int reading_min = 0;
int min_digits = 0;
int min_has_decpoint = 0;
int min_dec_digits = 0;
int min_piece = -1;
int reading_sec = 0;
int sec_digits = 0;
int sec_has_decpoint = 0;
int sec_dec_digits = 0;
int sec_piece = -1;
int round_pow = 0;
int format_length = ((NULL == format) ? 0 : strlen(format));
char * result;
int index, following_byte_index;
int multibyte_char_width = 1;
/* Initialize the working strs to blank. We may not populate all of them, and
* this allows us to concat them all at the end without worrying about how many
* we actually needed. */
for (index = 0; index < NUM_PIECES; index++)
{
pieces[index][0] = '\0';
}
/* If no format is provided, use a default. */
if (0 == format_length)
{
/* C2B0 is UTF-8 for the degree symbol. */
format = "D\xC2\xB0""M'S.SSS\"C";
format_length = strlen(format);
}
else if (format_length > WORK_SIZE)
{
/* Sanity check, we don't want to overwrite an entire piece of work and no one should need a 1K-sized
* format string anyway. */
lwerror("Bad format, exceeds maximum length (%d).", WORK_SIZE);
}
for (index = 0; index < format_length; index++)
{
char next_char = format[index];
switch (next_char)
{
case 'D':
if (reading_deg)
{
/* If we're reading degrees, add another digit. */
deg_has_decpoint ? deg_dec_digits++ : deg_digits++;
}
else
{
/* If we're not reading degrees, we are now. */
current_piece++;
deg_piece = current_piece;
if (deg_digits > 0)
{
lwerror("Bad format, cannot include degrees (DD.DDD) more than once.");
}
reading_deg = 1;
reading_min = 0;
reading_sec = 0;
deg_digits++;
}
break;
case 'M':
if (reading_min)
{
/* If we're reading minutes, add another digit. */
min_has_decpoint ? min_dec_digits++ : min_digits++;
}
else
{
/* If we're not reading minutes, we are now. */
current_piece++;
min_piece = current_piece;
if (min_digits > 0)
{
lwerror("Bad format, cannot include minutes (MM.MMM) more than once.");
}
reading_deg = 0;
reading_min = 1;
reading_sec = 0;
min_digits++;
}
break;
case 'S':
if (reading_sec)
{
/* If we're reading seconds, add another digit. */
sec_has_decpoint ? sec_dec_digits++ : sec_digits++;
}
else
{
/* If we're not reading seconds, we are now. */
current_piece++;
sec_piece = current_piece;
if (sec_digits > 0)
{
lwerror("Bad format, cannot include seconds (SS.SSS) more than once.");
}
reading_deg = 0;
reading_min = 0;
reading_sec = 1;
sec_digits++;
}
break;
case 'C':
/* We're done reading anything else we might have been reading. */
if (reading_deg || reading_min || reading_sec)
{
/* We were reading something, that means this is the next piece. */
reading_deg = 0;
reading_min = 0;
reading_sec = 0;
}
current_piece++;
if (compass_dir_piece >= 0)
{
lwerror("Bad format, cannot include compass dir (C) more than once.");
}
/* The compass dir is a piece all by itself. */
compass_dir_piece = current_piece;
current_piece++;
break;
case '.':
/* If we're reading deg, min, or sec, we want a decimal point for it. */
if (reading_deg)
{
deg_has_decpoint = 1;
}
else if (reading_min)
{
min_has_decpoint = 1;
}
else if (reading_sec)
{
sec_has_decpoint = 1;
}
else
{
/* Not reading anything, just pass through the '.' */
strncat(pieces[current_piece], &next_char, 1);
}
break;
default:
/* Any other char is just passed through unchanged. But it does mean we are done reading D, M, or S.*/
if (reading_deg || reading_min || reading_sec)
{
/* We were reading something, that means this is the next piece. */
current_piece++;
reading_deg = 0;
reading_min = 0;
reading_sec = 0;
}
/* Check if this is a multi-byte UTF-8 character. If so go ahead and read the rest of the bytes as well. */
multibyte_char_width = 1;
if (next_char & 0x80)
{
if ((next_char & 0xF8) == 0xF0)
{
multibyte_char_width += 3;
}
else if ((next_char & 0xF0) == 0xE0)
{
multibyte_char_width += 2;
}
else if ((next_char & 0xE0) == 0xC0)
{
multibyte_char_width += 1;
}
else
{
lwerror("Bad format, invalid high-order byte found first, format string may not be UTF-8.");
}
}
if (multibyte_char_width > 1)
{
if (index + multibyte_char_width >= format_length)
{
lwerror("Bad format, UTF-8 character first byte found with insufficient following bytes, format string may not be UTF-8.");
}
for (following_byte_index = (index + 1); following_byte_index < (index + multibyte_char_width); following_byte_index++)
{
if ((format[following_byte_index] & 0xC0) != 0x80)
{
lwerror("Bad format, invalid byte found following leading byte of multibyte character, format string may not be UTF-8.");
}
}
}
/* Copy all the character's bytes into the current piece. */
strncat(pieces[current_piece], &(format[index]), multibyte_char_width);
/* Now increment index past the rest of those bytes. */
index += multibyte_char_width - 1;
break;
}
if (current_piece >= NUM_PIECES)
{
lwerror("Internal error, somehow needed more pieces than it should.");
}
}
if (deg_piece < 0)
{
lwerror("Bad format, degrees (DD.DDD) must be included.");
}
/* Divvy the number up into D, DM, or DMS */
if (val < 0)
{
val *= -1;
is_negative = 1;
}
degrees = val;
if (min_digits > 0)
{
/* Break degrees to integer and use fraction for minutes */
minutes = modf(val, °rees) * 60;
}
if (sec_digits > 0)
{
if (0 == min_digits)
{
lwerror("Bad format, cannot include seconds (SS.SSS) without including minutes (MM.MMM).");
}
seconds = modf(minutes, &minutes) * 60;
if (sec_piece >= 0)
{
/* See if the formatted seconds round up to 60. If so, increment minutes and reset seconds. */
round_pow = pow(10, sec_dec_digits);
if (lround(seconds * round_pow) >= 60 * round_pow)
{
minutes += 1;
seconds = 0;
/* See if the formatted minutes round up to 60. If so, increment degrees and reset seconds. */
if (lround(minutes * round_pow) >= 60 * round_pow)
{
degrees += 1;
minutes = 0;
}
}
}
}
/* Handle the compass direction. If not using compass dir, display degrees as a positive/negative number. */
if (compass_dir_piece >= 0)
{
strcpy(pieces[compass_dir_piece], is_negative ? neg_dir_symbol : pos_dir_symbol);
}
else if (is_negative)
{
degrees *= -1;
}
/* Format the degrees into their string piece. */
if (deg_digits + deg_dec_digits + 2 > WORK_SIZE)
{
lwerror("Bad format, degrees (DD.DDD) number of digits was greater than our working limit.");
}
if(deg_piece >= 0)
{
snprintf(pieces[deg_piece], WORK_SIZE, "%*.*f", deg_digits, deg_dec_digits, degrees);
}
if (min_piece >= 0)
{
/* Format the minutes into their string piece. */
if (min_digits + min_dec_digits + 2 > WORK_SIZE)
{
lwerror("Bad format, minutes (MM.MMM) number of digits was greater than our working limit.");
}
snprintf(pieces[min_piece], WORK_SIZE, "%*.*f", min_digits, min_dec_digits, minutes);
}
if (sec_piece >= 0)
{
/* Format the seconds into their string piece. */
if (sec_digits + sec_dec_digits + 2 > WORK_SIZE)
{
lwerror("Bad format, seconds (SS.SSS) number of digits was greater than our working limit.");
}
snprintf(pieces[sec_piece], WORK_SIZE, "%*.*f", sec_digits, sec_dec_digits, seconds);
}
/* Allocate space for the result. Leave plenty of room for excess digits, negative sign, etc.*/
result = (char*)lwalloc(format_length + WORK_SIZE);
memset(result, 0, format_length + WORK_SIZE);
/* Append all the pieces together. There may be less than 9, but in that case the rest will be blank. */
strcpy(result, pieces[0]);
for (index = 1; index < NUM_PIECES; index++)
{
strcat(result, pieces[index]);
}
return result;
}
/* Print two doubles (lat and lon) in DMS form using the specified format.
* First normalizes them so they will display as -90 to 90 and -180 to 180.
* Format string may be null or 0-length, in which case a default format will be used.
* NOTE: Format string is required to be in UTF-8.
* NOTE2: returned string is lwalloc'ed, caller is responsible to lwfree it up
*/
static char * lwdoubles_to_latlon(double lat, double lon, const char * format)
{
char * lat_text;
char * lon_text;
char * result;
size_t sz;
/* Normalize lat/lon to the normal (-90 to 90, -180 to 180) range. */
lwprint_normalize_latlon(&lat, &lon);
/* This is somewhat inefficient as the format is parsed twice. */
lat_text = lwdouble_to_dms(lat, "N", "S", format);
lon_text = lwdouble_to_dms(lon, "E", "W", format);
/* lat + lon + a space between + the null terminator. */
sz = strlen(lat_text) + strlen(lon_text) + 2;
result = (char*)lwalloc(sz);
snprintf(result, sz, "%s %s", lat_text, lon_text);
lwfree(lat_text);
lwfree(lon_text);
return result;
}
/* Print the X (lon) and Y (lat) of the given point in DMS form using
* the specified format.
* First normalizes the values so they will display as -90 to 90 and -180 to 180.
* Format string may be null or 0-length, in which case a default format will be used.
* NOTE: Format string is required to be in UTF-8.
* NOTE2: returned string is lwalloc'ed, caller is responsible to lwfree it up
*/
char* lwpoint_to_latlon(const LWPOINT * pt, const char *format)
{
const POINT2D *p;
if (NULL == pt)
{
lwerror("Cannot convert a null point into formatted text.");
}
if (lwgeom_is_empty((LWGEOM *)pt))
{
lwerror("Cannot convert an empty point into formatted text.");
}
p = getPoint2d_cp(pt->point, 0);
return lwdoubles_to_latlon(p->y, p->x, format);
}
/*
* Print an ordinate value using at most **maxdd** number of decimal digits
* The actual number of printed decimal digits may be less than the
* requested ones if out of significant digits.
*
* The function will write at most OUT_DOUBLE_BUFFER_SIZE bytes, including the
* terminating NULL.
* It returns the number of bytes written (excluding the final NULL)
*
*/
int
lwprint_double(double d, int maxdd, char *buf)
{
int length;
double ad = fabs(d);
int precision = FP_MAX(0, maxdd);
if (ad <= OUT_MIN_DOUBLE || ad >= OUT_MAX_DOUBLE)
{
length = d2sexp_buffered_n(d, precision, buf);
}
else
{
length = d2sfixed_buffered_n(d, precision, buf);
}
buf[length] = '\0';
return length;
}
|