JamesRamm / archook

Searches the system for arcgis and makes arcpy available to python (regardless of pythonpath/system path/registry settings)
GNU General Public License v2.0
81 stars 28 forks source link

Unable to use with Flask app #13

Closed ishiland closed 6 years ago

ishiland commented 6 years ago

I am unable to use arcpy with my Flask application. archook and arcpy seem to get imported successfully, but when I call an arcpy method I get this:

Object: Tool or environment <FeatureClassToShapefile_conversion> not found or sometimes the Python.exe crashes altogether.

I have no issues running any of the functions in data_processes.py on its own, but when they are called from Flask I get those errors. Any suggestions on what the problem could be is much appreciated.

flaskapp.py:

from data_processes import list_esri_data, to_shapefile 
from flask import Flask, render_template, request, send_file, jsonify, url_for
import os

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('pages/home.html')

@app.route('/data/all')
def data_list():
    return jsonify(list_esri_data())

@app.route('/data/shapefile')
def download_shapefile():
    d = to_shapefile(request.args.get('dataset'))
    return send_file(filename_or_fp=d)

@app.context_processor
def override_url_for():
    return dict(url_for=dated_url_for)

def dated_url_for(endpoint, **values):
    if endpoint == 'static':
        filename = values.get('filename', None)
        if filename:
            file_path = os.path.join(app.root_path,
                                     endpoint, filename)
            values['q'] = int(os.stat(file_path).st_mtime)
    return url_for(endpoint, **values)

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port, debug=True)

data_processess.py: This does the arcpy work

import os, sys, shutil
import tempfile, zipfile
try:
    import archook 
    archook.get_arcpy()
    import arcpy
    print('Arcpy imported successfully')
except ImportError as e:
    print(e)
    sys.exit(e)

dir_path = os.path.dirname(os.path.realpath(__file__))

sde_file = os.path.join(dir_path, 'esri', 'os_SQL2K8IT04C.sde')

arcpy.env.workspace = sde_file

def list_esri_data():
    try:
        print('\nRetrieving Data\n')
        return arcpy.ListFeatureClasses()
    except Exception as e:
        print(e)

def to_shapefile(d):
    try:
        """
        For returning SDE featureclass as a shapefile.
        :param d: input featureclass
        :return: zipped shapefile
        """
        print('Downloading shapefile...')
        name = d.split(".")[-1]
        temp_shp_path = tempfile.mkdtemp()
        out_zip_path = os.path.join(dir_path, 'data', name)
        arcpy.FeatureClassToShapefile_conversion(d, temp_shp_path)
        zip(temp_shp_path, os.path.join(dir_path, 'data', name))
        shutil.rmtree(temp_shp_path)
        return out_zip_path
    except Exception as e:
        print(e)

def zip(src, dst):
    zf = zipfile.ZipFile("%s.zip" % (dst), "w", zipfile.ZIP_DEFLATED)
    abs_src = os.path.abspath(src)
    for dirname, subdirs, files in os.walk(src):
        for filename in files:
            absname = os.path.abspath(os.path.join(dirname, filename))
            arcname = absname[len(abs_src) + 1:]
            print 'zipping %s as %s' % (os.path.join(dirname, filename),
                                        arcname)
            zf.write(absname, arcname)
    zf.close()
ishiland commented 6 years ago

Never got this to work with Flask, but I will close this issue as maybe this is more Flask related. I ended up using ogr which worked for my needs.