Exemplo n.º 1
0
def main(args):
    # initialize logging
    formatter = logging.Formatter(
        "[%(asctime)s] [%(filename)30s:%(lineno)4s - %(funcName)30s()]\
                [%(threadName)5s] [%(name)10.10s] [%(levelname)8s] %(message)s"
    )
    # Grab the root logger
    logger = logging.getLogger()
    # Set the root logger logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
    logger.setLevel(logging.DEBUG)
    # Create a handler to print to the console
    sh = logging.StreamHandler(sys.stdout)
    sh.setFormatter(formatter)
    sh.setLevel(logging.INFO)
    # Create a handler to log to the specified file
    if args.log_file:
        rh = logging.handlers.RotatingFileHandler(args.log_file,
                                                  mode='a',
                                                  maxBytes=10485760)
        rh.setFormatter(formatter)
        rh.setLevel(logging.DEBUG)
        logger.addHandler(rh)
    # Add the handlers to the root logger
    logger.addHandler(sh)

    # Create the GIS
    logger.info("Authenticating...")
    gis = GIS(args.org,
              args.username,
              args.password,
              verify_cert=not args.skip_ssl_verification)
    if gis.properties["isPortal"]:
        raise RuntimeError("This script only works with ArcGIS Online")
    logger.info("Getting Workforce Project...")
    item = gis.content.get(args.project_id)
    if item is None:
        raise RuntimeError("Invalid Project Id")
    project = Project(gis.content.get(args.project_id))
    try:
        if not project._is_v2_project:
            raise Exception(
                "The project provided is not a v2 project. You can only use v2 (offline-enabled) projects with this script"
            )
    except AttributeError:
        raise Exception(
            "Cannot find the attribute is v2 project. Are you sure you have the API version 1.8.3 or greater installed? "
            "Check with `arcgis.__version__` in your Python console")
    logger.info("Phase 1: Joining assignments to assignment types...")
    d = int(datetime.datetime.now().timestamp())
    assignments_to_types = create_joined_view(
        gis, project.assignments_layer, project.assignment_types_table,
        project._assignment_schema.assignment_type,
        project._assignment_types.global_id,
        f"{project.title} Intermediate View 0 {d}", assignment_fields,
        assignment_type_fields)
    logger.info("Phase 2: Joining assignments to workers...")
    assignments_to_workers = create_joined_view(
        gis,
        project.assignments_layer,
        project.workers_layer,
        project._assignment_schema.worker_id,
        project._worker_schema.global_id,
        f"{project.title} Intermediate View 1 {d}",
        assignment_fields,
        worker_fields,
    )
    logger.info("Phase 3: Joining assignments to dispatchers...")
    assignments_to_dispatchers = create_joined_view(
        gis, project.assignments_layer, project.dispatchers_layer,
        project._assignment_schema.dispatcher_id,
        project._dispatcher_schema.global_id,
        f"{project.title} Intermediate View 2 {d}", assignment_fields,
        dispatcher_fields)
    change_source_field_name_to_joined_field_name(assignment_type_fields)
    change_source_field_name_to_joined_field_name(worker_fields)
    change_source_field_name_to_joined_field_name(dispatcher_fields)
    logger.info(
        "Phase 4: Joining assignments and assignment types to assignments and workers..."
    )
    assignments_types_workers = create_joined_view(
        gis, assignments_to_types.layers[0], assignments_to_workers.layers[0],
        project._assignment_schema.global_id,
        project._assignment_schema.global_id,
        f"{project.title} Intermediate View 3 {d}",
        assignment_type_fields + assignment_fields, worker_fields)
    logger.info(
        "Phase 5: Joining assignments and types and workers to  assignments and workers..."
    )
    if args.name:
        name = args.name
    else:
        name = f"{project.title} Joined View {d}"
    final_item = create_joined_view(
        gis, assignments_types_workers.layers[0],
        assignments_to_dispatchers.layers[0],
        project._assignment_schema.global_id,
        project._assignment_schema.global_id, name,
        assignment_fields + assignment_type_fields + worker_fields,
        dispatcher_fields)
    logger.info(f"Final Item: {final_item.title}")
    if args.create_dashboard:
        logger.info("Creating dashboard")

        # create new webmap
        map_item = project.dispatcher_webmap.save(
            item_properties={
                "title": project.title + " Dashboard Map",
                "tags": [],
                "snippet": "Dashboard Map"
            })
        new_webmap = WebMap(map_item)

        # swizzle in joined layer instead of assignments layer
        for i, layer in enumerate(new_webmap.layers):
            if layer["id"] == "Assignments_0":
                new_webmap.remove_layer(layer)
                new_webmap.add_layer(final_item)
                new_webmap.layers[i]["id"] = "Assignments_0"
                break
        new_webmap.update()

        # clone dashboard with your data instead of our data
        item = gis.content.get("af7cd356c21a4ded87d8cdd452fd8be3")
        item_mapping = {
            '377b2b2014f24b0ab9b053d9b2fed113': final_item.id,
            'e1904f5c56484163a021155f447adf34': project.workers_item.id,
            'bb7d2b495ecc4ea7810b28f16ef71cba': new_webmap.item.id
        }
        cloned_items = gis.content.clone_items([item],
                                               item_mapping=item_mapping,
                                               search_existing_items=False)
        if len(cloned_items) == 0:
            raise ValueError("Creating dashboard failed")

        # Save new name and share to group
        logger.info("Updating title and sharing to project group")
        new_title = project.title + " Dashboard"
        cloned_items[0].update(item_properties={"title": new_title})
        cloned_items[0].share(groups=[project.group])
        logger.info("Dashboard creation completed")
    logger.info("Script completed")
