Ejemplo n.º 1
0
def process_query(dialog=None,
                  query=None,
                  nominatim=None,
                  bbox=None,
                  output_dir=None,
                  prefix_file=None,
                  output_geometry_types=None,
                  layer_name="OsmQuery",
                  white_list_values=None,
                  config_outputs=None):
    """execute a query and send the result file to open_file."""

    # Prepare outputs
    dialog.set_progress_text(tr('Prepare outputs'))

    # Replace Nominatim or BBOX
    query = QueryPreparation(query, bbox, nominatim)

    # Getting the default overpass api and running the query
    server = get_setting('defaultOAPI')
    dialog.set_progress_text(tr('Downloading data from Overpass'))
    QApplication.processEvents()
    connexion_overpass_api = ConnexionOAPI(url=server, output="xml")
    osm_file = connexion_overpass_api.query(query.prepare_query())

    return open_file(dialog=dialog,
                     osm_file=osm_file,
                     output_geom_types=output_geometry_types,
                     white_list_column=white_list_values,
                     layer_name=layer_name,
                     output_dir=output_dir,
                     prefix_file=prefix_file,
                     config_outputs=config_outputs)
Ejemplo n.º 2
0
 def get_timestamp_overpass_api(self):
     """
     Get the timestamp of the current server
     """
     text = self.pushButton_OAPI_timestamp.text()
     self.pushButton_OAPI_timestamp.setText(
         tr('QuickOSM', 'Fetching the timestamp ...'))
     overpass_api = ConnexionOAPI(url=self.defaultServer)
     self.label_timestamp_oapi.setText(overpass_api.get_timestamp())
     self.pushButton_OAPI_timestamp.setText(text)
Ejemplo n.º 3
0
def process_query(
        dialog=None,
        query=None,
        nominatim=None,
        bbox=None,
        output_dir=None,
        prefix_file=None,
        output_geometry_types=None,
        layer_name="OsmQuery",
        white_list_values=None,
        config_outputs=None):
    """execute a query and send the result file to open_file."""

    # Check OGR
    if not is_ogr_version_ok():
        raise GDALVersion

    if not is_osm_driver_enabled():
        raise OsmDriverNotFound

    # Get output's format
    output_format = get_setting('outputFormat')

    # Prepare outputs
    dialog.set_progress_text(tr("QuickOSM", u"Prepare outputs"))

    # Replace Nominatim or BBOX
    query = prepare_query(query=query, nominatim_name=nominatim, extent=bbox)

    # Getting the default overpass api and running the query
    server = get_setting('defaultOAPI')
    dialog.set_progress_text(tr("QuickOSM", u"Downloading data from Overpass"))
    QApplication.processEvents()
    connexion_overpass_api = ConnexionOAPI(url=server, output="xml")
    osm_file = connexion_overpass_api.get_file_from_query(query)

    return open_file(
        dialog=dialog,
        osm_file=osm_file,
        output_geom_types=output_geometry_types,
        white_list_column=white_list_values,
        layer_name=layer_name,
        output_format=output_format,
        output_dir=output_dir,
        prefix_file=prefix_file,
        config_outputs=config_outputs)
Ejemplo n.º 4
0
def process_query(
        dialog=None,
        query=None,
        nominatim=None,
        bbox=None,
        output_dir=None,
        prefix_file=None,
        output_geometry_types=None,
        layer_name="OsmQuery",
        white_list_values=None,
        config_outputs=None):
    """execute a query and send the result file to open_file."""

    # Check OGR
    if not is_ogr_version_ok():
        raise GDALVersion

    if not is_osm_driver_enabled():
        raise OsmDriverNotFound

    # Get output's format
    output_format = get_setting('outputFormat')

    # Prepare outputs
    dialog.set_progress_text(tr("QuickOSM", u"Prepare outputs"))

    # Replace Nominatim or BBOX
    query = prepare_query(query=query, nominatim_name=nominatim, extent=bbox)

    # Getting the default overpass api and running the query
    server = get_setting('defaultOAPI')
    dialog.set_progress_text(tr("QuickOSM", u"Downloading data from Overpass"))
    QApplication.processEvents()
    connexion_overpass_api = ConnexionOAPI(url=server, output="xml")
    osm_file = connexion_overpass_api.get_file_from_query(query)

    return open_file(
        dialog=dialog,
        osm_file=osm_file,
        output_geom_types=output_geometry_types,
        white_list_column=white_list_values,
        layer_name=layer_name,
        output_format=output_format,
        output_dir=output_dir,
        prefix_file=prefix_file,
        config_outputs=config_outputs)
