Skip to content

Commit

Permalink
Merge pull request #39 from QGEP/only_rp_to_reaches
Browse files Browse the repository at this point in the history
Only allow to connect RP_TO to reaches
  • Loading branch information
m-kuhn authored Jun 6, 2019
2 parents b0fd517 + edebeb8 commit 98f726c
Showing 1 changed file with 46 additions and 17 deletions.
63 changes: 46 additions & 17 deletions src/tools/qgepmaptools.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,14 @@ def activate(self):
Called by QGIS whenever the tool is activated.
"""

def is_closer_to_start_of_edge(source, target):
feature = next(source.layer().getFeatures(QgsFeatureRequest(source.featureId())))
distance_from_start = feature.geometry().lineLocatePoint(QgsGeometry.fromPointXY(source.point()))
length = feature.geometry().length()

return distance_from_start < length / 2


# A dict of layers
# and for each layer the fields to use as foreign key
# as well as the possible target layers
Expand All @@ -626,24 +634,40 @@ def activate(self):
self.network_element_sources = {
QgepLayerManager.layer('vw_qgep_reach'): {
'fields': [
('rp_to_fk_wastewater_networkelement',
QCoreApplication.translate('QgepMapToolConnectNetworkElements', 'Reach Point To')),
('rp_from_fk_wastewater_networkelement',
QCoreApplication.translate('QgepMapToolConnectNetworkElements', 'Reach Point From'))
{
'id': 'rp_from_fk_wastewater_networkelement',
'name': QCoreApplication.translate('QgepMapToolConnectNetworkElements', 'Reach Point From'),
'filter': lambda source, target: target.layer() != QgepLayerManager.layer('vw_qgep_reach'),
'is_checked': lambda source, target: is_closer_to_start_of_edge(source, target)
},
{
'id': 'rp_to_fk_wastewater_networkelement',
'name': QCoreApplication.translate('QgepMapToolConnectNetworkElements', 'Reach Point To'),
'is_checked': lambda source, target: not is_closer_to_start_of_edge(source, target)
}
],
'target_layers': [
QgepLayerManager.layer('vw_wastewater_node'),
QgepLayerManager.layer('vw_qgep_reach')
]},
QgepLayerManager.layer('catchment_area'): {'fields': [
('fk_wastewater_networkelement_rw_current', QCoreApplication.translate(
'QgepMapToolConnectNetworkElements', 'Rainwater current')),
('fk_wastewater_networkelement_rw_planned', QCoreApplication.translate(
'QgepMapToolConnectNetworkElements', 'Rainwater planned')),
('fk_wastewater_networkelement_ww_current', QCoreApplication.translate(
'QgepMapToolConnectNetworkElements', 'Wastewater current')),
('fk_wastewater_networkelement_ww_planned', QCoreApplication.translate(
'QgepMapToolConnectNetworkElements', 'Wastewater planned'))
QgepLayerManager.layer('catchment_area'): {
'fields': [
{
'id': 'fk_wastewater_networkelement_rw_current',
'name': QCoreApplication.translate('QgepMapToolConnectNetworkElements', 'Rainwater current')
},
{
'id': 'fk_wastewater_networkelement_rw_planned',
'name': QCoreApplication.translate('QgepMapToolConnectNetworkElements', 'Rainwater planned')
},
{
'id': 'fk_wastewater_networkelement_ww_current',
'name': QCoreApplication.translate('QgepMapToolConnectNetworkElements', 'Wastewater current')
},
{
'id': 'fk_wastewater_networkelement_ww_planned',
'name': QCoreApplication.translate('QgepMapToolConnectNetworkElements', 'Wastewater planned')
}
],
'target_layers': [
QgepLayerManager.layer('vw_wastewater_node')
Expand Down Expand Up @@ -786,8 +810,14 @@ def connect_features(self, source, target):
properties = list()

for prop in self.network_element_sources[source.layer()]['fields']:
cbx = QCheckBox(prop[1])
cbx.setObjectName(prop[0])
if 'filter' in prop.keys():
if not prop['filter'](source, target):
continue
cbx = QCheckBox(prop['name'])
cbx.setObjectName(prop['id'])

if 'is_checked' in prop.keys():
cbx.setChecked(prop['is_checked'](source, target))
properties.append(cbx)
dlg.layout().addWidget(cbx)

Expand All @@ -809,8 +839,7 @@ def connect_features(self, source, target):
elif source.layer().updateFeature(source_feature):
self.iface.messageBar().pushMessage('QGEP',
self.tr('Connected {} to {}').format(
source_feature[
'identifier'],
source_feature['identifier'],
target_feature['identifier']),
Qgis.Info, 5)
else:
Expand Down

0 comments on commit 98f726c

Please sign in to comment.