/// <summary> /// Initializes this instance. /// </summary> protected override void Initialize() { SendMessage("Creating target data source"); if (!ExpressUtility.CreateFlatFileDataSource(_outputFile)) { throw new FdoETLException(ResourceUtil.GetStringFormatted("ERR_CANNOT_CREATE_DATA_FILE", _outputFile)); } _outConn = ExpressUtility.CreateFlatFileConnection(_outputFile); _outConn.Open(); ClassDefinition cd = _table.CreateClassDefinition(true); using (FdoFeatureService service = _outConn.CreateFeatureService()) { SendMessage("Applying schema to target"); FeatureSchema schema = new FeatureSchema("Schema1", "Default schema"); schema.Classes.Add(cd); service.ApplySchema(schema); } SendMessage("Copying any attached spatial contexts"); ExpressUtility.CopyAllSpatialContexts(_table.SpatialContexts, _outConn, true); Register(new FdoFeatureTableInputOperation(_table)); Register(new FdoOutputOperation(_outConn, cd.Name)); }
public bool CreateSqlite() { if (_view.CreateConnection && string.IsNullOrEmpty(_view.ConnectionName)) { _view.ShowError("Specify a connection name"); return(false); } if (ExpressUtility.CreateFlatFileDataSource("OSGeo.SQLite", _view.SQLiteFile)) { FdoConnection conn = ExpressUtility.CreateFlatFileConnection("OSGeo.SQLite", _view.SQLiteFile); if (FileService.FileExists(_view.FeatureSchemaDefinition)) { conn.Open(); using (FdoFeatureService service = conn.CreateFeatureService()) { service.LoadSchemasFromXml(_view.FeatureSchemaDefinition, _view.FixIncompatibilities); } } if (_view.CreateConnection) { _connMgr.AddConnection(_view.ConnectionName, conn); } else { conn.Dispose(); } } return(true); }
public override int Execute() { CommandStatus retCode = CommandStatus.E_OK; bool create = ExpressUtility.CreateFlatFileDataSource(_file); if (!create) { WriteLine("Failed to create file {0}", _file); retCode = CommandStatus.E_FAIL_CREATE_DATASTORE; return((int)retCode); } WriteLine("File {0} created", _file); if (_schema != null) { try { FdoConnection conn = ExpressUtility.CreateFlatFileConnection(_file); conn.Open(); using (FdoFeatureService service = conn.CreateFeatureService()) { service.LoadSchemasFromXml(_schema); WriteLine("Schema applied to {0}", _file); } retCode = CommandStatus.E_OK; } catch (Exception ex) { WriteException(ex); retCode = CommandStatus.E_FAIL_APPLY_SCHEMA; } } return((int)retCode); }
private bool ExportToFile(string provider, string file, int maxSchemas, int maxSpatialContexts) { var conf = _context.GetConfiguration(); if (maxSchemas > 0 && conf.Schemas.Count > maxSchemas) { ShowError("This provider only allows " + maxSchemas + " feature schema(s)"); return(false); } if (maxSpatialContexts > 0 && conf.SpatialContexts.Length > maxSpatialContexts) { ShowError("This provider only allows " + maxSpatialContexts + " spatial context(s)"); return(false); } if (ExpressUtility.CreateFlatFileDataSource(provider, file)) { var conn = ExpressUtility.CreateFlatFileConnection(provider, file); var schema = conf.Schemas[0]; schema = FdoSchemaUtil.CloneSchema(schema, true); using (var svc = conn.CreateFeatureService()) { IncompatibleSchema incs; if (!svc.CanApplySchema(schema, out incs)) { string msg = "This schema has incompatibilities. Attempt to fix it? " + incs.ToString(); if (!WrappedMessageBox.Confirm("Incompatible schema", msg, MessageBoxText.YesNo)) { return(false); } schema = svc.AlterSchema(schema, incs); } using (var cur = new TempCursor(Cursors.WaitCursor)) { foreach (var sc in conf.SpatialContexts) { svc.CreateSpatialContext(sc, true); } //Only for SQLite, but this is general enough var fscs = svc.DescribeSchema(); if (fscs.Count == 1) { var fsc = fscs[0]; foreach (ClassDefinition cls in schema.Classes) { var klass = FdoSchemaUtil.CloneClass(cls); fsc.Classes.Add(klass); } schema = fsc; } svc.ApplySchema(schema); } } conn.Close(); } return(true); }
public void TestCreateFlatFile() { Assert.IsTrue(ExpressUtility.CreateFlatFileDataSource("Test.sdf")); Assert.IsTrue(ExpressUtility.CreateFlatFileDataSource("Test.sqlite")); Assert.IsTrue(ExpressUtility.CreateFlatFileDataSource("Test.db")); File.Delete("Test.sdf"); File.Delete("Test.sqlite"); File.Delete("Test.db"); }
private void btnSave_Click(object sender, EventArgs e) { var schema = _schema; //Remove elements that have been unchecked. foreach (TreeNode clsNode in treeSchema.Nodes) { string className = clsNode.Name; int index = schema.Classes.IndexOf(className); if (!clsNode.Checked) { if (index >= 0) { schema.Classes.RemoveAt(index); } } else { if (index >= 0) { ClassDefinition clsDef = schema.Classes[index]; foreach (TreeNode propNode in clsNode.Nodes) { if (!propNode.Checked) { string propName = propNode.Text; int pidx = clsDef.Properties.IndexOf(propName); if (pidx >= 0) { clsDef.Properties.RemoveAt(pidx); if (clsDef.IdentityProperties.Contains(propName)) { int idpdx = clsDef.IdentityProperties.IndexOf(propName); clsDef.IdentityProperties.RemoveAt(idpdx); } if (clsDef.ClassType == ClassType.ClassType_FeatureClass) { FeatureClass fc = (FeatureClass)clsDef; if (fc.GeometryProperty.Name == propName) { fc.GeometryProperty = null; } } } } } } } } if (rdXml.Checked) { using (var ios = new IoFileStream(txtXml.Text, "w")) { using (var writer = new XmlWriter(ios, false, XmlWriter.LineFormat.LineFormat_Indent)) { schema.WriteXml(writer); writer.Close(); } ios.Close(); } MessageService.ShowMessage("Schema saved to: " + txtXml.Text); this.DialogResult = DialogResult.OK; } else if (rdFile.Checked) { string fileName = txtFile.Text; if (ExpressUtility.CreateFlatFileDataSource(fileName)) { FdoConnection conn = ExpressUtility.CreateFlatFileConnection(fileName); bool disposeConn = true; using (FdoFeatureService svc = conn.CreateFeatureService()) { svc.ApplySchema(schema); if (MessageService.AskQuestion("Schema saved to: " + txtFile.Text + " connect to it?", "Saved")) { FdoConnectionManager mgr = ServiceManager.Instance.GetService <FdoConnectionManager>(); string name = MessageService.ShowInputBox(ResourceService.GetString("TITLE_CONNECTION_NAME"), ResourceService.GetString("PROMPT_ENTER_CONNECTION"), ""); if (name == null) { return; } while (name == string.Empty || mgr.NameExists(name)) { MessageService.ShowError(ResourceService.GetString("ERR_CONNECTION_NAME_EMPTY_OR_EXISTS")); name = MessageService.ShowInputBox(ResourceService.GetString("TITLE_CONNECTION_NAME"), ResourceService.GetString("PROMPT_ENTER_CONNECTION"), name); if (name == null) { return; } } disposeConn = false; mgr.AddConnection(name, conn); } } if (disposeConn) { conn.Close(); conn.Dispose(); } this.DialogResult = DialogResult.OK; } } }
public bool CreateSdf() { if (_view.CreateConnection && string.IsNullOrEmpty(_view.ConnectionName)) { _view.ShowError("Specify a connection name"); return(false); } if (ExpressUtility.CreateFlatFileDataSource("OSGeo.SDF", _view.SdfFile)) { FdoDataStoreConfiguration dstore = null; if (FileService.FileExists(_view.FeatureSchemaDefinition)) { dstore = FdoDataStoreConfiguration.FromFile(_view.FeatureSchemaDefinition); //SDF only permits the following: // 1 feature schema // 1 spatial context if (dstore.Schemas.Count > 1) { _view.ShowError("Multiple schemas were found in this document. SDF only allows 1 feature schema"); return(false); } if (dstore.SpatialContexts.Length > 1) { _view.ShowError("Multiple spatial contexts were found in this doucment. SDF only allows 1 spatial context"); return(false); } } FdoConnection conn = ExpressUtility.CreateFlatFileConnection("OSGeo.SDF", _view.SdfFile); if (dstore != null) { using (var svc = conn.CreateFeatureService()) { if (dstore.SpatialContexts.Length == 1) { //Overwrite existing spatial context if it exists var sc = dstore.SpatialContexts[0]; var asc = svc.GetActiveSpatialContext(); if (asc != null) { sc.Name = asc.Name; } svc.CreateSpatialContext(sc, (asc != null)); } var schema = dstore.Schemas[0]; if (_view.FixIncompatibilities) { IncompatibleSchema incS; if (!svc.CanApplySchema(schema, out incS)) { schema = svc.AlterSchema(schema, incS); } } svc.ApplySchema(schema); } } if (_view.CreateConnection) { _connMgr.AddConnection(_view.ConnectionName, conn); } else { conn.Dispose(); } } return(true); }
public override int Execute() { CommandStatus retCode; FdoConnection srcConn = new FdoConnection(_srcProvider, _srcConnStr); FdoConnection destConn = null; //Directory given, assume SHP if (Directory.Exists(_destPath)) { destConn = new FdoConnection("OSGeo.SHP", "DefaultFileLocation=" + _destPath); } else { if (ExpressUtility.CreateFlatFileDataSource(_destPath)) { destConn = ExpressUtility.CreateFlatFileConnection(_destPath); } else { throw new FdoException("Could not create data source: " + _destPath); } } try { srcConn.Open(); destConn.Open(); string srcName = "SOURCE"; string dstName = "TARGET"; FdoBulkCopyOptions options = new FdoBulkCopyOptions(); options.RegisterConnection(srcName, srcConn); options.RegisterConnection(dstName, destConn); using (FdoFeatureService srcService = srcConn.CreateFeatureService()) using (FdoFeatureService destService = destConn.CreateFeatureService()) { //See if spatial context needs to be copied to target if (!string.IsNullOrEmpty(_srcSpatialContext)) { SpatialContextInfo srcCtx = srcService.GetSpatialContext(_srcSpatialContext); if (srcCtx != null) { Console.WriteLine("Copying spatial context: " + srcCtx.Name); ExpressUtility.CopyAllSpatialContexts(new SpatialContextInfo[] { srcCtx }, destConn, true); } } else { //Copy all ExpressUtility.CopyAllSpatialContexts(srcConn, destConn, true); } FeatureSchema srcSchema = null; //See if partial class list is needed if (_srcClasses.Count > 0) { WriteLine("Checking if partial schema discovery is supported: " + srcService.SupportsPartialSchemaDiscovery()); srcSchema = srcService.PartialDescribeSchema(_srcSchema, _srcClasses); } else //Full copy { WriteLine("No classes specified, reading full source schema"); srcSchema = srcService.GetSchemaByName(_srcSchema); } if (srcSchema == null) { WriteError("Could not find source schema: " + _srcSchema); retCode = CommandStatus.E_FAIL_SCHEMA_NOT_FOUND; } else { WriteLine("Checking source schema for incompatibilities"); FeatureSchema targetSchema = null; IncompatibleSchema incSchema; if (destService.CanApplySchema(srcSchema, out incSchema)) { int clsCount = srcSchema.Classes.Count; WriteLine("Applying source schema (containing " + clsCount + " classes) to target"); destService.ApplySchema(srcSchema, null, true); targetSchema = srcSchema; } else { WriteWarning("Incompatibilities were detected in source schema. Applying a modified version to target"); FeatureSchema fixedSchema = destService.AlterSchema(srcSchema, incSchema); int clsCount = fixedSchema.Classes.Count; WriteLine("Applying modified source schema (containing " + clsCount + " classes) to target"); destService.ApplySchema(fixedSchema, null, true); targetSchema = fixedSchema; } //Now set class copy options foreach (ClassDefinition cd in srcSchema.Classes) { FdoClassCopyOptions copt = new FdoClassCopyOptions(srcName, dstName, srcSchema.Name, cd.Name, targetSchema.Name, cd.Name); copt.FlattenGeometries = _flatten; options.AddClassCopyOption(copt); } if (_flatten) { WriteWarning("The switch -flatten has been defined. Geometries that are copied will have any Z or M coordinates removed"); } FdoBulkCopy copy = new FdoBulkCopy(options); copy.ProcessMessage += new MessageEventHandler(OnMessage); copy.ProcessCompleted += new EventHandler(OnCompleted); Console.WriteLine("Executing bulk copy"); copy.Execute(); List <Exception> errors = new List <Exception>(copy.GetAllErrors()); if (errors.Count > 0) { string file = GenerateLogFileName("bcp-error-"); LogErrors(errors, file); base.WriteError("Errors were encountered during bulk copy."); retCode = CommandStatus.E_FAIL_BULK_COPY_WITH_ERRORS; } else { retCode = CommandStatus.E_OK; } retCode = CommandStatus.E_OK; } } } catch (Exception ex) { WriteException(ex); retCode = CommandStatus.E_FAIL_UNKNOWN; } finally { srcConn.Dispose(); destConn.Dispose(); } return((int)retCode); }