Exemplo n.º 2
0
def update_maps(q, old_id, new_layer):
    # for each map on the server,
    # Find a reference to the new layer
    # In the map,
    #   Find the old layer
    #   Remove it
    #   Insert the new layer in the same place

    count = 0
    msg = ''
    maps = gis.content.search(q,
                              max_items=1000,
                              sort_field="title",
                              sort_order="asc",
                              outside_org=False,
                              item_type="Web Map")
    print("Found %d maps." % len(maps))

    for map in maps:
        i_did_update = False

        map_info = "%s (%s) %s" % (map.title, map.owner, map.type)
        #display(web_map)
        count += 1

        web_map = WEBMAP(map)
        for layer in web_map.layers:

            # I dont care about labels right now because i use a different workflow.
            # They can be directly replaced in Portal.
            #        if layer.layerType == 'VectorTileLayer' and layer.title == old_label_name:
            #           # I see you, old labels.
            #           print("county labels updated")
            #          i_did_update = True
            #           web_map.remove_layer(layer)
            #web_map.add_layer(new_label)

            try:
                layerId = layer.itemId
                layerType = layer.layerType
            except AttributeError:
                layerId = ''
                layerType = ''

            if layerType == 'ArcGISTiledMapServiceLayer' and layerId == old_id:
                msg += map_info + "\n"
                msg += "\ttitle:\"%s\" id:%s itemId:%s\n" % (layer.title,
                                                             layer.id, layerId)

                print("new layer", new_id)

                # ALAS ALAS
                # this puts the "added" layer at the TOP of the layer list.
                # making this script, well, useless

                web_map.remove_layer(layer)
                web_map.add_layer(new_layer,
                                  options={"title": "Clatsop County"})
                i_did_update = True
            else:
                print("32%s \"%s\" %s" % (layerId, layer.title, layerType))
            pass

        if i_did_update:

            # "update" will save changes made to the web map
            # alternatively "save" will create a new copy of the map.
            # Thumbnail can be a local file (to upload) or a URL.

            # UPDATE
            web_map.update()
            comment = "%s updated by \"%s\"" % (datestamp, myname)
            item = ITEM(gis, web_map.item.id)
            item.add_comment(comment)
            """
            # SAVE
            old_title = web_map.item.title

            # I need to grab the existing thumbnail from Portal
            #watermarked_thumbnail = "thumbnail_" + datestring + ".jpg" 
            #watermark("thumbnail.jpg", watermarked_thumbnail, old_title, (0,200,200,128))
            
            # comment indicates new map created
            comment = "%s created from \"%s\" by \"%s\"" % (datestamp, web_map.item.title, myname)

            item = web_map.save(item_properties={
                # These are REQUIRED
                    "title":   "CHANGED " +  old_title + ' ' + datestamp, 
                    "snippet": web_map.item.snippet, 
                    "tags":    web_map.item.tags
                },
                #thumbnail = watermarked_thumbnail,
#                folder = "TESTING_Brian"
            )
            item.add_comment(comment)
            """

    print(msg)
    return count