In [1]:
from algorithms.db import getPositions, normalize, deletePositions
from algorithms.position import Position
from algorithms import consolidation as cs
import matplotlib as mp
%pylab inline
Nos conectamos a la base de datos:
In [21]:
dbname = "bahia"
#recurso = "tetra:12082781"
recurso = "tetra:12082364"
limit = 2000
cmd = "SELECT id, recurso, latitud, longitud, velocidad, orientacion, UNIX_TIMESTAMP(fecha) FROM posicionesgps WHERE latitud <> 0 AND longitud <> 0 AND recurso='{0}' ORDER BY 7 ASC LIMIT {1};"
In [22]:
data = getPositions(dbname, cmd, recurso, limit)
Tipificamos las variables:
In [23]:
[normalData, normalLats, normalLongs] = normalize(data)
Consolidación por distancia:
In [39]:
result = cs.ConsolidationByDistance(normalData, 0, 0.1, None)
#result = cs.ConsolidationByTime(normalData, 20)
#result = cs.ConsolidationByThinning(normalData, 2, 5)
resultLats = []
resultLongs = []
borrarLats = []
borrarLongs = []
for p in result:
resultLats.append(p.lat)
resultLongs.append(p.lon)
borrar = [x for x in normalData if x not in result]
for p in borrar:
borrarLats.append(p.lat)
borrarLongs.append(p.lon)
In [40]:
plt.figure(1, figsize=(18,8))
plt.subplot(121)
ax = plt.gca()
ax.grid(True)
plt.plot(borrarLats, borrarLongs, 'ro');
plt.subplot(122)
ax = plt.gca()
ax.grid(True)
plt.plot(resultLats, resultLongs, 'bo');
In [41]:
print "De {0} posiciones se mantienen {1}".format(str(len(normalData)), str(len(result)))
plt.figure(1, figsize=(18,8))
ax = plt.gca()
ax.grid(True)
plt.plot(borrarLats, borrarLongs, 'ro',resultLats, resultLongs, 'bo');
Borrado de posiciones:
In [42]:
print "Se borran {0} posiciones".format(str(len(borrar)))
In [43]:
deletePositions("bahia_test", borrar);
In [ ]: