Skip to content

Commit

Permalink
Export all survey tables to CSV
Browse files Browse the repository at this point in the history
Saves separate CSVs to the 'exports' folder

Refs #384
  • Loading branch information
fungjj92 committed Jan 18, 2019
1 parent 4fa8aff commit 75a8053
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Script produced interim data
src/icp/pollinator/src/pollinator/reclass/*.json
src/icp/pollinator/src/pollinator/reclass/*.csv
src/icp/apps/beekeepers/exports/*

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
52 changes: 52 additions & 0 deletions src/icp/apps/beekeepers/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
import boto3
import rasterio
from pyproj import Proj
import psycopg2


PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
HOST = os.environ.get('ICP_DB_HOST')
DB_USER = os.environ.get('ICP_DB_USER')
DB_PW = os.environ.get('ICP_DB_PASSWORD')
DB_NAME = os.environ.get('ICP_DB_NAME')


def sample_at_point(geom, raster_path):
Expand Down Expand Up @@ -85,3 +93,47 @@ def __enter__(self):
def __exit__(self, *args):
"""Context manager close"""
self.remove_env_vars()


def export_survey_tables():
"""Export all types of beekeepers surveys to CSVs."""

db_options = "dbname={} user={} host={} password={}".format(
DB_NAME, DB_USER, HOST, DB_PW
)
connection = psycopg2.connect(db_options)
cur = connection.cursor()

tables = dict(
novembersurvey=None,
aprilsurvey=None,
monthlysurvey=None,
usersurvey="""
SELECT auth_user.username AS username, auth_user.email AS email,
beekeepers_usersurvey.*
FROM beekeepers_usersurvey
INNER JOIN auth_user ON beekeepers_usersurvey.user_id=auth_user.id
""",
survey="""
SELECT beekeepers_survey.*, beekeepers_apiary.lat AS lat,
beekeepers_apiary.lng AS lng
FROM beekeepers_survey
INNER JOIN beekeepers_apiary
ON beekeepers_survey.apiary_id=beekeepers_apiary.id
""",
)
# collect output CSVs to dedicated folder
dir = os.path.join(PROJECT_ROOT, 'exports')
if not os.path.exists(dir):
os.mkdir(dir)

for table, query in tables.iteritems():
if query is None:
query = "SELECT * FROM beekeepers_{}".format(table)
output_query = "COPY ({0}) TO STDOUT WITH CSV HEADER".format(query)

filename = "{}/{}.csv".format(dir, table)
with open(filename, 'w') as f:
cur.copy_expert(output_query, f)

connection.close()

0 comments on commit 75a8053

Please sign in to comment.