# Path: plenario/api/common.py
# RESPONSE_LIMIT = 1000
# CACHE_TIMEOUT = 60 * 60 * 6
# def unknown_object_json_handler(obj):
# def date_json_handler(obj):
# def crossdomain(origin=None, methods=None, headers=None,
#                 max_age=21600, attach_to_all=True,
#                 automatic_options=True):  # pragma: no cover
#     def get_methods():
#     def decorator(f):
#         def wrapped_function(*args, **kwargs):
# def make_cache_key(*args, **kwargs):
# def make_csv(data):
# def extract_first_geometry_fragment(geojson):
# def make_fragment_str(geojson_fragment, buffer=100):
# 
# Path: plenario/api/response.py
# def make_error(msg, status_code, arguments=None):
# 
#     if not arguments:
#         arguments = request.args
# 
#     resp = {
#         'meta': {
#             'status': 'error',
#             'message': msg,
#             'query': arguments
#         },
#         'objects': [],
#     }
# 
#     response = jsonify(resp)
#     response.status_code = status_code
#     return response
# 
# Path: plenario/database.py
# def create_database(bind: Engine, database: str) -> None:
# def drop_database(bind: Engine, database: str) -> None:
# def create_extension(bind: Engine, extension: str) -> None:
# def psql(path: str) -> None:
# def postgres_session_context():
# def redshift_session_context():
# 
# Path: plenario/utils/helpers.py
# def get_size_in_degrees(meters, latitude):
#     earth_circumference = 40041000.0  # meters, average circumference
#     degrees_per_meter = 360.0 / earth_circumference
# 
#     degrees_at_equator = meters * degrees_per_meter
# 
#     latitude_correction = 1.0 / math.cos(latitude * (math.pi / 180.0))
# 
#     degrees_x = degrees_at_equator * latitude_correction
#     degrees_y = degrees_at_equator
# 
#     return degrees_x, degrees_y


import json
import shapely.geometry
import shapely.wkb
import sqlalchemy as sa
from flask import jsonify, make_response, request
from sqlalchemy import Table, func
from sqlalchemy.exc import SQLAlchemyError
from plenario.api.common import CACHE_TIMEOUT, RESPONSE_LIMIT, cache, crossdomain, date_json_handler, make_cache_key
from plenario.api.response import make_error
from plenario.database import postgres_base, postgres_engine as engine, postgres_session
from plenario.utils.helpers import get_size_in_degrees
    from plenario.tasks import update_weather




@cache.cached(timeout=CACHE_TIMEOUT, key_prefix=make_cache_key)
@crossdomain(origin='*')
def weather_stations():
    raw_query_params = request.args.copy()

    stations_table = Table(
        'weather_stations',
        postgres_base.metadata,
        autoload=True,

# Path: plenario/api/common.py
# RESPONSE_LIMIT = 1000
# CACHE_TIMEOUT = 60 * 60 * 6
# def unknown_object_json_handler(obj):
# def date_json_handler(obj):
# def crossdomain(origin=None, methods=None, headers=None,
#                 max_age=21600, attach_to_all=True,
#                 automatic_options=True):  # pragma: no cover
#     def get_methods():
#     def decorator(f):
#         def wrapped_function(*args, **kwargs):
# def make_cache_key(*args, **kwargs):
# def make_csv(data):
# def extract_first_geometry_fragment(geojson):
# def make_fragment_str(geojson_fragment, buffer=100):
# 
# Path: plenario/api/response.py
# def make_error(msg, status_code, arguments=None):
# 
#     if not arguments:
#         arguments = request.args
# 
#     resp = {
#         'meta': {
#             'status': 'error',
#             'message': msg,
#             'query': arguments
#         },
#         'objects': [],
#     }
# 
#     response = jsonify(resp)
#     response.status_code = status_code
#     return response
# 
# Path: plenario/database.py
# def create_database(bind: Engine, database: str) -> None:
# def drop_database(bind: Engine, database: str) -> None:
# def create_extension(bind: Engine, extension: str) -> None:
# def psql(path: str) -> None:
# def postgres_session_context():
# def redshift_session_context():
# 
# Path: plenario/utils/helpers.py
# def get_size_in_degrees(meters, latitude):
#     earth_circumference = 40041000.0  # meters, average circumference
#     degrees_per_meter = 360.0 / earth_circumference
# 
#     degrees_at_equator = meters * degrees_per_meter
# 
#     latitude_correction = 1.0 / math.cos(latitude * (math.pi / 180.0))
# 
#     degrees_x = degrees_at_equator * latitude_correction
#     degrees_y = degrees_at_equator
# 
#     return degrees_x, degrees_y


# Path: plenario/api/sensor.py
import json
import shapely.geometry
import shapely.wkb
import sqlalchemy as sa
from flask import jsonify, make_response, request
from sqlalchemy import Table, func
from sqlalchemy.exc import SQLAlchemyError
from plenario.api.common import CACHE_TIMEOUT, RESPONSE_LIMIT, cache, crossdomain, date_json_handler, make_cache_key
from plenario.api.response import make_error
from plenario.database import postgres_base, postgres_engine as engine, postgres_session
from plenario.utils.helpers import get_size_in_degrees
    from plenario.tasks import update_weather





@cache.cached(timeout=CACHE_TIMEOUT, key_prefix=make_cache_key)
@crossdomain(origin='*')
def weather_stations():
    raw_query_params = request.args.copy()

    stations_table = Table(
        'weather_stations',
        postgres_base.metadata,
        autoload=True,

        autoload_with=engine,