Model
model
Package containing models, the pythonic representation of feature classes.
They inherit from BaseFeature, which extends the Pydantic BaseModel with some utility, and are resolved via an Appschema instance.
A feature collection is represented by the BaseCollection class.
Example
Load the BP_Plan model for XPlanung v6.0 and instantiate it with some data:
Appschema
Bases: BaseModel
Models a supported appschema.
Used to access metadata like name, version and description for an appschema.
During instantion, validation if an appschema is supported occurs, based on the modules
in appschema subdirectory.
The instance stores a reference to the appschema's module, which is used in the model_factory method the retrieve appschema classes.
Usage example
# get a featuretype from appschema
appschema = Appschema.from_prefix(prefix="xplan", version="6.0")
plan = appschema.model_factory(name="BP_Plan")
# show list of supported appschemas
Appschema.supported_appschemas()
# get an enum of supported appschemas
SupportedAppschemas = Appschema.enum()
SupportedAppschemas.XPLAN_6_0.value # -> XPlanGML 6_0
code
property
The enum code of the appschema.
top_level_featuretypes
cached
property
Names of the top-level owner feature types of this appschema.
A top-level owner existentially owns at least one part (an association
whose dependent_part is set) but is itself never the part of another
owner: the containment/deletion roots, e.g. BP_Plan. Derived from the
appschema's association metadata as the owner-set minus the part-set.
Returns:
| Type | Description |
|---|---|
list[str]
|
The owner feature type names, sorted. |
version
property
The version of the appschema in <major>.<minor> format.
enum()
classmethod
Return an enumeration of supported appschemas.
Source code in xplan_tools/model/base.py
from_enum(code)
classmethod
Return an Appschema instance from enum code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
an appschema code, e.g. |
required |
Source code in xplan_tools/model/base.py
from_module(module_name)
cached
classmethod
Builds an Appschema instance from module name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module_name
|
str
|
the fully qualified module name |
required |
Source code in xplan_tools/model/base.py
from_namespace(namespace)
classmethod
Builds an Appschema instance from the appschema's namespace.
Might return a compatible appschema version if no exact match is found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
str
|
the namespace URI of the appschema |
required |
Source code in xplan_tools/model/base.py
from_prefix(prefix, version)
classmethod
Builds an Appschema instance from the appschema's prefix and version.
Might return a compatible appschema version if no exact match is found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
the namespace prefix of the appschema, e.g. |
required |
version
|
str
|
the version of the appschema; major and minor are required |
required |
Source code in xplan_tools/model/base.py
model_factory(name)
Factory method for retrieving the corresponding pydantic model representation of a feature class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
name of the feature class or enumeration |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
requested FeatureType not found |
Returns:
| Type | Description |
|---|---|
type[BaseFeature]
|
The concrete feature class inheriting from BaseFeature. |
Source code in xplan_tools/model/base.py
supported_appschemas()
classmethod
Return a list of currently supported appschemas.
Source code in xplan_tools/model/base.py
BaseFeature
Bases: _ConfiguredBaseModel, GMLAdapter, CoretableAdapter, JsonFGAdapter
Base class for application schema classes.
It extends pydantic BaseModel with Feature-related helper methods as well as conversion capabilities from/to other formats via inheriting from respective adapter classes.
appschema()
cached
classmethod
Return metadata about the application schema for the feature class.
get_associations()
cached
classmethod
Returns the classes association fields.
Source code in xplan_tools/model/base.py
get_geom_field()
cached
classmethod
Returns the classes geometry field name, if any.
Source code in xplan_tools/model/base.py
get_geom_srid()
Returns the object's geometry's SRID, if any.
get_geom_types()
cached
classmethod
Returns the types of the geometry attribute.
Source code in xplan_tools/model/base.py
get_geom_wkt()
Returns the object's eWKT geometry's WKT representation withouth SRID, if any.
get_name()
cached
classmethod
get_property_info(name)
cached
classmethod
Property information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The property's name. |
required |
Returns:
| Type | Description |
|---|---|
PropertyInfo
|
A typed dataclass holding the information. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
The name was not found in the model fields. |
Source code in xplan_tools/model/base.py
model_dump_coretable()
model_dump_coretable_bulk()
Dumps the model data to feature, refs and inverse refs dicts to bulk insert in a database.
model_dump_gml(**kwargs)
BaseCollection
Bases: BaseModel
Container for features that provides validation of references.
The features are stored in a dictionary with their ID as key and the feature instance as value.
add_style_properties(to_text=False, always_populate_schriftinhalt=False)
Add styling properties to presentational objects.
This method parses object (dientZurDarstellungVon) and property (art) references from presentational objects and derives styling information (stylesheetId, schriftinhalt) based on a set of defined rules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_text
|
bool
|
Whether to convert symbolic presentational objects to textual ones. Defaults to False. |
False
|
always_populate_schriftinhalt
|
bool
|
Populate |
False
|
Source code in xplan_tools/model/base.py
check_references_and_srs(info)
Checks if all objects referenced via UUID are part of the collection and if all features have the same SRS, version and appschema.
An association reference is only supported if it is a UUID naming a feature of a
compatible type within the collection. An unresolvable UUID and an external URL
both raise, unless the validation context sets DROP_INVALID_REFS: they are then
removed from the feature and collected under the context's INVALID_REFS key,
which is how the caller gets them back:
context = {DROP_INVALID_REFS: True}
collection = BaseCollection.model_validate(
{"features": features, "srid": srid, "appschema": appschema},
context=context,
)
dropped = context.get(INVALID_REFS, [])
A mandatory role is never emptied that way - a feature that has to carry the reference raises either way.
The whole collection is checked before anything is raised or removed, so one bad
reference reports the rest with it and a failed check leaves the features as they
were. Dropping edits the feature objects the caller passed in: pydantic does not copy
BaseFeature instances when validating them into the collection, so they are the very
same objects.
Source code in xplan_tools/model/base.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 595 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 638 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 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 | |
from_features(features, srid, appschema, *, context=None)
classmethod
Builds a collection, passing context to the reference check.
The one constructor every repository uses, so the read options a caller can set -
DROP_INVALID_REFS above all - reach the validator the same way whichever format
was read.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
dict[UUID, BaseFeature] | list[BaseFeature]
|
The features, keyed by id or as a list. |
required |
srid
|
int
|
The collection's spatial reference system identifier. |
required |
appschema
|
Appschema
|
The appschema every feature belongs to. |
required |
context
|
dict[str, Any] | None
|
Pydantic validation context; see |
None
|
Returns:
| Type | Description |
|---|---|
BaseCollection
|
The validated collection. |
Source code in xplan_tools/model/base.py
get_features()
get_single_plans(with_name=False)
Yields BaseCollection objects for every plan in the original collection.
A plan collection is the containment closure of a top-level feature type, derived
from the appschema's dependent_part metadata rather than from hardcoded role
names: a role marked target points at a part the feature owns, a role marked
source points back at its owner. Both directions are followed, since a file may
populate only one of them.
A plan is self-contained, so a reference leaving its collection is invalid data and raises when the yielded collection validates its references.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the appschema declares no |
Source code in xplan_tools/model/base.py
list_to_dict(data)
classmethod
Takes a list of BaseFeatures and returns a BaseCollection dict.
Source code in xplan_tools/model/base.py
make_copy(with_id_map=False)
Return a copy of the collection with new IDs.
All feature IDs are renewed and respective references are updated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
with_id_map
|
bool
|
whether to additionally return a map of old IDs to new IDs |
False
|