Skip to content

Commit

Permalink
Processing - Always reproject the extent to 4326 when fetching parame…
Browse files Browse the repository at this point in the history
…ters
  • Loading branch information
Gustry committed Sep 1, 2021
1 parent 596dab7 commit 260885e
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 49 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ processing-doc:

code-doc:
cd .docker && ./code_doc.sh

lint:
flake8
pylint --rcfile=setup.cfg ./QuickOSM
26 changes: 6 additions & 20 deletions QuickOSM/quick_osm_processing/advanced/build_query.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
"""Processing algorithm for building a query."""

__copyright__ = 'Copyright 2021, 3Liz'
__license__ = 'GPL version 3'
__email__ = '[email protected]'

from typing import Dict

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from qgis.core import (
QgsCoordinateReferenceSystem,
QgsCoordinateTransform,
QgsProcessingAlgorithm,
QgsProcessingOutputString,
QgsProject,
)
from qgis.core import QgsProcessingAlgorithm, QgsProcessingOutputString

from QuickOSM.core.query_factory import QueryFactory
from QuickOSM.core.query_preparation import QueryPreparation
from QuickOSM.definitions.osm import QueryLanguage
from QuickOSM.qgis_plugin_tools.tools.i18n import tr

__copyright__ = 'Copyright 2021, 3Liz'
__license__ = 'GPL version 3'
__email__ = '[email protected]'

from QuickOSM.quick_osm_processing.build_input import (
BuildBasedAroundAreaQuery,
BuildBasedExtentQuery,
Expand Down Expand Up @@ -86,7 +79,7 @@ def build_query(self) -> Dict[str, str]:
query_preparation = QueryPreparation(
raw_query,
area=self.area,
extent=self.extent,
extent=self.extent, # It must be already in 4326 when fetching parameters
overpass=self.server
)
raw_query = query_preparation.prepare_query()
Expand Down Expand Up @@ -177,11 +170,4 @@ def processAlgorithm(self, parameters, context, feedback) -> Dict[str, str]:
self.feedback = feedback
self.fetch_based_parameters(parameters, context)

crs = self.parameterAsExtentCrs(parameters, self.EXTENT, context)

crs_4326 = QgsCoordinateReferenceSystem(4326)
transform = QgsCoordinateTransform(
crs, crs_4326, QgsProject.instance())
self.extent = transform.transform(self.extent)

return self.build_query()
24 changes: 6 additions & 18 deletions QuickOSM/quick_osm_processing/advanced/raw_query.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
"""Generate a raw query."""

__copyright__ = 'Copyright 2021, 3Liz'
__license__ = 'GPL version 3'
__email__ = '[email protected]'

from typing import Dict

from qgis.core import (
QgsCoordinateReferenceSystem,
QgsCoordinateTransform,
QgsProcessingAlgorithm,
QgsProcessingOutputString,
QgsProject,
)
from qgis.core import QgsProcessingAlgorithm, QgsProcessingOutputString

from QuickOSM.core.query_preparation import QueryPreparation
from QuickOSM.qgis_plugin_tools.tools.i18n import tr

__copyright__ = 'Copyright 2021, 3Liz'
__license__ = 'GPL version 3'
__email__ = '[email protected]'

from QuickOSM.quick_osm_processing.build_input import BuildRaw


Expand Down Expand Up @@ -70,16 +63,11 @@ def processAlgorithm(self, parameters, context, feedback) -> Dict[str, str]:
self.feedback = feedback
self.fetch_based_parameters(parameters, context)

crs_4326 = QgsCoordinateReferenceSystem(4326)
transform = QgsCoordinateTransform(
self.crs, crs_4326, QgsProject.instance())
self.extent = transform.transform(self.extent)

self.feedback.pushInfo('Prepare the url.')

query_preparation = QueryPreparation(
self.query,
extent=self.extent,
extent=self.extent, # It should be in 4326 already in this stage
area=self.area,
overpass=self.server
)
Expand Down
31 changes: 23 additions & 8 deletions QuickOSM/quick_osm_processing/build_input.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
"""Set up the parameters for the processing algorithms."""

__copyright__ = 'Copyright 2021, 3Liz'
__license__ = 'GPL version 3'
__email__ = '[email protected]'

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from qgis.core import (
QgsCoordinateReferenceSystem,
QgsCoordinateTransform,
QgsProcessingParameterDefinition,
QgsProcessingParameterExtent,
QgsProcessingParameterNumber,
QgsProcessingParameterString,
)

__copyright__ = 'Copyright 2021, 3Liz'
__license__ = 'GPL version 3'
__email__ = '[email protected]'

from QuickOSM.core.utilities.tools import get_setting
from QuickOSM.definitions.osm import QueryType
from QuickOSM.definitions.overpass import OVERPASS_SERVERS
Expand All @@ -30,6 +32,7 @@ def __init__(self):
self.feedback = None
self.area = None
self.extent = None
self.extent_crs = None
self.server = None
self.timeout = None

Expand Down Expand Up @@ -74,14 +77,20 @@ def __init__(self):
"""Constructor"""
super().__init__()
self.query = None
self.crs = None

def fetch_based_parameters(self, parameters, context):
"""Get the parameters."""
super().fetch_based_parameters(parameters, context)
self.query = self.parameterAsString(parameters, self.QUERY, context)
self.extent = self.parameterAsExtent(parameters, self.EXTENT, context)
self.crs = self.parameterAsExtentCrs(parameters, self.EXTENT, context)
self.extent_crs = self.parameterAsExtentCrs(parameters, self.EXTENT, context)

# Always transform to 4326
crs_4326 = QgsCoordinateReferenceSystem(4326)
transform = QgsCoordinateTransform(self.extent_crs, crs_4326, context.project())
self.extent = transform.transform(self.extent)
self.extent_crs = crs_4326

self.area = self.parameterAsString(parameters, self.AREA, context)

def add_top_parameters(self):
Expand Down Expand Up @@ -241,12 +250,18 @@ def fetch_based_parameters(self, parameters, context):
"""Get the parameters."""
super().fetch_based_parameters(parameters, context)
self.extent = self.parameterAsExtent(parameters, self.EXTENT, context)
self.extent_crs = self.parameterAsExtentCrs(parameters, self.EXTENT, context)

# Always transform to 4326
crs_4326 = QgsCoordinateReferenceSystem(4326)
transform = QgsCoordinateTransform(self.extent_crs, crs_4326, context.project())
self.extent = transform.transform(self.extent)
self.extent_crs = crs_4326

def add_top_parameters(self):
"""Set up the parameter."""
super().add_top_parameters()

param = QgsProcessingParameterExtent(self.EXTENT, tr('Extent'), optional=False)
help_string = tr('The extent as a rectangle to use when building the query.')
param.setHelp(help_string)
param.setHelp(tr('The extent as a rectangle to use when building the query.'))
self.addParameter(param)
5 changes: 3 additions & 2 deletions QuickOSM/quick_osm_processing/quickosm_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
QgsProcessingOutputVectorLayer,
QgsProcessingParameterFileDestination,
QgsProcessingUtils,
QgsReferencedRectangle,
)
from qgis.PyQt.QtGui import QIcon

