def _validate_server(server, server_url): """most provide server or serverUrl""" # logging.debug('[VALIDATOR]: validate server') if server and server not in servers.keys(): return error(status=400, detail="server not in list {}".format(servers.keys())) # logging.debug('[VALIDATOR]: validate server url') if not server_url and not server: return error(status=400, detail="either server or serverUrl is required")
def _validate_rendering_rule(rendering_rule): """Validation""" # must have a rendering rule and rule must be a valid JSON # logging.debug('[VALIDATOR]: validate rendering rule: {}'.format(rendering_rule)) if rendering_rule: try: json.loads(rendering_rule) except ValueError: return error(status=400, detail="renderingRule not a valid JSON") else: return error(status=400, detail="Must provide a valid renderingRule")
def compute_histograms(): """ ImageServer/computeHistograms Endpoint Make request to images sever and return a histogram """ logging.info('[ROUTER]: Forward request to ArcGIS Image Server') server = request.args.get('server', None) service = request.args.get('service', None) server_url = request.args.get('serverUrl', None) rendering_rule = request.args.get('renderingRule', None) mosaic_rule = request.args.get('mosaicRule', None) if mosaic_rule == '': mosaic_rule = None pixelSize = request.args.get('pixelSize', None) geostore_id = request.args.get('geostore', None) service_url = get_image_service_url(server, server_url, service) # ArcGIS Server request a multi part form # we have to send payload as files # values need to be tuples with the first value set to None payload = { "geometry": (None, json.dumps(get_esrijson_wm(geostore_id))), "geometryType": (None, "esriGeometryPolygon"), "renderingRule": (None, rendering_rule), "mosaicRule": (None, mosaic_rule), "pixelSize": (None, pixelSize), "f": (None, "json") } logging.debug('[ROUTER]: payload: {}'.format(payload)) #logging.debug('[ROUTER]: esrijson: {}'.format(payload['geometry'])) try: r = requests.post(service_url + "/computeHistograms", files=payload) logging.info('[ROUTER]: ImageServer response: {}'.format(r.text)) if r.status_code == 200: return jsonify(r.json()), 200 else: return error(status=r.status_code, detail=r.text) except: return error(status=400, detail='Not a valid request')
def _validate_service(service): """must provide service URI""" # logging.debug('[VALIDATOR]: validate service') if not service: return error(status=400, detail="service is required")
def _validate_geostore(geostore): """must have a geostore ID""" # logging.debug('[VALIDATOR]: validate geostore') if not geostore: return error(status=400, detail="Must provide a valid geostore ID")
def _validate_pixel_size(pixel_size): """pixelSize must be an integer or empty""" # logging.debug('[VALIDATOR]: validate pixel size') if pixel_size: try: int(pixel_size) except ValueError: return error(status=400, detail="pixelSize must be of Type Integer")
def get_esrijson_wm(geostore_id): """ get esrijson from geostore and reproject is into webmercator """ geostore = get_geostore(geostore_id) if "errors" in geostore.keys(): return error(status=400, detail=geostore["errors"]) else: esrijson = reproject_esrijson(geostore["data"]["attributes"]["esrijson"]) return esrijson
def _validate_mosaic_rule(mosaic_rule): """Validation""" # may have an optional mosaic rule. Rule must be a valid JSON # logging.debug('[VALIDATOR]: validate mosaic rule: {}'.format(mosaic_rule)) if mosaic_rule: try: json.loads(mosaic_rule) except ValueError: return error(status=400, detail="mosaicRule not a valid JSON") else: pass
def internal_server_error(e): return error(status=500, detail='Internal Server Error')
def gone(e): return error(status=410, detail='Gone')
def method_not_allowed(e): return error(status=405, detail='Method Not Allowed')
def page_not_found(e): return error(status=404, detail='Not Found')
def forbidden(e): return error(status=403, detail='Forbidden')
def wrapper(*args, **kwargs): logging.info('[VALIDATOR]: validate image service') server = request.args.get('server', None) service = request.args.get('service', None) server_url = request.args.get('serverUrl', None) geostore = request.args.get('geostore', None) pixel_size = request.args.get('pixelSize', None) rendering_rule = request.args.get('renderingRule', None) mosaic_rule = request.args.get('mosaicRule', None) if mosaic_rule == '': mosaic_rule = None logging.debug('[VALIDATOR]: server = {}'.format(server)) logging.debug('[VALIDATOR]: service = {}'.format(service)) logging.debug('[VALIDATOR]: server_url = {}'.format(server_url)) logging.debug('[VALIDATOR]: geostore = {}'.format(geostore)) logging.debug('[VALIDATOR]: pixel_size = {}'.format(pixel_size)) logging.debug('[VALIDATOR]: rendering_rule = {}'.format(rendering_rule)) logging.debug('[VALIDATOR]: mosaic_rule = {}'.format(mosaic_rule)) v = _validate_rendering_rule(rendering_rule) if v: logging.debug('[VALIDATOR]: {}'.format(json.loads(v[0].data))) return v v = _validate_mosaic_rule(mosaic_rule) if v: logging.debug('[VALIDATOR]: {}'.format(json.loads(v[0].data))) return v v = _validate_geostore(geostore) if v: logging.debug('[VALIDATOR]: {}'.format(json.loads(v[0].data))) return v v = _validate_pixel_size(pixel_size) if v: logging.debug('[VALIDATOR]: {}'.format(json.loads(v[0].data))) return v v = _validate_server(server, server_url) if v: logging.debug('[VALIDATOR]: {}'.format(json.loads(v[0].data))) return v v = _validate_service(service) if v: logging.debug('[VALIDATOR]: {}'.format(json.loads(v[0].data))) return v service_url = get_image_service_url(server, server_url, service) logging.debug('[VALIDATOR]: service_url {}'.format(service_url)) try: r = requests.get(service_url + "?f=pjson") if r.status_code == 200: if not (r.json()["serviceDataType"][:16] == 'esriImageService'): return error(status=400, detail="Not a valid Image Service URL") else: return error(status=400, detail="Not a valid Image Service URL") except: return error(status=400, detail="Not a valid Image Service URL") return func(*args, **kwargs)