Ejemplo n.º 5
0
    def generate_query(self, oql_output: bool = True):
        """Generate the query as final."""
        query = self.dialog.text_query.toPlainText()
        area = self.dialog.places_edits[Panels.Query].text()
        self.write_nominatim_file(Panels.Query)
        properties = self.gather_values()
        server = get_setting('defaultOAPI', OVERPASS_SERVERS[0]) + 'convert'
        query = QueryPreparation(query, properties['bbox'], area, server)
        query_string = query.prepare_query()
        if (oql_output
                and not query.is_oql_query()) or (not oql_output
                                                  and query.is_oql_query()):
            query.prepare_query()
            url = query.prepare_url(
                QueryLanguage.OQL if oql_output else QueryLanguage.XML)
            connexion_overpass_api = ConnexionOAPI(url)
            LOGGER.debug('Encoded URL: {}'.format(url))
            query_string = connexion_overpass_api.run_convert()
            query_string = html.unescape(query_string)

        self.dialog.text_query.setPlainText(query_string)
Ejemplo n.º 6
0
def process_query(
        dialog=None,
        query=None,
        area=None,
        bbox=None,
        output_dir=None,
        prefix_file=None,
        output_geometry_types=None,
        layer_name="OsmQuery",
        white_list_values=None,
        config_outputs=None):
    """execute a query and send the result file to open_file."""

    # Prepare outputs
    dialog.set_progress_text(tr('Prepare outputs'))

    # Getting the default overpass api and running the query
    server = get_setting('defaultOAPI', OVERPASS_SERVERS[0]) + 'interpreter'
    dialog.set_progress_text(
        tr('Downloading data from Overpass {server_name}'.format(
            server_name=server)))
    # Replace Nominatim or BBOX
    query = QueryPreparation(query, bbox, area, server)
    QApplication.processEvents()
    final_query = query.prepare_query()
    url = query.prepare_url()
    connexion_overpass_api = ConnexionOAPI(url)
    LOGGER.debug('Encoded URL: {}'.format(url))
    osm_file = connexion_overpass_api.run()

    return open_file(
        dialog=dialog,
        osm_file=osm_file,
        output_geom_types=output_geometry_types,
        white_list_column=white_list_values,
        layer_name=layer_name,
        output_dir=output_dir,
        prefix_file=prefix_file,
        final_query=final_query,
        config_outputs=config_outputs)
Ejemplo n.º 7
0
def process_query(
        dialog=None,
        query=None,
        nominatim=None,
        bbox=None,
        output_dir=None,
        prefix_file=None,
        output_geometry_types=None,
        layer_name="OsmQuery",
        white_list_values=None,
        config_outputs=None):
    """execute a query and send the result file to open_file."""

    # Prepare outputs
    dialog.set_progress_text(tr('Prepare outputs'))

    # Getting the default overpass api and running the query
    server = get_setting('defaultOAPI', OVERPASS_SERVERS[0]) + 'interpreter'
    dialog.set_progress_text(
        tr('Downloading data from Overpass {}').format(server))
    # Replace Nominatim or BBOX
    query = QueryPreparation(query, bbox, nominatim, server)
    QApplication.processEvents()
    final_query = query.prepare_query()
    url = query.prepare_url()
    connexion_overpass_api = ConnexionOAPI(url)
    LOGGER.debug('Encoded URL: {}'.format(url))
    osm_file = connexion_overpass_api.run()

    return open_file(
        dialog=dialog,
        osm_file=osm_file,
        output_geom_types=output_geometry_types,
        white_list_column=white_list_values,
        layer_name=layer_name,
        output_dir=output_dir,
        prefix_file=prefix_file,
        final_query=final_query,
        config_outputs=config_outputs)
