How to create new geodatabases


Summary
This article explains how to create several types of geodatabases, including personal, file, personal and workgroup ArcSDE, scratch (two types), and in-memory. It also describes how to create a connection file to an ArcSDE geodatabase, and how to create a shapefile workspace.

In this topic


About workspaces

A workspace is a container of spatial and nonspatial datasets such as feature classes, raster datasets, and tables. It provides methods to instantiate existing datasets and to create new datasets. The following are the three types of workspaces:
To create a workspace, you need to create the appropriate workspace factory. Each workspace type has its own workspace factory. A workspace factory is a dispenser of workspaces and allows a client to create a workspace specified by the directory, file name, and connection properties. A workspace factory is a cocreatable singleton object—a singleton object can only be instantiated once in a process. The workspace factory classes for geodatabase workspaces are found in the DataSourcesGDB library.
The Create method can be used to create a new esriFileSystemWorkspace, esriLocalDatabaseWorkspace, or a connection file to an esriRemoteDatabaseWorkspace. The optional connectionProperties parameter specifies any additional connection properties needed, such as the server, instance, and so on.
In the case where a connection file to an ArcSDE geodatabase is being created—if no connection properties are specified—a dialog box appears prompting the user for the required properties. The hWnd parameter tells the Create method the control or dialog box to use.
The Create method returns an IWorkspaceName object that can be used to open or return certain information about the workspace. The Create method cannot be used to create new geodatabases in an Enterprise, Personal, or Workgroup ArcSDE

Creating a personal geodatabase workspace stored in Access

The workspace factory required to create a personal geodatabase is an AccessWorkspaceFactory. The following code example creates a new personal geodatabase in the specified directory with the supplied name. The connectionProperties parameter is null as it is not required for the creation of a personal geodatabase.
[Java]
//Create an access workspace factory
IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactory();

//Create a new Access workspace\personal geodatabase
IWorkspaceName workspaceName = workspaceFactory.create("C:\\temp\\", "MyPGDB.mdb",
    null, 0);

//Cast for IName
IName name = (IName)workspaceName;

//Open a reference to the access workspace through the name object
IWorkspace workspace = new IWorkspaceProxy(name.open());

Creating a file geodatabase workspace

The code required to create a file geodatabase is almost identical to the code for creating a personal geodatabase. Both are local database workspaces, but as stated previously, a different type of workspace factory - the FileGDBWorkspaceFactory - is required. Additionally, the extension used on the database will be different; for a file geodatabase, the extension is .gdb.
The following code example creates a new file geodatabase in the specified directory with the supplied name, then shows how to connect to it. The connectionProperties parameter is null as it is not required for the creation of a file geodatabase.
[Java]
//Create a FileGDB workspace factory
IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory();

//Create a new File geodatabase
IWorkspaceName workspaceName = workspaceFactory.create("C:\\temp\\", "MyFGDB.gdb",
    null, 0);

//Cast for IName
IName name = (IName)workspaceName;

//Open a reference to the access workspace through the name object
IWorkspace workspace = new IWorkspaceProxy(name.open());

Creating a connection file (.sde) to an Enterprise ArcSDE workspace

When used in conjunction with an ArcSDE workspace, the Create method does not create the geodatabase. Instead, it creates a connection file to an esriRemoteDatabaseWorkspace. 
There are two different procedures used to create an ArcSDE geodatabase. One is used for creating ArcSDE personal and workgroup geodatabases. The other is used to create ArcSDE Enterprise geodatabases. ArcSDE Enterprise geodatabases are stored using separate database management system (DBMS) products. Therefore, before you can create the ArcSDE geodatabase system tables and your data tables, you must set up your DBMS. How you set up your DBMS is specific to the DBMS product you are using; therefore, consult your DBMS documentation for setup information.
The following code example demonstrates how to use the Create method to make an ArcSDE connection file to an ArcSDE geodatabase:
[Java]
static IWorkspaceName open_ArcSDE_Workspace(String server, String instance, String
    user, String password, String database, String version)throws Exception{
    IPropertySet propertySet = new PropertySet();
    propertySet.setProperty("SERVER", server);
    propertySet.setProperty("INSTANCE", instance);
    propertySet.setProperty("DATABASE", database);
    propertySet.setProperty("USER", user);
    propertySet.setProperty("PASSWORD", password);
    propertySet.setProperty("VERSION", version);

    IWorkspaceFactory2 workspaceFactory = new SdeWorkspaceFactory();
    return workspaceFactory.create("c:\\temp\\", "myconnection.sde", propertySet, 0);
}

Creating a geodatabase in a personal or workgroup ArcSDE workspace

The DataServerManager is used to access and administer one or more geodatabases stored on a data server. You can also open a connection to each geodatabase in a DataServer through the use of the IWorkspaceName and IName interfaces. The following code example shows how to connect to a geodatabase stored in a DataServer for a personal or workgroup ArcSDE using the DataServerManager.
Creating a geodatabase in a personal or workgroup ArcSDE workspace creates new databases on a SQL Server Express database instance. You must be an ArcSDE database server administrator to create new ArcSDE Personal or Workgroup geodatabases.
[Java]
// dataServerName parameter should in "tivo\\sqlexpress" format
static void createPersonalOrWorkgroupArcSdeWorkspace(String dataServerName)throws
    Exception{
    // Create a data server manager object.
    IDataServerManager dataServerManager = new DataServerManager();
    // Set the server name and connect to the server.
    dataServerManager.setServerName(dataServerName);
    dataServerManager.connect();
    // Cast to the admin interface, check permissions and create the geodatabase.
    IDataServerManagerAdmin dataServerManagerAdmin = (IDataServerManagerAdmin)
        dataServerManager;
    if (dataServerManagerAdmin.isConnectedUserAdministrator()){
        dataServerManagerAdmin.createGeodatabase("LandUse", "C:\\Temp\\LandUse.mdf",
            0, "", 0);
        // Create a Name object to open the workspace
        IWorkspaceName workspaceName = dataServerManagerAdmin.createWorkspaceName(
            "LandUse", "VERSION", "dbo.Default");
        IName name = (IName)workspaceName;
        IWorkspace workspace = (IWorkspace)name.open();
    }
}

