Exemplo n.º 1
0
def _future_op(gptool, task_url, job_info, job_id, param_db, return_values,
               return_messages):

    job_info = _analysis_job_status(gptool, task_url, job_info)
    resp = _analysis_job_results(gptool, task_url, job_info, job_id)

    # ---------------------async-out---------------------#
    output_dict = {}
    for retParamName in resp.keys():
        output_val = resp[retParamName]
        try:
            ret_param_name, ret_val = _get_output_value(
                gptool, output_val, param_db, retParamName)
            output_dict[ret_param_name] = ret_val
        except KeyError:
            pass  # cannot handle unexpected output as return tuple will change

    # tools with output map service - add another output:
    # result_layer = '' #***self.properties.resultMapServerName
    if gptool.properties.resultMapServerName != '':
        job_id = job_info.get("jobId")
        result_layer_url = gptool._url.replace(
            '/GPServer', '/MapServer') + '/jobs/' + job_id

        output_dict['result_layer'] = MapImageLayer(result_layer_url,
                                                    gptool._gis)

    num_returns = len(resp)
    if return_messages:
        return _return_output(num_returns, output_dict,
                              return_values), job_info

    return _return_output(num_returns, output_dict, return_values)
Exemplo n.º 2
0
    def validation(self):
        """
        Provides access to a validation manager.

        :returns: ValidationManager
        """
        if self._validation is None:
            from arcgis.mapping import MapImageLayer
            ms = MapImageLayer(url=os.path.dirname(self._flc.url) +
                               "/MapServer",
                               gis=self._gis)
            if 'validationserver' in ms.properties.supportedExtensions.lower():
                from arcgis.features._validation import ValidationManager
                url = os.path.dirname(self._flc.url) + "/ValidationServer"
                self._validation = ValidationManager(url=url,
                                                     version=self,
                                                     gis=self._gis)
        return self._validation