Ejemplo n.º 8
0
    def processAlgorithm(self, progress):
        progress.setInfo("Preparing the Overpass query")
        progress.setPercentage(0)

        server = self.getParameterValue(self.SERVER)
        query = self.getParameterValue(self.QUERY_STRING)
        nominatim = self.getParameterValue(self.NOMINATIM)

        # Extent of the layer
        extent = self.getParameterValue(self.EXTENT)
        if extent != "0,0,0,0":
            # x_min, x_max, y_min, y_max
            extent = [float(i) for i in extent.split(',')]
            # noinspection PyCallByClass
            geometry_extent = QgsGeometry.fromRect(
                QgsRectangle(extent[0], extent[2], extent[1], extent[3]))
            source_crs = iface.mapCanvas().mapRenderer().destinationCrs()
            crs_transform = QgsCoordinateTransform(
                source_crs, QgsCoordinateReferenceSystem("EPSG:4326"))
            geometry_extent.transform(crs_transform)
            extent = geometry_extent.boundingBox()
        else:
            extent = None

        if nominatim == "":
            nominatim = None

        # Make some transformation on the query ({{box}}, Nominatim, ...
        query = QueryPreparation(query, extent, nominatim)

        overpass_api = ConnexionOAPI(url=server, output="xml")
        progress.setInfo("Downloading data from Overpass")
        progress.setPercentage(5)
        osm_file = overpass_api.get_file_from_query(query.prepare_query())

        # Set the output file for Processing
        progress.setPercentage(100)
        self.setOutputValue(self.OUTPUT_FILE, osm_file)
Ejemplo n.º 9
0
    def processAlgorithm(self, progress):
        progress.setInfo("Preparing the Overpass query")
        progress.setPercentage(0)

        server = self.getParameterValue(self.SERVER)
        query = self.getParameterValue(self.QUERY_STRING)
        nominatim = self.getParameterValue(self.NOMINATIM)

        # Extent of the layer
        extent = self.getParameterValue(self.EXTENT)
        if extent != "0,0,0,0":
            # x_min, x_max, y_min, y_max
            extent = [float(i) for i in extent.split(',')]
            # noinspection PyCallByClass
            geometry_extent = QgsGeometry.fromRect(
                QgsRectangle(extent[0], extent[2], extent[1], extent[3]))
            source_crs = iface.mapCanvas().mapRenderer().destinationCrs()
            crs_transform = QgsCoordinateTransform(
                source_crs, QgsCoordinateReferenceSystem("EPSG:4326"))
            geometry_extent.transform(crs_transform)
            extent = geometry_extent.boundingBox()
        else:
            extent = None

        if nominatim == "":
            nominatim = None

        # Make some transformation on the query ({{box}}, Nominatim, ...
        query = prepare_query(query, extent, nominatim)

        overpass_api = ConnexionOAPI(url=server, output="xml")
        progress.setInfo("Downloading data from Overpass")
        progress.setPercentage(5)
        osm_file = overpass_api.get_file_from_query(query)

        # Set the output file for Processing
        progress.setPercentage(100)
        self.setOutputValue(self.OUTPUT_FILE, osm_file)
Ejemplo n.º 10
0
def reload_query(query: str,
                 layer_name: str = 'Reloaded_query',
                 dialog: QDialog = None,
                 new_file: bool = True):
    """ Reload a query with only the query """
    # Getting the default overpass api and running the query
    server = get_setting('defaultOAPI', OVERPASS_SERVERS[0]) + 'interpreter'

    query = QueryPreparation(query, overpass=server)
    final_query = query.prepare_query()
    url = query.prepare_url()
    connexion_overpass_api = ConnexionOAPI(url)
    LOGGER.debug('Encoded URL: {}'.format(url))
    osm_file = connexion_overpass_api.run()

    if new_file:
        layer_name += "_reloaded"
        return open_file(
            dialog=dialog,
            osm_file=osm_file,
            layer_name=layer_name,
            final_query=final_query,
        )
