Arcgis
Arcgis
Getting started
Import dependency
Sample script
app = Flask(__name__)
app.debug = True
class PrefixMiddleware(object):
#class for URL sorting
def __init__(self, app, prefix=''):
self.app = app
self.prefix = prefix
@app.route('/bar')
def bar():
return "The URL for this page is {}".format(url_for('bar'))
@app.route('/v1/test/get/response', methods=['GET'])
def api_all():
response = [
{'id': 0,
'desc': 'xxxx'},
{'id': 1,
'desc': 'xxxx'}
]
return jsonify(response)
@app.route('/v1/export', methods=['GET'])
def api_export():
args = request.args
token = args['token']
query = "https://services1.arcgis.com/vHnIGBHHqDR6y0CR/arcgis/rest
/services/vicfeaturelayer/FeatureServer/0/query?f=json&where=1%
3D1&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*
&orderByFields=OBJECTID%
20ASC&resultOffset=0&resultRecordCount=50&cacheHint=true&token=" + token
results =requests.get(query)
return results.json()
if __name__ == '__main__':
#print(getpass.getuser())
app.run(host='0.0.0.0',port=9010)
This is something I have been meaning to try, running python code with arcpy via rest API. This can be extremely helpful when working with web
hooks in survey123.
Pip install wfastcgi (I ran pip from the arcgis pro script folder)
Enable wfastcgi (I ran wfastcgi-enable from the arcgis pro script folder)
https://docs.microsoft.com/en-us/iis/configuration/system.webserver/fastcgi/
Confirming FastCGI on IIS
Create application on default site and add the following to the web.config (if there isn’t one, create one)
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="Python FastCGI" path="*" verb="*"
modules="FastCgiModule" scriptProcessor="C:
\Users\vtay\Documents\vstuff\PythonEnv\ws_usage_matrix\arcgispro-py3-
elasticsearch\python.exe|C:
\Users\vtay\Documents\vstuff\PythonEnv\ws_usage_matrix\arcgispro-py3-
elasticsearch\Lib\site-packages\wfastcgi.py" resourceType="Unspecified"
requireAccess="Script" />
</handlers>
<directoryBrowse enabled="true" />
</system.webServer>
<appSettings>
<!-- Required settings -->
<add key="WSGI_HANDLER" value="myapp.app" />
<add key="PYTHONPATH" value="D:\bitbucket\Esri
Australia\pythonFlaskIIS" />
</appSettings>
</configuration>
To be able to import arcpy, the pool identity have to be set or given access to ArcGIS Pro (I provide access to ArcGIS pro on the root level but it
wasn’t enough, changing the identity pool to myself works however)
If you have import arcgis, you will encounter the error (Underlying buffer detached). Workaround is to run pip uninstall ipython (i recommend you
make a copy of the python environment)
Still working on this, most likely the user it is running as doesn’t have a environment directory. E.G.
C:\Users\vtay\AppData\Roaming\ESRI\ArcGISPro.
Check
_environ = dict(os.environ)
_environ['USERNAME'] = 'vtey'
_environ['USERPROFILE'] = 'C:\\Users\\vtay'
os.environ.clear()
os.environ.update(_environ)
import arcpy
import arcpy
from arcgis.gis import GIS
app = Flask(__name__)
app.debug = True
class PrefixMiddleware(object):
#class for URL sorting
def __init__(self, app, prefix=''):
self.app = app
self.prefix = prefix
#arcpy.SignInToPortal(config.portal_url, config.
portal_username, config.portal_password)
@app.route('/bar')
def bar():
return "The URL for this page is {}".format(url_for('bar'))
@app.route('/v1/test/get/response', methods=['GET'])
def api_all():
response = [
{'id': 0,
'desc': 'xxxx'},
{'id': 1,
'desc': 'xxxx'}
]
return jsonify(response)
@app.route('/v1/export', methods=['GET'])
def api_export():
args = request.args
token = args['token']
query = "https://services1.arcgis.com/vHnIGBHHqDR6y0CR/arcgis/rest
/services/vicfeaturelayer/FeatureServer/0/query?f=json&where=1%
3D1&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*
&orderByFields=OBJECTID%
20ASC&resultOffset=0&resultRecordCount=50&cacheHint=true&token=" + token
results =requests.get(query)
return results.json()
@app.route('/v1/arcgis', methods=['GET'])
def api_arcgis():
gis = GIS(config.portal_url,
username=config.portal_username,
password=config.portal_password,
verify_cert=False)
fl = gis.content.get('b8103c3d1cce42fab0df686e2510d0a4')
layer = fl.layers[0]
return (layer.query().to_json)
@app.route('/v1/arcpy', methods=['GET'])
def api_arcpy():
fc = r"D:\SDE connection\LEA-304867.sde\arcgis.DBO.point"