Select to view content in your preferred language

How To: File Geodatabase using Java

1343
0
04-15-2010 10:49 AM
thomaslepkowski
Emerging Contributor
Hi,

Thought I'd start a thread on how to open and store a polygon into a file geodatabase. Others please post any best-practices comments or questions.

This a simple case where you have created an empty file geodatabase in ArcCatalog. You must setup a file geodatabase in ArcCatalog and create one polygon feature class named "module."

So, here's the essential code:

private void saveModule(Module module) {        

        IFeatureWorkspace featureWorkspace  = null;
        IFeatureClass featureClass                   = null;

         try {
            
            FileGDBWorkspaceFactory f =  new FileGDBWorkspaceFactory();

            IWorkspace workspace =    f.openFromFile("C:/fakepath/myGDB.gdb", 0);

            featureWorkspace = (IFeatureWorkspace) workspace;
            featureClass     = featureWorkspace.openFeatureClass("module");

            // create and store the new feature
            IFeature newFeature = featureClass.createFeature();
            newFeature.setShapeByRef(module.getPolygonElement().getGeometry());
            newFeature.store();
            
        } catch (IOException ex) {
            // handle exception
        }
        finally {
            
            // This removes the lock on the gdb
            featureWorkspace = null;
            featureClass     = null;
            
            System.gc();
           
        }

    }



If you need to copy the file geodatabase before using it, just call an OS method to copy the gdb directory, or write some java to copy the gdb directory.
0 Kudos
0 Replies