Skip to content

Commit

Permalink
add {{geocodeCoords:}} #46
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Feb 24, 2015
1 parent 7bb2a71 commit 3ed3d7e
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 39 deletions.
8 changes: 6 additions & 2 deletions Controller/Process.py
Expand Up @@ -186,18 +186,22 @@ def ProcessQuery(dialog = None, query=None, nominatim=None, bbox=None, outputDir
generate a query and send it to "processQuery"
'''
@staticmethod
def ProcessQuickQuery(dialog = None, key = None,value = None,bbox = None,nominatim = None,osmObjects = None, timeout=25, outputDir=None, prefixFile=None, outputGeomTypes=None):
def ProcessQuickQuery(dialog = None, key = None,value = None,bbox = None,nominatim = None, isAround = None, distance = None, osmObjects = None, timeout=25, outputDir=None, prefixFile=None, outputGeomTypes=None):

#Set the layername
layerName = ''
for i in [key,value,nominatim]:
if i:
layerName += i + "_"

if isAround:
layerName += str(distance) + "_"

#Delete last "_"
layerName = layerName[:-1]

#Building the query
queryFactory = QueryFactory(timeout=timeout,key=key,value=value,bbox=bbox,nominatim=nominatim,osmObjects=osmObjects)
queryFactory = QueryFactory(timeout=timeout, key=key, value=value, bbox=bbox, isAround=isAround, distance=distance, nominatim=nominatim, osmObjects=osmObjects)
query = queryFactory.make()

#Call ProcessQuery with the new query
Expand Down
17 changes: 17 additions & 0 deletions CoreQuickOSM/API/Nominatim.py
Expand Up @@ -91,5 +91,22 @@ def getFirstPolygonFromQuery(self,query):
if result['osm_type'] == "relation":
return result['osm_id']

#If no result has been return
raise NominatimAreaException

def getFirstPointFromQuery(self,query):
'''
Get first longitude, latitude of a Nominatim point
@param query: Query to execute
@type query: str
@raise NominatimAreaException:
@return: First relation's osm_id
@rtype: str
'''
data = self.query(query)
for result in data:
if result['osm_type'] == "node":
return result['lon'], result['lat']

#If no result has been return
raise NominatimAreaException
12 changes: 9 additions & 3 deletions CoreQuickOSM/QueryFactory.py
Expand Up @@ -30,7 +30,7 @@ class QueryFactory():

OSM_TYPES = ['node','way','relation']

def __init__(self,key = None,value = None,bbox = None,nominatim = None,osmObjects = OSM_TYPES, output = 'xml', timeout=25, printMode = 'body'):
def __init__(self,key = None,value = None,bbox = None,nominatim = None, isAround = None, distance = None, osmObjects = OSM_TYPES, output = 'xml', timeout=25, printMode = 'body'):
'''
Constructor with key=value according to OpenStreetMap
A bbox or nominatim can be provided
Expand All @@ -43,6 +43,8 @@ def __init__(self,key = None,value = None,bbox = None,nominatim = None,osmObject
@type bbox: QgsRectangle or bool or "{{bbox}}"
@param nominatim: a place
@type nominatim: str
@param isAround: around or in
@type isAround: bool
@param osmObjects: list of osm objects to query on (node/way/relation)
@type osmObjects: list
@param output:output of overpass : xml or json
Expand All @@ -56,6 +58,8 @@ def __init__(self,key = None,value = None,bbox = None,nominatim = None,osmObject
self.__value = value
self.__bbox = bbox
self.__nominatim = nominatim
self.__isAround = isAround
self.__distance = distance
self.__osmObjects = osmObjects
self.__timeout = timeout
self.__output = output
Expand Down Expand Up @@ -89,7 +93,7 @@ def make(self):
TAB = ' '
query = '<osm-script output="%s" timeout="%s"> \n' %(self.__output,self.__timeout)

if self.__nominatim:
if self.__nominatim and not self.__isAround:
query += TAB + '<id-query {{nominatimArea:'+self.__nominatim+'}} into="area"/> \n'

query += TAB + '<union>\n'
Expand All @@ -101,8 +105,10 @@ def make(self):
query += 'v="'+self.__value+'"'
query += '/> \n'

if self.__nominatim:
if self.__nominatim and not self.__isAround:
query += TAB + TAB + TAB + '<area-query from="area"/>\n'
elif self.__nominatim and self.__isAround:
query += TAB + TAB + TAB + '<around {{geocodeCoords:'+self.__nominatim+'}} radius="'+ str(self.__distance) +'" />\n'
elif self.__bbox:
query += TAB + TAB + TAB + '<bbox-query '+self.__bbox+'/>\n'
query += TAB + TAB + '</query>\n'
Expand Down
20 changes: 20 additions & 0 deletions CoreQuickOSM/Tools.py
Expand Up @@ -200,6 +200,26 @@ def PrepareQueryOqlXml(query,extent = None, nominatimName = None):
else:
newString = 'ref="'+str(area)+'" type="area"'
query = re.sub(r'{{(nominatimArea|geocodeArea):(.*)}}',newString, query)

#Replace geocodeCenter
nominatimQuery = re.search('{{(geocodeCoords):(.*)}}', query)
if nominatimQuery:
result = nominatimQuery.groups()
search = result[1]

if nominatimName:
search = nominatimName

#We perform a nominatim query
nominatim = Nominatim()
lon, lat = nominatim.getFirstPointFromQuery(search)

newString = None
if isOQL:
newString = str(lat)+','+str(lon)
else:
newString = 'lat="'+str(lat)+'" lon="'+str(lon)+'"'
query = re.sub(r'{{(geocodeCoords):(.*)}}',newString, query)

#Replace {{bbox}}
bboxQuery = re.search('{{bbox}}',query)
Expand Down
35 changes: 24 additions & 11 deletions ui/quick_query.py
Expand Up @@ -2,7 +2,7 @@

# Form implementation generated from reading ui file 'quick_query.ui'
#
# Created: Wed Oct 8 19:29:01 2014
# Created: Tue Feb 24 21:39:32 2015
# by: PyQt4 UI code generator 4.10.4
#
# WARNING! All changes made in this file will be lost!
Expand All @@ -26,14 +26,14 @@ def _translate(context, text, disambig):
class Ui_ui_quick_query(object):
def setupUi(self, ui_quick_query):
ui_quick_query.setObjectName(_fromUtf8("ui_quick_query"))
ui_quick_query.resize(604, 866)
ui_quick_query.resize(644, 805)
self.verticalLayout_3 = QtGui.QVBoxLayout(ui_quick_query)
self.verticalLayout_3.setObjectName(_fromUtf8("verticalLayout_3"))
self.scrollArea = QtGui.QScrollArea(ui_quick_query)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
self.scrollAreaWidgetContents = QtGui.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 590, 852))
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 630, 791))
self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
self.verticalLayout = QtGui.QVBoxLayout(self.scrollAreaWidgetContents)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
Expand Down Expand Up @@ -78,18 +78,31 @@ def setupUi(self, ui_quick_query):
self.comboBox_value.setSizeAdjustPolicy(QtGui.QComboBox.AdjustToContents)
self.comboBox_value.setObjectName(_fromUtf8("comboBox_value"))
self.formLayout.setWidget(1, QtGui.QFormLayout.FieldRole, self.comboBox_value)
self.label_3 = QtGui.QLabel(self.scrollAreaWidgetContents)
self.label_3.setObjectName(_fromUtf8("label_3"))
self.formLayout.setWidget(2, QtGui.QFormLayout.LabelRole, self.label_3)
self.radioButton_place = QtGui.QRadioButton(self.scrollAreaWidgetContents)
self.radioButton_place.setText(_fromUtf8(""))
self.radioButton_place.setChecked(True)
self.radioButton_place.setObjectName(_fromUtf8("radioButton_place"))
self.formLayout.setWidget(3, QtGui.QFormLayout.LabelRole, self.radioButton_place)
self.formLayout.setWidget(2, QtGui.QFormLayout.LabelRole, self.radioButton_place)
self.horizontalLayout_8 = QtGui.QHBoxLayout()
self.horizontalLayout_8.setObjectName(_fromUtf8("horizontalLayout_8"))
self.comboBox_in_around = QtGui.QComboBox(self.scrollAreaWidgetContents)
self.comboBox_in_around.setObjectName(_fromUtf8("comboBox_in_around"))
self.horizontalLayout_8.addWidget(self.comboBox_in_around)
self.lineEdit_nominatim = QtGui.QLineEdit(self.scrollAreaWidgetContents)
self.lineEdit_nominatim.setText(_fromUtf8(""))
self.lineEdit_nominatim.setObjectName(_fromUtf8("lineEdit_nominatim"))
self.formLayout.setWidget(3, QtGui.QFormLayout.FieldRole, self.lineEdit_nominatim)
self.horizontalLayout_8.addWidget(self.lineEdit_nominatim)
self.spinBox_distance_point = QtGui.QSpinBox(self.scrollAreaWidgetContents)
self.spinBox_distance_point.setMinimum(1)
self.spinBox_distance_point.setMaximum(999999999)
self.spinBox_distance_point.setSingleStep(100)
self.spinBox_distance_point.setProperty("value", 1000)
self.spinBox_distance_point.setObjectName(_fromUtf8("spinBox_distance_point"))
self.horizontalLayout_8.addWidget(self.spinBox_distance_point)
self.label_distance_point = QtGui.QLabel(self.scrollAreaWidgetContents)
self.label_distance_point.setObjectName(_fromUtf8("label_distance_point"))
self.horizontalLayout_8.addWidget(self.label_distance_point)
self.formLayout.setLayout(2, QtGui.QFormLayout.FieldRole, self.horizontalLayout_8)
self.radioButton_extentMapCanvas = QtGui.QRadioButton(self.scrollAreaWidgetContents)
self.radioButton_extentMapCanvas.setText(_fromUtf8(""))
self.radioButton_extentMapCanvas.setObjectName(_fromUtf8("radioButton_extentMapCanvas"))
Expand Down Expand Up @@ -123,7 +136,7 @@ def setupUi(self, ui_quick_query):
self.horizontalLayout_5.addWidget(self.pushButton_refreshLayers)
self.formLayout.setLayout(5, QtGui.QFormLayout.FieldRole, self.horizontalLayout_5)
self.verticalLayout.addLayout(self.formLayout)
self.groupBox = QgsCollapsibleGroupBox(self.scrollAreaWidgetContents)
self.groupBox = gui.QgsCollapsibleGroupBox(self.scrollAreaWidgetContents)
self.groupBox.setChecked(False)
self.groupBox.setObjectName(_fromUtf8("groupBox"))
self.verticalLayout_2 = QtGui.QVBoxLayout(self.groupBox)
Expand Down Expand Up @@ -261,8 +274,8 @@ def retranslateUi(self, ui_quick_query):
self.pushButton_mapFeatures.setText(_translate("ui_quick_query", "Help with key/value", None))
self.label.setText(_translate("ui_quick_query", "Key", None))
self.label_2.setText(_translate("ui_quick_query", "Value", None))
self.label_3.setText(_translate("ui_quick_query", "In", None))
self.lineEdit_nominatim.setPlaceholderText(_translate("ui_quick_query", "A village, a town, ...", None))
self.label_distance_point.setText(_translate("ui_quick_query", "m", None))
self.label_13.setText(_translate("ui_quick_query", "Extent of the map canvas", None))
self.label_15.setText(_translate("ui_quick_query", "Extent of a layer", None))
self.groupBox.setTitle(_translate("ui_quick_query", "Advanced", None))
Expand All @@ -282,5 +295,5 @@ def retranslateUi(self, ui_quick_query):
self.pushButton_runQuery.setText(_translate("ui_quick_query", "Run query", None))
self.label_progress.setText(_translate("ui_quick_query", "progress text", None))

