utils
Module for internal utility functions to share between modules
Classes:
| Name | Description |
|---|---|
Vector |
Simple Vector implementation that takes a start and end point. |
Functions:
| Name | Description |
|---|---|
box_on_point |
Build a rectangular box on a point |
build_mapx |
|
center_circle |
Create a circle using a center point and a radius |
convert_schema |
Convert a Schema from one format to another |
export_project_lyrx |
Pull all layers from a project file and output them in a directory as lyrx files |
export_project_maps |
Pull all layers from a project file and output them in a directory as mapx files |
get_subtype_count |
Get the subtype counts for a Table or FeatureClass |
get_subtype_counts |
Get a mapping of subtype counts for all featureclasses that have subtypes in the provided Dataset |
nat |
Natural sort key for use in string sorting |
patch_schema_rules |
Patch an exported Schema doc by re-linking attribute rules to table names |
print |
Print a message to the ArcGIS Pro message queue and stdout |
shortest_path |
Find the shortest path or paths given a source point, target point and network of Polylines |
split_at_points |
Split lines at provided points |
split_lines_at_points |
Split a Polyline or Sequence/Iterable of polylines at provided points |
two_point_circle |
Create a circle using a center point and an end point |
Attributes:
| Name | Type | Description |
|---|---|---|
LineCollection |
|
|
PointLike |
|
LineCollection = FeatureClass[Polyline, Any] | Sequence[Polyline] | Iterator[Polyline]
module-attribute
¶
PointLike = PointGeometry | Point
module-attribute
¶
Vector
dataclass
¶
Simple Vector implementation that takes a start and end point.
If PointGeometries are passed as the start and end points, the end point
will inherit the reference of the start point
Attributes:
| Name | Type | Description |
|---|---|---|
x1 |
float
|
The X coordinate of the startpoint |
y1 |
float
|
The Y coordinate of the startpoint |
x2 |
float
|
The X coordiante of the endpoint |
y2 |
float
|
The Y coordiante of the endpoint |
ang |
float
|
The angle of the vector in radians |
dist |
float
|
The magnitute of the vector (distance b/w start and end) |
cos |
float
|
the cos of the vector angle in radians |
sin |
float
|
The sin of the vector angle in radians |
mid |
Point
|
The midpoint of the vector along its magnitude |
Methods:
| Name | Description |
|---|---|
__init__ |
|
__post_init__ |
|
translate |
Translate the provided point along the vector direction. |
Source code in src/arcpie/utils.py
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 | |
end
instance-attribute
¶
start
instance-attribute
¶
__init__(start, end)
¶
__post_init__()
¶
Source code in src/arcpie/utils.py
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 | |
translate(point, dist=None)
¶
Translate the provided point along the vector direction.
The Point will be moved from its original location along the vector angle the provided distance.
The location of the Vector object is not taken into account, only angle and magnitude
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point
|
Point | PointGeometry
|
The point to translate along the given vector |
required |
dist
|
float | None
|
The distance to translate the point (default: |
None
|
Returns:
| Type | Description |
|---|---|
Point | PointGeometry
|
Return the provided geometry back translated |
Note
Whatever point type you provide will be given back to you
Source code in src/arcpie/utils.py
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 | |
box_on_point(center, width, height, angle=0.0, ref=None, start='tl')
¶
Build a rectangular box on a point
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
center
|
Point | PointGeometry
|
The center point of the box |
required |
width
|
float
|
The width of the box |
required |
height
|
float
|
The height of the box |
required |
angle
|
float
|
An angle to roatate the box by in radians (default: 0.0) |
0.0
|
ref
|
SpatialReference | None
|
An optional spatial reference to apply to the output polygon |
None
|
start
|
Literal['tl', 'tr', 'bl', 'br']
|
The corner of the box that should be the start point (default: 'tl') |
'tl'
|
Returns:
| Type | Description |
|---|---|
Polygon
|
A rectangular polygon (with provided ref or ref inhereted from center) |
Source code in src/arcpie/utils.py
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 591 592 593 594 | |
build_mapx(source_map, layers, tables)
¶
Source code in src/arcpie/utils.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
center_circle(center, radius, ref=None)
¶
Create a circle using a center point and a radius
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
center
|
Point | PointGeometry
|
The center of the circle |
required |
radius
|
float
|
(float): The dist |
required |
ref
|
SpatialReference | None
|
(SpatialReference|None): The SpatialReference to use with the returned geometry |
None
|
Returns:
| Type | Description |
|---|---|
Polyline
|
A Circular Polyline |
Note
If a PointGeometry are provided, it will be projected as the provided ref If no ref is provided, the shape will inherit the reference of the center
Reference resolution is as follows:
ref -> center.spatialReference
If no center reference can be found and no ref is provided, the returned geometry will have no reference
Source code in src/arcpie/utils.py
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 | |
convert_schema(schema, to='JSON')
¶
Convert a Schema from one format to another
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Dataset | Path | str
|
Path to the schemafile or Dataset to convert |
required |
to
|
Literal['JSON', 'XLSX', 'HTML', 'PDF', 'XML']
|
Target format (default: 'JSON') |
'JSON'
|
Yields:
| Name | Type | Description |
|---|---|---|
bytes |
BytesIO
|
Raw bytes object containing the schema file |
Source code in src/arcpie/utils.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
export_project_lyrx(project, out_dir, *, indent=4, sort=False, skip_empty=True)
¶
Pull all layers from a project file and output them in a directory as lyrx files
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project
|
Project
|
The |
required |
out_dir
|
Path | str
|
The target directory for the layer files |
required |
indent
|
int
|
Indentation level of the ouput files (default: 4) |
4
|
sort
|
bool
|
Sort the output file by key name (default: False) |
False
|
skip_empty
|
bool
|
Skips writing empty lyrx files for layers with no lyrx data (default: True) |
True
|
Usage
>>> export_project_lyrx(arcpie.Project('<path/to/aprx>'), '<path/to/output_dir>')
Note
Output structure will match the structure of the project:
Map -> Group -> Layer
Where each level is a directory. Group Layers will have a directory entry with individual
files for each layer they contain, as well as a single layerfile that contains all their
child layers.
Source code in src/arcpie/utils.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 | |
export_project_maps(project, out_dir, *, indent=4, sort=False)
¶
Pull all layers from a project file and output them in a directory as mapx files
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project
|
Project
|
The |
required |
out_dir
|
Path | str
|
The target directory for the mapx files |
required |
indent
|
int
|
Indentation level of the ouput files (default: 4) |
4
|
sort
|
bool
|
Sort the output file by key name (default: False) |
False
|
Usage
>>> export_project_maps(arcpie.Project('<path/to/aprx>'), '<path/to/output_dir>')
Source code in src/arcpie/utils.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
get_subtype_count(fc, drop_empty=False)
¶
Get the subtype counts for a Table or FeatureClass
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fc
|
Table | FeatureClass
|
The Table/FeatureClass you want subtype counts for |
required |
drop_empty
|
bool
|
Drop any counts that have no features from the output dictionary (default: False) |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
A mapping of subtype name to subtype count |
Source code in src/arcpie/utils.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
get_subtype_counts(gdb, *, drop_empty=False)
¶
Get a mapping of subtype counts for all featureclasses that have subtypes in the provided Dataset
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gdb
|
Dataset
|
The Dataset instance to get subtype counts for |
required |
drop_empty
|
bool
|
Drop any counts that have no features from the output dictionary (default: False) |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, int]]
|
A mapping of FeatureClass -> SubtypeName -> SubtypeCount |
Usage
>>> get_subtype_counts(Dataset('<path/to/gdb>', drop_empty=True))
{
'FC1':
{
'Default': 10
'Subtype 1': 6
...
},
...
}
Source code in src/arcpie/utils.py
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 | |
nat(val)
¶
Natural sort key for use in string sorting
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
str
|
A value that you want the natural sort key for |
required |
Returns:
| Type | Description |
|---|---|
tuple[tuple[int, ...], tuple[str, ...]
|
A tuple containing all numeric and |
tuple[str, ...]
|
string components in order of appearance. Best used as a sort key |
Usage
>>> pages = ['P-1.3', 'P-2.11', ...]
>>> pages.sort(key=nat)
>>> print(pages)
['P-1.1', 'P-1.2', ...]
Source code in src/arcpie/utils.py
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 | |
patch_schema_rules(schema, *, remove_rules=False)
¶
Patch an exported Schema doc by re-linking attribute rules to table names
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Path | str
|
The input schema to patch |
required |
remove_rules
|
bool
|
Remove attribute rules from the schema (default: False) |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
SchemaWorkspace |
SchemaWorkspace
|
A patched schema dictionary |
Source code in src/arcpie/utils.py
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 | |
print(*values, sep=' ', end='\n', file=None, flush=False, severity=None)
¶
Print a message to the ArcGIS Pro message queue and stdout set severity to 'WARNING' or 'ERROR' to print to the ArcGIS Pro message queue with the appropriate severity
Source code in src/arcpie/utils.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
shortest_path(source, target, network, *, all_paths=False, method='dijkstra', weighted=True, precision=6)
¶
shortest_path(
source: PointLike,
target: PointLike,
network: LineCollection,
*,
all_paths: Literal[False] = False,
method: Literal[
"dijkstra", "bellman-ford"
] = "dijkstra",
weighted: bool = True,
precision: int = 6,
) -> Polyline | None
shortest_path(
source: PointLike,
target: PointLike,
network: LineCollection,
*,
all_paths: Literal[True] = True,
method: Literal[
"dijkstra", "bellman-ford"
] = "dijkstra",
weighted: bool = True,
precision: int = 6,
) -> list[Polyline] | None
Find the shortest path or paths given a source point, target point and network of Polylines
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
PointGeometry | Point
|
The start point for the path |
required |
target
|
PointGeometry | Point
|
The end point for the path |
required |
network
|
FeatureClass[Polyline, Any] | Sequence[Polyline] | Iterator[Polyline]
|
The polylines to traverse |
required |
all_paths
|
bool
|
If True, yield all shortest paths from (default: False) |
False
|
method
|
Literal['dijkstra', 'bellman-ford']
|
The graph traversal algorithm to use (default: 'dijkstra') |
'dijkstra'
|
weighted
|
bool
|
Use line lengths to weight the paths (default: True) |
True
|
precision
|
int
|
Number of decimal places to round coordinates to (default: 6) |
6
|
Returns:
| Type | Description |
|---|---|
Polyline | None
|
The unioned polyline of the path or None if no path is found |
Yields:
| Type | Description |
|---|---|
Polyline
|
Yields all shortest paths if |
Raises:
| Type | Description |
|---|---|
ValueError
|
When input arguments are not of the correct types ( |
Example
path = shortest_path(p1, p2, line_features)
paths = shortest_path(p1, p2, line_features, all_paths=True)
if path is None:
print('No Path')
else:
print(path.length)
# Check that path(s) were found
if paths is None:
print('No Path')
else:
for p in paths:
print(p.length)
Source code in src/arcpie/utils.py
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 517 518 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 | |
split_at_points(lines, points, *, buffer=0.0, min_len=0.0)
¶
Split lines at provided points
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lines
|
FeatureClass[Polyline]
|
Line features to split |
required |
points
|
FeatureClass[PointGeometry]
|
Points to split on |
required |
buffer
|
float
|
Split buffer in feature units (default: 0.0 [exact]) |
0.0
|
min_len
|
float
|
Minumum length for a new line in feature units (default: 0.0) |
0.0
|
Yields:
| Type | Description |
|---|---|
tuple[int, Polyline]]
|
Tuples of parent OID and child shape |
Warning
When splitting features in differing projections, the point features will be projected into the spatial reference of the line features.
Example
>>> # Simple process for splitting lines in place
...
>>> # Initialize a set to capture the removed ids
>>> removed: set[int] = set()
>>> with lines.editor:
... # Insert new lines
... with lines.insert_cursor('SHAPE@') as cur:
... for parent, new_line in split_at_points(lines, points):
... cur.insertRow([new_line])
... removed.add(parent) # Add parent ID to removed
... # Remove old lines (if you're inserting to the same featureclass)
... with lines.update_cursor('OID@') as cur:
... for _ in filter(lambda r: r[0] in removed, cur):
... cur.deleteRow()
Source code in src/arcpie/utils.py
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 | |
split_lines_at_points(lines, points)
¶
Split a Polyline or Sequence/Iterable of polylines at provided points
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lines
|
Polyline | Sequence[Polyline] | Iterator[Polyline]
|
The line or lines to split |
required |
points
|
Sequence[PointGeometry] | Iterator[PointGeometry]
|
The points to split at |
required |
Yields:
| Type | Description |
|---|---|
Polyline
|
Segments of the polyline split at the input points |
Source code in src/arcpie/utils.py
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 | |
two_point_circle(center, end, ref=None)
¶
Create a circle using a center point and an end point
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
center
|
Point | PointGeometry
|
The center of the circle |
required |
end
|
Point | PointGeometry
|
(Point|PointGeometry): The end point of the arc (distance from center is Circle radius) |
required |
ref
|
SpatialReference | None
|
(SpatialReference|None): The SpatialReference to use with the returned geometry |
None
|
Returns:
| Type | Description |
|---|---|
Polyline
|
A Circular Polyline |
Note
If PointGeometries are provided, they will be projected as the provided ref If no ref is provided, the shape will inherit the reference of the center
Reference resolution is as follows:
ref -> center.spatialReference -> end.spatialReference
If both points are Point objects with no spatial reference, and no ref is provided, The returned Polyline will have no spatial reference
Source code in src/arcpie/utils.py
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 | |