4 votos

Error al exportar a una nueva carpeta usando ArcPy

Quiero crear una copia de seguridad de una clase de entidad y colocarlo en una carpeta nueva con el día de la fecha escrita en el nombre de la carpeta. También necesito a un archivo de salida para ser un shapefile.

La siguiente secuencia de comandos correctamente crear la nueva carpeta en el estilo que yo quería, pero no puede crear la salida:

ExecuteError: ERROR 000210: No se puede crear una salida \C:\BackupData\"C:\BackupData\GM_06April2017\LITTER_BINS.shp no pudo ejecutar (Seleccionar).

import arcpy
import os
import datetime
from arcpy import env

#Set the date to my preferred format ie: 06April2017
Cur_Date = datetime.datetime.now().strftime("%d%B%Y")
print Cur_Date

# Overwrite pre-existing files
arcpy.env.overwriteOutput = True

# Create a new folder called 'GM_' with the current date added to the folder name. Ie. GM_06April2017
newpath = r'C:\BackupData\GM_' + Cur_Date
if not os.path.exists(newpath): os.makedirs(newpath)

#Take a feature class called 'LITTER_BINS' from a File GDB and copy it to the newly created folder, but in shapefile format.
print "Starting backup process..."
env.workspace = r"C:\GIS_folder\Grounds_Maintenance.gdb"
arcpy.Select_analysis("LITTER_BINS", r"C:\Data\GM_" + newpath + "\LITTER_BINS.shp")
print "Bins"

print "Script finished"

4voto

Alex Tereshenkov Puntos 13433

Podría haber utilizado la herramienta de geoprocesamiento Copy Features , no el Select ya que no proporciona ninguna consulta SQL. Además, debe construir las rutas utilizando os.path.join correctamente. Intente imprimir las variables antes de ejecutar las funciones arcpy u obtenga un PyScripter y ejecútelo en el modo de depuración.

 import arcpy
import os
import datetime
from arcpy import env

#Set the date to my preferred format ie: 06April2017
Cur_Date = datetime.datetime.now().strftime("%d%B%Y")
print Cur_Date

# Overwrite pre-existing files
arcpy.env.overwriteOutput = True

# Create a new folder called 'GM_' with the current date added to the folder name. Ie. GM_06April2017
newpath = r'C:\GIS\Temp\BackupData\GM_{custom_date}'.format(custom_date=Cur_Date)
if not os.path.exists(newpath):
    os.makedirs(newpath)

#Take a feature class called 'LITTER_BINS' from a File GDB and copy it to the newly created folder, but in shapefile format.
print "Starting backup process..."
env.workspace = r"C:\GIS\Temp\ArcGISHomeFolder\Default.gdb"

fc_name = 'cities'
out_shp_path = os.path.join(newpath,'{fc_name}.shp'.format(fc_name=fc_name))
arcpy.CopyFeatures_management(fc_name, out_shp_path)
print "Bins"
print "Script finished"
 

1voto

heavyd Puntos 8845

Gracias por todos los consejos. Aquí está mi guión final que funciona un encanto. Tenga en cuenta que no necesité usar os.path.join en absoluto ... ¿Es la mejor práctica usarlo? ¿O no es siempre necesario? De todos modos aquí está (note que los directorios son mis directorios de la vida real)

 print "Script started..."
print "***"
print "This monthly script will back up all grounds maintenance feature classes as shapefiles."
print "***"
print "The backup folder is:"
print "K:\GIS2016\GIS_Data\Curo_data\Estates_data\Grounds_Maintenance\Grounds_Maintenance_Backups"
import arcpy
import os
import datetime
from arcpy import env


Cur_Date = datetime.datetime.now().strftime("%d%B%Y")
print Cur_Date

print "***"
print "Setting script to overwrite previous files..."
# Overwrite pre-existing files
arcpy.env.overwriteOutput = True


newpath = r'\\somernt\curo\GIS\GIS2016\GIS_Data\Curo_data\Estates_data\Grounds_Maintenance\Grounds_Maintenance_Backups\GM_' + Cur_Date
if not os.path.exists(newpath): os.makedirs(newpath)


print "***"
print "Starting backup process..."
env.workspace = r"\\somernt\curo\GIS\GIS2016\GIS_Data\Curo_data\Estates_data\Grounds_Maintenance\Grounds_Maintenance.gdb"
arcpy.CopyFeatures_management("LITTER_BINS", newpath + "\\LITTER_BINS.shp")
print "Bins"
arcpy.CopyFeatures_management("AMENITY_GRASS", newpath + "\\AMENITY_GRASS.shp")
print "Amenity grass"
arcpy.CopyFeatures_management("AMENITY_GRASS_SHELTERED", newpath + "\\AMENITY_GRASS_SHELTERED.shp")
print "Sheltered grass"
arcpy.CopyFeatures_management("HEDGES_1SIDE_ONLY", newpath + "\\HEDGES_1SIDE_ONLY.shp")
print "Hedges- 1 side only"
arcpy.CopyFeatures_management("HEDGES_1SIDE_TOP", newpath + "\\HEDGES_1SIDE_TOP.shp")
print "Hedges- 1 side and top"
arcpy.CopyFeatures_management("HEDGES_2SIDES_TOP", newpath + "\\HEDGES_2SIDES_TOP.shp")
print "Hedges- 2 side and top"
arcpy.CopyFeatures_management("LEAF_CLEARANCE", newpath + "\\LEAF_CLEARANCE.shp")
print "Leaf clearance"
arcpy.CopyFeatures_management("MEADOW_GRASS", newpath + "\\MEADOW_GRASS.shp")
print "Meadow grass"
arcpy.CopyFeatures_management("ROUGH_GROUND", newpath + "\\ROUGH_GROUND.shp")
print "Rough ground"
arcpy.CopyFeatures_management("SHELTERED_GRITTING_AREAS", newpath + "\\SHELTERED_GRITTING_AREAS.shp")
print "Sheltered gritting areas"
arcpy.CopyFeatures_management("SHRUBS_ORNAMENTAL", newpath + "\\SHRUBS_ORNAMENTAL.shp")
print "Shrubs"
arcpy.CopyFeatures_management("SHRUBS_WALL", newpath + "\\SHRUBS_WALL.shp")
print "Shrub wall"
arcpy.CopyFeatures_management("SOMER_GRIT_BINS", newpath + "\\SOMER_GRIT_BINS.shp")
print "Grit bins"
arcpy.CopyFeatures_management("SWEEPING", newpath + "\\SWEEPING.shp")
print "Sweeping (hard surfaces)"


print "Script finished"
 

i-Ciencias.com

I-Ciencias es una comunidad de estudiantes y amantes de la ciencia en la que puedes resolver tus problemas y dudas.
Puedes consultar las preguntas de otros usuarios, hacer tus propias preguntas o resolver las de los demás.

Powered by:

X