Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create DeleteFeatures_onebyone_Portal.ipynb #1653

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 182 additions & 0 deletions samples/05_content_publishers/DeleteFeatures_onebyone_Portal.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
{
"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
}