Polyline/polygon files¶
Polyline .pli(z) and polygon .pol files are basic spatial input files for a D-Flow FM model to select particular locations, and are used in various other input files.
They are represented by the classes below.
Model¶
models.py defines all classes and functions related to representing pol/pli(z) files.
Description
¶
Bases: BaseModel
Description of a single PolyObject.
The Description will be prepended to a block. Each line will start with a '*'.
Attributes:
Name | Type | Description |
---|---|---|
content |
str
|
The content of this Description. |
Source code in hydrolib/core/dflowfm/polyfile/models.py
9 10 11 12 13 14 15 16 17 18 19 |
|
Metadata
¶
Bases: BaseModel
Metadata of a single PolyObject.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
The name of the PolyObject |
n_rows |
int
|
The number of rows (i.e. Point instances) of the PolyObject |
n_columns |
int
|
The total number of values in a Point, including x, y, and z. |
Source code in hydrolib/core/dflowfm/polyfile/models.py
22 23 24 25 26 27 28 29 30 31 32 33 |
|
Point
¶
Bases: BaseModel
Point consisting of a x and y coordinate, an optional z coordinate and data.
Attributes:
Name | Type | Description |
---|---|---|
x |
float
|
The x-coordinate of this Point |
y |
float
|
The y-coordinate of this Point |
z |
Optional[float]
|
An optional z-coordinate of this Point. |
data |
Sequence[float]
|
The additional data variables of this Point. |
Source code in hydrolib/core/dflowfm/polyfile/models.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
PolyFile
¶
Bases: ParsableFileModel
Poly-file (.pol/.pli/.pliz) representation.
Source code in hydrolib/core/dflowfm/polyfile/models.py
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 |
|
PolyObject
¶
Bases: BaseModel
PolyObject describing a single block in a poly file.
The metadata should be consistent with the points: - The number of points should be equal to number of rows defined in the metadata - The data of each point should be equal to the number of columns defined in the metadata.
Attributes:
Name | Type | Description |
---|---|---|
description |
Optional[Description]
|
An optional description of this PolyObject |
metadata |
Metadata
|
The Metadata of this PolObject, describing the structure |
points |
List[Point]
|
The points describing this PolyObject, structured according to the Metadata |
Source code in hydrolib/core/dflowfm/polyfile/models.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
Parser¶
parser.py defines all classes and functions related to parsing pol/pli(z) files.
Block
¶
Bases: BaseModel
Block is a temporary object which will be converted into a PolyObject.
The fields are supposed to be set during the lifetime of this object. When all fields are set, finalize can be called.
Attributes:
Name | Type | Description |
---|---|---|
start_line |
int
|
The starting line of this current block. |
name |
Optional[str]
|
The name of this block. Defaults to None. |
dimensions |
Optional[Tuple[int, int]]
|
The dimensions (n_rows, n_columns) of this Block. Defaults to None. |
points |
Optional[List[Point]]
|
The points of this block. Defaults to None. |
ws_warnings |
List[ParseMsg]
|
The whitespace warnings associated with this block. Defaults to an empty list. |
empty_lines |
List[int]
|
The line numbers of the empty lines. Defaults to an empty list. |
Source code in hydrolib/core/dflowfm/polyfile/parser.py
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 |
|
finalize()
¶
Finalise this Block and return the constructed PolyObject and warnings
If the metadata or the points are None, then None is returned.
Returns:
Type | Description |
---|---|
Optional[Tuple[PolyObject, List[ParseMsg]]]
|
Optional[Tuple[PolyObject, List[ParseMsg]]]: The constructed PolyObject and warnings encountered while parsing it. |
Source code in hydrolib/core/dflowfm/polyfile/parser.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
ErrorBuilder
¶
ErrorBuilder provides the functionality to the Parser to keep track of errors.
Source code in hydrolib/core/dflowfm/polyfile/parser.py
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 |
|
__init__()
¶
Create a new ErorrorBuilder
Source code in hydrolib/core/dflowfm/polyfile/parser.py
180 181 182 |
|
end_invalid_block(line)
¶
Store the end line of the current block
If no invalid block currently exists, nothing will be done.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
line
|
int
|
the final line of this invalid block |
required |
Source code in hydrolib/core/dflowfm/polyfile/parser.py
203 204 205 206 207 208 209 210 211 212 |
|
finalize_previous_error()
¶
Finalize the current invalid block if it exists
If no current invalid block exists, None will be returned, and nothing will change. If a current block exists, it will be converted into a ParseMsg and returned. The current invalid block will be reset.
Returns:
Type | Description |
---|---|
Optional[ParseMsg]
|
Optional[ParseMsg]: The corresponding ParseMsg if an InvalidBlock exists. |
Source code in hydrolib/core/dflowfm/polyfile/parser.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
|
start_invalid_block(block_start, invalid_line, reason)
¶
Start a new invalid block if none exists at the moment
If we are already in an invalid block, or the previous one was never finalised, we will not log the reason, and assume it is one long invalid block.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
block_start
|
int
|
The start of the invalid block. |
required |
invalid_line
|
int
|
The actual offending line number. |
required |
reason
|
str
|
The reason why this block is invalid. |
required |
Source code in hydrolib/core/dflowfm/polyfile/parser.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
|
InvalidBlock
¶
Bases: BaseModel
InvalidBlock is a temporary object which will be converted into a ParseMsg.
Attributes:
Name | Type | Description |
---|---|---|
start_line |
int
|
The start line of this InvalidBlock |
end_line |
Optional[int]
|
The end line of this InvalidBlock if it is set. Defaults to None. |
invalid_line |
int
|
The line which is causing this block to be invalid. |
reason |
str
|
A human-readable string detailing the reason of the ParseMsg. |
Source code in hydrolib/core/dflowfm/polyfile/parser.py
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 |
|
to_msg()
¶
Convert this InvalidBlock to the corresponding ParseMsg
Returns:
Name | Type | Description |
---|---|---|
ParseMsg |
ParseMsg
|
The ParseMsg corresponding with this InvalidBlock |
Source code in hydrolib/core/dflowfm/polyfile/parser.py
164 165 166 167 168 169 170 171 172 173 174 |
|
ParseMsg
¶
Bases: BaseModel
ParseMsg defines a single message indicating a significant parse event.
Attributes:
Name | Type | Description |
---|---|---|
line_start |
int
|
The start line of the block to which this ParseMsg refers. |
line_end |
int
|
The end line of the block to which this ParseMsg refers. |
column |
Optional[Tuple[int, int]]
|
An optional begin and end column to which this ParseMsg refers. |
reason |
str
|
A human-readable string detailing the reason of the ParseMsg. |
Source code in hydrolib/core/dflowfm/polyfile/parser.py
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 |
|
format_parsemsg_to_string(file_path=None)
¶
Format string describing this ParseMsg
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
Optional[Path]
|
The file path mentioned in the message if specified. Defaults to None. |
None
|
Source code in hydrolib/core/dflowfm/polyfile/parser.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
Parser
¶
Parser provides the functionality to parse a polyfile line by line.
The Parser parses blocks describing PolyObject instances by relying on a rudimentary state machine. The states are encoded with the StateType. New lines are fed through the feed_line method. After each line the internal state will be updated. When a complete block is read, it will be converted into a PolyObject and stored internally. When finalise is called, the constructed objects, as well as any warnings and errors describing invalid blocks, will be returned.
Each state defines a feed_line method, stored in the _feed_line dict, which consumes a line and potentially transitions the state into the next. Each state further defines a finalise method, stored in the _finalise dict, which is called upon finalising the parser.
Invalid states are encoded with INVALID_STATE. In this state the Parser attempts to find a new block, and thus looks for a new description or name.
Unexpected whitespace before comments, names, and dimensions, as well as empty lines will generate a warning, and will be ignored by the parser.
Source code in hydrolib/core/dflowfm/polyfile/parser.py
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 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
|
__init__(file_path, has_z_value=False)
¶
Create a new Parser
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
Path
|
Name of the file being parsed, only used for providing proper warnings. |
required |
has_z_value
|
bool
|
Whether to interpret the third column as z-coordinates. Defaults to False. |
False
|
Source code in hydrolib/core/dflowfm/polyfile/parser.py
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 |
|
feed_line(line)
¶
Parse the next line with this Parser.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
line
|
str
|
The line to parse |
required |
Source code in hydrolib/core/dflowfm/polyfile/parser.py
305 306 307 308 309 310 311 312 313 314 315 316 317 |
|
finalize()
¶
Finalize parsing and return the constructed PolyObject.
Raises:
Type | Description |
---|---|
ValueError
|
When the plifile is invalid. |
Returns:
Name | Type | Description |
---|---|---|
PolyObject |
Sequence[PolyObject]
|
A PolyObject containing the constructed PolyObject instances. |
Source code in hydrolib/core/dflowfm/polyfile/parser.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
|
StateType
¶
Bases: IntEnum
The types of state of a Parser.
Source code in hydrolib/core/dflowfm/polyfile/parser.py
233 234 235 236 237 238 239 240 |
|
read_polyfile(filepath, has_z_values=None)
¶
Read the specified file and return the corresponding data.
The file is expected to follow the .pli(z) / .pol convention. A .pli(z) or .pol file is defined as consisting of a number of blocks of lines adhering to the following format:
- Optional description record consisting of one or more lines starting with '*'. These will be ignored.
- Name consisting of a character string
- Two integers, Nr and Nc, representing the numbers of rows and columns respectively
- Nr number of data points, consisting of Nc floats separated by whitespace
For example:
...
*
* Polyline L008
*
L008
4 2
131595.0 549685.0
131750.0 549865.0
131595.0 550025.0
131415.0 550175.0
...
Note that the points can be arbitrarily indented, and the comments are optional.
if no has_z_value has been defined, it will be based on the file path extensions of the filepath: - .pliz will default to True - .pli and .pol will default to False
Empty lines and unexpected whitespace will be flagged as warnings, and ignored.
If invalid syntax is detected within a block, an error will be created. This block will be ignored for the purpose of creating PolyObject instances. Once an error is encountered, any following lines will be marked as part of the invalid block, until a new valid block is found. Note that this means that sequential invalid blocks will be reported as a single invalid block. Such invalid blocks will be reported as warnings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath
|
Path
|
Path to the pli(z)/pol convention structured file. |
required |
has_z_values
|
Optional[bool]
|
Whether to create points containing a z-value. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
When the plifile is invalid. |
Returns:
Name | Type | Description |
---|---|---|
Dict |
Dict
|
The dictionary describing the data of a PolyObject. |
Source code in hydrolib/core/dflowfm/polyfile/parser.py
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 589 590 |
|
Serializer¶
Serializer
¶
Serializer provides several static serialize methods for the models.
Source code in hydrolib/core/dflowfm/polyfile/serializer.py
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 |
|
serialize_description(description)
staticmethod
¶
Serialize the Description to a string which can be used within a polyfile.
Returns:
Name | Type | Description |
---|---|---|
str |
Iterable[str]
|
The serialised equivalent of this Description |
Source code in hydrolib/core/dflowfm/polyfile/serializer.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
serialize_metadata(metadata)
staticmethod
¶
Serialize this Metadata to a string which can be used within a polyfile.
The number of rows and number of columns are separated by four spaces.
Returns:
Name | Type | Description |
---|---|---|
str |
Iterable[str]
|
The serialised equivalent of this Metadata |
Source code in hydrolib/core/dflowfm/polyfile/serializer.py
32 33 34 35 36 37 38 39 40 41 |
|
serialize_point(point, config)
staticmethod
¶
Serialize this Point to a string which can be used within a polyfile.
the point data is indented with 4 spaces, and the individual values are separated by 4 spaces as well.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
point
|
Point
|
The point to serialize. |
required |
config
|
SerializerConfig
|
The serialization configuration. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The serialised equivalent of this Point |
Source code in hydrolib/core/dflowfm/polyfile/serializer.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
serialize_poly_object(obj, config)
staticmethod
¶
Serialize this PolyObject to a string which can be used within a polyfile.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
PolyObject
|
The poly object to serializer. |
required |
config
|
SerializerConfig
|
The serialization configuration. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
Iterable[str]
|
The serialised equivalent of this PolyObject |
Source code in hydrolib/core/dflowfm/polyfile/serializer.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
|
write_polyfile(path, data, config)
¶
Write the data to a new file at path
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
Path
|
The path to write the data to |
required |
data
|
Sequence[PolyObject]
|
The poly objects to write |
required |
config
|
SerializerConfig
|
The serialization configuration. |
required |
Source code in hydrolib/core/dflowfm/polyfile/serializer.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|