Value Converters
convert_to_bool_string
Turns a value into a bool string, suitable for an XML. It is case-insensitive, meaning that the words can also be capitalised.
Accepted values
false,0,0.0,no,non,nein->Falsetrue,1,1.0,yes,oui,ja,sì->True
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
value to transform |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Examples:
result = xmllib.convert_to_bool_string(1)
# result == True
result = xmllib.convert_to_bool_string("nein")
# result == False
result = xmllib.convert_to_bool_string(None)
# raises XmllibInputError
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
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 | |
dms_to_decimal_degrees
Convert a coordinate given in degrees, minutes and seconds to decimal degrees,
which is the form a geolocation takes in CRS84.
The result is signed by the direction: north and east are positive, south and west are negative. It has four more decimal places than the seconds, so that converting it back gives the seconds at the precision they were given in.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
degrees
|
int | str
|
the whole degrees, 0 to 90 for north/south, 0 to 180 for east/west |
required |
minutes
|
int | str
|
the whole minutes, 0 to 59 |
required |
seconds
|
float | int | str
|
the seconds, at least 0 and less than 60 |
required |
direction
|
str
|
|
required |
Returns:
| Type | Description |
|---|---|
str
|
The coordinate in decimal degrees, as a string |
Examples:
result = xmllib.dms_to_decimal_degrees(47, 22, 13.2, "N")
# result == "47.37033"
result = xmllib.dms_to_decimal_degrees(8, 32, 24, "E")
# result == "8.5400"
result = xmllib.dms_to_decimal_degrees("33", "52", "4.36", "S")
# result == "-33.867878"
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
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 | |
crs84_to_lv95
Convert coordinates from CRS84 (WGS84) to the Swiss coordinate system LV95.
The conversion uses the approximate formulas of swisstopo.
They are accurate to about 1 metre, and they apply to Switzerland only.
add_geolocation() warns if a result is outside the range of LV95.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
GeographicCRS84
|
the coordinates in |
required |
Returns:
| Type | Description |
|---|---|
ProjectedLV95
|
The coordinates in |
Examples:
result = xmllib.crs84_to_lv95(
xmllib.GeographicCRS84.create_new(longitude="8.730497222", latitude="46.044130556")
)
# result == xmllib.ProjectedLV95.create_new(easting="2699999.76", northing="1099999.97")
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
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 | |
lv95_to_crs84
Convert coordinates from the Swiss coordinate system LV95 to CRS84 (WGS84).
The conversion uses the approximate formulas of swisstopo. They are accurate to about 1 metre, and they apply to Switzerland only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
ProjectedLV95
|
the coordinates in |
required |
Returns:
| Type | Description |
|---|---|
GeographicCRS84
|
The coordinates in |
Examples:
result = xmllib.lv95_to_crs84(xmllib.ProjectedLV95.create_new(easting="2700000", northing="1100000"))
# result == xmllib.GeographicCRS84.create_new(longitude="8.730499", latitude="46.044127")
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
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 | |
replace_newlines_with_tags
Converts the newlines in a string to XML tags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
string to convert |
required |
converter_option
|
NewlineReplacement
|
specifies what tag to use instead of the newline |
required |
Returns:
| Type | Description |
|---|---|
str
|
String with replaced values |
Examples:
result = xmllib.replace_newlines_with_tags(
"Start\nEnd", xmllib.NewlineReplacement.NONE
)
# result == "Start\nEnd"
result = xmllib.replace_newlines_with_tags(
"Start\nEnd", xmllib.NewlineReplacement.LINEBREAK
)
# result == "Start<br/>End"
result = xmllib.replace_newlines_with_tags(
"Start\n\nEnd", xmllib.NewlineReplacement.PARAGRAPH
)
# result == "<p>Start</p><p>End</p>"
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
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 | |
replace_newlines_with_paragraph_tags
Replace Start\nEnd with <p>Start</p><p>End</p>
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
string to be formatted |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted string with paragraph tags |
Examples:
result = xmllib.replace_newlines_with_paragraph_tags("Start\nEnd")
# result == "<p>Start</p><p>End</p>"
# multiple consecutive newlines will be treated as one newline
result = xmllib.replace_newlines_with_paragraph_tags("Start\n\nEnd")
# result == "<p>Start</p><p>End</p>"
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
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 | |
replace_newlines_with_br_tags
Replaces \n with <br/>
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
string to be formatted |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted string with break-line tags |
Examples:
result = xmllib.replace_newlines_with_br_tags("Start\nEnd")
# result == "Start<br/>End"
# multiple consecutive newlines will be converted into multiple break-lines
result = xmllib.replace_newlines_with_br_tags("Start\n\nEnd")
# result == "Start<br/><br/>End"
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
reformat_date
Reformats a date string into the DSP format.
- If the input cannot be reformatted according to the configuration, or if the result is not a valid DSP date, a warning is emitted and the original input is returned.
- If the input is empty, a warning is emitted and an empty string is returned.
- If the input is already a correctly formatted DSP-date, the original input is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date
|
str | int
|
date string to be reformatted |
required |
date_precision_separator
|
str | None
|
the separation between the day, month and year |
required |
date_range_separator
|
str | None
|
the separation between two dates |
required |
date_format
|
DateFormat
|
the format of the date, see |
required |
calendar
|
Calendar
|
the calendar of the date, see |
GREGORIAN
|
era
|
Era | None
|
the era of the date, see |
CE
|
resource_id
|
str | None
|
the ID of the associated resource, this is to improve the error message |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A reformatted date or the original input if the reformatted result is not a valid DSP date |
Examples:
# default configuration, starting with the day
result = xmllib.reformat_date(
date="1.11.2000",
date_precision_separator=".",
date_range_separator=None,
date_format=xmllib.DateFormat.DD_MM_YYYY
)
# result == "GREGORIAN:CE:2000-11-1:CE:2000-11-1"
# default configuration, but starting with the year
result = xmllib.reformat_date(
date="2000.11.1",
date_precision_separator=".",
date_range_separator=None,
date_format=xmllib.DateFormat.YYYY_MM_DD,
)
# result == "GREGORIAN:CE:2000-11-1:CE:2000-11-1"
# with a date range
result = xmllib.reformat_date(
date="1.11.2000-2001",
date_precision_separator=".",
date_range_separator="-",
date_format=xmllib.DateFormat.DD_MM_YYYY,
)
# result == "GREGORIAN:CE:2000-11-1:CE:2001"
# islamic calendar, where eras are not allowed
result = xmllib.reformat_date(
date="1.11.2000",
date_precision_separator=".",
date_range_separator=None,
date_format=xmllib.DateFormat.DD_MM_YYYY,
calendar=xmllib.Calendar.ISLAMIC,
era=None
)
# result == "ISLAMIC:2000-11-1:2000-11-1"
# with a different era
result = xmllib.reformat_date(
date="1.11.2000",
date_precision_separator=".",
date_range_separator="-",
date_format=xmllib.DateFormat.DD_MM_YYYY,
era=xmllib.Era.AD
)
# result == "GREGORIAN:AD:2000-11-1:AD:2000-11-1"
# reformatted date, no precision in the date string is required
result = xmllib.reformat_date(
date="2000",
date_precision_separator=".",
date_range_separator="-",
date_format=xmllib.DateFormat.DD_MM_YYYY,
)
# result == "GREGORIAN:CE:2000:CE:2000"
# already correctly formatted date
result = xmllib.reformat_date(
date="GREGORIAN:CE:2000:CE:2000",
date_precision_separator=".",
date_range_separator="-",
date_format=xmllib.DateFormat.DD_MM_YYYY,
)
# result == "GREGORIAN:CE:2000:CE:2000"
# invalid input: a warning is emitted and the original input is returned
result = xmllib.reformat_date(
date="not-a-date",
date_precision_separator=".",
date_range_separator="-",
date_format=xmllib.DateFormat.DD_MM_YYYY,
)
# WARNING is emitted
# result == "not-a-date"
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
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 481 482 483 484 | |
find_dates_in_string
Checks if a string contains date values (single dates, or date ranges), and return all found dates as set of DSP-formatted strings. Returns an empty set if no date was found. See XML documentation for details.
Notes
- If no era or calendar is given, dates are interpreted in the Common Era and the Gregorian calendar.
- Standalone numbers from 000-2999, in 3/4-digit form, are interpreted as years CE.
- If a number (with any number of digits) is followed by CE, C.E., AD, A.D., it is interpreted as years CE.
- If a number (with any number of digits) is followed by BCE, BC, B.C., B.C.E., av. J.-C., it is interpreted as years BCE.
- Dates written with slashes are always interpreted in a European manner: 5/11/2021 is the 5th of November.
- In the European notation, 2-digit years are expanded to 4 digits, with the current year as watershed:
- 30.4.24 -> 30.04.2024
- 30.4.50 -> 30.04.1950
Currently supported date formats
- 0476-09-04 -> GREGORIAN:CE:0476-09-04:CE:0476-09-04
- 0476_09_04 -> GREGORIAN:CE:0476-09-04:CE:0476-09-04
- 30.4.2021 -> GREGORIAN:CE:2021-04-30:CE:2021-04-30
- 30.4.21 -> GREGORIAN:CE:2021-04-30:CE:2021-04-30
- 5/11/2021 -> GREGORIAN:CE:2021-11-05:CE:2021-11-05
- Jan 26, 1993 -> GREGORIAN:CE:1993-01-26:CE:1993-01-26
- 26 Jan 1993 -> GREGORIAN:CE:1993-01-26:CE:1993-01-26
- 26 January 1993 -> GREGORIAN:CE:1993-01-26:CE:1993-01-26
-
- Jan. 1993 -> GREGORIAN:CE:1993-01-26:CE:1993-01-26
-
- Januar 1993 -> GREGORIAN:CE:1993-01-26:CE:1993-01-26
- 28.2.-1.12.1515 -> GREGORIAN:CE:1515-02-28:CE:1515-12-01
- 25.-26.2.0800 -> GREGORIAN:CE:0800-02-25:CE:0800-02-26
- 1.9.2022-3.1.2024 -> GREGORIAN:CE:2022-09-01:CE:2024-01-03
- 1848 -> GREGORIAN:CE:1848:CE:1848
- 1849/1850 -> GREGORIAN:CE:1849:CE:1850
- 1849/50 -> GREGORIAN:CE:1849:CE:1850
- 1845-50 -> GREGORIAN:CE:1845:CE:1850
- 840-50 -> GREGORIAN:CE:840:CE:850
- 840-1 -> GREGORIAN:CE:840:CE:841
- 9 BC / 9 B.C. / 9 B.C.E. / 9 BCE -> GREGORIAN:BC:9:BC:9
- 20 BCE - 50 CE -> GREGORIAN:BC:20:CE:50
- 1000-900 av. J.-C. -> GREGORIAN:BC:1000:BC:900
- 45 av. J.-C. -> GREGORIAN:BC:45:BC:45
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
string to check |
required |
Returns:
| Type | Description |
|---|---|
set[str]
|
(possibly empty) set of DSP-formatted date strings |
Examples:
result = xmllib.find_dates_in_string("1849/1850")
# result == {"GREGORIAN:CE:1849:CE:1850"}
result = xmllib.find_dates_in_string("not a valid date")
# result == {}
result = xmllib.find_dates_in_string("first date: 2024. Second: 2025.")
# result == {"GREGORIAN:CE:2024:CE:2024", "GREGORIAN:CE:2025:CE:2025"}
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 | |