#Initial setting coding: utf-8 layer = iface.activeLayer() #Data to be filled in manually, based on fisher sightings (example in bold). dia=1 #Inserting sighting day (Note: it does not accept leading zeros, i. e. dates 01 to 09 must be typed only as 1... 9) hora=str('12:13') #Inserting the sighting time (note: it must be enclosed in single quotes, be careful not to erase the quotes!) idponto1=142 #Using the row number from the excel worksheet closest to the sighting time idponto2=idponto1+1 t0=43980 #Inserting sighting time converted to seconds (multiply by 86400) sp=str('Caretta caretta') #Inserting species name. It must be enclosed in single quotes, be careful not to delete the quotes! n=1 #Inserting the sighting number #1. Selecting two points between the sighting. For example: sighting time is 8:33 (named as p3), and shoul be selected time points at 8:30 (p1) and 8:35 (p2) recorded. #Selection of two points layer.select(idponto1) for f in layer.selectedFeatures(): xa = f['X'] ya = f['Y'] ta = f['segundos'] layer.removeSelection() layer.select(idponto2) for f in layer.selectedFeatures(): xb = f['X'] yb = f['Y'] tb = f['segundos'] layer.removeSelection() point1 = QgsPointXY(xa,ya) point2 = QgsPointXY(xb,yb) #2. Calculating distance between two points (p1 and p2) in meters distance = QgsDistanceArea() m = distance.measureLine(point1, point2) print ("distancia:", m) #3. Calculating the time, in seconds, between two points (p1 and p2). The tracker was configured to use a 5-minute interval, however, due to data acquisition instability, location acquisitions can vary t= tb-ta print ("segundos:", t) #4. Using the average velocity formula (Vm=ΔD/ΔT) between two points vm=m/t print ("velocidade media", vm) #5. Creating a line between two points (p1 e p2) point1 = QgsPoint(xa,ya) point2 = QgsPoint(xb,yb) c = QgsVectorLayer('LineString?crs=epsg:4326&field=gid:integer', 'linha', 'memory') if c != None and c.isValid() : c.setProviderEncoding('UTF-8') caps = c.dataProvider().capabilities() #capabilities returns the capabilities (functionalities) available in the layer #Checking if it is possible to add features into the layer if caps and QgsVectorDataProvider.AddFeatures : feicao = QgsFeature(c.fields()) #Creating first feature feicao.setAttribute("gid",1); geom = QgsGeometry.fromPolyline([point1,point2]) feicao.setGeometry(geom) #Including the geometry into the feature #addFeatures returns two values: #First is True if features were added #Second is an object list with the added features (res, resFeicoes) = c.dataProvider().addFeatures([feicao]) QgsProject.instance().addMapLayers([c]) #Adding the created layer to the canvas #6. Inserting a new time (difference between the previous time and the sighting time), in seconds. Example: p3 – p1 = 3 min = 180 s. t=t0-ta #7. Calculating distance until the indicated time, using previously average speed calculated. d0=vm*t print ("distancia ponto:", d0) point1 = QgsPointXY(xa,ya) point2 = QgsPointXY(xb,yb) #8. Creating a buffer with the distance until the indicated time. b = QgsVectorLayer('LineString?crs=epsg:4326&field=gid:integer', 'buffer', 'memory') if b != None and c.isValid() : b.setProviderEncoding('UTF-8') caps = b.dataProvider().capabilities() #capabilities returns the capabilities (functionalities) available in the layer #Checking if it is possible to add features into the layer if caps and QgsVectorDataProvider.AddFeatures : feicao = QgsFeature(b.fields()) #Creating first feature feicao.setAttribute("gid",1); geome = QgsGeometry.fromPointXY(point1).buffer(d0,0) geometry = QgsGeometry.asQPolygonF(geome) g = QgsGeometry.createPolylineFromQPolygonF(geometry) ge = QgsGeometry.fromPolylineXY(g) feicao.setGeometry(ge) #Including the geometry into the feature #addFeatures returns two values: #First is True if features were added #Second is an object list with the added features (res, resFeicoes) = b.dataProvider().addFeatures([feicao]) QgsProject.instance().addMapLayers([b]) #Adding the created layer to the canvas #9. Making a line intersection between the buffer and the distance line and creating a point at this intersection (p3). # Creating a line between the points a = QgsVectorLayer('Point?crs=epsg:4326&field=gid:integer&field=especie:string&field=data:integer&field=hora:string', 'ponto {}'.format(n), 'memory') if a != None and a.isValid() : a.setProviderEncoding('UTF-8') caps = a.dataProvider().capabilities() #capabilities returns the capabilities (functionalities) available in the layer #Checking if it is possible to add features into the layer if caps and QgsVectorDataProvider.AddFeatures : feicao = QgsFeature(a.fields()) #Creating first feature feicao.setAttribute("gid",n); feicao.setAttribute("especie",sp); feicao.setAttribute("data",dia); feicao.setAttribute("hora",hora); p1 = QgsGeometry.intersection(geom,ge) feicao.setGeometry(p1) #Including the geometry into the feature #addFeatures returns two values: #First is True if features were added #Second is an object list with the added features (res, resFeicoes) = a.dataProvider().addFeatures([feicao]) #adiciona a camada criada na tela QgsProject.instance().addMapLayers([a]) QgsProject.instance().removeMapLayer(b) QgsProject.instance().removeMapLayer(c)