@@ -47,6 +47,10 @@ def generate_stats(only_new_results):
47
47
get_aggregated_results_by_project_id_geom (filename )
48
48
csv_to_geojson (filename )
49
49
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
+
50
54
filename = f'{ DATA_PATH } /api-data/agg_projects.csv'
51
55
get_aggregated_projects (filename )
52
56
@@ -63,6 +67,10 @@ def generate_stats(only_new_results):
63
67
get_aggregated_progress_by_project_id_geom (filename )
64
68
csv_to_geojson (filename )
65
69
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
+
66
74
logger .info ('start to export csv file for %s projects based on given project_id_list' % len (project_id_list ))
67
75
for project_id in project_id_list :
68
76
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):
273
281
logger .info ('saved aggregated results by project_id to %s' % filename )
274
282
275
283
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
+
276
314
def get_aggregated_results_by_project_id_and_date (filename , project_id ):
277
315
'''
278
316
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):
385
423
filename: str
386
424
'''
387
425
388
- # TODO: Export aggregated_progress_by_project_id_geom.csv as geojson
389
-
390
426
pg_db = auth .postgresDB ()
391
427
sql_query = """
392
428
COPY (
@@ -409,6 +445,37 @@ def get_aggregated_progress_by_project_id_geom(filename):
409
445
logger .info ('saved aggregated progress by project_id to %s' % filename )
410
446
411
447
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
+
412
479
def get_aggregated_progress_by_project_id_and_date (filename , project_id ):
413
480
'''
414
481
Export aggregated progress on a project_id and daily basis as csv file.
@@ -555,6 +622,32 @@ def csv_to_geojson(filename):
555
622
cast_datatypes_for_geojson (outfile )
556
623
557
624
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
+
558
651
def cast_datatypes_for_geojson (filename ):
559
652
'''
560
653
Go through geojson file and try to cast all values as float, except project_id
0 commit comments