This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
55 lines (45 loc) · 1.77 KB
/
__init__.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
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import os
import shutil
import tempfile
from contextlib import closing
from zipfile import BadZipfile
from flask import (Blueprint, flash, make_response, render_template, request,)
from werkzeug import secure_filename
from magma import UnsupportedFormat
from magma.upload import datasource, process
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
home_page = Blueprint('home_page', __name__)
@home_page.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
if 'data' in request.files and request.files['data']:
datafile = request.files['data']
else:
flash('A datafile is required to proceed.', 'danger')
return render_template('index.html')
fgdc = request.files.get('metadata') or StringIO('<metadata/>')
tmp = tempfile.mkdtemp()
filename = os.path.join(tmp, secure_filename(datafile.filename))
datafile.save(filename)
try:
with closing(datasource(filename)) as ds:
metadata = process(ds, fgdc)
if request.form['access'] == 'restricted':
metadata.set_restricted_access()
response = make_response(metadata.write())
response.headers['Content-type'] = 'text/xml'
response.headers['Content-Disposition'] = \
'attachment; filename=fgdc.xml'
return response
except BadZipfile:
flash('Could not open zipfile.', 'danger')
except UnsupportedFormat:
flash('Uploaded datafile should be either a zipped shapefile or a GeoTIFF.', 'danger')
finally:
shutil.rmtree(tmp)
return render_template('index.html')