In [1]:
from position import Position
from db import connect_db
from djCluster import DjCluster

import matplotlib.pyplot as plt
from datetime import datetime
%pylab inline


Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: ['datetime']
`%matplotlib` prevents importing * from pylab and numpy

In [2]:
cur_sal = connect_db("bahia")

limit = 2000
cmd = "SELECT * FROM posicionesgps WHERE latitud<>0 AND longitud<> 0 AND recurso='tetra:12082781' LIMIT {0};".format(limit)
cur_sal.execute(cmd)


Out[2]:
2000L

In [3]:
list_pos = []
for row in cur_sal.fetchall():
    q = Position(row[0] # id
               , row[2] # resource
               , row[3] # lat
               , row[4] # lon
               , row[5] # speed
               , row[6] # track
               , row[10] # date
               )
    list_pos.append(q)
print len(list_pos)


2000

In [15]:
listPosTyp = []
lats = []
longs = []

# Calculamos media lat
for pos in list_pos:
    lats.append(pos.lat)
meanLat = np.mean(lats)
# Calculamos media lon 
for pos in list_pos:
    longs.append(pos.lon)
meanLon = np.mean(longs)
# Calculamos desv lat
devLat = np.std(lats)
# Calculamos desv lon
devLon = np.std(longs)

latsTyp = []
longsTyp = []
for pos in list_pos:
    q = Position(pos.id
        , pos.resource
        , (pos.lat - meanLat)/devLat
        , (pos.lon - meanLon)/devLon
        , pos.speed
        , pos.track
        , pos.date
        )
    listPosTyp.append(q)
    latsTyp.append(q.lat)
    longsTyp.append(q.lon)

In [16]:
from djCluster import DjCluster

In [8]:
[result, noises] = DjCluster(listPosTyp, 0, 0.001, 1, None)

In [27]:
print len(result)


412

In [20]:
resultLat = []
resultLong = []
for clus in result:
    resultLat.append(clus.center.lat)
    resultLong.append(clus.center.lon)
    
noisesLat = []
noisesLong = []
for point in noises:
    noisesLat.append(point.lat)
    noisesLong.append(point.lon)

In [30]:
plt.figure(1, figsize=(15,10))
plt.subplot(111)
ax = plt.gca()
ax.grid(True)
plt.plot(latsTyp, longsTyp, 'ro', resultLat, resultLong, 'bo' noisesLat, noisesLong, 'bx');


  File "<ipython-input-30-7198b348a326>", line 5
    plt.plot(latsTyp, longsTyp, 'ro', resultLat, resultLong, 'bo' noisesLat, noisesLong, 'bx');
                                                                          ^
SyntaxError: invalid syntax

In [25]:
plt.figure(1, figsize=(15,10))
plt.subplot(111)
ax = plt.gca()
ax.grid(True)
plt.plot(latsTyp, longsTyp, 'ro');



In [ ]: