Geodatabase as workspace in Python

351
1
02-02-2013 12:42 AM
AngelaBlanco
New Contributor
I want to create a geodatabase as workspace using python. It woks if the Geodatabase is called data.gdb. But if I change the name of the geodatabase, the FC are stored as shapefile in the external folder but not into the geodatabase

import os
import arcpy
import sys
from arcpy import env
import os.path as path
import arcpy.cartography as CA

arcpy.env.overwriteOutput = True
Building = sys.argv [1]
DirOutPut = sys.argv[2]


arcpy.management.CreateFileGDB(sys.argv[2], "data_building.gdb", "CURRENT")
##With this Geodatabase name does not work. But it works with the name data.gdb, I need that the GDB has different name than data
results_gdb = os.path.join(DirOutPut + "\\"+"data_building.gdb")

arcpy.env.workspace = results_gdb

CA.SimplifyBuilding(Building ,"Building_symply", 0.5)
Tags (2)
0 Kudos
1 Reply
T__WayneWhitley
Frequent Contributor
Too many confusing variable references going on.  I see a problem with how you are referencing os.path, you're importing as 'path'....then later in the script you're still using os.path.
So, not sure if that's your problem, but still bad practice because how do you know how 'os.path' is being interpreted if you told Python that 'path' itself represents 'os.path' (with 'import os.path as path')??

I suggest this, simplify your 'os' import statement (if you aren't going to use the 'path' ref anyway) and until you get a better idea, just use the single import statement as shown:
import os, sys, arcpy
arcpy.env.overwriteOutput = True
Building = sys.argv[1]
DirOutPut = sys.argv[2]
 
arcpy.CreateFileGDB_management(DirOutPut, "data_building.gdb", "CURRENT") 
arcpy.env.workspace = os.path.join(DirOutPut, "data_building.gdb")


arcpy.SimplifyBuilding_cartography(Building,"Building_symply", 0.5) 


Try that --- not sure about the syntax of the SimplifyBuilding, but think you can take if from here....notice the toolbox aliases are after the command appended with an underscore character (e.g., '_management').  Probably other ways are acceptable, but this is the common way.

Enjoy,
Wayne


EDIT:
I don't see anything wrong with reference statements such as 'import arcpy.cartography as CA' coupled with your later statement 'CA.SimplifyBuilding...', but can be more confusing until you grow more accustomed to using such references.

Also, fetching arguments with 'sys.argv' is ok, but also consider the get parameter methods of arcpy - such as:

Building = arcpy.GetParameterAsText(0)


See--- (and also notice the other varieties under the 'Getting and Setting Parameters' topic)

GetParameterAsText (arcpy)
Desktop » Geoprocessing » ArcPy » ArcPy functions
http://resources.arcgis.com/en/help/main/10.1/index.html#//018v00000047000000
0 Kudos