-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_esri.py
executable file
·67 lines (55 loc) · 1.89 KB
/
parse_esri.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
64
65
66
67
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import re
import time
import stat
import csv
import json
import types
import argparse
attrs = ["geometryType", "spatialReference"]
geometryType = spatialReference = None
cols = []
ap = argparse.ArgumentParser(description='Parse JSON file from an ArcGIS REST API file')
ap.add_argument('infile', nargs='?', type=argparse.FileType('r'), const=sys.stdin, default=sys.stdin)
ap.add_argument('outfile', nargs='?', type=argparse.FileType('a'), const=sys.stdout, default=sys.stdout)
ap.add_argument('--headers', dest='headers', action='store_true')
ap.add_argument('--no-headers', dest='headers', action='store_false')
ap.set_defaults(headers=False)
args = ap.parse_args()
try:
js = json.loads(args.infile.read())
except:
print >> sys.stderr, args.infile
sys.exit()
if "spatialReference" in js and "wkid" in js["spatialReference"]:
spatialReference = js["spatialReference"]["wkid"]
if "geometryType" in js:
geometryType = js["geometryType"]
fields = []
for field in js["fields"]:
fields.append(field["name"])
outrows = []
for feature in js["features"]:
r = {}
r["spatialreference"] = spatialReference
r["geometrytype"] = geometryType
for a in fields:
r[a.lower()] = feature["attributes"][a]
geomtype = "POLYGON"
if "geometry" in feature:
r["geometry"] = re.sub(r" +", " ", re.sub(r"\], ?\[","|", re.sub(r"\]\], ?\[\[", ")|(", re.sub(r"\]\]\]$", "))", re.sub(r"^\[\[\[","SRID="+spatialReference+";"+geomtype+"((",str(feature["geometry"]["rings"]))))).replace("[","").replace("]","").replace(","," ").replace("|",","))
#r.time_fetched = time_fetched
outrows.append(r)
cols = []
for c in attrs:
cols.append(c.lower())
for c in fields:
cols.append(c.lower())
cols.append("geometry")
cw = csv.DictWriter(args.outfile, cols)
if args.headers:
cw.writeheader()
cw.writerows(outrows)