# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] slideshow={"slide_type": "slide"}
# # 02 - Managing Content
#
# ~ <NAME>
#
# <img src="./img/managing_content.png"  width=75%/>

# + [markdown] slideshow={"slide_type": "subslide"}
# ## What is Content?
#
# - Digital information
# - Excel spreadsheets, spatial data, etc..
# - Services
# - Web Maps
# - Imagery and Raster Data

# + [markdown] slideshow={"slide_type": "subslide"}
# ## What is Content?
#
# - Organizations allow users and administrators to place content into logical groups
# - Items, which content is derived, can be given metadata
#
#     + title
#     + type
#     + tags
#     
# - Item `tags` provide searchable content
# - Content can be categorized
# - Authoritative markings
#

# + [markdown] slideshow={"slide_type": "subslide"}
# ## What a User Sees
#
# <img src="./img/sample_item.png" />

# + [markdown] slideshow={"slide_type": "subslide"}
# ## What a Programmer Sees
#

# + slideshow={"slide_type": "-"}
from arcgis.gis import GIS
gis = GIS(profile='your_online_profile')
item = gis.content.get("ab399b847323487dba26809bf11ea91a")
item

# + slideshow={"slide_type": "fragment"}
item.__dict__

# + [markdown] slideshow={"slide_type": "slide"}
# ## Creating New Content
#
# - Now that we understand what content is, how do we get it into Portal programmatically?
#
# - The `ContentManager` is the actor we work with.
# - To put anyone of portal, we will use the `add` method along with some `ItemProperties`
#     + `title`, `type`, `tags`

# + [markdown] slideshow={"slide_type": "subslide"}
# ## Adding Content
#
# - `add()` uploads content to a GIS
# - Returns an `Item`
# - Item Properties - information about the item
#     + title, type and tags are always **required**

# + slideshow={"slide_type": "-"}
gis.content

# + slideshow={"slide_type": "-"}
new_item = gis.content.add(item_properties={
    'title' : 'School_Enrollment_Areas',
    'type' : "File Geodatabase",
    'tags' : 'Berlin, DevSummit'
}, data=r"./content/School_enrollment_areas.gdb.zip")
new_item

# + [markdown] slideshow={"slide_type": "subslide"}
# ## Question: I added spatial data, is it a service?

# + [markdown] slideshow={"slide_type": "subslide"}
# ## Updating Items
#
# - Allows for the modification of all `Item` properties
# - Add metadata and/or thumbnails
#

# + slideshow={"slide_type": "-"}
new_item.update(thumbnail='./content/tiger_thumbnail.jpg')
new_item

# + [markdown] slideshow={"slide_type": "subslide"}
# ## Publishing Data
#
# - Adding an item only places the data on the site.
# - It's sort of like a Google Drive and/or a cloud harddrive
# - Certain `Item` types can be published.
#     + sd, Feature Classes, csv, etc...
# - This creates a Hosted Feature Layer, Vector Tile, Tile Service

# + slideshow={"slide_type": "-"}
pitem = new_item.publish()
pitem

# + [markdown] slideshow={"slide_type": "subslide"}
# ## Visualized Map View

# + slideshow={"slide_type": "-"}
m = gis.map('Berlin')
m

# + slideshow={"slide_type": "-"}
m.add_layer(pitem)

# + [markdown] slideshow={"slide_type": "subslide"}
# ## Protecting Data
#
# - Provides the ability to disseminate data with `sharing`
# - Protecting ensure content does not get erased!
#     + There is **NO** undo button on AGOL/Enterprise

# + slideshow={"slide_type": "-"}
pitem.protect(True)
# -

pitem.delete()

# + slideshow={"slide_type": "subslide"}
pitem.protect(False)

# + slideshow={"slide_type": "-"}
pitem.delete()

# + [markdown] slideshow={"slide_type": "subslide"}
# ## Sharing Content
#
# <img src="./img/sharing.jpg"  width=70%/>
#
# - Once you have information as services and other data, it's important to allow others to access it

# + slideshow={"slide_type": "subslide"}
new_item.share(everyone=True)

# + slideshow={"slide_type": "-"}
new_item.shared_with

# + [markdown] slideshow={"slide_type": "subslide"}
# ## Creating Empty Services
#
# - Create services without data or an item to publish from
# - Fully customize schema via JSON

# + [markdown] slideshow={"slide_type": "-"}
# **1. Check if Service Name is Free**

# + slideshow={"slide_type": "fragment"}
gis.content.is_service_name_available("EuroDevSummit", service_type="featureService")

# + [markdown] slideshow={"slide_type": "subslide"}
# **2. Create an Empty Feature Service**

# + slideshow={"slide_type": "-"}
item = gis.content.create_service(name="EuroDevSummit")
item

# + [markdown] slideshow={"slide_type": "subslide"}
# **3. Create a New Layer**

# + slideshow={"slide_type": "-"}
print(item.layers)
# Get the FS and the Manager
from arcgis.features import FeatureLayerCollection
fs = FeatureLayerCollection.fromitem(item)
mgr = fs.manager

# + slideshow={"slide_type": "-"}
mgr.properties

# + slideshow={"slide_type": "subslide"}
import json
with open("./content/layer.json", 'r') as reader:
    print(mgr.add_to_definition(json_dict=json.loads(reader.read())))
item.layers[0]

# + slideshow={"slide_type": "skip"}
item.delete()

# + [markdown] slideshow={"slide_type": "slide"}
# ## Cloning Content
#
# <img src="./img/cloning.jpg" />
#
# - Allows for the copying of Hosted Feature Layers and items from one site to another
# - **WILL NOT** configure applications
#

# + [markdown] slideshow={"slide_type": "subslide"}
# **`clone_items` provides the ability to copy content from one site to another**
#
# <img src="./img/clone_items.jpg" />

# + [markdown] slideshow={"slide_type": "slide"}
# ### 15 Minute Break

StarcoderdataJupyterScriptsDedupFiltered