Expand Down Expand Up @@ -248,7 +249,7 @@ def processAlgorithm(self, parameters, context, feedback):
"quickosm:buildrawquery",
{
'AREA': self.area,
'EXTENT': self.extent,
'EXTENT': QgsReferencedRectangle(self.extent, self.extent_crs),
'QUERY': self.query,
'SERVER': self.server
},
Expand Down Expand Up @@ -430,7 +431,7 @@ def processAlgorithm(self, parameters, context, feedback):
query = processing.run(
"quickosm:buildqueryextent",
{
'EXTENT': self.extent,
'EXTENT': QgsReferencedRectangle(self.extent, self.extent_crs),
'KEY': self.key,
'SERVER': self.server,
'TIMEOUT': self.timeout,
Expand Down
46 changes: 45 additions & 1 deletion QuickOSM/test/test_processing_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

import processing

from qgis.core import QgsApplication, QgsVectorLayer
from qgis.core import (
QgsApplication,
QgsCoordinateReferenceSystem,
QgsProcessingContext,
QgsProject,
QgsVectorLayer,
)
from qgis.testing import unittest

from QuickOSM.qgis_plugin_tools.tools.resources import plugin_test_data_path
Expand Down Expand Up @@ -193,6 +199,44 @@ def test_process_extent_query(self):
self.assertIsInstance(result['OUTPUT_MULTILINESTRINGS'], QgsVectorLayer)
self.assertIsInstance(result['OUTPUT_MULTIPOLYGONS'], QgsVectorLayer)

def test_reprojection_extent_query(self):
"""Test for the reprojection from the project CRS."""
# In processing.run, we can see some 2154 coordinates.
# In the query, coordinates in 4326
handler = SequentialHandler()
handler.add(
'GET',
'/interpreter?data=[out:xml]%20[timeout:25];%0A(%0A%20%20%20%20node[%22landuse%22]'
'(%2045.0843,-0.3832,45.09649,-0.36385);%0A%20%20%20%20way[%22landuse%22]'
'(%2045.0843,-0.3832,45.09649,-0.36385);%0A%20%20%20%20relation[%22landuse%22]'
'(%2045.0843,-0.3832,45.09649,-0.36385);%0A);%0A(._;%3E;);%0Aout%20body;&info=QgisQuickOSMPlugin',
200,
{'Content-type': 'text/xml'},
open(plugin_test_data_path('overpass', 'empty_osm_file.xml'), 'r', encoding='utf8').read(),
)
project = QgsProject()
project.setCrs(QgsCoordinateReferenceSystem(2154))
context = QgsProcessingContext()
context.setProject(project)

with install_http_handler(handler):
result = processing.run(
'quickosm:downloadosmdataextentquery',
{
'KEY': 'landuse',
'VALUE': '',
'EXTENT': '433888.4776000,435466.5863000,6448484.3786000,6449771.2615000 [EPSG:2154]',
'TIMEOUT': 25,
'SERVER': 'http://localhost:{}/interpreter'.format(self.port),
},
context=context
)

self.assertIsInstance(result['OUTPUT_POINTS'], QgsVectorLayer)
self.assertIsInstance(result['OUTPUT_LINES'], QgsVectorLayer)
self.assertIsInstance(result['OUTPUT_MULTILINESTRINGS'], QgsVectorLayer)
self.assertIsInstance(result['OUTPUT_MULTIPOLYGONS'], QgsVectorLayer)


if __name__ == '__main__':
unittest.main()

0 comments on commit 260885e

Please sign in to comment.