Let's start with a few lines about what GDB is and what it is used for.
In ArcGIS Pro, a geodatabase (geographic database) is a container for storing and managing spatial and non-spatial data.
It provides a way to organize and structure your data within the ArcGIS platform.
Geodatabases support the storage of various types of geographic data, including features, attributes, relationships, topologies, and more.
They play a crucial role in managing and analyzing spatial information efficiently.
There are two main types of geodatabases in ArcGIS Pro.
File Geodatabase (FGDB):
This is a single-user geodatabase that is stored as a folder in the file system. It is suitable for small to medium-sized projects and is a good choice when you don't need to support multi-user editing or complex data relationships. File geodatabases are fast and efficient for read and write operations.
Enterprise Geodatabase:
This type of geodatabase is designed for multi-user editing and collaboration within an enterprise environment. It is typically stored in a relational database management system (RDBMS) such as Microsoft SQL Server, Oracle, PostgreSQL, or SAP HANA. Enterprise geodatabases support advanced geodatabase features like versioning, replication, and geodatabase archiving.
ArcGIS Pro provides tools and functionality to create, manage, and work with geodatabases. Users can import data into geodatabases, perform geoprocessing operations, create and manage relationships between datasets, and more.
Geodatabases are essential for maintaining data integrity, managing complex spatial relationships, and ensuring efficient data retrieval and analysis within the ArcGIS environment.
If you're referring to using the Python arcpy module to work with a Geodatabase, you can use functions like arcpy.
CreateFileGDB_management to create a Geodatabase, and then various arcpy functions to manage and analyze geospatial data within it.
In arcpy (the ArcGIS Python site package), you can use the CreateFileGDB_management or CreateEnterpriseGeodatabase_management functions to create a File Geodatabase (FGDB) or an Enterprise Geodatabase, respectively.
Below are examples for creating a File Geodatabase:
Creating a File Geodatabase in python:
import arcpy
# Set the workspace (folder) where you want to create the File Geodatabase
workspace = r"C:\Path\To\Your\Directory"
# Set the name of the File Geodatabase
gdb_name = "YourGeodatabase.gdb"
# Combine the workspace and geodatabase name to get the full path
gdb_path = arcpy.os.path.join(workspace, gdb_name)
# Create the File Geodatabase
arcpy.CreateFileGDB_management(workspace, gdb_name)
print(f"File Geodatabase '{gdb_name}' created at: {gdb_path}")
Replace "C:\Path\To\Your\Directory" with the path to the directory where you want to create the geodatabase, and "YourGeodatabase.gdb" with the desired name for your geodatabase.
Creating an Enterprise Geodatabase:
Creating an enterprise geodatabase involves more parameters, as it requires a connection file and connection properties for the underlying relational database management system (RDBMS).
Below is a simplified example for creating an enterprise geodatabase on Microsoft SQL Server:
import arcpy
# Set the workspace (folder) where you want to create the Connection File
workspace = r"C:\Path\To\Your\Directory"
# Set the name of the Connection File
connection_file_name = "YourConnectionFile.sde"
# Combine the workspace and connection file name to get the full path
connection_file_path = arcpy.os.path.join(workspace, connection_file_name)
# Set the parameters for creating the enterprise geodatabase
database_platform = "SQL_SERVER"
instance_name = "YourSQLServerInstance"
database_name = "YourDatabaseName"
account_authentication = "DATABASE_AUTH"
username = "YourDBUsername"
password = "YourDBPassword"
version_type = "TRANSACTIONAL"
# Create the Connection File and Enterprise Geodatabase
arcpy.CreateDatabaseConnection_management
(workspace,
connection_file_name,
database_platform,
instance_name,
account_authentication,
username,
password,
version_type)
print(f"Enterprise Geodatabase Connection File
'{connection_file_name}' created at:
{connection_file_path}")
Replace the placeholder values with your specific database connection details.
Remember to adjust the parameters according to your specific requirements and the RDBMS you are using.
The examples provided here are simplified, and you may need to refer to the arcpy documentation for more details on additional parameters and options.
Let's take a detailed look at how to actually create a file database with python.
The output of this code is.
A sample of the code we used:
import arcpy
arcpy.management.CreateFileGDB(
out_folder_path=r"C:\GISProject\GISBlog\Tools_Projects",
out_name="Roads"
)
On the first line, we use a built-in library in ArcGIS Pro called arcpy.
From this library, we utilize the 'CreateFileGDB' function, which is part of the 'arcpy.Management' module.
The function takes two parameters: the path to the workspace where we will create our new database, named "out_folder_path."
In our case, the workspace path is set to r"C:\GISProject\GISBlog\Tools_Projects," where "r" is a parameter indicating that this constant is a path.
The next parameter is the name of our file database, with "out_name" serving as the variable name. In this instance, we set the value to "Roads."
After running the code with "Run," let's check if our working database has been successfully created.
Of course our database is empty.
Let's add data with python to the file database.
Let's add data with python to the file database.
To load data into the file database, we use the "FeatureClassToGeodatabase" command, which is part of "arcpy.conversion".
Let's check the contents of the file database.
As you can see, it already contains the shp file we loaded.
Of course, once we've created our Roads file database, we don't need to run arcpy.management.CreateFileGDB.
The code we've used so far is:
import arcpy
arcpy.management.CreateFileGDB(
out_folder_path=r"C:\GISProject\GISBlog\Tools_Projects",
out_name="Roads"
)
arcpy.conversion.FeatureClassToGeodatabase(
Input_Features=
r"C:\GISProject\10m_physical\ne_10m_antarctic_ice_shelves_polys.shp",
Output_Geodatabase=
r"C:\GISProject\GISBlog\Tools_Projects\Roads.gdb"
)
In this case, it will be useful for us to check if the file database exists in the specified path.
If it exists, skip its creation; if it is deleted for some reason, create it again.
For this purpose, we will export as parameters the name of the database file as well as the path to the workspace.
To work with the operating system, we need the built-in 'os' module in Python.
With its help, we assemble the full access path to the database file: 'gdb_path = os.path.join(folder_path, gdb_name)'.
import arcpy
# Set the path to the toolbox
toolbox_path = r'C:\Path\To\Your\Toolbox.tbx'
# List all tools in the toolbox
tools = arcpy.ListTools('*_*', toolbox_path)
for tool in tools:
print(tool)
The result of this which is:
If we manually delete the file database "Roads.gdb"
After executing the code, we will get the following result.
Which means our database is created from scratch. Of course it no longer contains the loaded data.
If it is not displayed in the catalog, after executing the code, use refresh - F5.
The code we used to check is:
import arcpy
import os
folder_path = r"C:\GISProject\GISBlog\Tools_Projects"
gdb_name = "Roads.gdb"
gdb_path = os.path.join(folder_path, gdb_name)
if not arcpy.Exists(gdb_path):
# If it doesn't exist, create the geodatabase
arcpy.management.CreateFileGDB(folder_path, gdb_name)
print(f"Geodatabase {gdb_name} created.")
else:
print(f"Geodatabase {gdb_name} exist in folder {folder_path}")
Now let's combine the file database creation and import with the necessary checks.
Our code will look like this:
The execution of the code on the existing file database 'Roads.gdb' is correct.
import arcpy
import os
folder_path = r"C:\GISProject\GISBlog\Tools_Projects"
gdb_name = "Roads.gdb"
gdb_path = os.path.join(folder_path, gdb_name)
if not arcpy.Exists(gdb_path):
# If it doesn't exist, create the geodatabase
arcpy.management.CreateFileGDB(folder_path, gdb_name)
print(f"Geodatabase {gdb_name} created.")
else:
arcpy.conversion.FeatureClassToGeodatabase(
Input_Features=
r"C:\GISProject\10m_physical\ne_10m_antarctic_ice_shelves_polys.shp",
Output_Geodatabase=gdb_path)
print(f"Geodatabase {gdb_name} exist in folder {folder_path}")
print(f"Operation completed in {gdb_name}.")
But let's delete the "Roads.gdb" database and run the code from scratch.
As you can see the file database has been created but is empty.
This is because only the creation part is executed, but not the import part of the SHP file.
In order for our code to work correctly with both existing and new file databases, we will move the import part outside the check.
We check, through the catalog in ArcGIS Pro.
As this is our final code.
import arcpy
import os
folder_path = r"C:\GISProject\GISBlog\Tools_Projects"
gdb_name = "Roads.gdb"
gdb_path = os.path.join(folder_path, gdb_name)
if not arcpy.Exists(gdb_path):
arcpy.management.CreateFileGDB(folder_path, gdb_name)
print(f"Geodatabase {gdb_name} created.")
else:
print(f"Geodatabase {gdb_name} exist in folder {folder_path}")
arcpy.conversion.FeatureClassToGeodatabase(
Input_Features=
r"C:\GISProject\10m_physical\ne_10m_antarctic_ice_shelves_polys.shp",
Output_Geodatabase=gdb_path)
print(f"Operation completed in {gdb_name}."
Thank you for your attention.
In future posts, we will explore how to use Python to check for classes, fields, and values in a file database.
Please consider subscribing to our newsletter.