Exemplo n.º 3
0
    def _execute(self, params):
        caller_fnname = inspect.stack()[1][3]

        # print("Will call " + url +  " with these parameters:")

        name_type = self._method_params[caller_fnname]
        name_name = self._param_names[caller_fnname]
        return_values = self._return_values[caller_fnname]

        task_name = name_type[caller_fnname]
        url = self.url + "/" + task_name + "/execute"

        #---------------------in---------------------#

        for key, value in params.items():
            # print(k + " = " + str(v))
            if key in name_type:
                py_type = name_type[key]
                if py_type in [FeatureSet, LinearUnit, DataFile, RasterData]:
                    if type(value) in [
                            FeatureSet, LinearUnit, DataFile, RasterData
                    ]:
                        params[key] = value.to_dict()
                    elif _is_geoenabled(value) or hasattr(value, 'spatial'):
                        params[key] = value.spatial.__feature_set__
                    elif type(value) in [SpatialDataFrame]:
                        params[key] = value.__feature_set__
                    elif type(value) == str:
                        try:
                            klass = py_type  # type[value]
                            params[key] = klass.from_str(value)
                        except:
                            pass
                elif py_type == datetime.datetime:
                    params[key] = _date_handler(value)
        #--------------------------------------------#

        params.update({"f": "json"})

        gp_params = {}

        for param_name, param_value in params.items():
            gp_param_name = name_name.get(param_name, param_name)
            gp_params[gp_param_name] = param_value

        # copy environment variables if set
        if 'env:outSR' not in params and arcgis.env.out_spatial_reference is not None:
            gp_params['env:outSR'] = arcgis.env.out_spatial_reference

        if 'env:processSR' not in params and arcgis.env.process_spatial_reference is not None:
            gp_params['env:processSR'] = arcgis.env.process_spatial_reference

        if 'returnZ' not in params and arcgis.env.return_z is not False:
            gp_params['returnZ'] = True

        if 'returnM' not in params and arcgis.env.return_m is not False:
            gp_params['returnM'] = True

        resp = None

        if self.properties.executionType == 'esriExecutionTypeSynchronous':
            resp = self._con.post(url, gp_params, token=self._token)

            output_dict = {}

            for result in resp['results']:
                retParamName = result['paramName']
                ret_param_name = _camelCase_to_underscore(retParamName)
                ret_type = name_type[ret_param_name]
                ret_val = None
                if ret_type in [FeatureSet, LinearUnit, DataFile, RasterData]:
                    jsondict = result['value']
                    if jsondict is not None:
                        if 'mapImage' in jsondict:  # http://resources.esri.com/help/9.3/arcgisserver/apis/rest/gpresult.html#mapimage
                            ret_val = jsondict
                        else:
                            result_obj = ret_type.from_dict(jsondict)
                            result_obj._con = self._con
                            result_obj._token = self._token
                            ret_val = result_obj
                    else:
                        ret_val = jsondict
                else:
                    ret_val = result['value']

                output_dict[ret_param_name] = ret_val

            num_returns = len(resp['results'])
            if num_returns == 1:
                return output_dict[name_type['return_name']]
            else:
                ret_names = []
                for return_value in return_values:
                    ret_names.append(return_value['name'])

                NamedTuple = collections.namedtuple('ToolOutput', ret_names)
                tool_output = NamedTuple(**
                                         output_dict)  #TODO: preserve ordering
                return tool_output

        else:
            task_url = "{}/{}".format(self.url, task_name)
            submit_url = "{}/submitJob".format(task_url)
            job_info = self._con.post(submit_url, gp_params, token=self._token)
            job_id = job_info['jobId']
            try:
                isCan = False
                job_info = super()._analysis_job_status(task_url, job_info)
            except KeyboardInterrupt:
                cancel_url = "%s/jobs/%s/cancel" % (task_url,
                                                    job_info['jobId'])
                params = {'f': "json"}
                job_info = self._con.get(path=cancel_url, params=params)
                isCan = True
            if isCan:
                job_info = super()._analysis_job_status(task_url, job_info)
            resp = super()._analysis_job_results(task_url, job_info, job_id)
            # print('***'+str(resp))

            output_dict = {}
            for retParamName in resp.keys():
                ret_param_name = _camelCase_to_underscore(retParamName)
                ret_type = name_type[ret_param_name]
                ret_val = None
                if ret_type in [FeatureSet, LinearUnit, DataFile, RasterData]:
                    jsondict = resp[retParamName]
                    if jsondict is not None:
                        if 'mapImage' in jsondict:
                            ret_val = jsondict
                        else:
                            result = ret_type.from_dict(jsondict)
                            result._con = self._con
                            result._token = self._token
                            ret_val = result
                    else:
                        ret_val = jsondict
                else:
                    ret_val = resp[retParamName]

                output_dict[ret_param_name] = ret_val

            # tools with output map service - add another output:
            result_layer = self.properties.resultMapServerName
            if result_layer != '':
                #job_id = job_info.get("jobId")
                result_layer_url = self.url.replace(
                    '/GPServer', '/MapServer') + '/jobs/' + job_id

                output_dict['result_layer'] = MapImageLayer(
                    result_layer_url, self._gis)

            num_returns = len(resp)
            if num_returns == 1:
                return output_dict[name_type[
                    'return_name']]  # *** output_dict[return_values[0]['name']]
            else:
                ret_names = []
                for return_value in return_values:
                    ret_names.append(return_value['name'])

                NamedTuple = collections.namedtuple('ToolOutput', ret_names)
                tool_output = NamedTuple(**
                                         output_dict)  #TODO: preserve ordering
                return tool_output
Exemplo n.º 4
0
# Script to clone disaster management templates from ArcGIS Online solutions

# region Import libs and connect to the GIS
from arcgis.gis import GIS
from arcgis.mapping import WebMap, MapImageLayer

# connect to gis
target_gis = GIS(profile='profile_emergency')
source_gis = GIS()
print("=====================================================================\n")
print("CLONING DISASTER MANAGEMENT SOLUTION TEMPLATES")

# search for informative web layers
modis_hotspots = MapImageLayer('https://utility.arcgis.com/usrsvcs/servers/3dba28abdadd4b4a9b39aaa6e79dcb21/rest/'
                               'services/LiveFeeds/MODIS_Thermal/MapServer')
parcels_at_risk_item = target_gis.content.get('55755e4103374bac9aacd42ef78271f6')  # empty at first
parcels_at_risk_flayer = parcels_at_risk_item.layers[0]
# endregion

# Specify the damage assessment web map and dashboard items
item_ids = ['19d2496962714c0e805004cd9ff9df2d', '8ed4a4fa19d94171b838e9af2e5d8996']
source_items = [source_gis.content.get(item_id) for item_id in item_ids]

# Clone items
new_items = target_gis.content.clone_items(source_items, folder='Forest Fire',
                                           copy_data=False, search_existing_items=False)
print("Cloning items complete. Renaming items")

