arcgissamples\raster\CreateFGDBRasterCatalog.java
/* Copyright 2010 ESRI * * All rights reserved under the copyright laws of the United States * and applicable international laws, treaties, and conventions. * * You may freely redistribute and use this sample code, with or * without modification, provided you include the original copyright * notice and use restrictions. * * See the use restrictions. * */ /* Copyright 2010 ESRI * * All rights reserved under the copyright laws of the United States * and applicable international laws, treaties, and conventions. * * You may freely redistribute and use this sample code, with or * without modification, provided you include the original copyright * notice and use restrictions. * * See the use restrictions at <your ArcGIS install location>/DeveloperKit10.0/userestrictions.txt. * */ package arcgissamples.raster; import java.io.File; import com.esri.arcgis.datasourcesGDB.FileGDBWorkspaceFactory; import com.esri.arcgis.datasourcesraster.RasterWorkspace; import com.esri.arcgis.datasourcesraster.RasterWorkspaceFactory; import com.esri.arcgis.geodatabase.*; import com.esri.arcgis.geometry.ISpatialReference; import com.esri.arcgis.geometry.UnknownCoordinateSystem; import com.esri.arcgis.geometry.esriGeometryType; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; import com.esri.arcgis.system.esriLicenseProductCode; import com.esri.arcgis.system.esriLicenseStatus; public class CreateFGDBRasterCatalog { public CreateFGDBRasterCatalog() { } public static void main(String[] args) { System.out.println("Starting CreateFGDBRasterCatalog - An ArcObjects Java SDK Developer Sample"); try { // Initialize the engine and licenses. EngineInitializer.initializeEngine(); AoInitialize aoInit = new AoInitialize(); initializeArcGISLicenses(aoInit); //Get DEVKITHOME Home String devKitHome = System.getenv("AGSDEVKITJAVA"); String outputPath = getOutputDir(); String catalogName = "rastercatalog"; String rasterWorkspace = devKitHome + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "raster" + File.separator + "rasterworkspace"; CreateFGDBRasterCatalog convert = new CreateFGDBRasterCatalog(); convert.convertFileRasterToGDBRaster(rasterWorkspace, outputPath, catalogName); aoInit.shutdown(); } catch(Exception e) { e.printStackTrace(); } } /** * Converts a file based raster to FGDB based raster * @param rasterWorkspace * @param catalogPath * @throws Exception */ public void convertFileRasterToGDBRaster(String rasterWorkspace, String outputPath, String catalogName) throws Exception { // create a file GDB String fgdbName = "CreateFGDBRasterCatalog.gdb"; try { createFileGDB(outputPath, fgdbName); } catch (Exception e) { System.out.println("ERROR while creating File GDB. Please check if it already exists.\n" + e.getMessage()); } //init a rasterworkspaceex FileGDBWorkspaceFactory fileGDBFactory = new FileGDBWorkspaceFactory(); IWorkspace fgdbWorkspace = fileGDBFactory.openFromFile(outputPath + File.separator + fgdbName, 0); Workspace rasterWS = new Workspace(fgdbWorkspace); if (rasterWS == null) { throw new Exception("Invalid Workspace"); } //create a raster catalog RasterCatalog rasterCatalog = new RasterCatalog(rasterWS.createRasterCatalog(catalogName, createFields(false, null, null), "shape", "raster", "defaults")); // Get the Raster datasets to be loaded RasterWorkspaceFactory inputRasterWSFactory = new RasterWorkspaceFactory(); RasterWorkspace inputRasterWorkspace = new RasterWorkspace(inputRasterWSFactory.openFromFile(rasterWorkspace, 0)); IEnumDataset edatasets = inputRasterWorkspace.getDatasets(esriDatasetType.esriDTRasterDataset); IDataset raster = edatasets.next(); int rasterFieldIndex = rasterCatalog.getRasterFieldIndex(); FeatureClass featureClass = new FeatureClass(rasterCatalog); FeatureCursor cursor = new FeatureCursor(featureClass.IFeatureClass_insert(false)); // Load each raster into the catalog while (raster != null) { IRasterDataset ds = new IRasterDatasetProxy(raster); System.out.print("Loading " + ds.getCompleteName() + "..."); IFeatureBuffer featureBuffer = featureClass.createFeatureBuffer(); // Create a RasterValue... RasterStorageDef rasterStorageDef = new RasterStorageDef(); rasterStorageDef.setCompressionType(esriRasterSdeCompressionTypeEnum.esriRasterSdeCompressionTypeRunLength); rasterStorageDef.setPyramidResampleType(rstResamplingTypes.RSP_BilinearInterpolation); rasterStorageDef.setPyramidLevel(-1); RasterValue rv = new RasterValue(); rv.setRasterDatasetByRef(ds); rv.setRasterStorageDefByRef(rasterStorageDef); // Add the raster to the catalog featureBuffer.setValue(rasterFieldIndex, rv); cursor.insertFeature(featureBuffer); raster = (edatasets.next()); System.out.println("done."); } System.out.println("\nAll rasters in " + rasterWorkspace + " loaded into " + outputPath + File.separator + fgdbName + + File.separatorChar + rasterCatalog.getName()+ "."); } /** * Creates a File Geodatabase * @param fgdbPath String * @param fgdbName String * @throws Exception */ public void createFileGDB(String fgdbPath, String fgdbName) throws Exception { if(fgdbPath == null || fgdbName == null || fgdbName.equalsIgnoreCase(" ")) { throw new Exception("\n**Error in createFileGDB(): One of the following errors has occured: " + "\n1. One or more parameters are null. " + "\n2. Supplied FGDBName has nothing but spaces. " + "\n3. Supplied FGDBPath is invalid or the directory does not exist\n"); } //Create a local FGDB. Don't, if it already exists. IWorkspaceFactory wFactory = new FileGDBWorkspaceFactory(); if(!wFactory.isWorkspace(fgdbPath + File.separator + fgdbName)) { wFactory.create(fgdbPath, fgdbName, null, 0); } } /** * Creates fields to be used in Raster table in gdb * @param bIsByRef boolean * @return IFields * @throws Exception */ public Fields createFields(boolean isByRef, ISpatialReference rasterSpatRef, ISpatialReference geoSpatRef) throws Exception { // ++ if fields are missing from input, create default Fields fields = new Fields(); // add OID field fields.addField(createOIDField("ObjectID")); // add NAME field fields.addField(createNameField("name")); // add RASTER field fields.addField(createRasterField("raster", isByRef, rasterSpatRef)); // add SHAPE field fields.addField(createShapeField("shape", geoSpatRef)); return fields; } /** * Creates NAME field * @param name String * @return IField * @throws Exception */ public IField createNameField(String name) throws Exception { Field field = new Field(); field.setName(name); field.setType(esriFieldType.esriFieldTypeString); return field; } /** * Creates Object ID field * @param oidFieldName String * @return IField * @throws Exception */ public Field createOIDField(String oidFieldName) throws Exception { Field field = new Field(); field.setName(oidFieldName); field.setType(esriFieldType.esriFieldTypeOID); return field; } /** * Creates a Raster column * @param rasterFieldName String * @param isManagedByGDB boolean * @param spatRef ISpatialReference * @return Field * @throws Exception */ public Field createRasterField(String rasterFieldName, boolean isManagedByGDB, ISpatialReference spatRef) throws Exception { Field rasterField = new Field(); rasterField.setName(rasterFieldName); rasterField.setType(esriFieldType.esriFieldTypeRaster); RasterDef rasterDef = new RasterDef(); rasterDef.setDescription("This is a test catalog"); rasterDef.setIsManaged(!isManagedByGDB); if (spatRef == null) { spatRef = new UnknownCoordinateSystem(); } rasterDef.setSpatialReferenceByRef(spatRef); rasterField.setRasterDefByRef(rasterDef); return rasterField; } /** * Creates SHAPE field * @param shapeFieldName String * @param spatRef ISpatialReference * @return IField * @throws Exception */ public Field createShapeField(String shapeFieldName, ISpatialReference spatRef) throws Exception { Field field = new Field(); field.setName(shapeFieldName); field.setType(esriFieldType.esriFieldTypeGeometry); GeometryDef geometryDef = new GeometryDef(); geometryDef.setGeometryType(esriGeometryType.esriGeometryPolygon); if (spatRef == null) { spatRef = new UnknownCoordinateSystem(); spatRef.setDomain(-10000, 10000, -10000, 10000); } geometryDef.setSpatialReferenceByRef(spatRef); geometryDef.setAvgNumPoints(2); geometryDef.setGridCount(1); geometryDef.setGridSize(0, 1000); field.setGeometryDefByRef(geometryDef); return field; } /** * Initializes the lowest available ArcGIS License */ static void initializeArcGISLicenses(AoInitialize ao) { try { if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeEngine) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable) ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeEngine); else if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcView) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable) ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcView); else if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcEditor) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable) ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcEditor); else if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcInfo) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable) ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcInfo); else { System.err.println("Could not initialize an Engine, ArcView, ArcEditor, or ArcInfo license. Exiting application."); System.exit(-1); } } catch (Exception e) { e.printStackTrace(); } } /** * Retrieves output directory * @return */ private static String getOutputDir() { String userDir; if (System.getProperty("os.name").toLowerCase().indexOf("win") > -1) { userDir = System.getenv("UserProfile"); } else { userDir = System.getenv("HOME"); } String outputDir = userDir + File.separator + "arcgis_sample_output"; System.out.println("Creating output directory - " + outputDir); new File(outputDir).mkdir(); return outputDir; } }