def process_quick_query(dialog: QDialog = None, description: str = None, type_multi_request: list = None, query_type: QueryType = None, key: Union[str, List[str]] = None, value: Union[str, List[str]] = None, bbox: QgsRectangle = None, area: str = None, distance: int = None, osm_objects: List[OsmType] = None, metadata: str = 'body', timeout: int = 25, output_directory: str = None, output_format: Format = None, prefix_file: str = None, layer_name: str = None, white_list_values: dict = None, output_geometry_types: list = None, config_outputs: dict = None) -> int: """ Generate a query and send it to process_query. """ if dialog.feedback_process.isCanceled(): return None # Building the query query_factory = QueryFactory(type_multi_request=type_multi_request, query_type=query_type, key=key, value=value, area=area, around_distance=distance, osm_objects=osm_objects, timeout=timeout, print_mode=metadata) query = query_factory.make(QueryLanguage.OQL) if description is None: description = query_factory.friendly_message() LOGGER.info(description) LOGGER.info('Query: {}'.format(layer_name)) # Call process_query with the new query return process_query( dialog=dialog, query=query, description=description, key=key, value=value, type_multi_request=type_multi_request, area=area, bbox=bbox, output_dir=output_directory, output_format=output_format, prefix_file=prefix_file, output_geometry_types=output_geometry_types, layer_name=layer_name, white_list_values=white_list_values, config_outputs=config_outputs, )
def build_query(self) -> Dict[str, str]: """Build the query requested.""" query_factory = QueryFactory( query_type=self.QUERY_TYPE, key=self.key, value=self.value, area=self.area, around_distance=self.distance, timeout=self.timeout) raw_query = query_factory.make(QueryLanguage.OQL) self.feedback.pushInfo(query_factory.friendly_message()) query_preparation = QueryPreparation( raw_query, area=self.area, extent=self.extent, overpass=self.server ) raw_query = query_preparation.prepare_query() url = query_preparation.prepare_url() outputs = { self.OUTPUT_URL: url, self.OUTPUT_OQL_QUERY: raw_query, } return outputs
def update_friendly(self): """Updates the QuickQuery friendly label (label_qq_friendly).""" try: p = self.gather_values() except QuickOsmException as e: self.dialog.display_quickosm_exception(e) return except Exception as e: self.dialog.display_critical_exception(e) return # Make the query, in order to create the friendly message query_factory = QueryFactory(query_type=p['query_type'], key=p['key'], value=p['value'], area=p['place'], around_distance=p['distance'], osm_objects=p['osm_objects'], timeout=p['timeout']) try: msg = query_factory.friendly_message() except QuickOsmException as e: self.dialog.label_qq_friendly.setStyleSheet( 'QLabel { color : red; }') self.dialog.label_qq_friendly.setText(str(e)) except Exception as e: self.dialog.display_critical_exception(e) self.dialog.label_qq_friendly.setText('') else: self.dialog.label_qq_friendly.setStyleSheet('') self.dialog.label_qq_friendly.setText(msg)
def processAlgorithm(self, progress): key = self.getParameterValue(self.FIELD_KEY) value = self.getParameterValue(self.FIELD_VALUE) extent = self.getParameterValue(self.FIELD_EXTENT) nominatim = self.getParameterValue(self.FIELD_NOMINATIM) if nominatim == '': nominatim = None if value == '': value = None timeout = self.getParameterValue(self.FIELD_TIMEOUT) # Missing OSMObjects query_factory = QueryFactory( key=key, value=value, nominatim=nominatim, bbox=extent, timeout=timeout) query = query_factory.make() self.setOutputValue(self.OUTPUT_QUERY, query)
def process_quick_query(dialog=None, key=None, value=None, bbox=None, area=None, is_around=None, distance=None, osm_objects=None, timeout=25, output_directory=None, prefix_file=None, output_geometry_types=None): """ Generate a query and send it to process_query. """ # TODO # Move this logic UP # Copy/paste in quick_query_dialog.py distance_string = None if is_around and area: query_type = QueryType.AroundArea distance_string = '{}'.format(distance) elif not is_around and area: query_type = QueryType.InArea distance = None elif bbox: query_type = QueryType.BBox else: query_type = QueryType.NotSpatial # End todo LOGGER.info('QueryFactory: the key is "{}" and the value is "{}"'.format( key, value)) # Building the query query_factory = QueryFactory(query_type=query_type, key=key, value=value, area=area, around_distance=distance, osm_objects=osm_objects, timeout=timeout) query = query_factory.make() # Generate layer name as following (if defined) if not key: key = tr('allKeys') expected_name = [key, value, area, distance_string] layer_name = '_'.join([f for f in expected_name if f]) LOGGER.info('Query: {}'.format(layer_name)) # Call process_query with the new query return process_query(dialog=dialog, query=query, area=area, bbox=bbox, output_dir=output_directory, prefix_file=prefix_file, output_geometry_types=output_geometry_types, layer_name=layer_name)
def test_replace_template(self): """Test replace template.""" query = ' area="paris"' expected = ' {{geocodeArea:paris}}' self.assertEqual(QueryFactory.replace_template(query), expected) query = ' bbox="custom"' expected = ' {{bbox}}' self.assertEqual(QueryFactory.replace_template(query), expected)
def process_quick_query(dialog=None, key=None, value=None, bbox=None, nominatim=None, is_around=None, distance=None, osm_objects=None, timeout=25, output_directory=None, prefix_file=None, output_geometry_types=None): """ Generate a query and send it to process_query. """ # TODO # Move this logic UP # Copy/paste in quick_query_dialog.py distance_string = None if is_around and nominatim: query_type = QueryType.AroundNominatimPlace distance_string = str(distance) elif not is_around and nominatim: query_type = QueryType.InNominatimPlace elif bbox: query_type = QueryType.BBox else: query_type = QueryType.NotSpatial # End todo # Building the query query_factory = QueryFactory(query_type=query_type, key=key, value=value, nominatim_place=nominatim, around_distance=distance, osm_objects=osm_objects, timeout=timeout) query = query_factory.make() # Generate layer name as following (if defined) if not key: key = tr('allKeys') expected_name = [key, value, nominatim, distance_string] layer_name = '_'.join([f for f in expected_name if f]) # Call process_query with the new query return process_query(dialog=dialog, query=query, nominatim=nominatim, bbox=bbox, output_dir=output_directory, prefix_file=prefix_file, output_geometry_types=output_geometry_types, layer_name=layer_name)
def test_check_parameters(self): """Test check parameters query.""" # Nominatim and BBOX. query = QueryFactory(nominatim="foo", bbox=True) self.assertRaises(QueryFactoryException, query.check_parameters) # Missing key. query = QueryFactory(nominatim="foo") self.assertRaises(QueryFactoryException, query.check_parameters) # Missing osm object query = QueryFactory(key="foo", osm_objects=[]) self.assertRaises(QueryFactoryException, query.check_parameters) # Wrong osm object. query = QueryFactory(key="foo", osm_objects=["bar"]) self.assertRaises(QueryFactoryException, query.check_parameters) # Missing distance if "around". query = QueryFactory(key="foo", osm_objects=["node"], is_around=True) self.assertRaises(QueryFactoryException, query.check_parameters) # Missing nominatim if "around". query = QueryFactory(key="foo", osm_objects=["node"], is_around=True, distance=10) self.assertRaises(QueryFactoryException, query.check_parameters) # Good query. query = QueryFactory("foo", "bar") try: query.check_parameters() except QueryFactoryException, e: self.fail(e.msg)
def process_quick_query( dialog=None, key=None, value=None, bbox=None, nominatim=None, is_around=None, distance=None, osm_objects=None, timeout=25, output_directory=None, prefix_file=None, output_geometry_types=None, ): """ generate a query and send it to process_query """ # Set the layer name layer_name = "" for i in [key, value, nominatim]: if i: layer_name += i + "_" if is_around: layer_name += str(distance) + "_" # Delete last "_" layer_name = layer_name[:-1] # Building the query query_factory = QueryFactory( timeout=timeout, key=key, value=value, bbox=bbox, is_around=is_around, distance=distance, nominatim=nominatim, osm_objects=osm_objects, ) query = query_factory.make() # Call process_query with the new query return process_query( dialog=dialog, query=query, nominatim=nominatim, bbox=bbox, output_dir=output_directory, prefix_file=prefix_file, output_geometry_types=output_geometry_types, layer_name=layer_name, )
def show_query(self, output: QueryLanguage): """Show the query in the main window.""" self.write_nominatim_file(self.panel) try: properties = self.gather_values() except QuickOsmException as error: self.dialog.display_quickosm_exception(error) return except Exception as error: self.dialog.display_critical_exception(error) return # Transfer each output with zip check_boxes = zip( self.dialog.output_buttons[Panels.QuickQuery], self.dialog.output_buttons[Panels.Query]) for couple in check_boxes: couple[1].setChecked(couple[0].isChecked()) # Transfer the language of the query self.query_language_updated() # Transfer the output self.dialog.output_directory_q.setFilePath(properties['output_directory']) index_format = self.dialog.combo_format_q.findData(properties['output_format']) self.dialog.combo_format_q.setCurrentIndex(index_format) if properties['prefix_file']: self.dialog.line_file_prefix_q.setText(properties['prefix_file']) self.dialog.line_file_prefix_q.setEnabled(True) # Make the query query_factory = QueryFactory( type_multi_request=properties['type_multi_request'], query_type=properties['query_type'], key=properties['key'], value=properties['value'], area=properties['place'], around_distance=properties['distance'], osm_objects=properties['osm_objects'], timeout=properties['timeout'], print_mode=properties['metadata'] ) try: query = query_factory.make(output) except QuickOsmException as error: self.dialog.display_quickosm_exception(error) except Exception as error: self.dialog.display_critical_exception(error) else: self.dialog.text_query.setPlainText(query) item = self.dialog.menu_widget.item(self.dialog.query_menu_index) self.dialog.menu_widget.setCurrentItem(item)
def test_replace_template(self): """Test replace template.""" query = ' area="paris"' expected = ' {{geocodeArea:paris}}' self.assertEqual(QueryFactory.replace_template(query), expected) query = ' area_coords="paris,france"' expected = ' {{geocodeCoords:paris,france}}' self.assertEqual(QueryFactory.replace_template(query), expected) query = ' bbox="custom"' expected = ' {{bbox}}' self.assertEqual(QueryFactory.replace_template(query), expected)
def test_replace_template(self): """Test replace template.""" query = ' area="paris"' expected = " {{geocodeArea:paris}}" self.assertEqual(QueryFactory.replace_template(query), expected) query = ' area_coords="paris,france"' expected = " {{geocodeCoords:paris,france}}" self.assertEqual(QueryFactory.replace_template(query), expected) query = ' bbox="custom"' expected = " {{bbox}}" self.assertEqual(QueryFactory.replace_template(query), expected)
def save_query(self): """Save a query in a preset.""" properties = self.gather_values() # Make the query query_factory = QueryFactory( type_multi_request=properties['type_multi_request'], query_type=properties['query_type'], key=properties['key'], value=properties['value'], area=properties['place'], around_distance=properties['distance'], osm_objects=properties['osm_objects'], timeout=properties['timeout'], print_mode=properties['metadata'] ) query = query_factory.make(QueryLanguage.OQL) description = query_factory.friendly_message() q_manage = QueryManagement( query=query, name=properties['layer_name'], description=description, advanced=False, type_multi_request=properties['type_multi_request'], keys=properties['key'], values=properties['value'], area=properties['place'], bbox=properties['bbox'], output_geometry_types=properties['outputs'], output_directory=properties['output_directory'], output_format=properties['output_format'] ) if self.existing_preset: preset_folder = query_preset() presets = filter( lambda folder: os.path.isdir(join(preset_folder, folder)), os.listdir(preset_folder)) chosen_preset = QInputDialog.getItem( self.dialog, tr('Add in an existing preset'), tr('Please select the preset in which the query will be added:'), presets, editable=False ) if chosen_preset[1]: q_manage.add_query_in_preset(chosen_preset[0]) else: q_manage.add_preset(properties['layer_name']) self.dialog.external_panels[Panels.MapPreset].update_personal_preset_view() item = self.dialog.menu_widget.item(self.dialog.preset_menu_index) self.dialog.menu_widget.setCurrentItem(item)
def process_quick_query( dialog=None, key=None, value=None, bbox=None, nominatim=None, is_around=None, distance=None, osm_objects=None, timeout=25, output_directory=None, prefix_file=None, output_geometry_types=None): """ generate a query and send it to process_query """ # Set the layer name layer_name = u'' for i in [key, value, nominatim]: if i: layer_name += i + u'_' if is_around: layer_name += '%s_' % distance # Delete last "_" layer_name = layer_name[:-1] # Building the query query_factory = QueryFactory( timeout=timeout, key=key, value=value, bbox=bbox, is_around=is_around, distance=distance, nominatim=nominatim, osm_objects=osm_objects) query = query_factory.make() # Call process_query with the new query return process_query( dialog=dialog, query=query, nominatim=nominatim, bbox=bbox, output_dir=output_directory, prefix_file=prefix_file, output_geometry_types=output_geometry_types, layer_name=layer_name)
def process_quick_query( dialog=None, query_type=None, key=None, value=None, bbox=None, area=None, distance=None, osm_objects=None, timeout=25, output_directory=None, prefix_file=None, output_geometry_types=None): """ Generate a query and send it to process_query. """ # Building the query query_factory = QueryFactory( query_type=query_type, key=key, value=value, area=area, around_distance=distance, osm_objects=osm_objects, timeout=timeout ) query = query_factory.make() LOGGER.info(query_factory.friendly_message()) # Generate layer name as following (if defined) if not key: key = tr('allKeys') distance_string = None if distance: distance_string = '{}'.format(distance) expected_name = [key, value, area, distance_string] layer_name = '_'.join([f for f in expected_name if f]) LOGGER.info('Query: {}'.format(layer_name)) # Call process_query with the new query return process_query( dialog=dialog, query=query, area=area, bbox=bbox, output_dir=output_directory, prefix_file=prefix_file, output_geometry_types=output_geometry_types, layer_name=layer_name)
def test_make(self): """Test make query.""" query = QueryFactory('foo', 'bar', True) expected = u'<osm-script output="xml" timeout="25">\n ' \ u'<union>\n <query type="node">\n ' \ u'<has-kv k="foo" v="bar"/>\n ' \ u'<bbox-query {{bbox}}/>\n </query>\n ' \ u'<query type="way">\n ' \ u'<has-kv k="foo" v="bar"/>\n ' \ u'<bbox-query {{bbox}}/>\n </query>\n ' \ u'<query type="relation">\n ' \ u'<has-kv k="foo" v="bar"/>\n ' \ u'<bbox-query {{bbox}}/>\n </query>\n ' \ u'</union>\n <union>\n <item/>\n ' \ u'<recurse type="down"/>\n </union>\n ' \ u'<print mode="body"/>\n</osm-script>\n' self.assertEqual(query.make(), expected)
def test_generate_xml(self): """Test generate XML.""" query = QueryFactory(key="foo", value="bar", nominatim="paris") expected = ( u'<osm-script output="xml" timeout="25">' u'<id-query area="paris" into="area_0"/><union>' u'<query type="node"><has-kv k="foo" v="bar"/>' u'<area-query from="area_0" /></query><query type="way">' u'<has-kv k="foo" v="bar"/><area-query from="area_0" />' u'</query><query type="relation">' u'<has-kv k="foo" v="bar"/><area-query from="area_0" />' u'</query></union><union><item /><recurse type="down"/>' u'</union><print mode="body" /></osm-script>' ) self.assertEqual(query.generate_xml(), expected) query = QueryFactory(key="foo", bbox=True, timeout=35) expected = ( u'<osm-script output="xml" timeout="35"><union>' u'<query type="node"><has-kv k="foo" />' u'<bbox-query bbox="custom" /></query><query type="way">' u'<has-kv k="foo" /><bbox-query bbox="custom" /></query>' u'<query type="relation"><has-kv k="foo" />' u'<bbox-query bbox="custom" /></query></union><union>' u'<item /><recurse type="down"/></union>' u'<print mode="body" /></osm-script>' ) self.assertEqual(query.generate_xml(), expected) query = QueryFactory(key="foo", nominatim="paris;dubai", osm_objects=["node"]) expected = ( u'<osm-script output="xml" timeout="25">' u'<id-query area="paris" into="area_0"/>' u'<id-query area="dubai" into="area_1"/><union>' u'<query type="node"><has-kv k="foo" />' u'<area-query from="area_0" /></query><query type="node">' u'<has-kv k="foo" /><area-query from="area_1" /></query>' u'</union><union><item /><recurse type="down"/></union>' u'<print mode="body" /></osm-script>' ) self.assertEqual(query.generate_xml(), expected) query = QueryFactory(key="foo", is_around=True, distance=1000, print_mode="meta", nominatim="a") expected = ( u'<osm-script output="xml" timeout="25"><union>' u'<query type="node"><has-kv k="foo" />' u'<around area_coords="a" radius="1000" /></query>' u'<query type="way"><has-kv k="foo" />' u'<around area_coords="a" radius="1000" /></query>' u'<query type="relation"><has-kv k="foo" />' u'<around area_coords="a" radius="1000" /></query>' u"</union>" u'<union><item /><recurse type="down"/></union>' u'<print mode="meta" /></osm-script>' ) self.assertEqual(query.generate_xml(), expected)
def build_query(self): query_factory = QueryFactory( query_type=self.QUERY_TYPE, key=self.key, value=self.value, area=self.area, around_distance=self.distance, timeout=self.timeout) raw_query = query_factory.make() query_preparation = QueryPreparation( raw_query, nominatim_place=self.area, extent=self.extent, overpass=self.server ) raw_query = query_preparation.prepare_query() url = query_preparation.prepare_url() outputs = { self.OUTPUT_URL: url, self.OUTPUT_OQL_QUERY: raw_query, } return outputs
def show_query(self): """Show the query in the main window.""" try: p = self.gather_values() except QuickOsmException as e: self.dialog.display_quickosm_exception(e) return except Exception as e: self.dialog.display_critical_exception(e) return # Transfer each output with zip check_boxes = zip(self.dialog.output_buttons[Panels.QuickQuery], self.dialog.output_buttons[Panels.Query]) for couple in check_boxes: couple[1].setChecked(couple[0].isChecked()) # Transfer the output self.dialog.output_directory_q.setFilePath(p['output_directory']) if p['prefix_file']: self.dialog.line_file_prefix_q.setText(p['prefix_file']) self.dialog.line_file_prefix_q.setEnabled(True) # Make the query query_factory = QueryFactory(query_type=p['query_type'], key=p['key'], value=p['value'], area=p['place'], around_distance=p['distance'], osm_objects=p['osm_objects'], timeout=p['timeout']) try: query = query_factory.make() except QuickOsmException as e: self.dialog.display_quickosm_exception(e) except Exception as e: self.dialog.display_critical_exception(e) else: self.dialog.text_query.setPlainText(query) item = self.dialog.menu_widget.item(self.dialog.query_menu_index) self.dialog.menu_widget.setCurrentItem(item)
def build_query(self): query_factory = QueryFactory( query_type=self.QUERY_TYPE, key=self.key, value=self.value, area=self.area, around_distance=self.distance, timeout=self.timeout) raw_query = query_factory.make() query_preparation = QueryPreparation( raw_query, area=self.area, extent=self.extent, overpass=self.server ) raw_query = query_preparation.prepare_query() url = query_preparation.prepare_url() outputs = { self.OUTPUT_URL: url, self.OUTPUT_OQL_QUERY: raw_query, } return outputs
class TestQueryFactory(unittest.TestCase): def setUp(self): self.maxDiff = None pass def tearDown(self): pass def test_check_parameters(self): """Test check parameters query.""" # Nominatim and BBOX. query = QueryFactory(nominatim='foo', bbox=True) self.assertRaises(QueryFactoryException, query.check_parameters) # Missing key. query = QueryFactory(nominatim='foo') self.assertRaises(QueryFactoryException, query.check_parameters) # Missing osm object query = QueryFactory(key='foo', osm_objects=[]) self.assertRaises(QueryFactoryException, query.check_parameters) # Wrong osm object. query = QueryFactory(key='foo', osm_objects=['bar']) self.assertRaises(QueryFactoryException, query.check_parameters) # Missing distance if "around". query = QueryFactory(key='foo', osm_objects=['node'], is_around=True) self.assertRaises(QueryFactoryException, query.check_parameters) # Missing nominatim if "around". query = QueryFactory(key='foo', osm_objects=['node'], is_around=True, distance=10) self.assertRaises(QueryFactoryException, query.check_parameters) # Good query. query = QueryFactory('foo', 'bar') try: query.check_parameters() except QueryFactoryException, e: self.fail(e.msg) query = QueryFactory('foo', nominatim='bar') try: query.check_parameters() except QueryFactoryException, e: self.fail(e.msg)
def processAlgorithm(self, progress): key = self.getParameterValue(self.FIELD_KEY) value = self.getParameterValue(self.FIELD_VALUE) extent = self.getParameterValue(self.FIELD_EXTENT) nominatim = self.getParameterValue(self.FIELD_NOMINATIM) if nominatim == '': nominatim = None if value == '': value = None timeout = self.getParameterValue(self.FIELD_TIMEOUT) # Missing OSMObjects query_factory = QueryFactory(key=key, value=value, nominatim=nominatim, bbox=extent, timeout=timeout) query = query_factory.make() self.setOutputValue(self.OUTPUT_QUERY, query)
def show_query(self): """ Show the query in the main window """ # We have to find the widget in the stacked widget of the main window query_widget = None index_quick_query_widget = None for i in range(iface.QuickOSM_mainWindowDialog.stackedWidget.count()): widget = iface.QuickOSM_mainWindowDialog.stackedWidget.widget(i) if widget.__class__.__name__ == "QueryWidget": query_widget = iface.QuickOSM_mainWindowDialog.stackedWidget.\ widget(i) index_quick_query_widget = i break # Get all values key = self.comboBox_key.currentText() value = self.comboBox_value.currentText() nominatim = self.lineEdit_nominatim.text() timeout = self.spinBox_timeout.value() output_directory = self.output_directory.filePath() prefix_file = self.lineEdit_filePrefix.text() query_type = self.cb_query_type.currentData() is_around = query_type == 'around' distance = self.spinBox_distance_point.value() # If bbox, we must set None to nominatim, we can't have both bbox = None if query_type in ['layer', 'canvas']: nominatim = None bbox = True elif query_type in ['attributes']: nominatim = None if nominatim == '': nominatim = None # Which osm objects ? osm_objects = self._get_osm_objects() # Which geometry at the end ? query_widget.checkBox_points.setChecked( self.checkBox_points.isChecked()) query_widget.checkBox_lines.setChecked(self.checkBox_lines.isChecked()) query_widget.checkBox_multilinestrings.setChecked( self.checkBox_multilinestrings.isChecked()) query_widget.checkBox_multipolygons.setChecked( self.checkBox_multipolygons.isChecked()) # What kind of extent query # query_widget.radioButton_extentLayer.setChecked( # self.radioButton_extentLayer.isChecked()) # query_widget.radioButton_extentMapCanvas.setChecked( # self.radioButton_extentMapCanvas.isChecked()) # Transfer the combobox from QuickQuery to Query # if self.comboBox_extentLayer.count(): # query_widget.radioButton_extentLayer.setCheckable(True) # Transfer the output query_widget.output_directory.setFilePath(output_directory) if prefix_file: query_widget.lineEdit_filePrefix.setText(prefix_file) query_widget.lineEdit_filePrefix.setEnabled(True) # TODO # Move this logic UP # Copy/paste in quick_query_dialog.py if is_around and nominatim: query_type = QueryType.AroundNominatimPlace elif not is_around and nominatim: query_type = QueryType.InNominatimPlace elif bbox: query_type = QueryType.BBox else: query_type = QueryType.NotSpatial # End todo # Make the query query_factory = QueryFactory(query_type=query_type, key=key, value=value, nominatim_place=nominatim, around_distance=distance, osm_objects=osm_objects, timeout=timeout) query = query_factory.make() query_widget.textEdit_query.setPlainText(query) iface.QuickOSM_mainWindowDialog.listWidget.setCurrentRow( index_quick_query_widget) iface.QuickOSM_mainWindowDialog.exec_()
def show_query(self): """ Show the query in the main window """ # We have to find the widget in the stacked widget of the main window query_widget = None index_quick_query_widget = None for i in xrange(iface.QuickOSM_mainWindowDialog.stackedWidget.count()): widget = iface.QuickOSM_mainWindowDialog.stackedWidget.widget(i) if widget.__class__.__name__ == "QueryWidget": query_widget = iface.QuickOSM_mainWindowDialog.stackedWidget.\ widget(i) index_quick_query_widget = i break # Get all values key = unicode(self.comboBox_key.currentText()) value = unicode(self.comboBox_value.currentText()) nominatim = unicode(self.lineEdit_nominatim.text()) timeout = self.spinBox_timeout.value() output_directory = self.lineEdit_browseDir.text() prefix_file = self.lineEdit_filePrefix.text() if self.comboBox_in_around.currentIndex() == 1: is_around = True else: is_around = False distance = self.spinBox_distance_point.value() # If bbox, we must set None to nominatim, we can't have both bbox = None if self.radioButton_extentLayer.isChecked() or \ self.radioButton_extentMapCanvas.isChecked(): nominatim = None bbox = True if nominatim == '': nominatim = None # Which osm objects ? osm_objects = self._get_osm_objects() # Which geometry at the end ? query_widget.checkBox_points.setChecked( self.checkBox_points.isChecked()) query_widget.checkBox_lines.setChecked(self.checkBox_lines.isChecked()) query_widget.checkBox_multilinestrings.setChecked( self.checkBox_multilinestrings.isChecked()) query_widget.checkBox_multipolygons.setChecked( self.checkBox_multipolygons.isChecked()) query_widget.radioButton_extentLayer.setChecked( self.radioButton_extentLayer.isChecked()) query_widget.radioButton_extentMapCanvas.setChecked( self.radioButton_extentMapCanvas.isChecked()) # Transfer the combobox from QuickQuery to Query if self.comboBox_extentLayer.count(): query_widget.radioButton_extentLayer.setCheckable(True) query_widget.comboBox_extentLayer.setModel( self.comboBox_extentLayer.model()) # Transfer the output query_widget.lineEdit_browseDir.setText(output_directory) if prefix_file: query_widget.lineEdit_filePrefix.setText(prefix_file) query_widget.lineEdit_filePrefix.setEnabled(True) # Make the query query_factory = QueryFactory(timeout=timeout, key=key, value=value, bbox=bbox, nominatim=nominatim, is_around=is_around, distance=distance, osm_objects=osm_objects) query = query_factory.make() query_widget.textEdit_query.setPlainText(query) iface.QuickOSM_mainWindowDialog.listWidget.setCurrentRow( index_quick_query_widget) iface.QuickOSM_mainWindowDialog.exec_()
def show_query(self): """ Show the query in the main window """ # We have to find the widget in the stacked widget of the main window query_widget = None index_quick_query_widget = None for i in xrange(iface.QuickOSM_mainWindowDialog.stackedWidget.count()): widget = iface.QuickOSM_mainWindowDialog.stackedWidget.widget(i) if widget.__class__.__name__ == "QueryWidget": query_widget = iface.QuickOSM_mainWindowDialog.stackedWidget.\ widget(i) index_quick_query_widget = i break # Get all values key = unicode(self.comboBox_key.currentText()) value = unicode(self.comboBox_value.currentText()) nominatim = unicode(self.lineEdit_nominatim.text()) timeout = self.spinBox_timeout.value() output_directory = self.lineEdit_browseDir.text() prefix_file = self.lineEdit_filePrefix.text() if self.comboBox_in_around.currentIndex() == 1: is_around = True else: is_around = False distance = self.spinBox_distance_point.value() # If bbox, we must set None to nominatim, we can't have both bbox = None if self.radioButton_extentLayer.isChecked() or \ self.radioButton_extentMapCanvas.isChecked(): nominatim = None bbox = True if nominatim == '': nominatim = None # Which osm objects ? osm_objects = self._get_osm_objects() # Which geometry at the end ? query_widget.checkBox_points.setChecked( self.checkBox_points.isChecked()) query_widget.checkBox_lines.setChecked( self.checkBox_lines.isChecked()) query_widget.checkBox_multilinestrings.setChecked( self.checkBox_multilinestrings.isChecked()) query_widget.checkBox_multipolygons.setChecked( self.checkBox_multipolygons.isChecked()) query_widget.radioButton_extentLayer.setChecked( self.radioButton_extentLayer.isChecked()) query_widget.radioButton_extentMapCanvas.setChecked( self.radioButton_extentMapCanvas.isChecked()) # Transfer the combobox from QuickQuery to Query if self.comboBox_extentLayer.count(): query_widget.radioButton_extentLayer.setCheckable(True) query_widget.comboBox_extentLayer.setModel( self.comboBox_extentLayer.model()) # Transfer the output query_widget.lineEdit_browseDir.setText(output_directory) if prefix_file: query_widget.lineEdit_filePrefix.setText(prefix_file) query_widget.lineEdit_filePrefix.setEnabled(True) # Make the query query_factory = QueryFactory( timeout=timeout, key=key, value=value, bbox=bbox, nominatim=nominatim, is_around=is_around, distance=distance, osm_objects=osm_objects) query = query_factory.make() query_widget.textEdit_query.setPlainText(query) iface.QuickOSM_mainWindowDialog.listWidget.setCurrentRow( index_quick_query_widget) iface.QuickOSM_mainWindowDialog.exec_()
def process_quick_query( dialog=None, key=None, value=None, bbox=None, nominatim=None, is_around=None, distance=None, osm_objects=None, timeout=25, output_directory=None, prefix_file=None, output_geometry_types=None): """ Generate a query and send it to process_query. """ # TODO # Move this logic UP # Copy/paste in quick_query_dialog.py distance_string = None if is_around and nominatim: query_type = QueryType.AroundArea distance_string = '{}'.format(distance) elif not is_around and nominatim: query_type = QueryType.InArea elif bbox: query_type = QueryType.BBox else: query_type = QueryType.NotSpatial # End todo LOGGER.info('QueryFactory: the key is "{}" and the value is "{}"'.format( key, value)) # Building the query query_factory = QueryFactory( query_type=query_type, key=key, value=value, area=nominatim, around_distance=distance, osm_objects=osm_objects, timeout=timeout ) query = query_factory.make() # Generate layer name as following (if defined) if not key: key = tr('allKeys') expected_name = [key, value, nominatim, distance_string] layer_name = '_'.join([f for f in expected_name if f]) LOGGER.info('Query: {}'.format(layer_name)) # Call process_query with the new query return process_query( dialog=dialog, query=query, nominatim=nominatim, bbox=bbox, output_dir=output_directory, prefix_file=prefix_file, output_geometry_types=output_geometry_types, layer_name=layer_name)
self.assertRaises(QueryFactoryException, query.check_parameters) # Good query. query = QueryFactory('foo', 'bar') try: query.check_parameters() except QueryFactoryException, e: self.fail(e.msg) query = QueryFactory('foo', nominatim='bar') try: query.check_parameters() except QueryFactoryException, e: self.fail(e.msg) query = QueryFactory('foo', bbox=True) try: query.check_parameters() except QueryFactoryException, e: self.fail(e.msg) query = QueryFactory('foo', is_around=True, distance=50, nominatim='a') try: query.check_parameters() except QueryFactoryException, e: self.fail(e.msg) def test_replace_template(self): """Test replace template.""" query = ' area="paris"' expected = ' {{geocodeArea:paris}}'
def test_generate_xml(self): """Test generate XML.""" query = QueryFactory(key='foo', value='bar', nominatim='paris') expected = u'<osm-script output="xml" timeout="25">' \ u'<id-query area="paris" into="area_0"/><union>' \ u'<query type="node"><has-kv k="foo" v="bar"/>' \ u'<area-query from="area_0" /></query><query type="way">' \ u'<has-kv k="foo" v="bar"/><area-query from="area_0" />' \ u'</query><query type="relation">' \ u'<has-kv k="foo" v="bar"/><area-query from="area_0" />' \ u'</query></union><union><item /><recurse type="down"/>' \ u'</union><print mode="body" /></osm-script>' self.assertEqual(query.generate_xml(), expected) query = QueryFactory(key='foo', bbox=True, timeout=35) expected = u'<osm-script output="xml" timeout="35"><union>' \ u'<query type="node"><has-kv k="foo" />' \ u'<bbox-query bbox="custom" /></query><query type="way">' \ u'<has-kv k="foo" /><bbox-query bbox="custom" /></query>' \ u'<query type="relation"><has-kv k="foo" />' \ u'<bbox-query bbox="custom" /></query></union><union>' \ u'<item /><recurse type="down"/></union>' \ u'<print mode="body" /></osm-script>' self.assertEqual(query.generate_xml(), expected) query = QueryFactory( key='foo', nominatim='paris;dubai', osm_objects=['node']) expected = u'<osm-script output="xml" timeout="25">' \ u'<id-query area="paris" into="area_0"/>' \ u'<id-query area="dubai" into="area_1"/><union>' \ u'<query type="node"><has-kv k="foo" />' \ u'<area-query from="area_0" /></query><query type="node">' \ u'<has-kv k="foo" /><area-query from="area_1" /></query>' \ u'</union><union><item /><recurse type="down"/></union>' \ u'<print mode="body" /></osm-script>' self.assertEqual(query.generate_xml(), expected) query = QueryFactory( key='foo', is_around=True, distance=1000, print_mode='meta', nominatim='a') expected = u'<osm-script output="xml" timeout="25"><union>' \ u'<query type="node"><has-kv k="foo" />' \ u'<around area_coords="a" radius="1000" /></query>' \ u'<query type="way"><has-kv k="foo" />' \ u'<around area_coords="a" radius="1000" /></query>' \ u'<query type="relation"><has-kv k="foo" />' \ u'<around area_coords="a" radius="1000" /></query>' \ u'</union>' \ u'<union><item /><recurse type="down"/></union>' \ u'<print mode="meta" /></osm-script>' self.assertEqual(query.generate_xml(), expected)
self.assertRaises(QueryFactoryException, query.check_parameters) # Good query. query = QueryFactory("foo", "bar") try: query.check_parameters() except QueryFactoryException, e: self.fail(e.msg) query = QueryFactory("foo", nominatim="bar") try: query.check_parameters() except QueryFactoryException, e: self.fail(e.msg) query = QueryFactory("foo", bbox=True) try: query.check_parameters() except QueryFactoryException, e: self.fail(e.msg) query = QueryFactory("foo", is_around=True, distance=50, nominatim="a") try: query.check_parameters() except QueryFactoryException, e: self.fail(e.msg) def test_replace_template(self): """Test replace template.""" query = ' area="paris"' expected = " {{geocodeArea:paris}}"
def show_query(self): """ Show the query in the main window """ # We have to find the widget in the stacked widget of the main window query_widget = None index_quick_query_widget = None for i in range(iface.QuickOSM_mainWindowDialog.stackedWidget.count()): widget = iface.QuickOSM_mainWindowDialog.stackedWidget.widget(i) if widget.__class__.__name__ == "QueryWidget": query_widget = iface.QuickOSM_mainWindowDialog.stackedWidget.\ widget(i) index_quick_query_widget = i break # Get all values key = self.comboBox_key.currentText() value = self.comboBox_value.currentText() nominatim = self.lineEdit_nominatim.text() timeout = self.spinBox_timeout.value() output_directory = self.output_directory.filePath() prefix_file = self.lineEdit_filePrefix.text() query_type = self.cb_query_type.currentData() is_around = query_type == 'around' distance = self.spinBox_distance_point.value() # If bbox, we must set None to nominatim, we can't have both bbox = None if query_type in ['layer', 'canvas']: nominatim = None bbox = True elif query_type in ['attributes']: nominatim = None if nominatim == '': nominatim = None # Which osm objects ? osm_objects = self._get_osm_objects() # Which geometry at the end ? query_widget.checkBox_points.setChecked( self.checkBox_points.isChecked()) query_widget.checkBox_lines.setChecked( self.checkBox_lines.isChecked()) query_widget.checkBox_multilinestrings.setChecked( self.checkBox_multilinestrings.isChecked()) query_widget.checkBox_multipolygons.setChecked( self.checkBox_multipolygons.isChecked()) # What kind of extent query # query_widget.radioButton_extentLayer.setChecked( # self.radioButton_extentLayer.isChecked()) # query_widget.radioButton_extentMapCanvas.setChecked( # self.radioButton_extentMapCanvas.isChecked()) # Transfer the combobox from QuickQuery to Query # if self.comboBox_extentLayer.count(): # query_widget.radioButton_extentLayer.setCheckable(True) # Transfer the output query_widget.output_directory.setFilePath(output_directory) if prefix_file: query_widget.lineEdit_filePrefix.setText(prefix_file) query_widget.lineEdit_filePrefix.setEnabled(True) # TODO # Move this logic UP # Copy/paste in quick_query_dialog.py if is_around and nominatim: query_type = QueryType.AroundArea elif not is_around and nominatim: query_type = QueryType.InArea elif bbox: query_type = QueryType.BBox else: query_type = QueryType.NotSpatial # End todo # Make the query query_factory = QueryFactory( query_type=query_type, key=key, value=value, area=nominatim, around_distance=distance, osm_objects=osm_objects, timeout=timeout ) query = query_factory.make() query_widget.textEdit_query.setPlainText(query) iface.QuickOSM_mainWindowDialog.listWidget.setCurrentRow( index_quick_query_widget) iface.QuickOSM_mainWindowDialog.exec_()
def test_check_parameters(self): """Test check parameters query.""" # Nominatim and BBOX. query = QueryFactory(nominatim='foo', bbox=True) self.assertRaises(QueryFactoryException, query.check_parameters) # Missing key. query = QueryFactory(nominatim='foo') self.assertRaises(QueryFactoryException, query.check_parameters) # Missing osm object query = QueryFactory(key='foo', osm_objects=[]) self.assertRaises(QueryFactoryException, query.check_parameters) # Wrong osm object. query = QueryFactory(key='foo', osm_objects=['bar']) self.assertRaises(QueryFactoryException, query.check_parameters) # Missing distance if "around". query = QueryFactory(key='foo', osm_objects=['node'], is_around=True) self.assertRaises(QueryFactoryException, query.check_parameters) # Missing nominatim if "around". query = QueryFactory( key='foo', osm_objects=['node'], is_around=True, distance=10) self.assertRaises(QueryFactoryException, query.check_parameters) # Good query. query = QueryFactory('foo', 'bar') try: query.check_parameters() except QueryFactoryException as e: self.fail(e.msg) query = QueryFactory('foo', nominatim='bar') try: query.check_parameters() except QueryFactoryException as e: self.fail(e.msg) query = QueryFactory('foo', bbox=True) try: query.check_parameters() except QueryFactoryException as e: self.fail(e.msg) query = QueryFactory('foo', is_around=True, distance=50, nominatim='a') try: query.check_parameters() except QueryFactoryException as e: self.fail(e.msg)