-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
executable file
·52 lines (45 loc) · 1.7 KB
/
utils.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from flask import Flask, request
from flask_restful import Resource, Api
from sqlalchemy import create_engine
from json import dumps
from flask.ext.jsonpify import jsonify
from geojson import Feature, Point, FeatureCollection
from shapely.geometry import Point,mapping
import json
import requests
import psycopg2
from time import gmtime, strftime
import sys
print "Content-Type: text/plain;charset=utf-8"
print
app = Flask(__name__)
api = Api(app)
class Bicing(Resource):
def get(self,minuts):
conn = psycopg2.connect("dbname='dbname' user='username' host='localhost' password='yourpassword'")
cursor = conn.cursor()
query = cursor.execute("select lat,lon, extract(epoch from date_trunc('minute', hora))*1000,ABS(bikesdifference) from bicing where ABS(bikesdifference) !=0 AND extract(epoch from date_trunc('minute', hora))='%s'"%minuts )
rows = cursor.fetchall()
gjson_dict = {}
gjson_dict["type"] = "FeatureCollection"
feat_list = []
for row in rows:
type_dict = {}
pt_dict = {}
prop_dict = {}
type_dict["type"] = "Feature"
pt_dict["type"] = "Point"
type_dict["geometry"] = mapping(Point(row[1],row[0]))
prop_dict["hora"] = row[2]
prop_dict["bikes"] = row[3]
#prop_dict["minuts"] = row[4]
type_dict["properties"] = prop_dict
feat_list.append(type_dict)
gjson_dict["features"] = feat_list
result = {'data':[i for i in rows]}
return jsonify(gjson_dict)
api.add_resource(Bicing, '/bicing/<string:minuts>')
if __name__ == '__main__':
app.run(port='5002')