Skip to content

Commit 946363f

Browse files
committed
export geojson with centroids #164
1 parent 0fcd6ab commit 946363f

File tree

1 file changed

+95
-2
lines changed

1 file changed

+95
-2
lines changed

mapswipe_workers/mapswipe_workers/generate_stats.py

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def generate_stats(only_new_results):
4747
get_aggregated_results_by_project_id_geom(filename)
4848
csv_to_geojson(filename)
4949

50+
filename = f'{DATA_PATH}/api-data/agg_res_by_project_id_centroid.csv'
51+
get_aggregated_results_by_project_id_centroid(filename)
52+
csv_to_geojson(filename)
53+
5054
filename = f'{DATA_PATH}/api-data/agg_projects.csv'
5155
get_aggregated_projects(filename)
5256

@@ -63,6 +67,10 @@ def generate_stats(only_new_results):
6367
get_aggregated_progress_by_project_id_geom(filename)
6468
csv_to_geojson(filename)
6569

70+
filename = f'{DATA_PATH}/api-data/agg_progress_by_project_id_centroid.csv'
71+
get_aggregated_progress_by_project_id_centroid(filename)
72+
csv_to_geojson(filename)
73+
6674
logger.info('start to export csv file for %s projects based on given project_id_list' % len(project_id_list))
6775
for project_id in project_id_list:
6876
filename = f'{DATA_PATH}/api-data/agg_res_by_task_id/agg_res_by_task_id_{project_id}.csv'
@@ -273,6 +281,36 @@ def get_aggregated_results_by_project_id_geom(filename):
273281
logger.info('saved aggregated results by project_id to %s' % filename)
274282

275283

284+
def get_aggregated_results_by_project_id_centroid(filename):
285+
'''
286+
Export results aggregated on project_id basis as csv file.
287+
288+
Parameters
289+
----------
290+
filename: str
291+
'''
292+
293+
pg_db = auth.postgresDB()
294+
sql_query = """COPY (
295+
SELECT
296+
r.*
297+
,p.name
298+
,p.project_details
299+
,ST_AsText(ST_Centroid(p.geom)) as geom
300+
FROM
301+
aggregated_results_by_project_id as r , projects as p
302+
WHERE
303+
r.project_id = p.project_id
304+
) TO STDOUT WITH (FORMAT CSV, HEADER, FORCE_QUOTE(project_id, name, project_details))"""
305+
306+
with open(filename, 'w') as f:
307+
pg_db.copy_expert(sql_query, f)
308+
309+
del pg_db
310+
311+
logger.info('saved aggregated results by project_id to %s' % filename)
312+
313+
276314
def get_aggregated_results_by_project_id_and_date(filename, project_id):
277315
'''
278316
Export results aggregated on project_id and daily basis as csv file.
@@ -385,8 +423,6 @@ def get_aggregated_progress_by_project_id_geom(filename):
385423
filename: str
386424
'''
387425

388-
# TODO: Export aggregated_progress_by_project_id_geom.csv as geojson
389-
390426
pg_db = auth.postgresDB()
391427
sql_query = """
392428
COPY (
@@ -409,6 +445,37 @@ def get_aggregated_progress_by_project_id_geom(filename):
409445
logger.info('saved aggregated progress by project_id to %s' % filename)
410446

411447

448+
def get_aggregated_progress_by_project_id_centroid(filename):
449+
'''
450+
Export aggregated progress on a project_id basis as csv file.
451+
452+
Parameters
453+
----------
454+
filename: str
455+
'''
456+
457+
pg_db = auth.postgresDB()
458+
sql_query = """
459+
COPY (
460+
SELECT
461+
r.*
462+
,p.name
463+
,p.project_details
464+
,ST_AsText(ST_Centroid(p.geom)) as geom
465+
FROM
466+
aggregated_progress_by_project_id as r,
467+
projects as p
468+
WHERE
469+
p.project_id = r.project_id
470+
) TO STDOUT WITH (FORMAT CSV, HEADER, FORCE_QUOTE(project_id, name, project_details))"""
471+
472+
with open(filename, 'w') as f:
473+
pg_db.copy_expert(sql_query, f)
474+
475+
del pg_db
476+
logger.info('saved aggregated progress by project_id to %s' % filename)
477+
478+
412479
def get_aggregated_progress_by_project_id_and_date(filename, project_id):
413480
'''
414481
Export aggregated progress on a project_id and daily basis as csv file.
@@ -555,6 +622,32 @@ def csv_to_geojson(filename):
555622
cast_datatypes_for_geojson(outfile)
556623

557624

625+
def csv_to_geojson_centroids(filename):
626+
'''
627+
Use ogr2ogr to convert csv file to GeoJSON
628+
'''
629+
630+
outfile = filename.replace('.csv', '_centroids.geojson')
631+
632+
# need to remove file here because ogr2ogr can't overwrite when choosing GeoJSON
633+
if os.path.isfile(outfile):
634+
os.remove(outfile)
635+
filename_without_path = filename.split('/')[-1].replace('.csv', '')
636+
# TODO: remove geom column from normal attributes in sql query
637+
subprocess.run([
638+
"ogr2ogr",
639+
"-f",
640+
"GeoJSON",
641+
outfile,
642+
filename,
643+
"-sql",
644+
f'SELECT *, ST_Centroid(CAST(geom as geometry)) FROM "{filename_without_path}"'
645+
], check=True)
646+
logger.info(f'converted {filename} to {outfile}.')
647+
648+
cast_datatypes_for_geojson(outfile)
649+
650+
558651
def cast_datatypes_for_geojson(filename):
559652
'''
560653
Go through geojson file and try to cast all values as float, except project_id

0 commit comments

Comments
 (0)