@@ -42,14 +42,9 @@ def generate_stats(only_new_results):
42
42
43
43
filename = f'{ DATA_PATH } /api-data/agg_res_by_project_id.csv'
44
44
get_aggregated_results_by_project_id (filename )
45
+ csv_to_geojson (filename , 'geom' )
46
+ csv_to_geojson (filename , 'centroid' )
45
47
46
- filename = f'{ DATA_PATH } /api-data/agg_res_by_project_id_geom.csv'
47
- get_aggregated_results_by_project_id_geom (filename )
48
- csv_to_geojson (filename )
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
48
54
49
filename = f'{ DATA_PATH } /api-data/agg_projects.csv'
55
50
get_aggregated_projects (filename )
@@ -62,14 +57,8 @@ def generate_stats(only_new_results):
62
57
63
58
filename = f'{ DATA_PATH } /api-data/agg_progress_by_project_id.csv'
64
59
get_aggregated_progress_by_project_id (filename )
65
-
66
- filename = f'{ DATA_PATH } /api-data/agg_progress_by_project_id_geom.csv'
67
- get_aggregated_progress_by_project_id_geom (filename )
68
- csv_to_geojson (filename )
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 )
60
+ csv_to_geojson (filename , 'geom' )
61
+ csv_to_geojson (filename , 'centroid' )
73
62
74
63
logger .info ('start to export csv file for %s projects based on given project_id_list' % len (project_id_list ))
75
64
for project_id in project_id_list :
@@ -240,63 +229,14 @@ def get_aggregated_results_by_project_id(filename):
240
229
filename: str
241
230
'''
242
231
243
- pg_db = auth .postgresDB ()
244
- sql_query = "COPY (SELECT * FROM aggregated_results_by_project_id) TO STDOUT WITH CSV HEADER"
245
-
246
- with open (filename , 'w' ) as f :
247
- pg_db .copy_expert (sql_query , f )
248
-
249
- del pg_db
250
-
251
- logger .info ('saved aggregated results by project_id to %s' % filename )
252
-
253
-
254
- def get_aggregated_results_by_project_id_geom (filename ):
255
- '''
256
- Export results aggregated on project_id basis as csv file.
257
-
258
- Parameters
259
- ----------
260
- filename: str
261
- '''
262
-
263
232
pg_db = auth .postgresDB ()
264
233
sql_query = """COPY (
265
234
SELECT
266
235
r.*
267
236
,p.name
268
237
,p.project_details
269
238
,ST_AsText(p.geom) as geom
270
- FROM
271
- aggregated_results_by_project_id as r , projects as p
272
- WHERE
273
- r.project_id = p.project_id
274
- ) TO STDOUT WITH (FORMAT CSV, HEADER, FORCE_QUOTE(project_id, name, project_details))"""
275
-
276
- with open (filename , 'w' ) as f :
277
- pg_db .copy_expert (sql_query , f )
278
-
279
- del pg_db
280
-
281
- logger .info ('saved aggregated results by project_id to %s' % filename )
282
-
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
239
+ ,ST_AsText(ST_Centroid(p.geom)) as centroid
300
240
FROM
301
241
aggregated_results_by_project_id as r , projects as p
302
242
WHERE
@@ -403,26 +343,6 @@ def get_aggregated_progress_by_project_id(filename):
403
343
filename: str
404
344
'''
405
345
406
- pg_db = auth .postgresDB ()
407
- sql_query = "COPY (SELECT * FROM aggregated_progress_by_project_id) TO STDOUT WITH CSV HEADER"
408
-
409
- with open (filename , 'w' ) as f :
410
- pg_db .copy_expert (sql_query , f )
411
-
412
- del pg_db
413
-
414
- logger .info ('saved aggregated progress by project_id to %s' % filename )
415
-
416
-
417
- def get_aggregated_progress_by_project_id_geom (filename ):
418
- '''
419
- Export aggregated progress on a project_id basis as csv file.
420
-
421
- Parameters
422
- ----------
423
- filename: str
424
- '''
425
-
426
346
pg_db = auth .postgresDB ()
427
347
sql_query = """
428
348
COPY (
@@ -431,37 +351,7 @@ def get_aggregated_progress_by_project_id_geom(filename):
431
351
,p.name
432
352
,p.project_details
433
353
,ST_AsText(p.geom) as geom
434
- FROM
435
- aggregated_progress_by_project_id as r,
436
- projects as p
437
- WHERE
438
- p.project_id = r.project_id
439
- ) TO STDOUT WITH (FORMAT CSV, HEADER, FORCE_QUOTE(project_id, name, project_details))"""
440
-
441
- with open (filename , 'w' ) as f :
442
- pg_db .copy_expert (sql_query , f )
443
-
444
- del pg_db
445
- logger .info ('saved aggregated progress by project_id to %s' % filename )
446
-
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
354
+ ,ST_AsText(ST_Centroid(p.geom)) as centroid
465
355
FROM
466
356
aggregated_progress_by_project_id as r,
467
357
projects as p
@@ -597,38 +487,12 @@ def get_last_result():
597
487
return last_update
598
488
599
489
600
- def csv_to_geojson (filename ):
490
+ def csv_to_geojson (filename , geometry_field = 'geom' ):
601
491
'''
602
492
Use ogr2ogr to convert csv file to GeoJSON
603
493
'''
604
494
605
- outfile = filename .replace ('csv' , 'geojson' )
606
- # need to remove file here because ogr2ogr can't overwrite when choosing GeoJSON
607
- if os .path .isfile (outfile ):
608
- os .remove (outfile )
609
- filename_without_path = filename .split ('/' )[- 1 ].replace ('.csv' , '' )
610
- # TODO: remove geom column from normal attributes in sql query
611
- subprocess .run ([
612
- "ogr2ogr" ,
613
- "-f" ,
614
- "GeoJSON" ,
615
- outfile ,
616
- filename ,
617
- "-sql" ,
618
- f'SELECT *, CAST(geom as geometry) FROM "{ filename_without_path } "'
619
- ], check = True )
620
- logger .info (f'converted { filename } to { outfile } .' )
621
-
622
- cast_datatypes_for_geojson (outfile )
623
-
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
-
495
+ outfile = filename .replace ('.csv' , f'_{ geometry_field } .geojson' )
632
496
# need to remove file here because ogr2ogr can't overwrite when choosing GeoJSON
633
497
if os .path .isfile (outfile ):
634
498
os .remove (outfile )
@@ -641,7 +505,7 @@ def csv_to_geojson_centroids(filename):
641
505
outfile ,
642
506
filename ,
643
507
"-sql" ,
644
- f'SELECT *, ST_Centroid( CAST(geom as geometry) ) FROM "{ filename_without_path } "'
508
+ f'SELECT *, CAST({ geometry_field } as geometry) FROM "{ filename_without_path } "'
645
509
], check = True )
646
510
logger .info (f'converted { filename } to { outfile } .' )
647
511
0 commit comments