Skip to content

Commit

Permalink
Implement basic elements_aggregation processing algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
merydian committed Aug 18, 2023
1 parent 01e0dc4 commit 50d2af0
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 7 deletions.
13 changes: 10 additions & 3 deletions ohsomeTools/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,22 @@
SUPPORTED_API_VERSIONS = ["1.4.1"]
EXTRACTION_SPECS = {
"contributions": [
"",
"bbox",
"centroid",
"geometry",
"latest/bbox",
"latest/centroid",
"latest/geometry",
],
"elements": ["bbox", "centroid", "geometry"],
"elementsFullHistory": ["bbox", "centroid", "geometry"],
"elements": ["", "bbox", "centroid", "geometry"],
"elementsFullHistory": ["", "bbox", "centroid", "geometry"],
}
AGGREGATION_SPECS = {
"contributions/count": ["density"],
"elements/": [""],
"contributions/count": ["", "density"],
"elements/area": [
"",
"density",
"density/groupBy/boundary",
"density/groupBy/boundary/groupBy/tag",
Expand All @@ -60,6 +63,7 @@
"ratio/groupBy/boundary",
],
"elements/count": [
"",
"density",
"density/groupBy/boundary",
"density/groupBy/boundary/groupBy/tag",
Expand All @@ -74,6 +78,7 @@
"ratio/groupBy/boundary",
],
"elements/length": [
"",
"density",
"density/groupBy/boundary",
"density/groupBy/boundary/groupBy/tag",
Expand All @@ -88,6 +93,7 @@
"ratio/groupBy/boundary",
],
"elements/perimeter": [
"",
"density",
"density/groupBy/boundary",
"density/groupBy/boundary/groupBy/tag",
Expand All @@ -102,6 +108,7 @@
"ratio/groupBy/boundary",
],
"users/count": [
"",
"density",
"density/groupBy/boundary",
"density/groupBy/tag",
Expand Down
2 changes: 0 additions & 2 deletions ohsomeTools/common/request_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ def postprocess_results(self) -> bool:
if not self.result or not len(self.result):
return False
if "extractRegion" in self.result:

vlayer: QgsVectorLayer = QgsVectorLayer(
json.dumps(
self.result.get("extractRegion").get("spatialExtent")
Expand Down Expand Up @@ -449,7 +448,6 @@ def finished(self, valid_result):
short_msg = msg = (
f"The request was successful:" + default_message
)

self.postprocess_results()
logger.log(msg, Qgis.Info)
self.iface.messageBar().pushMessage(
Expand Down
252 changes: 252 additions & 0 deletions ohsomeTools/proc/elements_aggregation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""

from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (
QgsProcessingParameterNumber,
QgsProcessingAlgorithm,
QgsProcessingParameterEnum,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterString,
QgsProcessingParameterBoolean,
QgsProcessingParameterDateTime,
QgsWkbTypes,
)

from ohsomeTools.common import AGGREGATION_SPECS
from .procDialog import run_processing_alg
from qgis.utils import iface


class ElementsAggregation(QgsProcessingAlgorithm):
"""
This is an example algorithm that takes a vector layer and
creates a new identical one.
It is meant to be used as an example of how to create your own
algorithms and explain methods and variables used to do it. An
algorithm like this will be available in all elements, and there
is not need for additional work.
All Processing algorithms should extend the QgsProcessingAlgorithm
class.
"""

# Constants used to refer to parameters and outputs. They will be
# used when calling the algorithm from another algorithm, or when
# calling from the QGIS console.

LAYER = "LAYER"
PARAMETER = "PARAMETER"
INPUT = "INPUT"
FILTER = "FILTER"
check_activate_temporal = "check_activate_temporal"
check_show_metadata = "check_show_metadata"
timeout_input = "timeout_input"
check_clip_geometry = "check_clip_geometry"
property_groups_check_tags = "property_groups_check_tags"
property_groups_check_metadata = "property_groups_check_metadata"
data_aggregation_format = "data_aggregation_format"
date_start = "date_start"
date_end = "date_end"
YEARS = "YEARS"
MONTHS = "MONTHS"
DAYS = "DAYS"
RADIUS = "RADIUS"
check_keep_geometryless = "check_keep_geometryless"
check_merge_geometries = "check_merge_geometries"
group_by_values_line_edit = "group_by_values_line_edit"
group_by_key_line_edit = "group_by_key_line_edit"
formats = ["json", "geojson"]
parameters = [i.split('/')[1] for i in AGGREGATION_SPECS.keys() if 'elements' in i]
group_by = []
DENSITY = 'DENSITY'

def tr(self, string):
"""
Returns a translatable string with the self.tr() function.
"""
return QCoreApplication.translate("Processing", string)

def createInstance(self):
return ElementsAggregation()

def name(self):
"""
Returns the algorithm name, used for identifying the algorithm. This
string should be fixed for the algorithm, and must not be localised.
The name should be unique within each provider. Names should contain
lowercase alphanumeric characters only and no spaces or other
formatting characters.
"""
return "elementsaggregation"

def displayName(self):
"""
Returns the translated algorithm name, which should be used for any
user-visible display of the algorithm name.
"""
return self.tr("Elements Aggregation")

def group(self):
"""
Returns the name of the group this algorithm belongs to. This string
should be localised.
"""
return self.tr("Data Aggregation")

def groupId(self):
"""
Returns the unique ID of the group this algorithm belongs to. This
string should be fixed for the algorithm, and must not be localised.
The group id should be unique within each provider. Group id should
contain lowercase alphanumeric characters only and no spaces or other
formatting characters.
"""
return "dataaggregation"

def shortHelpString(self):
"""
Returns a localised short helper string for the algorithm. This string
should provide a basic description about what the algorithm does and the
parameters and outputs associated with it.
"""
return self.tr("Example algorithm short description")

def initAlgorithm(self, config=None):
"""
Here we define the inputs and output of the algorithm, along
with some other properties.
"""

# We add the input vector features source. It can have any kind of
# geometry.
self.addParameter(
QgsProcessingParameterVectorLayer(
self.LAYER, self.tr("Query Layer")
)
)

self.addParameter(
QgsProcessingParameterEnum(
self.PARAMETER,
self.tr("Aggregation Type"),
options=self.parameters,
defaultValue=0,
)
)

self.addParameter(
QgsProcessingParameterBoolean(
self.DENSITY,
self.tr("Density"),
defaultValue=False,
)
)

self.addParameter(
QgsProcessingParameterDateTime(
self.date_start, "Start Date", defaultValue="2007-10-08"
)
)

self.addParameter(
QgsProcessingParameterDateTime(
self.date_end, "End Date", defaultValue="2023-07-28"
)
)

self.addParameter(
QgsProcessingParameterString(
self.FILTER,
self.tr("Filter"),
defaultValue="building=* or (type:way and highway=residential)",
)
)

self.addParameter(
QgsProcessingParameterNumber(
self.RADIUS,
"Radius [m]",
type=QgsProcessingParameterNumber.Integer,
defaultValue=100,
)
)

'''self.addParameter(
QgsProcessingParameterEnum(
self.PARAMETER,
self.tr("Group By"),
options=self.group_by,
defaultValue=0,
optional=True
)
)'''

def processAlgorithm(self, parameters, context, feedback):
"""
Here is where the processing itself takes place.
"""
layer = self.parameterAsLayer(parameters, self.LAYER, context)

if layer.geometryType() == QgsWkbTypes.PointGeometry:
geom = 1
elif layer.geometryType() == QgsWkbTypes.PolygonGeometry:
geom = 2
else:
# implement user information
pass

if self.parameterAsBool(parameters, self.DENSITY, context):
density = '/density'
else:
density = ''

processingParams = {
"geom": geom,
"selection": "data-Aggregation",
"preference": f"elements/{self.parameters[self.parameterAsInt(parameters, self.PARAMETER, context)]}{density}",
"filter": self.parameterAsString(parameters, self.FILTER, context),
"LAYER": self.parameterAsLayer(parameters, self.LAYER, context),
"RADIUS": self.parameterAsInt(parameters, self.RADIUS, context),
"date_start": self.parameterAsDateTime(
parameters, self.date_start, context
),
"date_end": self.parameterAsDateTime(
parameters, self.date_end, context
),
'preference_specification': '',
'YEARS': 0,
'MONTHS': 0,
'DAYS': 1,
'data_aggregation_format': 'json',
'check_show_metadata': '',
'timeout_input': 0,

}

run_processing_alg(processingParams, feedback)

# To run another Processing algorithm as part of this algorithm, you can use
# processing.run(...). Make sure you pass the current context and feedback
# to processing.run to ensure that all temporary layer outputs are available
# to the executed algorithm, and that the executed algorithm can send feedback
# reports to the user (and correctly handle cancellation and progress reports!)

# Return the results of the algorithm. In this case our only result is
# the feature sink which contains the processed features, but some
# algorithms may return multiple feature sinks, calculated numeric
# statistics, etc. These should all be included in the returned
# dictionary, with keys matching the feature corresponding parameter
# or output names.
return {}
9 changes: 7 additions & 2 deletions ohsomeTools/proc/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
from ohsomeTools.proc.data_extraction.elementsFullHistory import (
ElementsFullHistory,
)
from ohsomeTools.proc import elements_aggregation

from ohsomeTools.proc.metadata.metadata import Metadata

from ohsomeTools import RESOURCE_PREFIX, PLUGIN_NAME, __version__
Expand All @@ -61,7 +63,7 @@ def loadAlgorithms(self):
"""
Loads all algorithms belonging to this provider.
"""
# data-aggregation
'''# data-aggregation
self.addAlgorithm(ContributionsCount())
self.addAlgorithm(ElementsArea())
self.addAlgorithm(ElementsCount())
Expand All @@ -75,7 +77,10 @@ def loadAlgorithms(self):
self.addAlgorithm(ElementsFullHistory())
# metadata
self.addAlgorithm(Metadata())
self.addAlgorithm(Metadata())'''
self.addAlgorithm(elements_aggregation.ElementsAggregation())



def icon(self):
return QIcon(RESOURCE_PREFIX + "icon_ohsome.png")
Expand Down

0 comments on commit 50d2af0

Please sign in to comment.