Creating a shapefile workspace

A ShapefileWorkspaceFactory is used to create a folder in which shapefiles can be created.  As opposed to the geodatabase workspace factories, which create geodatabases or connection files, the ShapefileWorkspaceFactory does not create a shapefile. Instead, it creates a folder or workspace for shapefiles. The following code example demonstrates how to create a shapefile workspace:
[Java]
IWorkspaceFactory2 workspaceFactory = new ShapefileWorkspaceFactory();
IWorkspaceName worName = workspaceFactory.create("c:\\temp\\", "streetshapefiles",
    null, 0);

IName name = (IName)worName;
IWorkspace workspace = new IWorkspaceProxy(name.open());

Creating a scratch workspace

A ScratchWorkspaceFactory is used to connect to a temporary database stored in Microsoft Access or in a file geodatabase. These workspaces are commonly used to hold the results of a selection set or to hold the results of an analysis operation. This workspace factory is fundamentally different than the AccessWorkspaceFactory or FileGDBworkspaceFactory, and therefore, the manner in which you create and work with them is different:
A new type of scratch workspace factory exists at ArcGIS 9.2 that uses a file geodatabase instead of an Access database. The new scratch workspace factory, FileGDBScratchWorkspaceFactory, does not replace the existing ScratchWorkspaceFactory but instead, is meant to take advantage of file geodatabases in that it is cross-platform and can handle larger amounts of data.
The following code example shows how to get the current scratch workspace to use as part of a selection, using an Access geodatabase:
[Java]
static IWorkspace openScratchWorkspace(ITable parcelTable, IQueryFilter queryFilter)
    throws Exception{
    //Create a new scratch workspace factory
    IScratchWorkspaceFactory workspaceFactory = new ScratchWorkspaceFactory();
    IScratchWorkspaceFactory2 workspaceFactory2 = (IScratchWorkspaceFactory2)
        workspaceFactory;
    //Get the current scratch workspace
    IWorkspace scratchWorkspace = workspaceFactory2.getCurrentScratchWorkspace();
    //Select from an existing table, called parcelTable, using an existing query, called queryFilter
    //putting the resulting selection set in the temporary workspace
    ISelectionSet selSet = parcelTable.select(queryFilter,
        esriSelectionType.esriSelectionTypeHybrid,
        esriSelectionOption.esriSelectionOptionNormal, scratchWorkspace);
    return scratchWorkspace;
}
The following code example shows how to get the current scratch workspace to use as part of a selection, using a file geodatabase:
[Java]
static IWorkspace openFileGdbScratchWorkspace(ITable parcelTable, IQueryFilter
    queryFilter)throws Exception{
    // Create a new file scratch workspace factory
    IScratchWorkspaceFactory workspaceFactory = new FileGDBScratchWorkspaceFactory();
    IScratchWorkspaceFactory2 workspaceFactory2 = (IScratchWorkspaceFactory2)
        workspaceFactory;
    //Get the current scratch workspace
    IWorkspace scratchWorkspace = workspaceFactory2.getCurrentScratchWorkspace();
    //Select from an existing table, called parcelTable, using an existing query, called queryFilter
    //putting the resulting selection set in the temporary workspace
    ISelectionSet selSet = parcelTable.select(queryFilter,
        esriSelectionType.esriSelectionTypeHybrid,
        esriSelectionOption.esriSelectionOptionNormal, scratchWorkspace);
    return scratchWorkspace;
}

Creating an InMemory workspace

An InMemoryWorkspaceFactoryis used to create a temporary workspace that is stored in memory. These workspaces are commonly used to hold the results of an analysis operation or to hold objects in memory before persisting them to disk. When the last reference to the workspace is released, the workspace is destroyed and the memory released.
Feature classes contained in InMemory workspaces support the full geodatabase model. They can be used in conjunction with cursors, selections, and so on.  However, InMemory workspaces do not support all aspects of the geodatabase model; advanced datasets such as topology and geometric networks are not supported. The following code example shows how to create and open an InMemory workspace:
[Java]
//Create an InMemory workspace factory
IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactory();

//Create a new InMemory geodatabase
IWorkspaceName workspaceName = workspaceFactory.create("", "MyWorkspace", null, 0);

//Cast for IName
IName name = (IName)workspaceName;

//Open a reference to the access workspace through the name object
IWorkspace inmemWor = new IWorkspaceProxy(name.open());


See Also:

How to connect to a geodatabase




Development licensingDeployment licensing
ArcGIS for Desktop BasicArcGIS for Desktop Basic
ArcGIS for Desktop StandardArcGIS for Desktop Standard
ArcGIS for Desktop AdvancedArcGIS for Desktop Advanced
Engine Developer KitEngine: Geodatabase Update