Skip to content

Commit

Permalink
#1149 switch to formatting geojson in postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielwol committed Feb 18, 2025
1 parent 53fbe36 commit 5eeea9f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 39 deletions.
52 changes: 13 additions & 39 deletions dags/ecocounter_open_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
README_PATH = os.path.join(repo_path, 'volumes/ecocounter/readme.md')
DOC_MD = get_readme_docmd(README_PATH, DAG_NAME)
EXPORT_PATH = '/home/airflow/open_data/permanent-bike-counters' #'/data/open_data/permanent-bike-counters'
BASH_ENV = {
'HOST': '{{ conn.ecocounter_bot.host }}',
'LOGIN': '{{ conn.ecocounter_bot.login }}',
'PGPASSWORD': '{{ conn.ecocounter_bot.password }}',
'EXPORT_PATH': EXPORT_PATH
}

default_args = {
'owner': ','.join(DAG_OWNERS),
Expand Down Expand Up @@ -132,14 +138,7 @@ def insert_15min(yr):
)
return t.execute(context=context)

@task.bash(
env = {
'HOST': '{{ conn.ecocounter_bot.host }}',
'LOGIN': '{{ conn.ecocounter_bot.login }}',
'PGPASSWORD': '{{ conn.ecocounter_bot.password }}',
'EXPORT_PATH': EXPORT_PATH
}
)
@task.bash(env = BASH_ENV)
def download_daily_open_data()->str:
return '''/usr/bin/psql -h $HOST -U $LOGIN -d bigdata -c \
"SELECT
Expand All @@ -151,12 +150,7 @@ def download_daily_open_data()->str:
--csv -o "$EXPORT_PATH/cycling_permanent_counts_daily.csv"'''

@task.bash(
env = {
'HOST': '{{ conn.ecocounter_bot.host }}',
'LOGIN': '{{ conn.ecocounter_bot.login }}',
'PGPASSWORD': '{{ conn.ecocounter_bot.password }}',
'EXPORT_PATH': EXPORT_PATH
},
env = BASH_ENV,
map_index_template="{{ yr }}"
)
def download_15min_open_data(yr)->str:
Expand All @@ -175,14 +169,7 @@ def download_15min_open_data(yr)->str:
insert_daily(yr) >> download_daily_open_data()
insert_15min(yr) >> download_15min_open_data(yr)

@task.bash(
env = {
'HOST': '{{ conn.ecocounter_bot.host }}',
'LOGIN': '{{ conn.ecocounter_bot.login }}',
'PGPASSWORD': '{{ conn.ecocounter_bot.password }}',
'EXPORT_PATH': EXPORT_PATH
}
)
@task.bash(env = BASH_ENV)
def download_locations_open_data()->str:
return '''/usr/bin/psql -h $HOST -U $LOGIN -d bigdata -c \
"SELECT location_dir_id, location_name, direction, linear_name_full, side_street,
Expand All @@ -192,24 +179,11 @@ def download_locations_open_data()->str:
ORDER BY location_dir_id;" \
--csv -o "$EXPORT_PATH/cycling_permanent_counts_locations.csv"'''

@task.bash(
env = {
'HOST': '{{ conn.ecocounter_bot.host }}',
'LOGIN': '{{ conn.ecocounter_bot.login }}',
'PGPASSWORD': '{{ conn.ecocounter_bot.password }}',
'EXPORT_PATH': EXPORT_PATH
}
)
@task.bash(env = BASH_ENV)
def download_locations_open_data_geojson()->str:
return '''/usr/bin/ogr2ogr -f "GeoJSON" \
"$EXPORT_PATH/cycling_permanent_counts_locations.geojson" \
PG:"host=$HOST dbname=bigdata" \
-sql "SELECT location_dir_id, location_name, direction, linear_name_full, side_street,
longitude, latitude, centreline_id, bin_size, latest_calibration_study,
first_active, last_active, date_decommissioned, technology
FROM open_data.cycling_permanent_counts_locations
ORDER BY location_dir_id;" \
-nln cycling_permanent_counts_locations'''
return '''/usr/bin/psql -h $HOST -U $LOGIN -d bigdata -c \
"SELECT featurecollection FROM open_data.cycling_permanent_counts_locations_geojson;" \
--tuples-only -o "$EXPORT_PATH/cycling_permanent_counts_locations.geojson"'''

@task.bash()
def output_readme()->str:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
CREATE OR REPLACE VIEW open_data.cycling_permanent_counts_locations_geojson AS
WITH features AS (
SELECT json_build_object(

Check failure on line 3 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected line break and indent of 8 spaces before 'json_build_object'.
'type', 'Feature',

Check failure on line 4 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 12 spaces.
'id', location_dir_id,

Check failure on line 5 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 12 spaces.
'geometry', st_asgeojson(st_setsrid(st_makepoint(longitude, latitude), 4326))::jsonb,

Check failure on line 6 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 12 spaces.
'attributes', json_build_object(

Check failure on line 7 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 12 spaces.
'location_dir_id', location_dir_id,

Check failure on line 8 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 16 spaces.
'location_name', location_name,

Check failure on line 9 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 16 spaces.
'direction', direction,

Check failure on line 10 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 16 spaces.
'linear_name_full', linear_name_full,

Check failure on line 11 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 16 spaces.
'side_street', side_street,

Check failure on line 12 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 16 spaces.
'centreline_id', centreline_id,

Check failure on line 13 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 16 spaces.
'bin_size', bin_size,

Check failure on line 14 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 16 spaces.
'latest_calibration_study', latest_calibration_study,

Check failure on line 15 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 16 spaces.
'first_active', first_active,

Check failure on line 16 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 16 spaces.
'last_active', last_active,

Check failure on line 17 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 16 spaces.
'date_decommissioned', date_decommissioned,

Check failure on line 18 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 16 spaces.
'technology', technology

Check failure on line 19 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 16 spaces.
)

Check failure on line 20 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 12 spaces.
) AS features

Check failure on line 21 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 8 spaces.
FROM open_data.cycling_permanent_counts_locations
)

--aggregate features into FeatureCollection
SELECT json_build_object(

Check failure on line 26 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected line break and indent of 4 spaces before 'json_build_object'.
'type',

Check failure on line 27 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 8 spaces.
'FeatureCollection',

Check failure on line 28 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 8 spaces.
'features',

Check failure on line 29 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 8 spaces.
json_agg(features.features)

Check failure on line 30 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 8 spaces.
) AS featurecollection

Check failure on line 31 in volumes/open_data/sql/create-view-cycling_permanent_counts_locations_geojson.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Expected indent of 4 spaces.
FROM features;

ALTER VIEW open_data.cycling_permanent_counts_locations_geojson OWNER TO od_admins;

GRANT SELECT ON open_data.cycling_permanent_counts_locations_geojson TO ecocounter_bot;
GRANT SELECT ON open_data.cycling_permanent_counts_locations_geojson TO bdit_humans;

0 comments on commit 5eeea9f

Please sign in to comment.