Ejemplo n.º 11
0
    def process_road(self, context, url):
        """Major step of the process"""

        if self.feedback.isCanceled():
            self.feedback.reportError(
                'The algorithm has been canceled during the building of the url.'
            )
            return self.OUTPUT_CANCEL

        self.feedback.setCurrentStep(1)
        self.feedback.pushInfo('Downloading data and OSM file.')

        connexion_overpass_api = ConnexionOAPI(url)
        osm_file = connexion_overpass_api.run()

        if self.feedback.isCanceled():
            self.feedback.reportError(
                'The algorithm has been canceled during the download.')
            return self.OUTPUT_CANCEL

        self.feedback.setCurrentStep(2)
        self.feedback.pushInfo('Processing downloaded file.')

        out_dir = dirname(self.file) if self.file else None
        out_file = basename(self.file)[:-5] if self.file else None

        osm_parser = OsmParser(osm_file=osm_file,
                               output_format=Format.GeoPackage,
                               output_dir=out_dir,
                               prefix_file=out_file,
                               feedback_alg=True,
                               feedback=self.feedback)

        layers = osm_parser.processing_parse()

        if self.feedback.isCanceled():
            self.feedback.reportError(
                'The algorithm has been canceled during the parsing.')
            return self.OUTPUT_CANCEL

        self.feedback.setCurrentStep(7)
        self.feedback.pushInfo('Decorating the requested layers.')

        layer_output = []
        OUTPUT = {
            'points': self.OUTPUT_POINTS,
            'lines': self.OUTPUT_LINES,
            'multilinestrings': self.OUTPUT_MULTILINESTRINGS,
            'multipolygons': self.OUTPUT_MULTIPOLYGONS
        }

        for layer in layers:
            layers[layer]['layer_decorated'] = processing.run(
                "quickosm:decoratelayer",
                {'LAYER': layers[layer]['vector_layer']},
                feedback=self.feedback)
            context.temporaryLayerStore().addMapLayer(
                layers[layer]['vector_layer'])
            layer_output.append(
                QgsProcessingUtils.mapLayerFromString(
                    layers[layer]['vector_layer'].id(), context, True))
            if 'tags' in layers[layer]:
                layer_details = QgsProcessingContext.LayerDetails(
                    'OSMQuery_' + layer, context.project(), OUTPUT[layer])
                if 'colour' in layers[layer]['tags']:
                    layer_details.setPostProcessor(
                        SetColoringPostProcessor.create(layers[layer]['tags']))
                context.addLayerToLoadOnCompletion(
                    layers[layer]['vector_layer'].id(), layer_details)

        if self.feedback.isCanceled():
            self.feedback.reportError(
                'The algorithm has been canceled during the post processing.')
            return self.OUTPUT_CANCEL

        outputs = {
            self.OUTPUT_POINTS: layer_output[0].id(),
            self.OUTPUT_LINES: layer_output[1].id(),
            self.OUTPUT_MULTILINESTRINGS: layer_output[2].id(),
            self.OUTPUT_MULTIPOLYGONS: layer_output[3].id(),
        }
        return outputs
Ejemplo n.º 12
0
def process_query(dialog: QDialog = None,
                  query: str = None,
                  description: str = None,
                  area: Union[str, List[str]] = None,
                  key: Union[str, List[str]] = None,
                  value: Union[str, List[str]] = None,
                  type_multi_request: list = None,
                  bbox: QgsRectangle = None,
                  output_dir: str = None,
                  output_format: Format = None,
                  prefix_file: str = None,
                  output_geometry_types: list = None,
                  layer_name: str = "OsmQuery",
                  white_list_values: dict = None,
                  config_outputs: dict = None) -> int:
    """execute a query and send the result file to open_file."""
    # Save the query in the historic
    q_manage = QueryManagement(query=query,
                               name=prefix_file if prefix_file else layer_name,
                               description=description,
                               advanced=value is None,
                               type_multi_request=type_multi_request,
                               keys=key,
                               values=value,
                               area=area,
                               bbox=bbox,
                               output_geometry_types=output_geometry_types,
                               white_list_column=white_list_values)
    q_manage.write_query_historic()

    if dialog.feedback_process.isCanceled():
        return None

    # Prepare outputs
    dialog.set_progress_text(tr('Prepare outputs'))

    # Getting the default overpass api and running the query
    server = get_setting('defaultOAPI', OVERPASS_SERVERS[0]) + 'interpreter'
    dialog.set_progress_text(
        tr('Downloading data from Overpass {server_name}'.format(
            server_name=server)))
    # Replace Nominatim or BBOX
    query = QueryPreparation(query, bbox, area, server)
    QApplication.processEvents()
    final_query = query.prepare_query()
    url = query.prepare_url()
    connexion_overpass_api = ConnexionOAPI(url)
    LOGGER.debug('Encoded URL: {}'.format(url))
    osm_file = connexion_overpass_api.run()

    return open_file(dialog=dialog,
                     osm_file=osm_file,
                     output_geom_types=output_geometry_types,
                     white_list_column=white_list_values,
                     key=key,
                     layer_name=layer_name,
                     output_dir=output_dir,
                     output_format=output_format,
                     prefix_file=prefix_file,
                     final_query=final_query,
                     config_outputs=config_outputs,
                     feedback=dialog.feedback_process)