-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
62 lines (53 loc) · 1.65 KB
/
app.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 os
import arcpy
from flask import Flask, jsonify
from flask.ext.cors import CORS
from dotenv import load_dotenv
import xmltodict
# Load and set environment variables
dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
load_dotenv(dotenv_path)
METADATA_TRANSLATOR = os.environ.get('METADATA_TRANSLATOR')
arcpy.env.workspace = os.environ.get('WORKSPACE')
app = Flask(__name__)
CORS(app)
def dict_from_obj(obj, keys):
""" Helper to construct dict from object """
dict_ = {}
for key in keys:
dict_[key] = getattr(obj, key)
return dict_
@app.route('/feature-classes')
def get_feature_classes():
"""
Get list of feature class names
"""
return jsonify(feature_classes=arcpy.ListFeatureClasses())
@app.route('/feature-classes/<item>')
def describe(item):
"""
Get basic info about feature class: name, file, shape type, fields
"""
desc = arcpy.Describe(item)
desc_dict = dict_from_obj(desc, ['name', 'file', 'shapeType'])
desc_dict['fields'] = []
for field in desc.fields:
desc_dict['fields'].append(dict_from_obj(field, ['name', 'length', 'aliasName', 'type']))
return jsonify(desc_dict)
@app.route('/feature-classes/<item>/metadata')
def metadata(item):
"""
Get feature class metadata as XML. Uses METADATA_TRANSLATOR environment
variable for metadata schema
"""
output_file = './tmp/' + item + '.xml'
try: # Make sure file doesn't exist already (can't overwrite)
os.remove(output_file)
except OSError:
pass
arcpy.ExportMetadata_conversion(item, METADATA_TRANSLATOR, output_file)
with open(output_file) as f:
file_contents = xmltodict.parse(f.read())
return jsonify(file_contents)
if __name__ == '__main__':
app.run(host='0.0.0.0')