forked from LEOXINGU/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fotos_track.py
202 lines (178 loc) · 7.02 KB
/
fotos_track.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"""
/***************************************************************************
3 CGEO
3th Brazilian Geoinformation Center
-------------------
begin : 2017-06-14
copyright : (C) 2017 by Leandro Franca - Cartographic Engineer @ Brazilian Army
email : franca.leandro@eb.mil.br
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation. *
* *
***************************************************************************/
"""
# Fotos Nao Geo para Geo
##3. Fotos com Track=name
##LF3) GPS, Fotos e Midias=group
##Pasta_com_fotos=folder
##Arquivo_GPX=file
##Shapefile_de_Fotos=output vector
import PIL.Image, PIL.ExifTags
from qgis.gui import QgsMessageBar
from qgis.utils import iface
from qgis.core import *
import time
import os
import datetime
import xml.dom.minidom
from PyQt4.QtCore import *
import re
import numpy as np
pasta = Pasta_com_fotos
sourceGPXPath = Arquivo_GPX
saida = Shapefile_de_Fotos
os.chdir(str(pasta))
lista = os.listdir(pasta)
tam = len(lista)
# Funcao para gerar o padrao data-hora
def data_hora(texto):
data_hora = texto.replace(' ',':')
data_hora = data_hora.split(':')
ano = int(data_hora[0])
mes = int(data_hora[1])
dia = int(data_hora[2])
hora = int(data_hora[3])
minuto = int(data_hora[4])
segundo = int(data_hora[5])
data_hora = datetime.datetime(ano, mes, dia, hora, minuto, segundo)
return data_hora
# Verificando imangens
progress.setInfo('Verificando imagens...<br/>')
imagens = []
for arquivo in lista:
if (arquivo[-3:]).lower() == 'jpg':
img = PIL.Image.open(arquivo)
if img._getexif():
exif = {
PIL.ExifTags.TAGS[k]: v
for k, v in img._getexif().items()
if k in PIL.ExifTags.TAGS
}
else:
exif = {}
date_time = None
if 'DateTimeOriginal' in exif:
date_time = data_hora(exif['DateTimeOriginal'])
imagens += [[arquivo, date_time]]
elif 'DateTime' in exif:
date_time = data_hora(exif['DateTimeOriginal'])
imagens += [[arquivo, date_time]]
class TrackPoint( object ):
def __init__(self, lat, lon, elev, time):
self.lat = float(lat)
self.lon = float(lon)
self.time = time
self.elev = float(elev)
# CRIAR SHAPEFILE
# Criar campos
fields = QgsFields()
fields.append(QgsField('id', QVariant.Int))
fields.append(QgsField('nome', QVariant.String))
fields.append(QgsField('date_time', QVariant.String))
fields.append(QgsField('direcao', QVariant.Int))
# Criando o shapefile
encoding = 'utf-8'
formato = 'ESRI Shapefile'
crs_epsg =4326
crs = QgsCoordinateReferenceSystem()
crs.createFromSrid(crs_epsg)
writer = QgsVectorFileWriter(saida, encoding, fields, QGis.WKBPoint, crs, formato)
# LER ARQUIVO GPX
progress.setInfo('Lendo arquivo GPX...<br/>')
LISTA_TRACK = []
doc = xml.dom.minidom.parse( sourceGPXPath )
track_n=0
# reading GPX file
for trk_node in doc.getElementsByTagName( 'trk'):
track_n = track_n+1
# v1.0
trkname = 'noname'
trknameNodeList = trk_node.getElementsByTagName( 'name' )
if trknameNodeList.length != 0:
trkname = trk_node.getElementsByTagName( 'name' )[0].firstChild.data
trksegments = trk_node.getElementsByTagName( 'trkseg' )
points = []
# for each segment in GPX ...
for trksegment in trksegments:
trk_pts = trksegment.getElementsByTagName( 'trkpt' )
# ... read each track point
for tkr_pt in trk_pts:
# get latitude and longitude
ptLat =tkr_pt.getAttribute("lat")
ptLon = tkr_pt.getAttribute("lon")
ptEle = None
ptTime = None
# get elevation
if len(tkr_pt.getElementsByTagName("ele")) != 0:
if len(tkr_pt.getElementsByTagName("ele")[0].childNodes) != 0:
ptEle = tkr_pt.getElementsByTagName("ele")[0].childNodes[0].data
# get date and time
if len(tkr_pt.getElementsByTagName("time")) != 0:
if len(tkr_pt.getElementsByTagName("time")[0].childNodes) != 0:
ptTime = tkr_pt.getElementsByTagName("time")[0].childNodes[0].data
ptTime = re.sub(r"(\d{2}):(\d{2}):(\d{2})Z",r"\1:\2:\3.000Z",ptTime)
ptTime = datetime.datetime.strptime(ptTime, "%Y-%m-%dT%H:%M:%S.%fZ") -datetime.timedelta(hours=3)
# add point to array
points.append(TrackPoint(ptLat, ptLon, ptEle, ptTime))
# Adicionando pontos a lista
if ptTime:
for ponto in points:
pnt = QgsGeometry.fromPoint(QgsPoint(ponto.lon, ponto.lat))
date_time = ponto.time
LISTA_TRACK +=[[pnt, date_time]]
# Verificar pontos pela proximidade temporal
progress.setInfo('Relacionando data - hora...<br/>')
tam = len(LISTA_TRACK)
feat = QgsFeature()
cont = 0
for img in imagens:
nome = img[0]
DT_img = img[1]
sentinela = False
for ind in range(tam-2):
DT1 = LISTA_TRACK[ind][1]
DT2 = LISTA_TRACK[ind+1][1]
difer1 = (DT_img - DT1).total_seconds()
difer2 = (DT_img - DT2).total_seconds()
if (difer1>=0 and difer2<0):
sentinela = True
if difer1 == 0:
cont +=1
geom = LISTA_TRACK[ind][0]
feat.setGeometry(geom)
att = [cont, nome, str(DT_img), None]
feat.setAttributes(att)
writer.addFeature(feat)
else:
cont +=1
# Fazer interpolacao entre os dois pontos
P1 = np.array([LISTA_TRACK[ind][0].asPoint()[0], LISTA_TRACK[ind][0].asPoint()[1]])
P2 = np.array([LISTA_TRACK[ind+1][0].asPoint()[0], LISTA_TRACK[ind+1][0].asPoint()[1]])
detaT = (DT2-DT1).total_seconds()
Px = P1 + (difer1/detaT)*(P2-P1)
geom = QgsGeometry.fromPoint(QgsPoint(Px[0], Px[1]))
feat.setGeometry(geom)
att = [cont, nome, str(DT_img), None]
feat.setAttributes(att)
ok = writer.addFeature(feat)
if not sentinela:
progress.setInfo('O arquivo %s nao esta dentro do track!<br/>' %nome)
del writer
progress.setInfo('<b>Operacao concluida!</b><br/><br/>')
progress.setInfo('<b>Leandro França - Eng Cart</b><br/>')
iface.messageBar().pushMessage(u'Situacao', "Operacao Concluida com Sucesso!", level=QgsMessageBar.INFO, duration=5)
time.sleep(3)