gws.base.feature

Features.

A feature is a set of named attributes that belongs to a model. The model defines which attribute is the unique id (uidName) and which one holds the geometry (geometryName), as a gws.Shape. See gws.base.model for how models read and write features.

Besides its attributes, a feature carries the raw data from its source (record), the data from or for the client (props), rendered views (views, e.g. title or label from feature templates) and validation errors (errors).

This package contains the feature implementation and the new function to create features.

Example:

f = gws.base.feature.new(model=model, attributes={'id': 1, 'name': 'A'})
f.set('geom', gws.lib.shape.from_wkt('POINT(1 2)', crs))
f.uid()             # '1', if the model's uidName is 'id'
f.to_geojson()

Source code: gws.base.feature

Package Contents

class gws.base.feature.Feature(model: gws.Model)

Bases: gws.Feature

Feature object.

attributes

Attribute values as python values.

category = ''

Category for grouping features in the client, e.g. the source layer name.

createWithFeatures = []

Related features a new feature is created with.

cssSelector = ''

CSS selector for the feature style in the client.

errors = []

Validation errors.

insertedPrimaryKey = ''

Primary key of the row inserted when the feature was created.

isNew = False

The feature is new and not yet saved.

model

Model the feature belongs to.

views

Rendered views, keyed by view name, e.g. title or label.

get(name, default=None)

Get an attribute value.

Parameters:
  • name – Attribute name.

  • default – Value to return if the attribute does not exist.

Returns:

The attribute value, or default.

has(name)

Check if an attribute exists.

Parameters:

name – Attribute name.

Returns:

True if the attribute exists.

raw(name)

Get a raw attribute value from the record.

Parameters:

name – Attribute name.

Returns:

The value, or None if the record has no such attribute.

render_views(templates, **kwargs)

Render feature views and store them in views.

Each template renders the view named after the last part of its subject, e.g. feature.title renders title. The templates receive the feature attributes, feature and the extra arguments.

Parameters:
  • templates – Feature templates.

  • **kwargs – Extra template arguments.

Returns:

The feature itself.

set(name, value)

Set an attribute value.

Parameters:
  • name – Attribute name.

  • value – Attribute value.

Returns:

The feature itself.

shape()

Get the feature geometry.

Returns:

The geometry, or None if the model has no geometry or the feature has no value.

to_geojson(keep_crs=False) → dict

Convert the feature to a GeoJSON feature.

The geometry is converted to WGS84 unless keep_crs is set. The uid is added as the id property.

Parameters:

keep_crs – Keep the geometry coordinates in the feature CRS.

Returns:

A GeoJSON feature dict.

to_svg(view, label=None, style=None)

Render the feature geometry as SVG fragments for a map view.

Parameters:
  • view – Map view. The geometry is transformed to its CRS.

  • label – Label text.

  • style – Style.

Returns:

A list of SVG elements, empty if the feature has no geometry.

transform_to(crs) → gws.Feature

Transform the feature geometry to another CRS, in place.

Parameters:

crs – Target CRS.

Returns:

The feature itself.

uid()

Get the unique id of the feature.

Returns:

The value of the uid attribute as a string, an empty string if the model has no uid attribute, or None if the feature has no value.

gws.base.feature.new(model: gws.Model, attributes: dict | None = None, record: gws.FeatureRecord | None = None, props: gws.FeatureProps | None = None) → gws.Feature

Create a feature.

Parameters:
  • model – Model the feature belongs to.

  • attributes – Feature attributes.

  • record – Raw data from the source. Defaults to an empty record.

  • props – Data from the client. Defaults to empty props.

Returns:

The feature.