Exemplo n.º 1
0
        /// <summary>
        /// Gets all class names from the specified flat-file data source
        /// </summary>
        /// <param name="sourceFile"></param>
        /// <returns></returns>
        public static string[] GetClassNames(string sourceFile)
        {
            List <string> classnames = new List <string>();
            FdoConnection source     = null;

            try
            {
                source = CreateFlatFileConnection(sourceFile);
                source.Open();
                using (FdoFeatureService svc = source.CreateFeatureService())
                {
                    using (FeatureSchemaCollection schemas = svc.DescribeSchema())
                    {
                        foreach (FeatureSchema sch in schemas)
                        {
                            foreach (ClassDefinition cd in sch.Classes)
                            {
                                classnames.Add(cd.Name);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (source != null)
                {
                    source.Dispose();
                }
            }
            return(classnames.ToArray());
        }
Exemplo n.º 2
0
        public override int Execute()
        {
            IConnection conn = null;
            try
            {
                conn = CreateConnection(_provider, _connstr);
                conn.Open();
            }
            catch (OSGeo.FDO.Common.Exception ex)
            {
                WriteException(ex);
                return (int)CommandStatus.E_FAIL_CONNECT;
            }

            using (FdoFeatureService service = new FdoFeatureService(conn))
            {
                using (FeatureSchemaCollection schemas = service.DescribeSchema())
                {
                    Console.WriteLine("\nSchemas in this connection: {0}", schemas.Count);
                    foreach (FeatureSchema fs in schemas)
                    {
                        Console.WriteLine("\nName: {0}\n", fs.Name);
                        Console.WriteLine("\tQualified Name: {0}", fs.QualifiedName);
                        Console.WriteLine("\tAttributes:");
                        WriteAttributes(fs.Attributes);
                    }
                }
            }

            conn.Close();
            return (int)CommandStatus.E_OK;
        }
Exemplo n.º 3
0
        public override int Execute()
        {
            IConnection conn = null;

            try
            {
                conn = CreateConnection(_provider, _connstr);
                conn.Open();
            }
            catch (OSGeo.FDO.Common.Exception ex)
            {
                WriteException(ex);
                return((int)CommandStatus.E_FAIL_CONNECT);
            }

            using (FdoFeatureService service = new FdoFeatureService(conn))
            {
                using (FeatureSchemaCollection schemas = service.DescribeSchema())
                {
                    Console.WriteLine("\nSchemas in this connection: {0}", schemas.Count);
                    foreach (FeatureSchema fs in schemas)
                    {
                        Console.WriteLine("\nName: {0}\n", fs.Name);
                        Console.WriteLine("\tQualified Name: {0}", fs.QualifiedName);
                        Console.WriteLine("\tAttributes:");
                        WriteAttributes(fs.Attributes);
                    }
                }
            }

            conn.Close();
            return((int)CommandStatus.E_OK);
        }
Exemplo n.º 4
0
        void CreateSchemaNodes(TreeNode connNode)
        {
            Action <TimeSpan> act  = (ts) => LoggingService.Info("Connection " + connNode.Name + ": Schema population completed in " + ts.TotalMilliseconds + "ms");
            FdoConnection     conn = _connMgr.GetConnection(connNode.Name);

            if (conn != null)
            {
                using (FdoFeatureService service = conn.CreateFeatureService())
                {
                    if (service.SupportsPartialSchemaDiscovery())
                    {
                        using (var measure = new TimeMeasurement(act))
                        {
                            List <string> schemaNames = service.GetSchemaNames();

                            //Pre-sort
                            SortedList <string, string> sorted = new SortedList <string, string>();
                            foreach (string name in schemaNames)
                            {
                                sorted.Add(name, name);
                            }

                            foreach (string name in schemaNames)
                            {
                                TreeNode schemaNode = CreateSchemaNode(name, true);
                                connNode.Nodes.Add(schemaNode);
                                schemaNode.Nodes.Add(ResourceService.GetString("TEXT_LOADING"));
                            }
                        }
                    }
                    else
                    {
                        using (var measure = new TimeMeasurement(act))
                        {
                            FeatureSchemaCollection schemas = service.DescribeSchema();

                            //Pre-sort
                            SortedList <string, FeatureSchema> sorted = new SortedList <string, FeatureSchema>();
                            foreach (FeatureSchema schema in schemas)
                            {
                                sorted.Add(schema.Name, schema);
                            }

                            foreach (FeatureSchema schema in schemas)
                            {
                                TreeNode schemaNode = CreateSchemaNode(schema.Name, false);
                                GetClassNodesFull(schema, schemaNode);
                                connNode.Nodes.Add(schemaNode);
                                schemaNode.Expand();
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 5
0
 public static IList <OSGeo.FDO.Schema.ClassDefinition> GetClasses(string title, string message, FdoConnection conn)
 {
     OSGeo.FDO.Schema.FeatureSchemaCollection schemas = null;
     using (FdoFeatureService service = conn.CreateFeatureService())
     {
         schemas = service.DescribeSchema();
     }
     if (schemas != null)
     {
         FdoMultiClassPicker diag = new FdoMultiClassPicker(title, message, schemas);
         if (diag.ShowDialog() == DialogResult.OK)
         {
             return(diag.SelectedClasses);
         }
     }
     return(null);
 }
Exemplo n.º 6
0
        /// <summary>
        /// Creates a FDO bulk copy task. The target file will be created as part of
        /// this method call. If the target path is a directory, it is assumed that
        /// SHP files are to be created and copied to.
        /// </summary>
        /// <param name="sourceFile">The path to the source file.</param>
        /// <param name="targetPath">
        /// The path to the target file/directory. If it is a directory, it is assumed
        /// that SHP files are to be created and copied to.
        /// </param>
        /// <param name="copySpatialContexts">If true, will also copy spatial contexts</param>
        /// <param name="fixIncompatibleSchema">If true, will try to fix the source schema to make it compatible with the target connection. If false, an exception will be thrown</param>
        /// <param name="flattenGeometries">If true, will strip all Z and M coordinates from all geometries that are copied</param>
        /// <returns></returns>
        public static FdoBulkCopy CreateBulkCopy(string sourceFile, string targetPath, bool copySpatialContexts, bool fixIncompatibleSchema, bool flattenGeometries)
        {
            FdoBulkCopyOptions options = null;
            FdoConnection      source  = null;
            FdoConnection      target  = null;

            try
            {
                //Is a directory. Implies a SHP connection
                if (IsShp(targetPath))
                {
                    //SHP doesn't actually support CreateDataStore. We use the following technique:
                    // - Connect to base directory
                    // - Clone source schema and apply to SHP connection.
                    // - A SHP file and related files are created for each feature class.
                    string shpdir = Directory.Exists(targetPath) ? targetPath : Path.GetDirectoryName(targetPath);
                    source = CreateFlatFileConnection(sourceFile);
                    target = new FdoConnection("OSGeo.SHP", "DefaultFileLocation=" + shpdir);

                    source.Open();

                    //Verify source has only classes with single geometry storage and only one geometry
                    using (FdoFeatureService svc = source.CreateFeatureService())
                    {
                        using (FeatureSchemaCollection schemas = svc.DescribeSchema())
                        {
                            foreach (FeatureSchema sch in schemas)
                            {
                                foreach (ClassDefinition cd in sch.Classes)
                                {
                                    int geomProps = 0;
                                    foreach (PropertyDefinition pd in cd.Properties)
                                    {
                                        if (pd.PropertyType == PropertyType.PropertyType_GeometricProperty)
                                        {
                                            GeometricPropertyDefinition gp    = pd as GeometricPropertyDefinition;
                                            GeometricType[]             types = FdoGeometryUtil.GetGeometricTypes(gp.GeometryTypes);
                                            if (types.Length != 1 || (types.Length == 1 && types[0] == GeometricType.GeometricType_All))
                                            {
                                                throw new FdoETLException(string.Format("Source file cannot be copied to a SHP file. {0}:{1}.{2} has more than one geometry storage type", sch.Name, cd.Name, pd.Name));
                                            }
                                            geomProps++;
                                        }
                                    }
                                    if (geomProps > 1)
                                    {
                                        throw new FdoETLException("Source file cannot be copied to a SHP file. One or more feature classes have more than one geometry property");
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (!CreateFlatFileDataSource(targetPath))
                    {
                        throw new FdoException("Unable to create data source on: " + targetPath);
                    }
                    source = CreateFlatFileConnection(sourceFile);
                    target = CreateFlatFileConnection(targetPath);
                }

                //Source and target connections may have been opened before this point
                if (source.State == FdoConnectionState.Closed)
                {
                    source.Open();
                }

                if (target.State == FdoConnectionState.Closed)
                {
                    target.Open();
                }

                string srcName = "SOURCE";
                string dstName = "TARGET";

                Dictionary <string, FdoConnection> connections = new Dictionary <string, FdoConnection>();
                connections.Add(srcName, source);
                connections.Add(dstName, target);

                options = new FdoBulkCopyOptions(connections, true);

                if (copySpatialContexts)
                {
                    CopyAllSpatialContexts(source, target, true);
                }

                using (FdoFeatureService srcService = source.CreateFeatureService())
                    using (FdoFeatureService destService = target.CreateFeatureService())
                    {
                        FeatureSchemaCollection schemas = srcService.DescribeSchema();
                        //Assume single-schema
                        FeatureSchema fs = schemas[0];
                        //Clone and apply to target
                        FeatureSchema      targetSchema = FdoSchemaUtil.CloneSchema(fs);
                        IncompatibleSchema incSchema;
                        string             sourceSchemaName = fs.Name;
                        string             targetSchemaName = string.Empty;

                        bool canApply = destService.CanApplySchema(targetSchema, out incSchema);
                        if (canApply)
                        {
                            destService.ApplySchema(targetSchema);
                            targetSchemaName = targetSchema.Name;
                        }
                        else
                        {
                            if (fixIncompatibleSchema)
                            {
                                FeatureSchema fixedSchema = destService.AlterSchema(targetSchema, incSchema);
                                destService.ApplySchema(fixedSchema);
                                targetSchemaName = fixedSchema.Name;
                            }
                            else
                            {
                                throw new Exception(incSchema.ToString());
                            }
                        }

                        //Copy all classes
                        foreach (ClassDefinition cd in fs.Classes)
                        {
                            FdoClassCopyOptions copt = new FdoClassCopyOptions(srcName, dstName, sourceSchemaName, cd.Name, targetSchemaName, cd.Name);
                            copt.Name = "Copy source to target [" + cd.Name + "]";
                            copt.FlattenGeometries = flattenGeometries;
                            options.AddClassCopyOption(copt);

                            //Flick on batch support if we can
                            if (destService.SupportsBatchInsertion())
                            {
                                copt.BatchSize = 300; //Madness? THIS IS SPARTA!
                            }
                        }
                    }
            }
            catch (Exception)
            {
                if (source != null)
                {
                    source.Dispose();
                }
                if (target != null)
                {
                    target.Dispose();
                }

                throw;
            }
            return(new FdoBulkCopy(options));
        }