@@ -4,6 +4,7 @@ const fs = require('fs');
4
4
const readline = require ( 'readline' ) ;
5
5
6
6
const config = require ( './config' ) ;
7
+ const { containsInvalidCoordinate } = require ( './utils' ) ;
7
8
8
9
/**
9
10
* Processes an array of changesets asynchronously.
@@ -16,12 +17,16 @@ async function processChangesets(changesets, date, hour) {
16
17
// Array to store the file paths of the processed changesets
17
18
const results = [ ] ;
18
19
19
- // Process each changeset asynchronously
20
- await Promise . all ( changesets . map ( async ( changeset ) => {
21
- // Process the changeset and get the result
22
- const result = await processChangeset ( changeset ) ;
23
- results . push ( result ) ;
24
- } ) ) ;
20
+ // Process changesets in batches of 10
21
+ const batchSize = 100 ;
22
+ for ( let i = 0 ; i < changesets . length ; i += batchSize ) {
23
+ const batch = changesets . slice ( i , i + batchSize ) ;
24
+ await Promise . all ( batch . map ( async ( changeset ) => {
25
+ // Process the changeset and get the result
26
+ const result = await processChangeset ( changeset ) ;
27
+ results . push ( result ) ;
28
+ } ) ) ;
29
+ }
25
30
26
31
// Combine the processed changesets into a single file
27
32
combineResults ( results , date , hour ) ;
@@ -35,26 +40,38 @@ async function processChangesets(changesets, date, hour) {
35
40
async function processChangeset ( changeset ) {
36
41
// Process the changeset asynchronously and return the result
37
42
const url = `https://real-changesets.s3.amazonaws.com/${ changeset } .json` ;
43
+ // console.log(`Processing changeset ${changeset}`);
38
44
try {
39
45
const response = await axios . get ( url ) ;
40
46
const data = response . data ;
41
47
const geojson = changesetParser ( data ) ;
48
+ // console.log(`geojson: ${JSON.stringify(geojson)}`);
42
49
const features = geojson . features ;
50
+
51
+ if ( features . length === 0 ) {
52
+ console . log ( `No features found in changeset ${ changeset } ` ) ;
53
+ return ;
54
+ }
43
55
const filePath = `${ config . DATA_PATH } /${ changeset } _features.json` ;
44
- await fs . writeFile ( filePath , '' , ( error ) => {
45
- if ( error ) {
46
- console . error ( `Error writing to file: ${ error } ` ) ;
47
- }
48
- } ) ;
49
- features . forEach ( async ( feature ) => {
50
- const featureString = JSON . stringify ( feature ) ;
51
-
52
- await fs . appendFile ( filePath , `${ featureString } \n` , ( error ) => {
53
- if ( error ) {
54
- console . error ( `Error writing feature to file: ${ error } ` ) ;
56
+
57
+ await Promise . all ( features . map ( async ( feature ) => {
58
+ if ( feature !== null && feature !== undefined ) {
59
+ if ( containsInvalidCoordinate ( feature . geometry . coordinates ) ) {
60
+ console . log ( `Dropping invalid feature ${ feature . properties . id } in changeset ${ changeset } ` ) ;
61
+ console . log ( `Feature geometry containing invalid co-ordinates: ${ JSON . stringify ( feature . geometry ) } ` ) ;
62
+ return ;
55
63
}
56
- } ) ;
57
- } ) ;
64
+ const featureString = JSON . stringify ( feature ) ;
65
+
66
+ await fs . appendFile ( filePath , `${ featureString } \n` , ( error ) => {
67
+ if ( error ) {
68
+ console . error ( `Error writing feature to file: ${ error } ` ) ;
69
+ }
70
+ } ) ;
71
+ } else {
72
+ console . log ( `undefined feature skipped in changeset ${ changeset } ` )
73
+ }
74
+ } ) ) ;
58
75
return filePath ;
59
76
} catch ( error ) {
60
77
console . error ( `Error processing changeset ${ changeset } : ${ error } ` ) ;
@@ -78,6 +95,9 @@ async function combineResults(results, date, hour) {
78
95
79
96
for ( let i = 0 ; i < results . length ; i ++ ) {
80
97
const filePath = results [ i ] ;
98
+ if ( ! filePath ) {
99
+ continue ;
100
+ }
81
101
const inputStream = fs . createReadStream ( filePath ) ;
82
102
83
103
const rl = readline . createInterface ( {
@@ -88,7 +108,7 @@ async function combineResults(results, date, hour) {
88
108
89
109
rl . on ( 'line' , ( line ) => {
90
110
outputStream . write ( divider ) ;
91
- divider = ',' ;
111
+ divider = ',\n ' ;
92
112
outputStream . write ( line ) ;
93
113
} ) ;
94
114
@@ -98,7 +118,7 @@ async function combineResults(results, date, hour) {
98
118
} ) ;
99
119
100
120
rl . on ( 'error' , ( error ) => {
101
- reject ( error ) ;
121
+ console . error ( `Error reading file: ${ error } ` ) ;
102
122
} ) ;
103
123
} ) ;
104
124
}
@@ -108,4 +128,4 @@ async function combineResults(results, date, hour) {
108
128
console . log ( `Combined results written to ${ outputStream . path } ` ) ;
109
129
}
110
130
111
- module . exports = { processChangesets } ;
131
+ module . exports = { processChangesets, processChangeset } ;
0 commit comments