forked from DocNow/twarc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geofilter.py
executable file
·63 lines (47 loc) · 1.75 KB
/
geofilter.py
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
#!/usr/bin/env python
from __future__ import print_function
import argparse
import json
import sys
from shapely.geometry import shape
def process(line, has_coordinates=None, has_place=None, fence=None):
tweet = json.loads(line)
coordinates = tweet.get('coordinates')
place = tweet.get('place')
if any([
has_coordinates and not coordinates,
has_coordinates is False and coordinates,
has_place and not place,
has_place is False and place,
]):
return
if fence and (coordinates or place):
if coordinates:
location = shape(coordinates)
else:
location = shape(place['bounding_box'])
if not fence.contains(location):
return
print(line.strip('\n'))
def main():
parser = argparse.ArgumentParser()
parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
default=sys.stdin)
parser.add_argument('--yes-coordinates', dest='has_coordinates',
action='store_true')
parser.add_argument('--no-coordinates', dest='has_coordinates',
action='store_false')
parser.add_argument('--yes-place', dest='has_place', action='store_true')
parser.add_argument('--no-place', dest='has_place', action='store_false')
parser.add_argument('--fence', default=None,
help='geojson file with geofence')
parser.set_defaults(has_coordinates=None, has_place=None)
args = parser.parse_args()
fence = None
if args.fence:
with open(args.fence, 'r') as f:
fence = shape(json.loads(f.read()))
for line in args.infile:
process(line, args.has_coordinates, args.has_place, fence)
if __name__ == '__main__':
main()