-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgdelt_reshaper.js
145 lines (141 loc) · 5.05 KB
/
gdelt_reshaper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
Aggregation pipeline to compress Actor1, Actor2, and Action fields into it's own subdocument.
*/
// Select the database to use.
use('GDELT2');
// Build an aggregation pipeline to compress Actor1, Actor2, and Action fields into it's own subdocument.
// Map lat/long to GeoJSON coordinates.
const GDELTAggregation = [
{
$addFields: {
Actor1Geo: {
"type": "Point",
"coordinates": [
{$convert: {input: "$Actor1Geo_Long", to: "double", onError: 0.00, onNull: 0.00}},
{$convert: {input: "$Actor1Geo_Lat", to: "double", onError: 0.00, onNull: 0.00}}
]
},
Actor2Geo: {
"type": "Point",
"coordinates": [
{$convert: {input: "$Actor2Geo_Long", to: "double", onError: 0.00, onNull: 0.00}},
{$convert: {input: "$Actor2Geo_Lat", to: "double", onError: 0.00, onNull: 0.00}}
]
},
ActionGeo: {
"type": "Point",
"coordinates": [
{$convert: {input: "$ActionGeo_Long", to: "double", onError: 0.00, onNull: 0.00}},
{$convert: {input: "$ActionGeo_Lat", to: "double", onError: 0.00, onNull: 0.00}}
]
}
}
},
{
$addFields: {
Actor1: {
Name: '$Actor1Name',
"Code": '$Actor1Code',
CountryCode: '$Actor1CountryCode',
KnownGroupCode: '$Actor1KnownGroupCode',
EthnicCode: '$Actor1EthnicCode',
Religion1Code: '$Actor1Religion1Code',
Religion2Code: '$Actor1Religion2Code',
Type1Code: '$Actor1Type1Code',
Type2Code: '$Actor1Type2Code',
Type3Code: '$Actor1Type3Code',
Geo: "$Actor1Geo",
Geo_Type: "$Actor1Geo_Type",
Geo_Fullname: "$Actor1Geo_Fullname",
Geo_CountryCode: "$Actor1Geo_CountryCode",
Geo_ADM1Code: "$Actor1Geo_ADM1Code",
Geo_ADM2Code: "$Actor1Geo_ADM2Code",
Geo_FeatureID: "$Actor1Geo_FeatureID"
},
Actor2: {
Name: '$Actor2Name',
"Code": '$Actor2Code',
CountryCode: '$Actor2CountryCode',
KnownGroupCode: '$Actor2KnownGroupCode',
EthnicCode: '$Actor2EthnicCode',
Religion1Code: '$Actor2Religion1Code',
Religion2Code: '$Actor2Religion2Code',
Type1Code: '$Actor2Type1Code',
Type2Code: '$Actor2Type2Code',
Type3Code: '$Actor2Type3Code',
Geo: "$Actor2Geo",
Geo_Type: "$Actor2Geo_Type",
Geo_Fullname: "$Actor2Geo_Fullname",
Geo_CountryCode: "$Actor2Geo_CountryCode",
Geo_ADM1Code: "$Actor2Geo_ADM1Code",
Geo_ADM2Code: "$Actor2Geo_ADM2Code",
Geo_FeatureID: "$Actor2Geo_FeatureID"
},
Action: {
"Geo": "$ActionGeo",
"Geo_Type": "$ActionGeo_Type",
"Geo_Fullname": "$ActionGeo_Fullname",
"Geo_CountryCode": "$ActionGeo_CountryCode",
"Geo_ADM1Code": "$ActionGeo_ADM1Code",
"Geo_ADM2Code": "$ActionGeo_ADM2Code",
"Geo_FeatureID": "$ActionGeo_FeatureID"
},
}
},
{
$unset: [
"Actor1Name",
"Actor1Code",
"Actor1CountryCode",
"Actor1KnownGroupCode",
"Actor1EthnicCode",
"Actor1Religion1Code",
"Actor1Religion2Code",
"Actor1Type1Code",
"Actor1Type2Code",
"Actor1Type3Code",
"Actor1Geo",
"Actor1Geo_Type",
"Actor1Geo_Fullname",
"Actor1Geo_CountryCode",
"Actor1Geo_ADM1Code",
"Actor1Geo_ADM2Code",
"Actor1Geo_FeatureID",
"Actor2Code",
"Actor2Name",
"Actor2CountryCode",
"Actor2KnownGroupCode",
"Actor2EthnicCode",
"Actor2Religion1Code",
"Actor2Religion2Code",
"Actor2Type1Code",
"Actor2Type2Code",
"Actor2Type3Code",
"Actor2Geo",
"Actor2Geo_Type",
"Actor2Geo_Fullname",
"Actor2Geo_CountryCode",
"Actor2Geo_ADM1Code",
"Actor2Geo_ADM2Code",
"Actor2Geo_FeatureID",
"ActionGeo",
"ActionGeo_Type",
"ActionGeo_Fullname",
"ActionGeo_CountryCode",
"ActionGeo_ADM1Code",
"ActionGeo_ADM2Code",
"ActionGeo_FeatureID",
"ActionGeo_Lat",
"ActionGeo_Long",
"Actor1Geo_Lat",
"Actor1Geo_Long",
"Actor2Geo_Lat",
"Actor2Geo_Long"
]
},
{
$out: "events"
}
];
db.eventscsv.aggregate(GDELTAggregation);
exit;