Skip to content

Commit

Permalink
Initial file drop
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn authored Jun 18, 2024
1 parent fa64c80 commit d03dbc5
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
4 changes: 4 additions & 0 deletions icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
110 changes: 110 additions & 0 deletions main.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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 positionSource: iface.findItemByObjectName('positionSource')
property var dashBoard: iface.findItemByObjectName('dashBoard')
property var overlayFeatureFormDrawer: iface.findItemByObjectName('overlayFeatureFormDrawer')

Component.onCompleted: {
iface.addItemToPluginsToolbar(snapButton)
}

Loader {
id: cameraLoader
active: false
sourceComponent: Component {
id: cameraComponent

QFieldItems.QFieldCamera {
id: qfieldCamera
visible: false

Component.onCompleted: {
open()
}

onFinished: (path) => {
close()
snap(path)
}

onCanceled: {
close()
}

onClosed: {
cameraLoader.active = false
}
}
}
}

QfToolButton {
id: snapButton
bgcolor: Theme.darkGray
iconSource: Theme.getThemeVectorIcon('ic_camera_photo_black_24dp')
iconColor: Theme.mainColor
round: true

onClicked: {
dashBoard.ensureEditableLayerSelected()

if (!positionSource.active || !positionSource.positionInformation.latitudeValid || !positionSource.positionInformation.longitudeValid) {
mainWindow.displayToast(qsTr('Snap requires positioning to be active and returning a valid position'))
return
}

if (dashBoard.activeLayer.geometryType() != Qgis.GeometryType.Point) {
mainWindow.displayToast(qsTr('Snap requires the active vector layer to be a point geometry'))
return
}

let fieldNames = dashBoard.activeLayer.fields.names
if (fieldNames.indexOf('photo') == -1 && fieldNames.indexOf('picture') == -1) {
mainWindow.displayToast(qsTr('Snap requires the active vector layer to contain a field named \'photo\' or \'picture\''))
return
}

cameraLoader.active = true
}
}

function snap(path) {
let today = new Date()
let relativePath = 'DCIM/' + today.getFullYear()
+ (today.getMonth() +1 ).toString().padStart(2,0)
+ today.getDate().toString().padStart(2,0)
+ today.getHours().toString().padStart(2,0)
+ today.getMinutes().toString().padStart(2,0)
+ today.getSeconds().toString().padStart(2,0)
+ '.' + FileUtils.fileSuffix(path)
platformUtilities.renameFile(path, qgisProject.homePath + '/' + relativePath)

let pos = positionSource.projectedPosition
let wkt = 'POINT(' + pos.x + ' ' + pos.y + ')'

let geometry = GeometryUtils.createGeometryFromWkt(wkt)
let feature = FeatureUtils.createFeature(dashBoard.activeLayer, geometry)

let fieldNames = feature.fields.names
if (fieldNames.indexOf('photo') > -1) {
feature.setAttribute(fieldNames.indexOf('photo'), relativePath)
} else if (fieldNames.indexOf('picture') > -1) {
feature.setAttribute(fieldNames.indexOf('picture'), relativePath)
}

overlayFeatureFormDrawer.featureModel.feature = feature
overlayFeatureFormDrawer.state = 'Add'
overlayFeatureFormDrawer.open()
}
}
7 changes: 7 additions & 0 deletions metadata.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[general]
name=Snap!
description=Digitize points through snapping photos
author=OPENGIS.ch
icon=icon.svg
version=1.0
homepage=https://www.opengis.ch/

0 comments on commit d03dbc5

Please sign in to comment.