catatom2osm.geo.layer.base module

class catatom2osm.geo.layer.base.BaseLayer(path, baseName, providerLib='ogr')[source]

Bases: qgis._core.QgsVectorLayer

Base class for application layers.

append(layer, rename=None, resolve=None, query=None, **kwargs)[source]

Copy all features from layer.

Parameters
  • layer (QgsVectorLayer) – Source layer

  • rename (dict) – Translation of attributes names

  • resolve (dict) – xlink reference fields

  • query (func) – function with args feature and kwargs that returns a boolean deciding if each feature will be included or not

  • kwargs – aditional arguments for query function

Examples

>>> query = lambda feat, kwargs: feat['foo']=='bar'
Will copy only features with a value 'bar' in the field 'foo'.
>>> query = lambda feat, kwargs: layer.is_inside(feat, kwargs['zone'])
Will copy only features inside zone.

See also copy_feature().

bounding_box(expression=None)[source]

Return bounding box in overpass format.

Use features matching expression or all features if expression is None.

copy_feature(feature, rename=None, resolve=None)[source]

Return a copy of feature renaming attributes or resolving xlink references.

Parameters
  • feature (QgsFeature) – Source feature

  • rename (dict) – Translation of attributes names

  • resolve (dict) – xlink reference fields

Examples

With this:

>>> rename = {'spec': 'specification'}
>>> resolve = {
...     'PD_id': ('component_href', r'[\w\.]+PD[\.0-9]+'),
...     'TN_id': ('component_href', r'[\w\.]+TN[\.0-9]+'),
...     'AU_id': ('component_href', r'[\w\.]+AU[\.0-9]+')
... }

You get:

>>> original_attributes = ['localId', 'specification', 'component_href']
>>> original_values = [
...     '38.012.1.12.0295603CS6109N',
...     'Parcel', (
...         '(3:#ES.SDGC.PD.38.012.38570,'
...         '#ES.SDGC.TN.38.012.1,'
...         '#ES.SDGC.AU.38.012)'
...     )
... ]
>>> final_attributes = ['localId', 'spec', 'PD_id', 'TN_id', 'AU_id']
>>> final_values = [
...     '38.012.1.12.0295603CS6109N',
...     'Parcel',
...     'ES.SDGC.PD.38.012.38570',
...     'ES.SDGC.TN.38.012.1',
...     'ES.SDGC.AU.38.012'
... ]
count(expression='', unique='')[source]

Return number of features for this search expression.

static create_shp(name, crs, fields=<qgis._core.QgsFields object>, geom_type=6)[source]
static delete_shp(path)[source]
export(path, driver_name='ESRI Shapefile', overwrite=True, target_crs_id=None)[source]

Write layer to file.

Parameters
  • path (str) – Path of the output file

  • driver_name (str) – Defaults to ESRI Shapefile.

  • overwrite (bool) – Defaults to True

  • target_crs_id (int) – Defaults to source CRS

static get_crs_transform(source_crs, target_crs)[source]
get_index()[source]

Return a QgsSpatialIndex of all features in this layer.

get_overpass_bbox(bbox)[source]

Transform bbox to EPSG 4326 and returns str in overpass format.

get_progressbar(description, total=None)[source]

Return progress bar with ‘description’ for ‘total’ iterations.

static get_writer(name, crs, fields=<qgis._core.QgsFields object>, geom_type=6)[source]
join_field(source_layer, target_field_name, join_field_name, field_names_subset, prefix='')[source]

Replace qgis table join mechanism.

I’m not able to work with it in standalone script mode (without GUI).

Parameters
  • source_layer (QgsVectorLayer) – Source layer.

  • target_field_name (str) – Join field in the target layer.

  • join_fieldsName (str) – Join field in the source layer.

  • field_names_subset (list) – List of field name strings for the target layer.

  • prefix (str) – An optional prefix to add to the target fields names

reproject(target_crs=None)[source]

Reproject all features in this layer to a new CRS.

Parameters

target_crs (QgsCoordinateReferenceSystem) – New CRS to apply.

search(expression='')[source]

Return a features iterator for this search expression.

to_osm(tags_translation=<function all_tags>, data=None, tags={}, upload='never')[source]

Export this layer to an Osm data set.

Parameters
  • tags_translation (function) – Function to translate fields to tags. By defaults convert all fields.

  • data (Osm) – OSM data set to append. By default creates a new one.

  • upload (str) – upload attribute of the osm dataset, default ‘never’

  • tags (dict) – tags to update config.changeset_tags

Returns

OSM data set

Return type

Osm

translate_field(field_name, translations, clean=True)[source]

Transform the values of a field.

Parameters
  • field_name (str) – Name of the field to transform

  • translations (dict) – A dictionary used to transform field values

  • clean (bool) – If true (default), delete features without translation

writeAsVectorFormat(name, driver_name, target_crs=None)[source]