I am developing a plugin for QGIS (a Geographic Information Systems) software which uses Python. I have calculated four areas of interest, it does this by identifying each feature on the map (called map_layer
) which falls into certain categories determined by an expression.
For example, Area 1
consists of three categories with the following expressions:
Category Expression
NULL or Good and Excellent "Category_A" IS NULL OR (( "Category_A" = 'Good') AND ( "Category_B" = 'Excellent'))
Good and Standard ("Category_A" = 'Good') AND ("Category_B" = 'Standard')
Meh and Excellent ("Category_A" = 'Meh') AND ("Category_B" = 'Excellent')
For each category, the geometric area is then calculated, totalled and shown in a QLineEdit box. This is then repeated for Area 2
, Area 3
and Area 4
.
My question is how could I make the following code more concise?
def land_area():
# Clear all area text boxes
self.dockwidget.area_Score1_lineEdit.clear()
self.dockwidget.area_Score2_lineEdit.clear()
self.dockwidget.area_Score3_lineEdit.clear()
self.dockwidget.area_Score4_lineEdit.clear()
##########################
######### AREA 1 #########
# NULL or Good and Excellent
# Set area to 0
area1_score = 0
area1_cat1 = QgsExpression( """ "Category_A" IS NULL OR (( "Category_A" = 'Good') AND ( "Category_B" = 'Excellent')) """ )
area1_cat1_feat = map_layer.getFeatures( QgsFeatureRequest( area1_cat1 ) )
area1_cat1_ids = [i for i in area1_cat1_feat]
for f in area1_cat1_ids:
area1_score += f.geometry().area()
# Good and Standard
area1_cat2 = QgsExpression( """ ("Category_A" = 'Good') AND ("Category_B" = 'Standard') """ )
area1_cat2_feat = map_layer.getFeatures( QgsFeatureRequest( area1_cat2 ) )
area1_cat2_ids = [i for i in area1_cat2_feat]
for f in area1_cat2_ids:
area1_score += f.geometry().area()
# Meh and Excellent
area1_cat3 = QgsExpression( """ ("Category_A" = 'Meh') AND ("Category_B" = 'Excellent') """ )
area1_cat3_feat = map_layer.getFeatures( QgsFeatureRequest( area1_cat3 ) )
area1_cat3_ids = [i for i in area1_cat3_feat]
for f in area1_cat3_ids:
area1_score += f.geometry().area()
##########################
######### AREA 2 #########
# Good and Terrible
# Set area to 0
area2_score = 0
area2_cat1 = QgsExpression( """ ( "Category_A" = 'Good') AND ( "Category_B" = 'Terrible') """ )
area2_cat1_feat = map_layer.getFeatures( QgsFeatureRequest( area2_cat1 ) )
area2_cat1_ids = [i for i in area2_cat1_feat]
for f in area2_cat1_ids:
area2_score += f.geometry().area()
# Meh and Standard
area2_cat2 = QgsExpression( """ ( "Category_A" = 'Meh') AND ( "Category_B" = 'Standard') """ )
area2_cat2_feat = map_layer.getFeatures( QgsFeatureRequest( area2_cat2 ) )
area2_cat2_ids = [i for i in area2_cat2_feat]
for f in area2_cat2_ids:
area2_score += f.geometry().area()
# Bad and Excellent
area2_cat3 = QgsExpression( """ ( "Category_A" = 'Bad') AND ( "Category_B" = 'Excellent') """ )
area2_cat3_feat = map_layer.getFeatures( QgsFeatureRequest( area2_cat3 ) )
area2_cat3_ids = [i for i in area2_cat3_feat]
for f in area2_cat3_ids:
area2_score += f.geometry().area()
##########################
######### AREA 3 #########
# Meh and Terrible
# Set area to 0
area3_score = 0
area3_cat1 = QgsExpression( """ ( "Category_A" = 'Meh') AND ( "Category_B" = 'Terrible') """ )
area3_cat1_feat = map_layer.getFeatures( QgsFeatureRequest( area3_cat1 ) )
area3_cat1_ids = [i for i in area3_cat1_feat]
for f in area3_cat1_ids:
area3_score += f.geometry().area()
# Bad and Standard
area3_cat2 = QgsExpression( """ ( "Category_A" = 'Bad') AND ( "Category_B" = 'Standard') """ )
area3_cat2_feat = map_layer.getFeatures( QgsFeatureRequest( area3_cat2 ) )
area3_cat2_ids = [i for i in area3_cat2_feat]
for f in area3_cat2_ids:
area3_score += f.geometry().area()
# Bad and Terrible
area3_cat3 = QgsExpression( """ ( "Category_A" = 'Bad') AND ( "Category_B" = 'Terrible') """ )
area3_cat3_feat = map_layer.getFeatures( QgsFeatureRequest( area3_cat3 ) )
area3_cat3_ids = [i for i in area3_cat3_feat]
for f in area3_cat3_ids:
area3_score += f.geometry().area()
##########################
######### AREA 4 #########
# Unavailable
# Set area to 0
area4_score = 0
area4_cat = QgsExpression( """ ( "Category_A" = 'Unavailable') """ )
area4_cat_feat = map_layer.getFeatures( QgsFeatureRequest( area4_cat ) )
area4_cat_ids = [i for i in area4_cat_feat]
for f in area4_cat_ids:
area4_score += f.geometry().area()
########################
self.dockwidget.area_Score1_lineEdit.setText("{:,.0f}".format(area1_score))
self.dockwidget.area_Score2_lineEdit.setText("{:,.0f}".format(area2_score))
self.dockwidget.area_Score3_lineEdit.setText("{:,.0f}".format(area3_score))
self.dockwidget.area_Score4_lineEdit.setText("{:,.0f}".format(area4_score))