From b89021b71d288f7b5e62308b354a6adc954f8d2c Mon Sep 17 00:00:00 2001 From: qsavoye <88184311+qsavoye@users.noreply.github.com> Date: Tue, 9 Jul 2024 18:48:16 +0200 Subject: [PATCH] Add files via upload --- README.md | 3 ++ icon.svg | 36 ++++++++++++++ main.qml | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++ metadata.txt | 7 +++ 4 files changed, 184 insertions(+) create mode 100644 README.md create mode 100644 icon.svg create mode 100644 main.qml create mode 100644 metadata.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..4755cbf --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# QField Geocoder Addresses Plugin + +This [QField](https://github.com/opengisch/QField/) plugin search addresses and center to current map. diff --git a/icon.svg b/icon.svg new file mode 100644 index 0000000..6bdfa95 --- /dev/null +++ b/icon.svg @@ -0,0 +1,36 @@ + + + + + + \ No newline at end of file diff --git a/main.qml b/main.qml new file mode 100644 index 0000000..eca62e6 --- /dev/null +++ b/main.qml @@ -0,0 +1,138 @@ +import QtQuick +import QtQuick.Controls + +import org.qfield +import org.qgis +import Theme + +import "qrc:/qml" as QFieldItems + +Item { + id: plugin + + property var mainWindow: iface.mainWindow() + property var mapCanvas: iface.findItemByObjectName('mapCanvas') + property var overlayFeatureFormDrawer: iface.findItemByObjectName('overlayFeatureFormDrawer') + property var locatorItem: iface.mainWindow().contentItem.children[4].children[9] + property var searchFieldRect: iface.mainWindow().contentItem.children[4].children[9].children[0] + property var resultBox: iface.mainWindow().contentItem.children[4].children[9].children[5] + property var geommetryHightLigth: iface.findItemByObjectName('mapCanvas').children[15] + + Component.onCompleted: { + locatorAddressItem.parent = locatorItem + } + + Rectangle { + id: locatorAddressItem + z: 1 + width: searchFieldRect.width - 24 + height: searchFieldRect.visible ? 30+32 : 0 + anchors.top: resultBox.bottom + anchors.left: searchFieldRect.left + anchors.topMargin: resultBox.height == 0 ? 24 : 0 + color: Theme.mainBackgroundColor + visible: searchFieldRect.visible && searchFieldRect.children[0].displayText !== '' + clip: true + + Rectangle { + id:rectTitle + height:30 + width:parent.width + anchors.top:parent.top + color: Theme.mainColor + opacity: 0.95 + + Text { + id: nameCell + anchors.left: parent.left + anchors.right: parent.right + text: 'Search for location address' + leftPadding: 5 + topPadding: 8 + font.bold: false + font.pointSize: Theme.resultFont.pointSize + color: "white" + elide: Text.ElideRight + horizontalAlignment: Text.AlignLeft + } + } + Rectangle { + anchors.left: parent.left + anchors.top: rectTitle.bottom + height:32 + width:parent.width + color: Theme.mainBackgroundColor + + Text { + id: addressCell + anchors.left: parent.left + anchors.right: parent.right + text: 'Got to '+ searchFieldRect.children[0].displayText + ' location' + leftPadding: 5 + topPadding: 8 + font.bold: false + font.pointSize: Theme.resultFont.pointSize + color: Theme.mainTextColor + } + } + + MouseArea { + id: mouseArea + anchors.left: parent.left + anchors.top: parent.top + anchors.bottom: parent.bottom + anchors.right: parent.right + + onClicked: { + fetchGeocoder(searchFieldRect.children[0].displayText) + } + } + } + + function fetchGeocoder(location) + { + let request = new XMLHttpRequest(); + + request.onreadystatechange = function() { + if (request.readyState === XMLHttpRequest.DONE) + { + var responseObject = JSON.parse(request.response) + for (const obj of responseObject) { + var displayPoint = GeometryUtils.reprojectPoint(GeometryUtils.point(obj.lon, obj.lat), CoordinateReferenceSystemUtils.wgs84Crs(), mapCanvas.mapSettings.destinationCrs); + // mapCanvas.mapSettings.setCenter(displayPoint) + var temp = [GeometryUtils.reprojectPoint(GeometryUtils.point(obj.boundingbox[2], obj.boundingbox[0]), CoordinateReferenceSystemUtils.wgs84Crs(), mapCanvas.mapSettings.destinationCrs), + GeometryUtils.reprojectPoint(GeometryUtils.point(obj.boundingbox[3], obj.boundingbox[1]), CoordinateReferenceSystemUtils.wgs84Crs(), mapCanvas.mapSettings.destinationCrs) + ] + mapCanvas.mapSettings.setExtentFromPoints(temp, 125, true) + locatorItem.state = 'off' + + if (obj.geojson && obj.geojson.type == 'Polygon') + { + let str = 'POLYGON ((' + for (const coord of obj.geojson.coordinates[0]) { + var point = GeometryUtils.reprojectPoint(GeometryUtils.point(coord[0], coord[1]), CoordinateReferenceSystemUtils.wgs84Crs(), mapCanvas.mapSettings.destinationCrs); + str += point.x +' ' + point.y + ', ' + } + str = str.substring(0, str.length - 2) + str += '))' + let geom = GeometryUtils.createGeometryFromWkt(str) + geommetryHightLigth.geometryWrapper.qgsGeometry = geom + geommetryHightLigth.geometryWrapper.crs = mapCanvas.mapSettings.destinationCrs + } else { + let str = 'POINT (' + displayPoint.x + ' ' + displayPoint.y+')' + let geom = GeometryUtils.createGeometryFromWkt(str) + geommetryHightLigth.geometryWrapper.qgsGeometry = geom + geommetryHightLigth.geometryWrapper.crs = mapCanvas.mapSettings.destinationCrs + } + mainWindow.displayToast(obj.display_name) + break + + } + +} +} +request.open("GET", "https://nominatim.openstreetmap.org/search.php?q="+location+"&polygon_geojson=1&format=jsonv2") +request.setRequestHeader('User-Agent', 'FAKE-USER-AGENT'); +request.send(); +} +} \ No newline at end of file diff --git a/metadata.txt b/metadata.txt new file mode 100644 index 0000000..b75f0b2 --- /dev/null +++ b/metadata.txt @@ -0,0 +1,7 @@ +[general] +name=Address Geocoder using nominatim +description=Search addresses in search bar. +author=Quentin Savoye +icon=icon.svg +version=1.0 +homepage=https://github.com/qsavoye