Con questa modalità disabilitiamo l'aggiornamento degli indici spaziali e degli indici sugli attributi (questi ultimi solo per il file geodatabase) mentre si esegue un'operazione di inserimento di rows o features. Gli indici verranno ricostruiti una volta che si disabilita la modalità Load-Only.
Quando una feature class o una tabella è in modalità load-only altre applicazioni non possono lavorare su questi dati, pertanto occorre acquisire un lock esclusivo dello schema sulla tabella o feature class tramite l'interfaccia ISchemaLock
Qui potete vedere un esempio di caricamento dati in una feature class in modalità Load-Only
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.esriSystem;
using System.IO;
using ESRI.ArcGIS.Geodatabase;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF;
using System.Diagnostics;
namespace caCursors
{
class Program
{
static void Main(string[] args)
{
Arguments CommandLine = new Arguments(args);
if ((args.Length == 0) (CommandLine["help"] != null) (CommandLine["h"] != null))
{
Console.WriteLine("The arguments to the utility are:");
Console.WriteLine("caCursors [-hhelp] [-lloadOnlyMode]");
Console.WriteLine("caCursors -l true");
Console.WriteLine("Press Enter to continue...");
Console.ReadLine();
return;
}
bool loadOnlyMode;
if ((CommandLine["loadOnlyMode"] != null) (CommandLine["l"] != null))
{
string sloadOnlyMode = CommandLine["l"] ?? CommandLine["loadOnlyMode"];
bool result;
if (!Boolean.TryParse(sloadOnlyMode, out result))
{
Console.WriteLine("Parametro loadOnlyMode non corretto. Indicare: {0} o {1}", Boolean.TrueString, Boolean.FalseString);
return;
}
loadOnlyMode = result;
}
else
{
Console.WriteLine("Indicare il parametro loadOnlyMode: caCursors -l ({0} or {1})", Boolean.TrueString, Boolean.FalseString);
return;
}
#region Licensing
IAoInitialize aoInitialize = new AoInitializeClass();
esriLicenseStatus licenseStatus = aoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo);
if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)
{
Console.WriteLine("Licenza ArcInfo non trovata, errore numero: {0}", licenseStatus);
return;
}
#endregion
Console.WriteLine("Cancellazione gdb ...");
if (Directory.Exists("Comuni.gdb"))
Directory.Delete("Comuni.gdb", true);
Console.WriteLine("Copia gdb per test ...");
Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
IWorkspaceName sourceWorkspaceName = new WorkspaceNameClass { PathName = @"..\..\Data\Comuni.gdb", WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory" };
IWorkspaceName copiedWorkspaceName = null;
workspaceFactory.Copy(sourceWorkspaceName, Environment.CurrentDirectory, out copiedWorkspaceName);
IName copiedName = (IName)copiedWorkspaceName;
IWorkspace workspace = (IWorkspace)copiedName.Open();
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("Comuni");
IWorkspace workspaceInput = ShapefileWorkspaceFromPath(@"..\..\Data");
IFeatureClass featureClassInput = ((IFeatureWorkspace)workspaceInput).OpenFeatureClass("Comuni");
Console.WriteLine("Inserimento dati in gdb ...");
InsertCursor(workspace, featureClass, featureClassInput, loadOnlyMode);
aoInitialize.Shutdown();
}
private static void InsertCursor(IWorkspace workspace, IFeatureClass featureClass, IFeatureClass featureClassInput, bool LoadOnlyMode)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
IFeatureClassLoad featureClassLoad = (IFeatureClassLoad)featureClass;
ISchemaLock schemaLock = null;
if (LoadOnlyMode)
{
featureClassLoad.LoadOnlyMode = LoadOnlyMode;
schemaLock = (ISchemaLock)featureClass;
}
IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;
workspaceEdit.StartEditing(true);
workspaceEdit.StartEditOperation();
using (ComReleaser comReleaser = new ComReleaser())
{
IFeatureCursor featureCursor = featureClass.Insert(true);
comReleaser.ManageLifetime(featureCursor);
IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();
comReleaser.ManageLifetime(featureBuffer);
IFeatureCursor featureCursorInput = featureClassInput.Search(null, true);
comReleaser.ManageLifetime(featureCursorInput);
try
{
IFeature feature = null;
while ((feature = featureCursorInput.NextFeature()) != null)
{
featureBuffer.Shape = feature.ShapeCopy;
featureBuffer.set_Value(2, feature.get_Value(2)); //istat_new (campo con indice)
featureBuffer.set_Value(5, feature.get_Value(5)); //comune (campo con indice)
//for (int i = 2; i < 8; i++)
// featureBuffer.set_Value(i, feature.get_Value(i));
featureCursor.InsertFeature(featureBuffer);
}
featureCursor.Flush();
workspaceEdit.StopEditOperation();
workspaceEdit.StopEditing(true);
}
catch (COMException)
{
workspaceEdit.AbortEditOperation();
workspaceEdit.StopEditing(false);
}
}
if (LoadOnlyMode)
{
featureClassLoad.LoadOnlyMode = false;
schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds);
Console.WriteLine(elapsedTime);
Console.WriteLine("Premere invio ...");
Console.Read();
}
public static IWorkspace ShapefileWorkspaceFromPath(String path)
{
Type factoryType = Type.GetTypeFromProgID(
"esriDataSourcesFile.ShapefileWorkspaceFactory");
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
Activator.CreateInstance(factoryType);
return workspaceFactory.OpenFromFile(path, 0);
}
}
}
Scarica qui la soluzione.
Nessun commento:
Posta un commento