# Customize items for current emergency
target_group = target_gis.groups.search('Properties at risk')[0]
for new_item in new_items:
Exemplo n.º 5
0
def _execute_gp_tool(gis,
                     task_name,
                     params,
                     param_db,
                     return_values,
                     use_async,
                     url,
                     webtool=False,
                     add_token=True,
                     return_messages=False,
                     future=False):
    if gis is None:
        gis = arcgis.env.active_gis

    gp_params = {"f": "json"}

    # ---------------------in---------------------#
    for param_name, param_value in params.items():
        #print(param_name + " = " + str(param_value))
        if param_name in param_db:
            py_type, gp_param_name = param_db[param_name]
            if param_value is None:
                param_value = ''
            gp_params[gp_param_name] = param_value
            if py_type == FeatureSet:
                if webtool:
                    if isinstance(param_value, (tuple, list)):
                        gp_params[gp_param_name] = [
                            _layer_input_gp(p) for p in param_value
                        ]
                    else:
                        gp_params[gp_param_name] = _layer_input_gp(param_value)

                else:
                    try:
                        from arcgis.features.geo._accessor import _is_geoenabled
                    except:

                        def _is_geoenabled(o):
                            return False

                    if type(param_value) == FeatureSet:
                        gp_params[gp_param_name] = param_value.to_dict()
                    elif _is_geoenabled(param_value):
                        gp_params[gp_param_name] = json.loads(
                            json.dumps(param_value.spatial.__feature_set__,
                                       default=_date_handler))
                    elif type(param_value) == str:

                        try:
                            klass = py_type
                            gp_params[gp_param_name] = klass.from_str(
                                param_value)

                        except:
                            pass

            elif py_type in [LinearUnit, DataFile, RasterData]:
                if type(param_value) in [LinearUnit, DataFile, RasterData]:
                    gp_params[gp_param_name] = param_value.to_dict()

                elif type(param_value) == str:

                    try:
                        klass = py_type
                        gp_params[gp_param_name] = klass.from_str(param_value)

                    except:
                        pass

                elif isinstance(param_value, arcgis.gis.Layer):
                    gp_params[gp_param_name] = param_value._lyr_dict

            elif py_type == datetime.datetime:
                gp_params[gp_param_name] = _date_handler(param_value)
    # --------------------------------------------#

    _set_env_params(gp_params, params)

    # for param_name, param_value in gp_params.items():
    #     print(param_name + " = " + str(param_value))

    gptool = arcgis.gis._GISResource(url, gis)

    if use_async:
        task_url = "{}/{}".format(url, task_name)
        submit_url = "{}/submitJob".format(task_url)
        if add_token and submit_url.lower().find("arcgis.com") == -1:
            try:
                job_info = gptool._con.post(submit_url,
                                            gp_params,
                                            token=gptool._token)
            except:
                job_info = gptool._con.post(submit_url, gp_params)
        else:
            job_info = gptool._con.post(submit_url, gp_params)
        job_id = job_info['jobId']
        if future:
            executor = concurrent.futures.ThreadPoolExecutor(1)
            future = executor.submit(
                _future_op,
                *(gptool, task_url, job_info, job_id, param_db, return_values,
                  return_messages))
            executor.shutdown(False)
            gpjob = GPJob(future=future,
                          gptool=gptool,
                          jobid=job_id,
                          task_url=task_url,
                          gis=gptool._gis,
                          notify=arcgis.env.verbose)
            return gpjob
        job_info = _analysis_job_status(gptool, task_url, job_info)
        resp = _analysis_job_results(gptool, task_url, job_info, job_id)

        # ---------------------async-out---------------------#
        output_dict = {}
        for retParamName in resp.keys():
            output_val = resp[retParamName]
            try:
                ret_param_name, ret_val = _get_output_value(
                    gptool, output_val, param_db, retParamName)
                output_dict[ret_param_name] = ret_val
            except KeyError:
                pass  # cannot handle unexpected output as return tuple will change

        # tools with output map service - add another output:
        # result_layer = '' #***self.properties.resultMapServerName
        if gptool.properties.resultMapServerName != '':
            job_id = job_info.get("jobId")
            result_layer_url = url.replace('/GPServer',
                                           '/MapServer') + '/jobs/' + job_id

            output_dict['result_layer'] = MapImageLayer(
                result_layer_url, gptool._gis)

        num_returns = len(resp)
        if return_messages:
            return _return_output(num_returns, output_dict,
                                  return_values), job_info

        return _return_output(num_returns, output_dict, return_values)

    else:  # synchronous
        exec_url = url + "/" + task_name + "/execute"
        if add_token:
            resp = gptool._con.post(exec_url, gp_params, token=gptool._token)
        else:
            resp = gptool._con.post(exec_url, gp_params)

        output_dict = {}

        for result in resp['results']:
            retParamName = result['paramName']

            output_val = result['value']
            try:
                ret_param_name, ret_val = _get_output_value(
                    gptool, output_val, param_db, retParamName)
                output_dict[ret_param_name] = ret_val
            except KeyError:
                pass  # cannot handle unexpected output as return tuple will change

        num_returns = len(resp['results'])
        if return_messages:
            return _return_output(num_returns, output_dict,
                                  return_values), job_info
        return _return_output(num_returns, output_dict, return_values)
