Created
February 5, 2017 12:14
-
-
Save snorfalorpagus/da66cc9564e74bf6e75b4a71b07f0ce4 to your computer and use it in GitHub Desktop.
Flask upload fiona
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from flask import Flask, request, redirect, url_for, flash | |
from werkzeug.utils import secure_filename | |
import uuid | |
UPLOAD_FOLDER = os.path.join(os.path.dirname(__file__), "uploads") | |
app = Flask(__name__) | |
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER | |
def allowed_file(filename): | |
extension = filename.rsplit(".")[1] | |
return "." in filename and extension.lower() == "zip" | |
@app.route("/", methods=["GET", "POST"]) | |
def upload_file(): | |
if request.method == "POST": | |
if "file" not in request.files: | |
flash("No file part") | |
return redirect(request.url) | |
file = request.files["file"] | |
if file.filename == "": | |
flash("No selected file") | |
return redirect(request.url) | |
if file and allowed_file(file.filename): | |
filename = str(uuid.uuid4())+".zip" | |
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) | |
file.save(filepath) | |
result = check_result(filepath) | |
os.unlink(filepath) | |
return result | |
return """ | |
<!doctype html> | |
<title>Upload new File</title> | |
<h1>Upload new File</h1> | |
<form action="" method=post enctype=multipart/form-data> | |
<p><input type=file name=file> | |
<input type=submit value=Upload> | |
</form> | |
""" | |
# TODO: use jinja2, otherwise this isn't secure! | |
import fiona | |
def check_result(filename): | |
vfs = "zip://{}".format(filename) | |
with fiona.open(path="/example.shp", vfs="zip://example.zip", layer="example") as src: | |
num_features = len(src) | |
if num_features != 1: | |
return "Invalid number of features. Expecting 1, got {}".format(num_features) | |
feature = src.next() | |
if "name" not in feature["properties"].keys(): | |
return "Missing \"name\" field." | |
name = feature["properties"]["name"] | |
if name != "London": | |
return "Wrong name. Expecting London, got \"{}\"".format(name) | |
return "Everything looks good!" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment