I am writing a custom function in PyQGIS to use in expression editor: the aim of the function is to retrieve the first feature that contains the current feature from a polygon layer.
def get_containing_feature(value1, feature, parent):
cont_feature = None
cont_layer = QgsMapLayerRegistry.instance().mapLayersByName(value1)[0]
crsSrc = QgsCoordinateReferenceSystem(4326) # WGS 84
crsDest = cont_layer.crs()
xform = QgsCoordinateTransform(crsSrc, crsDest)
curr_geom = feature.geometry()
curr_geom2 = xform.transform(curr_geom.asPoint())
features = cont_layer.getFeatures()
for feat in features:
if feat.geometry().contains(curr_geom2):
cont_feature = feat
break
return cont_feature
The layers aren't in the same crs so I need a transformation The polygon layer is accessed by name and I can get its crs using .crs() method, but I don't know how to get the feature's layer and crs (currently EPSG: 4326)