-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_geo.py
62 lines (52 loc) · 1.46 KB
/
generate_geo.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
import argparse
import csv
import json
import sys
def crime_to_feature(crime):
return {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
float(crime["Longitude"]),
float(crime["Latitude"])
]
},
"properties": {
"outcome": crime["Last outcome category"]
}
}
def convert(inputs, output, crime_type):
geojson = {
"type": "FeatureCollection",
"features": []
}
for filename in inputs:
with open(filename, 'rb') as csvfile:
crimes = csv.DictReader(csvfile)
for crime in crimes:
if crime_type and crime_type != crime['Crime type']:
continue
geojson["features"].append(crime_to_feature(crime))
return json.dump(geojson, output)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Create GeoJSON from police.uk crime data")
parser.add_argument(
"inputs",
metavar="csvfile",
type=str,
nargs="+",
help="CSV files to convert")
parser.add_argument(
"--output",
dest="output",
type=argparse.FileType('w'),
default=sys.stdout)
parser.add_argument(
"--crime-type",
dest="crime_type",
type=str,
nargs="?")
args = parser.parse_args()
convert(args.inputs, args.output, args.crime_type)