Exemplo n.º 6
0
    def __call__(cls, url=None, item=None, server=None, initialize=False):
        """generates the proper type of layer from a given url"""
        from .. import ServicesDirectory
        hasLayer = False
        if url is None and \
           item is None:
            raise ValueError(
                "A URL to the service or an arcgis.Item is required.")
        elif url is None and item is not None:
            url = item.url

        if isinstance(server, Connection) or \
           hasattr(server, 'token'):
            connection = server
        elif isinstance(server, (GIS, ServicesDirectory)):
            connection = server._con
        else:
            try:
                parsed = urlparse(url)
                site_url = "{scheme}://{nl}/{wa}".format(
                    scheme=parsed.scheme,
                    nl=parsed.netloc,
                    wa=parsed.path[1:].split('/')[0])
                connection = Connection(
                    baseurl=site_url)  # anonymous connection
                server = ServicesDirectory(url=site_url)
            except:
                parsed = urlparse(url)
                site_url = "https://{nl}/rest/services".format(
                    scheme=parsed.scheme, nl=parsed.netloc)
                connection = Connection(
                    baseurl=site_url,
                    all_ssl=parsed.scheme == "https")  # anonymous connection
                server = ServicesDirectory(url=site_url)
        base_name = os.path.basename(url)
        if base_name.isdigit():
            base_name = os.path.basename(url.replace("/" + base_name, ""))
            hasLayer = True
        if base_name.lower() == "mapserver":
            if hasLayer:
                return FeatureLayer(url=url, gis=server)
            else:
                return MapImageLayer(url=url, gis=server)
        elif base_name.lower() == "featureserver":
            if hasLayer:
                return FeatureLayer(url=url, gis=server)
            else:
                return FeatureLayerCollection(url=url, gis=server)
        elif base_name.lower() == "imageserver":
            return ImageryLayer(url=url, gis=server)
        elif base_name.lower() == "gpserver":
            return Toolbox(url=url, gis=server)
        elif base_name.lower() == "geometryserver":
            return GeometryService(url=url, gis=server)
        elif base_name.lower() == "mobileserver":
            return Layer(url=url, gis=server)
        elif base_name.lower() == "geocodeserver":
            return Geocoder(location=url, gis=server)
        elif base_name.lower() == "globeserver":
            if hasLayer:
                return Layer(url=url, gis=server)
            return Layer(url=url, gis=server)
        elif base_name.lower() == "geodataserver":
            return GeoData(url=url, connection=connection)
        elif base_name.lower() == "naserver":
            return NetworkDataset(url=url, gis=server)
        elif base_name.lower() == "sceneserver":
            return SceneLayer(url=url, gis=server)
        elif base_name.lower() == "schematicsserver":
            return SchematicLayers(url=url, gis=server)
        elif base_name.lower() == "vectortileserver":
            return VectorTileLayer(url=url, gis=server)
        else:
            return Layer(url=url, gis=server)
        return type.__call__(cls, url, connection, item, initialize)
Exemplo n.º 7
0
    elif answer == 'n':
        pass
    else:
        fix_answer(answer, map, webmap_name)


# get username and password
username = input('ArcGIS Online username: '******'ArcGIS Online password: '******'https://www.arcgis.com', username, passwd)
print(my_gis)

# connect to EMEF's map server, get the points and the admin boundaries
ef_points = MapImageLayer(
    'https://geopub.epa.gov/arcgis/rest/services/EMEF/efpoints/MapServer/')
ef_boundaries = MapImageLayer(
    'https://geopub.epa.gov/arcgis/rest/services/EMEF/Boundaries/MapServer/')

superfund_points = ef_points.layers[0]
toxic_releases = ef_points.layers[1]
counties = ef_boundaries.layers[5]
states = ef_boundaries.layers[6]

# we know which layers to add, so create blank webmap to add it to. This one has dark gray canvas
darkGray = my_gis.content.search('title:dark',
                                 outside_org=True,
                                 item_type='web map')[0]
my_webmap = WebMap(darkGray)

# add the feature layers to the dark gray canvas map