from qgis.gui import QgsCollapsibleGroupBox
from qgis import gui
from QuickOSM import resources_rc
65 changes: 44 additions & 21 deletions ui/quick_query.ui
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>604</width>
<height>866</height>
<width>644</width>
<height>805</height>
</rect>
</property>
<property name="windowTitle">
Expand All @@ -24,8 +24,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>590</width>
<height>852</height>
<width>630</width>
<height>791</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
Expand Down Expand Up @@ -112,13 +112,6 @@
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>In</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QRadioButton" name="radioButton_place">
<property name="text">
<string/>
Expand All @@ -128,15 +121,45 @@
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="lineEdit_nominatim">
<property name="text">
<string/>
</property>
<property name="placeholderText">
<string>A village, a town, ...</string>
</property>
</widget>
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_8">
<item>
<widget class="QComboBox" name="comboBox_in_around"/>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_nominatim">
<property name="text">
<string/>
</property>
<property name="placeholderText">
<string>A village, a town, ...</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="spinBox_distance_point">
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>999999999</number>
</property>
<property name="singleStep">
<number>100</number>
</property>
<property name="value">
<number>1000</number>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_distance_point">
<property name="text">
<string>m</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="4" column="0">
<widget class="QRadioButton" name="radioButton_extentMapCanvas">
Expand Down Expand Up @@ -465,7 +488,7 @@
<customwidget>
<class>QgsCollapsibleGroupBox</class>
<extends>QGroupBox</extends>
<header>qgis/gui.h</header>
<header>qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
</customwidgets>
Expand Down

0 comments on commit 3ed3d7e

Please sign in to comment.