From 10fd0f87cce91ed7f1f94a4c9b5d11de875f0243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Mes=C3=A9n-Delgado?= <55415644+rmesend@users.noreply.github.com> Date: Mon, 11 Sep 2023 10:25:55 -0600 Subject: [PATCH 1/2] Create DeleteFeatures_onebyone_Portal.ipynb one notebook to delete elements in AGOL one by one. --- .../DeleteFeatures_onebyone_Portal.ipynb | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 samples/05_content_publishers/DeleteFeatures_onebyone_Portal.ipynb diff --git a/samples/05_content_publishers/DeleteFeatures_onebyone_Portal.ipynb b/samples/05_content_publishers/DeleteFeatures_onebyone_Portal.ipynb new file mode 100644 index 0000000000..030306bc06 --- /dev/null +++ b/samples/05_content_publishers/DeleteFeatures_onebyone_Portal.ipynb @@ -0,0 +1,32 @@ +from arcgis.gis import GIS + +# Iniciar sesión en ArcGIS Online +gis = GIS("https://www.arcgis.com", "YOURUSER", "YOURPASSWORD") + +# Obtener el Feature Service por su ID +feature_service_id = "YOURFEATURESERVICEID" # Ejemplo: 1234567890abcdef1234567890abcdef +feature_service = gis.content.get(feature_service_id) + +print("Feature Service: " + feature_service.title) + + +# Obtener la capa dentro del Feature Service +layer = feature_service.layers[0] # can be ajusted + + +print("Feature Service: " + layer.properties.name) + + +# Consultar los elementos que deseas eliminar (por ejemplo, todos los elementos) +query_result = layer.query() + + +print("Número de elementos a eliminar: " + str(len(query_result.features))) + + +feature.attributes + + +# Borrar cada elemento uno por uno +for feature in query_result.features: + layer.edit_features(deletes=str(feature.attributes['objectid'])) From 0879e74aaf82dabb5cfde3a60ea9c993aab1cc9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Mes=C3=A9n-Delgado?= <55415644+rmesend@users.noreply.github.com> Date: Tue, 12 Sep 2023 09:21:26 -0600 Subject: [PATCH 2/2] Delete features row by row MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This Python code leverages the `arcgis` library to interact with ArcGIS Online. It begins by establishing a session on ArcGIS Online using your credentials (username and password). You'll need to replace `"tu_usuario"` and `"tu_contraseña"` with your actual ArcGIS Online username and password. Next, it identifies a specific Feature Service within ArcGIS Online by its ID, which you should replace with the actual ID of your desired Feature Service. The code then accesses the first layer within this Feature Service and performs a query to retrieve a list of elements from the layer. Subsequently, it goes through each retrieved element one by one, deleting them individually using the `edit_features` method with the 'deletes' operation. The 'OBJECTID' attribute is used to uniquely identify and remove each element. Finally, it prints a success message to indicate that the elements have been successfully deleted. It's important to ensure that you have the necessary permissions to perform editing operations on the Feature Service you are working with. This is an update of this FAQ: https://support.esri.com/en-us/knowledge-base/faq-is-it-possible-to-batch-delete-features-in-arcgis-o-000013805 --- .../DeleteFeatures_onebyone_Portal.ipynb | 214 +++++++++++++++--- 1 file changed, 182 insertions(+), 32 deletions(-) diff --git a/samples/05_content_publishers/DeleteFeatures_onebyone_Portal.ipynb b/samples/05_content_publishers/DeleteFeatures_onebyone_Portal.ipynb index 030306bc06..8286f96008 100644 --- a/samples/05_content_publishers/DeleteFeatures_onebyone_Portal.ipynb +++ b/samples/05_content_publishers/DeleteFeatures_onebyone_Portal.ipynb @@ -1,32 +1,182 @@ -from arcgis.gis import GIS - -# Iniciar sesión en ArcGIS Online -gis = GIS("https://www.arcgis.com", "YOURUSER", "YOURPASSWORD") - -# Obtener el Feature Service por su ID -feature_service_id = "YOURFEATURESERVICEID" # Ejemplo: 1234567890abcdef1234567890abcdef -feature_service = gis.content.get(feature_service_id) - -print("Feature Service: " + feature_service.title) - - -# Obtener la capa dentro del Feature Service -layer = feature_service.layers[0] # can be ajusted - - -print("Feature Service: " + layer.properties.name) - - -# Consultar los elementos que deseas eliminar (por ejemplo, todos los elementos) -query_result = layer.query() - - -print("Número de elementos a eliminar: " + str(len(query_result.features))) - - -feature.attributes - - -# Borrar cada elemento uno por uno -for feature in query_result.features: - layer.edit_features(deletes=str(feature.attributes['objectid'])) +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "from arcgis.gis import GIS\n", + "\n", + "# Iniciar sesión en ArcGIS Online\n", + "gis = GIS(\"https://www.arcgis.com\", \"YOURUSER\", \"YOURPASSWORD\")\n", + "\n", + "# Obtener el Feature Service por su ID\n", + "feature_service_id = \"YOURFEATURESERVICEID\" # Ejemplo: 1234567890abcdef1234567890abcdef\n", + "feature_service = gis.content.get(feature_service_id)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Feature Service: Servicios Técnicos\n" + ] + } + ], + "source": [ + "print(\"Feature Service: \" + feature_service.title)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "# Obtener la capa dentro del Feature Service, request the layer by index\n", + "layer = feature_service.layers[0] # Puedes ajustar el índice según tu cas de uso" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Feature Service: Servicios_Tecnicos\n" + ] + } + ], + "source": [ + "print(\"Feature Service: \" + layer.properties.name)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "# Consultar los elementos que deseas eliminar (por ejemplo, todos los elementos) , consult the features you want to delete (e.g. all features)\n", + "query_result = layer.query()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Número de elementos a eliminar: 64025\n" + ] + } + ], + "source": [ + "print(\"Número de elementos a eliminar: \" + str(len(query_result.features))) # Imprimir el número de elementos a eliminar , print the number of features to delete" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'objectid': 1,\n", + " 'globalid': '3929a7d7-8183-412a-94ec-df0bbb86d249',\n", + " 'zona': '1',\n", + " 'finca': 'Los Ángeles',\n", + " 'MuestreosST': 'Plagas fruta',\n", + " 'id_muestreador': 76,\n", + " 'lote': 21422,\n", + " 'seccion': 100,\n", + " 'grupo': 22,\n", + " 'cosecha': 2,\n", + " 'edad_plagas1': None,\n", + " 'block1': None,\n", + " 'camas_plagasblock': None,\n", + " 'camas_plagas1medio': None,\n", + " 'camas_plagas1medioATK': None,\n", + " 'camas_plagasblockATK': None,\n", + " 'edad_fruta1': '117',\n", + " 'block2': 'medio',\n", + " 'camas_fruta1': '1',\n", + " 'edad_larva1': None,\n", + " 'block3': None,\n", + " 'camas_larva1': None,\n", + " 'edad_pdt1': None,\n", + " 'evaluacion': None,\n", + " 'edad_prepa1': None,\n", + " 'blockprepa': None,\n", + " 'CreationDate': 1685038623651,\n", + " 'Creator': 'Admin_Pindeco',\n", + " 'EditDate': 1685120477962,\n", + " 'Editor': 'Admin_Pindeco'}" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "feature.attributes" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + " # Borrar cada elemento uno por uno , DeleteFeatures_onebyone\n", + "for feature in query_result.features:\n", + " layer.edit_features(deletes=str(feature.attributes[\"objectid\"]))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# borrar todos los elementos de una sola vez - Delete all features at once\n", + "layer.edit_features(deletes=str([feature.attributes['objectid'] for feature in query_result.features]))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.16" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}