forked from Remi-C/interactive_map_tracking
/
imt_tools.py
276 lines (209 loc) · 8.29 KB
/
imt_tools.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# -*- coding: utf-8 -*-
"""
/***************************************************************************
interactive map tracking
A QGIS plugin
Tools for Interactive Map Tools
-------------------
begin : 2015-02-10
git sha : $Format:%H$
copyright : (C) 2015 by IGN
email : lionel.atty@ign.fr
***************************************************************************/
/***************************************************************************
* *
* 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; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
__author__ = 'latty'
from qgis.core import *
import os
import socket
defaultQtDateFormatString = "yyyy-MM-ddThh:mm:ss.zzz"
#
if os.name != "nt":
import fcntl
import struct
def get_interface_ip(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
def get_lan_ip():
""" Finding the ip lan address (OS independant)
url: http://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib#
:return: String of the IP
:rtype: str
"""
ip = socket.gethostbyname(socket.gethostname())
if ip.startswith("127.") and os.name != "nt":
interfaces = ["eth0","eth1","eth2","wlan0","wlan1","wifi0","ath0","ath1","ppp0"]
for ifname in interfaces:
try:
ip = get_interface_ip(ifname)
break;
except IOError:
pass
return ip
def find_index_field_by_name(field_names, field_name):
""" Search a field (by name) in a list of fields.
:param field_names: list of field name
:type field_names: list[str]
:param field_name: field name
:type field_name: str
:return: If found return the index of the field in the list Else return -1
:rtype: int
"""
# find index for field 'user-id'
try:
id_for_user_id = field_names.index(field_name)
except ValueError:
# field non trouve !
return -1
return id_for_user_id
# url: http://stackoverflow.com/questions/117014/how-to-retrieve-name-of-current-windows-user-ad-or-local-using-python
def get_os_username():
"""
:return:
"""
import getpass
return getpass.getuser()
def get_timestamp():
import time
# Python time
return time.time()
def convert_timestamp_to_qdatetime(timestamp):
from PyQt4.QtCore import QDateTime
from math import modf
timestamp_frac, timestamp_whole = modf(timestamp)
# Qt time
qdatetime = QDateTime()
qdatetime.setTime_t(int(timestamp_whole))
qdatetime = qdatetime.addMSecs(int(timestamp_frac*1000))
#
return qdatetime
# def convert_timestamp_to_qt_string_format(timestamp, QtDateFormat):
# # String Qt time
# return convert_timestamp_to_qdatetime(timestamp).toString(QtDateFormat)
def convert_timestamp_to_qt_string_format(timestamp, QtDateFormatString=defaultQtDateFormatString):
# String Qt time
return convert_timestamp_to_qdatetime(timestamp).toString(QtDateFormatString)
# def get_timestamp_from_qt_string_format(QtDateFormat):
# """ Retrieve timestamp from the system and convert into a ISO QString/QDateTime format.
#
# urls:
# - https://docs.python.org/2/library/datetime.html
# - http://stackoverflow.com/questions/2935041/how-to-convert-from-timestamp-to-date-in-qt
# - http://stackoverflow.com/questions/3387655/safest-way-to-convert-float-to-integer-in-python
# - http://pyqt.sourceforge.net/Docs/PyQt4/qt.html -> Qt.DateFormat
# Qt.ISODate = 1 : ISO 8601 extended format: either YYYY-MM-DD for dates or YYYY-MM-DDTHH:mm:ss, YYYY-MM-DDTHH:mm:ssTZD
# (e.g., 1997-07-16T19:20:30+01:00) for combined dates and times.
#
# :param QtDateFormat: enum (value) for ISO conversion (from Qt C++ API) [default=1(=Qt.ISODate)]
# :type QtDateFormat: int
#
# :return: TimeStamp in ISO QString from QDateTime format.
# :rtype: QString
# """
# return convert_timestamp_to_qt_string_format(get_timestamp(), QtDateFormat)
def get_timestamp_from_qt_string_format(QtDateFormatString=defaultQtDateFormatString):
return convert_timestamp_to_qt_string_format(get_timestamp(), QtDateFormatString)
def construct_listpoints_from_extent(_extent):
""" Construct a list of QGIS points from QGIS extent.
:param _extent: QGIS extent
:type _extent: QgsRectangle
:return: List of QGIS points
:rtype: list[QgsPoint]
"""
x1 = _extent.xMinimum()
y1 = _extent.yMinimum()
x3 = _extent.xMaximum()
y3 = _extent.yMaximum()
x2 = x1
y2 = y3
x4 = x3
y4 = y1
return [QgsPoint(x1, y1),
QgsPoint(x2, y2),
QgsPoint(x3, y3),
QgsPoint(x4, y4),
QgsPoint(x1, y1)]
def find_layer_in_qgis_legend_interface(_iface, _layername):
"""
:param _iface:
:param _layername:
:return:
"""
try:
layer_searched = [layer_searched
for layer_searched in _iface.legendInterface().layers()
if layer_searched.name() == _layername]
return layer_searched[0]
except:
return None
import time
class TpTimer:
def __init__(self):
self.currentTime = self.default_timers()
self.dict_process_timeupdate = {}
self.dict_process_delay = {}
@staticmethod
def default_timers():
return [time.time(), time.time()]
@staticmethod
def default_delay():
return 0.0
def get_current_time(self):
self.update_current_time()
return self.currentTime[0]
def __getitem__(self, key):
return self.dict_process_timeupdate.setdefault(key, self.default_timers())
def update_current_time(self):
self.currentTime = [time.time(), self.currentTime[0]]
def delta(self):
return self.currentTime[0] - self.currentTime[1]
def delta(self, key):
list_times = self.__getitem__(key)
return list_times[0] - list_times[1]
def delta_with_current_time(self, key):
self.update_current_time()
list_times = self.__getitem__(key)
return self.currentTime[0] - list_times[0]
def update(self, key):
self.update_current_time()
list_times = self.__getitem__(key)
self.dict_process_timeupdate[key] = [self.currentTime[0], list_times[0]]
return self.dict_process_timeupdate[key]
def get_delay(self, process_name):
return self.dict_process_delay.setdefault(process_name, self.default_delay())
def set_delay(self, delay_name, time_delay):
self.dict_process_delay[delay_name] = time_delay
def is_time_to_update(self, process_name, delay_name):
return self.delta_with_current_time(process_name) >= self.get_delay(delay_name)
import shlex
from subprocess import call, PIPE, STDOUT
def get_return_code_of_simple_cmd(cmd, stderr=STDOUT):
"""Execute a simple external command and return its exit status."""
args = shlex.split(cmd)
return call(args, stdout=PIPE, stderr=stderr)
def is_network_alive(url="www.google.com"):
#cmd = "ping -c 1 " + url
cmd = "curl --output /dev/null --silent --head --fail" + url
return get_return_code_of_simple_cmd(cmd) == 0
from PyQt4.QtNetwork import QTcpSocket
def isConnected(url):
socket = QTcpSocket()
socket.connectToHost(url, 80)
return socket.waitForConnected(1000)
DEFAULT_SEGMENT_EPSILON = 1e-08
def extent_equal(r1, r2, epsilon=DEFAULT_SEGMENT_EPSILON):
return abs(r1.xMaximum() - r2.xMaximum()) <= epsilon and \
abs(r1.yMaximum() - r2.yMaximum()) <= epsilon and \
abs(r1.xMinimum() - r2.xMinimum()) <= epsilon and \
abs(r1.yMinimum() - r2.yMinimum()) <= epsilon