Exemple #1
0
        private void LoadFeatureSource(string featureSourceId)
        {
            FeatureSourceDescription desc = _conn.FeatureService.DescribeFeatureSource(featureSourceId);

            List <ClassDefinition> classes = new List <ClassDefinition>(desc.AllClasses);

            cmbFeatureClasses.DisplayMember = "QualifiedName";
            cmbFeatureClasses.DataSource    = classes;

            txtResourceId.Text = featureSourceId;
        }
Exemple #2
0
        protected override void OnLoad(EventArgs e)
        {
            if (_cachedDesc == null)
            {
                _cachedDesc = _edsvc.CurrentConnection.FeatureService.DescribeFeatureSource(txtFeatureSource.Text);
            }

            //Init cached schemas and selected class
            if (!string.IsNullOrEmpty(txtFeatureClass.Text))
            {
                var cls = _cachedDesc.GetClass(txtFeatureClass.Text);
                if (cls != null)
                {
                    _selectedClass = cls;
                }
            }
            else
            {
                SetFeatureClass(_cachedDesc.Schemas[0].Classes[0]);
            }
        }
Exemple #3
0
        public void TestLoadFromStream()
        {
            var spatialContext = new FdoSpatialContextListSpatialContext()
            {
                Name                 = "Default",
                Description          = "Test Spatial Context",
                CoordinateSystemName = "Default",
                CoordinateSystemWkt  = "",
                ExtentType           = FdoSpatialContextListSpatialContextExtentType.Static,
                Extent               = new FdoSpatialContextListSpatialContextExtent
                {
                    LowerLeftCoordinate = new FdoSpatialContextListSpatialContextExtentLowerLeftCoordinate
                    {
                        X = "-180.0",
                        Y = "-90.0"
                    },
                    UpperRightCoordinate = new FdoSpatialContextListSpatialContextExtentUpperRightCoordinate
                    {
                        X = "180.0",
                        Y = "90"
                    }
                },
                XYTolerance = 0.00001,
                ZTolerance  = 0.00001
            };

            var doc    = new GenericConfigurationDocument();
            var schema = new FeatureSchema("Default", "");
            var cls    = new ClassDefinition("Test", "");

            var id = new DataPropertyDefinition("ID", "")
            {
                DataType = DataPropertyType.Int32
            };
            var name = new DataPropertyDefinition("Name", "")
            {
                IsNullable = true
            };
            var geom = new GeometricPropertyDefinition("Geometry", "")
            {
                SpatialContextAssociation = spatialContext.Name,
                GeometricTypes            = FeatureGeometricType.Point
            };

            cls.AddProperty(id, true);
            cls.AddProperty(name);
            cls.AddProperty(geom);
            cls.DefaultGeometryPropertyName = geom.Name;

            schema.AddClass(cls);

            doc.AddSchema(schema);
            doc.AddSpatialContext(spatialContext);

            var xmldoc = new XmlDocument();

            using (var ms = new MemoryStream())
            {
                var xml = doc.ToXml();
                xmldoc.LoadXml(xml);
                using (var xw = XmlWriter.Create(ms))
                {
                    xmldoc.WriteTo(xw);
                }
                //Rewind
                ms.Position = 0L;

                var fsd = new FeatureSourceDescription(ms);
                Assert.Single(fsd.SchemaNames);
                foreach (var sn in fsd.SchemaNames)
                {
                    var sch1 = fsd.GetSchema(sn);
                    Assert.NotNull(sch1);

                    Assert.Equal(schema.Name, sch1.Name);

                    foreach (var klass in schema.Classes)
                    {
                        var c1 = fsd.GetClass($"{schema.Name}:{klass.Name}");
                        var c2 = fsd.GetClass(schema.Name, klass.Name);
                        Assert.Equal(klass.Name, c1.Name);
                        Assert.Equal(c1.Name, c2.Name);
                        Assert.Equal(3, klass.Properties.Count);
                        Assert.Equal(klass.Properties.Count, c1.Properties.Count);
                        Assert.Equal(c1.Properties.Count, c2.Properties.Count);
                    }
                }
            }
        }
Exemple #4
0
        private string[] ExecuteBaseProcedure(LengthyOperationProgressCallBack cb, IBaseLoadProcedure proc, ref bool firstExecution)
        {
            List <string> resCreatedOrUpdated = new List <string>();

            var files     = proc.SourceFile;
            int pcPerFile = (int)(100 / files.Count);
            int current   = 0;

            string root = proc.RootPath;

            if (!root.EndsWith("/")) //NOXLATE
            {
                root += "/";         //NOXLATE
            }
            string sdp = proc.SpatialDataSourcesPath;
            string lp  = proc.LayersPath;

            if (!string.IsNullOrEmpty(sdp))
            {
                if (!sdp.EndsWith("/")) //NOXLATE
                {
                    sdp += "/";         //NOXLATE
                }
            }

            if (!string.IsNullOrEmpty(lp))
            {
                if (!lp.EndsWith("/")) //NOXLATE
                {
                    lp += "/";         //NOXLATE
                }
            }

            string fsRoot    = (string.IsNullOrEmpty(sdp) ? root : sdp) + proc.SpatialDataSourcesFolder;
            string layerRoot = (string.IsNullOrEmpty(lp) ? root : lp) + proc.LayersFolder;

            if (!fsRoot.EndsWith("/"))    //NOXLATE
            {
                fsRoot += "/";            //NOXLATE
            }
            if (!layerRoot.EndsWith("/")) //NOXLATE
            {
                layerRoot += "/";         //NOXLATE
            }
            List <string> resToUpdate = new List <string>();

            if (proc.ResourceId != null && proc.ResourceId.Count > 0)
            {
                resToUpdate.AddRange(proc.ResourceId);
                firstExecution = false;
            }
            else
            {
                firstExecution = true;
            }

            foreach (string file in files)
            {
                bool success = false;
                if (System.IO.File.Exists(file))
                {
                    //GOTCHA: We are assuming these SDF files are not SDF2 files. This is
                    //because there is no multi-platform solution to convert SDF2 files to SDF3

                    string resName  = System.IO.Path.GetFileNameWithoutExtension(file);
                    string dataName = System.IO.Path.GetFileName(file);
                    string dsId     = fsRoot + resName + ".DrawingSource";      //NOXLATE
                    string fsId     = fsRoot + resName + ".FeatureSource";      //NOXLATE
                    string lyrId    = layerRoot + resName + ".LayerDefinition"; //NOXLATE

                    if (proc.GenerateSpatialDataSources)
                    {
                        //Skip only if we have an update list and this resource id is not in it
                        bool skip = (resToUpdate.Count > 0 && !resToUpdate.Contains(fsId));
                        if (!skip)
                        {
                            if (proc.Type == LoadType.Dwf)
                            {
                                //Process is as follows:
                                //
                                // 1. Create and save drawing source document.
                                // 2. Upload dwf file as resource data for this document.

                                //Step 1: Create and save drawing source document.
                                IDrawingSource ds = ObjectFactory.CreateDrawingSource();
                                ds.SourceName      = dataName;
                                ds.CoordinateSpace = proc.CoordinateSystem;
                                ds.ResourceID      = dsId;
                                this.Parent.ResourceService.SaveResource(ds);
                                resCreatedOrUpdated.Add(dsId);
                                cb(this, new LengthyOperationProgressArgs(string.Format(Strings.TemplateCreated, dsId), current));

                                //Step 2: Load resource data for document
                                this.Parent.ResourceService.SetResourceData(dsId, dataName, ResourceDataType.File, System.IO.File.OpenRead(file));
                                cb(this, new LengthyOperationProgressArgs(string.Format(Strings.TemplateLoaded, file), current));

                                ds.RegenerateSheetList(this.Parent);
                                this.Parent.ResourceService.SaveResource(ds);
                                ds.UpdateExtents(this.Parent);
                                this.Parent.ResourceService.SaveResource(ds);
                            }
                            else
                            {
                                //Process is as follows:
                                //
                                // 1. Create and save feature source document.
                                // 2. Upload sdf file as resource data for this document.
                                // 3. Test the connection, it should check out.
                                // 4. If no spatial contexts are detected, assign a default one from the load procedure and save the modified feature source.

                                //Step 1: Create feature source document
                                string provider = "OSGeo.SDF"; //NOXLATE

                                switch (proc.Type)
                                {
                                case LoadType.Sqlite:
                                    provider = "OSGeo.SQLite";     //NOXLATE
                                    break;
                                }
                                var conp = new NameValueCollection();
                                conp["File"] = StringConstants.MgDataFilePath + dataName;
                                var fs = ObjectFactory.CreateFeatureSource(provider, conp);
                                fs.ResourceID = fsId;

                                this.Parent.ResourceService.SaveResource(fs);
                                resCreatedOrUpdated.Add(fsId);
                                cb(this, new LengthyOperationProgressArgs(string.Format(Strings.TemplateCreated, fsId), current));

                                //TODO: When the infrastructure is available to us (ie. A portable .net FDO/MG Feature Service API wrapper)
                                //Maybe then we can actually implement the generalization and duplicate record handling properties. Until then, we skip
                                //these options

                                //Step 2: Load resource data for document
                                this.Parent.ResourceService.SetResourceData(fsId, dataName, ResourceDataType.File, System.IO.File.OpenRead(file));

                                cb(this, new LengthyOperationProgressArgs(string.Format(Strings.TemplateCreated, file), current));

                                //Step 3: Test to make sure we're all good so far
                                string result = this.Parent.FeatureService.TestConnection(fsId);

                                //LocalNativeConnection returns this string, so I'm assuming this is the "success" result
                                if (result == "No errors" || result.ToLower() == "true") //NOXLATE
                                {
                                    //Step 4: Test to see if default cs needs to be specified
                                    FdoSpatialContextList spatialContexts = this.Parent.FeatureService.GetSpatialContextInfo(fsId, false);
                                    if (!string.IsNullOrEmpty(proc.CoordinateSystem))
                                    {
                                        //Case 1: No spatial contexts. Register one using SupplementalContextInfo
                                        if (spatialContexts.SpatialContext.Count == 0)
                                        {
                                            //Register the default CS from the load procedure
                                            fs.AddSpatialContextOverride(new OSGeo.MapGuide.ObjectModels.FeatureSource.v1_0_0.SpatialContextType()
                                            {
                                                Name             = "Default", //NOXLATE
                                                CoordinateSystem = proc.CoordinateSystem
                                            });

                                            //Update this feature source
                                            this.Parent.ResourceService.SaveResource(fs);

                                            cb(this, new LengthyOperationProgressArgs(string.Format(Strings.TemplateSetSpatialContext, fsId), current));
                                        }
                                        else if (spatialContexts.SpatialContext.Count == 1) //Case 2: One spatial context, but its WKT is blank. Override using SupplementalContextInfo
                                        {
                                            var sc = spatialContexts.SpatialContext[0];
                                            if (string.IsNullOrEmpty(sc.CoordinateSystemWkt))
                                            {
                                                //Register the default CS from the load procedure
                                                fs.AddSpatialContextOverride(new OSGeo.MapGuide.ObjectModels.FeatureSource.v1_0_0.SpatialContextType()
                                                {
                                                    Name             = sc.Name,
                                                    CoordinateSystem = proc.CoordinateSystem
                                                });

                                                //Update this feature source
                                                this.Parent.ResourceService.SaveResource(fs);

                                                cb(this, new LengthyOperationProgressArgs(string.Format(Strings.TemplateSetSpatialContext, fsId), current));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (proc.GenerateLayers)
                    {
                        //Skip only if we have an update list and this resource id is not in it
                        bool skip = (resToUpdate.Count > 0 && !resToUpdate.Contains(lyrId));
                        if (!skip)
                        {
                            if (proc.Type == LoadType.Dwf)
                            {
                                //Process is as follows
                                //
                                // 1. Enumerate the sheets on the drawing source
                                // 2. Set the referenced sheet to the first known sheet

                                var dwSvc = (IDrawingService)Parent.GetService((int)ServiceType.Drawing);
                                var list  = dwSvc.EnumerateDrawingSections(dsId);
                                if (list.Section.Count > 0)
                                {
                                    //Create drawing layer
                                    var ld = ObjectFactory.CreateDefaultLayer(LayerType.Drawing, new Version(1, 0, 0));
                                    var dl = ld.SubLayer as IDrawingLayerDefinition;
                                    dl.ResourceId = dsId;
                                    //Use the first one
                                    dl.Sheet = list.Section[0].Name;

                                    ld.ResourceID = lyrId;

                                    this.Parent.ResourceService.SaveResource(ld);
                                    resCreatedOrUpdated.Add(lyrId);
                                    cb(this, new LengthyOperationProgressArgs(string.Format(Strings.TemplateCreated, lyrId), current));
                                }
                            }
                            else
                            {
                                //NOTE: Because we are working against 1.0.0 object types this will always create 1.0.0 Layer Definition
                                //resources

                                //Process is as follows
                                //
                                // 1. Describe the schema of the feature source
                                // 2. If it contains at least one feature class, create a layer definition
                                // 3. Set the following layer definition properties:
                                //    - Feature Source: the feature source id
                                //    - Feature Class: the first feature class in the schema
                                //    - Geometry: the first geometry property in the first feature class
                                // 4. Infer the supported geometry types for this feature class. Toggle supported styles accordingly.

                                //Step 1: Describe the schema
                                //
                                //NOTE: I think we can get away with the full schema walk here. It's very unlikely we will be uploading a flat
                                //file with hundreds of classes. Even then, flat-file schema walk performance blows RDBMS walking performance
                                //out of the water anyway.
                                FeatureSourceDescription desc = this.Parent.FeatureService.DescribeFeatureSource(fsId);

                                if (desc.HasClasses())
                                {
                                    //Step 2: Find the first feature class with a geometry property
                                    ClassDefinition             clsDef = null;
                                    GeometricPropertyDefinition geom   = null;

                                    bool done = false;

                                    foreach (ClassDefinition cls in desc.AllClasses)
                                    {
                                        if (done)
                                        {
                                            break;
                                        }

                                        foreach (PropertyDefinition prop in cls.Properties)
                                        {
                                            if (done)
                                            {
                                                break;
                                            }

                                            if (prop.Type == OSGeo.MapGuide.MaestroAPI.Schema.PropertyDefinitionType.Geometry)
                                            {
                                                clsDef = cls;
                                                geom   = (GeometricPropertyDefinition)prop;
                                                done   = true;
                                            }
                                        }
                                    }

                                    if (clsDef != null && geom != null)
                                    {
                                        var ld = ObjectFactory.CreateDefaultLayer(LayerType.Vector, new Version(1, 0, 0));

                                        //Step 3: Assign default properties
                                        ld.ResourceID = lyrId;
                                        var vld = ld.SubLayer as IVectorLayerDefinition;
                                        vld.ResourceId  = fsId;
                                        vld.FeatureName = clsDef.QualifiedName;
                                        vld.Geometry    = geom.Name;

                                        //Step 4: Infer geometry storage support and remove unsupported styles
                                        var geomTypes = geom.GetIndividualGeometricTypes();
                                        var scale     = vld.GetScaleRangeAt(0);

                                        var remove = new List <string>();
                                        if (Array.IndexOf(geomTypes, FeatureGeometricType.Point) < 0)
                                        {
                                            remove.Add(FeatureGeometricType.Point.ToString().ToLower());
                                        }
                                        if (Array.IndexOf(geomTypes, FeatureGeometricType.Curve) < 0)
                                        {
                                            remove.Add(FeatureGeometricType.Curve.ToString().ToLower());
                                        }
                                        if (Array.IndexOf(geomTypes, FeatureGeometricType.Surface) < 0)
                                        {
                                            remove.Add(FeatureGeometricType.Surface.ToString().ToLower());
                                        }

                                        scale.RemoveStyles(remove);

                                        this.Parent.ResourceService.SaveResource(ld);
                                        resCreatedOrUpdated.Add(lyrId);
                                        cb(this, new LengthyOperationProgressArgs(string.Format(Strings.TemplateCreated, lyrId), current));
                                    }
                                }
                            }
                        }
                    }
                    success = true;
                }
                else
                {
                    cb(this, new LengthyOperationProgressArgs(string.Format(Strings.TemplateFileNotFound, file), current));
                }

                //This file is now fully processed, so increment progress
                current += pcPerFile;

                if (success)
                {
                    cb(this, new LengthyOperationProgressArgs(string.Format(Strings.TemplateFileProcessed, file), current));
                }
            }
            return(resCreatedOrUpdated.ToArray());
        }
Exemple #5
0
        private string[] ExecuteShpLoadProcedure(LengthyOperationProgressCallBack cb, IShpLoadProcedure shpl, ref bool firstExecution)
        {
            List <string> resCreatedOrUpdated = new List <string>();

            var shpFiles  = shpl.SourceFile;
            int pcPerFile = (int)(100 / shpFiles.Count);
            int current   = 0;

            string root = shpl.RootPath;

            if (!root.EndsWith("/")) //NOXLATE
            {
                root += "/";         //NOXLATE
            }
            string sdp = shpl.SpatialDataSourcesPath;
            string lp  = shpl.LayersPath;

            if (!string.IsNullOrEmpty(sdp))
            {
                if (!sdp.EndsWith("/")) //NOXLATE
                {
                    sdp += "/";         //NOXLATE
                }
            }

            if (!string.IsNullOrEmpty(lp))
            {
                if (!lp.EndsWith("/")) //NOXLATE
                {
                    lp += "/";         //NOXLATE
                }
            }

            string fsRoot    = (string.IsNullOrEmpty(sdp) ? root : sdp) + shpl.SpatialDataSourcesFolder;
            string layerRoot = (string.IsNullOrEmpty(lp) ? root : lp) + shpl.LayersFolder;

            if (!fsRoot.EndsWith("/"))    //NOXLATE
            {
                fsRoot += "/";            //NOXLATE
            }
            if (!layerRoot.EndsWith("/")) //NOXLATE
            {
                layerRoot += "/";         //NOXLATE
            }
            List <string> resToUpdate = new List <string>();

            if (shpl.ResourceId != null)
            {
                resToUpdate.AddRange(shpl.ResourceId);
                firstExecution = false;
            }
            else
            {
                firstExecution = true;
            }

            Dictionary <string, List <string> > extraFiles = new Dictionary <string, List <string> >();

            //Unlike SDF, a SHP file actually consists of multiple files
            foreach (string shp in shpFiles)
            {
                if (!extraFiles.ContainsKey(shp))
                {
                    extraFiles[shp] = new List <string>();
                }
                //we want to preserve casing for everything before the extension
                string prefix = shp.Substring(0, shp.LastIndexOf(".") + 1); //NOXLATE
                extraFiles[shp].Add(prefix + "shx");                        //NOXLATE
                extraFiles[shp].Add(prefix + "dbf");                        //NOXLATE
                extraFiles[shp].Add(prefix + "idx");                        //NOXLATE
                extraFiles[shp].Add(prefix + "prj");                        //NOXLATE
                extraFiles[shp].Add(prefix + "cpg");                        //NOXLATE

                //TODO: Are we missing anything else?
            }

            foreach (string file in shpFiles)
            {
                bool success = false;
                if (System.IO.File.Exists(file))
                {
                    string resName  = System.IO.Path.GetFileNameWithoutExtension(file);
                    string dataName = System.IO.Path.GetFileName(file);

                    string fsId  = fsRoot + resName + ".FeatureSource";      //NOXLATE
                    string lyrId = layerRoot + resName + ".LayerDefinition"; //NOXLATE

                    if (shpl.GenerateSpatialDataSources)
                    {
                        //Skip only if we have an update list and this resource id is not in it
                        bool skip = (resToUpdate.Count > 0 && !resToUpdate.Contains(fsId));
                        if (!skip)
                        {
                            //Process is as follows:
                            //
                            // 1. Create and save feature source document.
                            // 2. Upload sdf file as resource data for this document.
                            // 3. Test the connection, it should check out.
                            // 4. If no spatial contexts are detected, assign a default one from the load procedure and save the modified feature source.

                            //Step 1: Create feature source document
                            var conp = new NameValueCollection();
                            conp["DefaultFileLocation"] = StringConstants.MgDataFilePath + dataName; //NOXLATE
                            var fs = ObjectFactory.CreateFeatureSource("OSGeo.SHP", conp);           //NOXLATE
                            fs.ResourceID = fsId;

                            this.Parent.ResourceService.SaveResource(fs);
                            resCreatedOrUpdated.Add(fsId);
                            cb(this, new LengthyOperationProgressArgs(string.Format(Strings.TemplateCreated, fsId), current));

                            //TODO: When the infrastructure is available to us (ie. A portable .net FDO/MG Feature Service API wrapper)
                            //Maybe then we can actually implement the generalization and conversion properties. Until then, we skip
                            //these options

                            //Step 2: Load resource data for document
                            this.Parent.ResourceService.SetResourceData(fsId, dataName, ResourceDataType.File, System.IO.File.OpenRead(file));

                            cb(this, new LengthyOperationProgressArgs(string.Format(Strings.TemplateLoaded, file), current));

                            //Load supplementary files
                            foreach (string extraFile in extraFiles[file])
                            {
                                string dn = System.IO.Path.GetFileName(extraFile);
                                if (System.IO.File.Exists(extraFile))
                                {
                                    this.Parent.ResourceService.SetResourceData(fsId, dn, ResourceDataType.File, System.IO.File.OpenRead(extraFile));
                                    cb(this, new LengthyOperationProgressArgs(string.Format(Strings.TemplateLoaded, extraFile), current));
                                }
                            }

                            //Step 3: Test to make sure we're all good so far
                            string result = this.Parent.FeatureService.TestConnection(fsId);

                            if (Utility.IsSuccessfulConnectionTestResult(result))
                            {
                                //Step 4: Test to see if default cs needs to be specified
                                FdoSpatialContextList spatialContexts = this.Parent.FeatureService.GetSpatialContextInfo(fsId, false);
                                if (!string.IsNullOrEmpty(shpl.CoordinateSystem))
                                {
                                    bool hasPrj = false;
                                    //If there is no prj file, we can just upload one with the specified WKT
                                    var resData = this.Parent.ResourceService.EnumerateResourceData(fs.ResourceID);
                                    foreach (var resd in resData.ResourceData)
                                    {
                                        if (resd.Name == resName + ".prj") //NOXLATE
                                        {
                                            hasPrj = true;
                                            break;
                                        }
                                    }
                                    //Case 1: No .prj file. Most probable
                                    if (!hasPrj)
                                    {
                                        string tmp = System.IO.Path.GetTempFileName();
                                        System.IO.File.WriteAllText(tmp, shpl.CoordinateSystem);

                                        using (var fsr = System.IO.File.OpenRead(tmp))
                                        {
                                            this.Parent.ResourceService.SetResourceData(fs.ResourceID, resName + ".prj", ResourceDataType.File, fsr); //NOXLATE
                                            cb(this, new LengthyOperationProgressArgs(string.Format(Strings.TemplateUploadedPrj, resName), current));
                                        }

                                        try
                                        {
                                            System.IO.File.Delete(tmp);
                                        }
                                        catch { }
                                    }
                                    else if (spatialContexts.SpatialContext.Count == 0) //Case 2: No Spatial contexts. Declare one using SupplementalContextInfo
                                    {
                                        //Register the default CS from the load procedure
                                        fs.AddSpatialContextOverride(new OSGeo.MapGuide.ObjectModels.FeatureSource.v1_0_0.SpatialContextType()
                                        {
                                            Name             = "Default", //NOXLATE
                                            CoordinateSystem = shpl.CoordinateSystem
                                        });

                                        //Update this feature source
                                        this.Parent.ResourceService.SaveResource(fs);

                                        cb(this, new LengthyOperationProgressArgs(string.Format(Strings.TemplateSetSpatialContext, fsId), current));
                                    }
                                    else if (spatialContexts.SpatialContext.Count == 1) //Case 3: One spatial context with blank WKT. Override it using the SupplementalContextInfo
                                    {
                                        var sc = spatialContexts.SpatialContext[0];
                                        if (string.IsNullOrEmpty(sc.CoordinateSystemWkt))
                                        {
                                            //Register the default CS from the load procedure
                                            fs.AddSpatialContextOverride(new OSGeo.MapGuide.ObjectModels.FeatureSource.v1_0_0.SpatialContextType()
                                            {
                                                Name             = sc.Name,
                                                CoordinateSystem = shpl.CoordinateSystem
                                            });

                                            //Update this feature source
                                            this.Parent.ResourceService.SaveResource(fs);

                                            cb(this, new LengthyOperationProgressArgs(string.Format(Strings.TemplateSetSpatialContext, fsId), current));
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (shpl.GenerateLayers)
                    {
                        //Skip only if we have an update list and this resource id is not in it
                        bool skip = (resToUpdate.Count > 0 && !resToUpdate.Contains(lyrId));
                        if (!skip)
                        {
                            //NOTE: Because we are working against 1.0.0 object types this will always create 1.0.0 Layer Definition
                            //resources

                            //Process is as follows
                            //
                            // 1. Describe the schema of the feature source
                            // 2. If it contains at least one feature class, create a layer definition
                            // 3. Set the following layer definition properties:
                            //    - Feature Source: the feature source id
                            //    - Feature Class: the first feature class in the schema
                            //    - Geometry: the first geometry property in the first feature class
                            // 4. Infer the supported geometry types for this feature class. Toggle supported styles accordingly.

                            //Step 1: Describe the schema
                            //
                            //NOTE: I think we can get away with the full schema walk here. It's very unlikely we will be uploading a flat
                            //file with hundreds of classes. Even then, flat-file schema walk performance blows RDBMS walking performance
                            //out of the water anyway.
                            FeatureSourceDescription desc = this.Parent.FeatureService.DescribeFeatureSource(fsId);

                            //Step 2: Find the first feature class with a geometry property
                            ClassDefinition             clsDef = null;
                            GeometricPropertyDefinition geom   = null;

                            bool done = false;

                            foreach (ClassDefinition cls in desc.AllClasses)
                            {
                                if (done)
                                {
                                    break;
                                }

                                foreach (PropertyDefinition prop in cls.Properties)
                                {
                                    if (done)
                                    {
                                        break;
                                    }

                                    if (prop.Type == OSGeo.MapGuide.MaestroAPI.Schema.PropertyDefinitionType.Geometry)
                                    {
                                        clsDef = cls;
                                        geom   = (GeometricPropertyDefinition)prop;
                                        done   = true;
                                    }
                                }
                            }

                            if (clsDef != null && geom != null)
                            {
                                var ld = ObjectFactory.CreateDefaultLayer(LayerType.Vector, new Version(1, 0, 0));

                                //Step 3: Assign default properties
                                ld.ResourceID = lyrId;
                                var vld = ld.SubLayer as IVectorLayerDefinition;
                                vld.ResourceId  = fsId;
                                vld.FeatureName = clsDef.QualifiedName;
                                vld.Geometry    = geom.Name;

                                //Step 4: Infer geometry storage support and remove unsupported styles
                                var scale     = vld.GetScaleRangeAt(0);
                                var geomTypes = geom.GetIndividualGeometricTypes();
                                var remove    = new List <string>();
                                if (Array.IndexOf(geomTypes, FeatureGeometricType.Point) < 0)
                                {
                                    remove.Add(FeatureGeometricType.Point.ToString().ToLower());
                                }
                                if (Array.IndexOf(geomTypes, FeatureGeometricType.Curve) < 0)
                                {
                                    remove.Add(FeatureGeometricType.Curve.ToString().ToLower());
                                }
                                if (Array.IndexOf(geomTypes, FeatureGeometricType.Surface) < 0)
                                {
                                    remove.Add(FeatureGeometricType.Surface.ToString().ToLower());
                                }

                                scale.RemoveStyles(remove);

                                this.Parent.ResourceService.SaveResource(ld);
                                resCreatedOrUpdated.Add(lyrId);
                                cb(this, new LengthyOperationProgressArgs(string.Format(Strings.TemplateCreated, lyrId), current));
                            }
                        }
                    }
                    success = true;
                }
                else
                {
                    cb(this, new LengthyOperationProgressArgs(string.Format(Strings.TemplateFileNotFound, file), current));
                }

                //This file is now fully processed, so increment progress
                current += pcPerFile;

                if (success)
                {
                    cb(this, new LengthyOperationProgressArgs(string.Format(Strings.TemplateSuccess, file), current));
                }
            }

            return(resCreatedOrUpdated.ToArray());
        }
Exemple #6
0
        public void TestFeatureSchemaRoundtrip()
        {
            FeatureSchema   schema = new FeatureSchema("Default", "Default Schema");
            ClassDefinition cls    = new ClassDefinition("Class1", "Test Class");

            cls.AddProperty(new DataPropertyDefinition("ID", "ID Property")
            {
                IsAutoGenerated = true,
                DataType        = DataPropertyType.Int64,
                IsNullable      = false,
            }, true);

            var prop = cls.FindProperty("ID") as DataPropertyDefinition;

            Assert.AreEqual(1, cls.IdentityProperties.Count);
            Assert.AreEqual(1, cls.Properties.Count);
            Assert.NotNull(prop);
            Assert.AreEqual(DataPropertyType.Int64, prop.DataType);
            Assert.IsTrue(prop.IsAutoGenerated);
            Assert.IsFalse(prop.IsReadOnly);
            Assert.IsFalse(prop.IsNullable);

            cls.AddProperty(new DataPropertyDefinition("Name", "The name")
            {
                DataType = DataPropertyType.String,
                Length   = 255
            });

            prop = cls.FindProperty("Name") as DataPropertyDefinition;

            Assert.AreEqual(1, cls.IdentityProperties.Count);
            Assert.AreEqual(2, cls.Properties.Count);
            Assert.NotNull(prop);
            Assert.AreEqual(DataPropertyType.String, prop.DataType);
            Assert.IsFalse(prop.IsAutoGenerated);
            Assert.IsFalse(prop.IsReadOnly);
            Assert.IsFalse(prop.IsNullable);

            cls.AddProperty(new DataPropertyDefinition("Date", "The date")
            {
                DataType   = DataPropertyType.DateTime,
                IsNullable = true
            });

            prop = cls.FindProperty("Date") as DataPropertyDefinition;

            Assert.AreEqual(1, cls.IdentityProperties.Count);
            Assert.AreEqual(3, cls.Properties.Count);
            Assert.NotNull(prop);
            Assert.AreEqual(DataPropertyType.DateTime, prop.DataType);
            Assert.IsFalse(prop.IsAutoGenerated);
            Assert.IsFalse(prop.IsReadOnly);
            Assert.IsTrue(prop.IsNullable);

            schema.AddClass(cls);
            Assert.AreEqual(1, schema.Classes.Count);

            XmlDocument doc = new XmlDocument();

            schema.WriteXml(doc, doc);

            string path = Path.GetTempFileName();

            doc.Save(path);

            FeatureSourceDescription fsd = new FeatureSourceDescription(Utils.OpenFile(path));

            Assert.AreEqual(1, fsd.Schemas.Length);

            schema = fsd.Schemas[0];
            Assert.NotNull(schema);

            cls = schema.GetClass("Class1");
            Assert.NotNull(cls);

            prop = cls.FindProperty("ID") as DataPropertyDefinition;

            Assert.AreEqual(1, cls.IdentityProperties.Count);
            Assert.AreEqual(3, cls.Properties.Count);
            Assert.NotNull(prop);
            Assert.AreEqual(DataPropertyType.Int64, prop.DataType);
            Assert.IsTrue(prop.IsAutoGenerated);
            Assert.IsFalse(prop.IsReadOnly);
            Assert.IsFalse(prop.IsNullable);

            prop = cls.FindProperty("Name") as DataPropertyDefinition;

            Assert.AreEqual(1, cls.IdentityProperties.Count);
            Assert.AreEqual(3, cls.Properties.Count);
            Assert.NotNull(prop);
            Assert.AreEqual(DataPropertyType.String, prop.DataType);
            Assert.IsFalse(prop.IsAutoGenerated);
            Assert.IsFalse(prop.IsReadOnly);
            Assert.IsFalse(prop.IsNullable);

            prop = cls.FindProperty("Date") as DataPropertyDefinition;

            Assert.AreEqual(1, cls.IdentityProperties.Count);
            Assert.AreEqual(3, cls.Properties.Count);
            Assert.NotNull(prop);
            Assert.AreEqual(DataPropertyType.DateTime, prop.DataType);
            Assert.IsFalse(prop.IsAutoGenerated);
            Assert.IsFalse(prop.IsReadOnly);
            Assert.IsTrue(prop.IsNullable);
        }
Exemple #7
0
        public void TestMySqlSchema()
        {
            var fds = new FeatureSourceDescription(Utils.OpenFile("UserTestData\\gen_default1_MySql_master.xml"));

            Assert.AreEqual(1, fds.Schemas.Length);

            var fs = fds.GetSchema("AutoGen");

            Assert.IsNotNull(fs);

            Assert.AreEqual(12, fs.Classes.Count);

            var c1  = fds.GetClass("AutoGen:rtable1");
            var c2  = fds.GetClass("AutoGen:rtable2");
            var c3  = fds.GetClass("AutoGen:rtable5");
            var c4  = fds.GetClass("AutoGen:rtable6");
            var c5  = fds.GetClass("AutoGen:rtable7");
            var c6  = fds.GetClass("AutoGen:table1");
            var c7  = fds.GetClass("AutoGen:table3");
            var c8  = fds.GetClass("AutoGen:table4");
            var c9  = fds.GetClass("AutoGen:table5");
            var c10 = fds.GetClass("AutoGen:table6");
            var c11 = fds.GetClass("AutoGen:table7");
            var c12 = fds.GetClass("AutoGen:view1");

            Assert.NotNull(c1);
            Assert.NotNull(c2);
            Assert.NotNull(c3);
            Assert.NotNull(c4);
            Assert.NotNull(c5);
            Assert.NotNull(c6);
            Assert.NotNull(c7);
            Assert.NotNull(c8);
            Assert.NotNull(c9);
            Assert.NotNull(c10);
            Assert.NotNull(c11);
            Assert.NotNull(c12);

            Assert.AreEqual(1, c1.IdentityProperties.Count);
            Assert.AreEqual(1, c2.IdentityProperties.Count);
            Assert.AreEqual(1, c3.IdentityProperties.Count);
            Assert.AreEqual(1, c4.IdentityProperties.Count);
            Assert.AreEqual(1, c5.IdentityProperties.Count);
            Assert.AreEqual(1, c6.IdentityProperties.Count);
            Assert.AreEqual(2, c7.IdentityProperties.Count);
            Assert.AreEqual(1, c8.IdentityProperties.Count);
            Assert.AreEqual(1, c9.IdentityProperties.Count);
            Assert.AreEqual(2, c10.IdentityProperties.Count);
            Assert.AreEqual(1, c11.IdentityProperties.Count);
            Assert.AreEqual(0, c12.IdentityProperties.Count);

            Assert.AreEqual(c1.Properties.Count, 3);
            Assert.AreEqual(c2.Properties.Count, 5);
            Assert.AreEqual(c3.Properties.Count, 3);
            Assert.AreEqual(c4.Properties.Count, 4);
            Assert.AreEqual(c5.Properties.Count, 3);
            Assert.AreEqual(c6.Properties.Count, 47);
            Assert.AreEqual(c7.Properties.Count, 3);
            Assert.AreEqual(c8.Properties.Count, 4);
            Assert.AreEqual(c9.Properties.Count, 2);
            Assert.AreEqual(c10.Properties.Count, 3);
            Assert.AreEqual(c11.Properties.Count, 2);
            Assert.AreEqual(c12.Properties.Count, 3);

            Assert.AreEqual(c1, fds.GetClass("AutoGen", "rtable1"));
            Assert.AreEqual(c2, fds.GetClass("AutoGen", "rtable2"));
            Assert.AreEqual(c3, fds.GetClass("AutoGen", "rtable5"));
            Assert.AreEqual(c4, fds.GetClass("AutoGen", "rtable6"));
            Assert.AreEqual(c5, fds.GetClass("AutoGen", "rtable7"));
            Assert.AreEqual(c6, fds.GetClass("AutoGen", "table1"));
            Assert.AreEqual(c7, fds.GetClass("AutoGen", "table3"));
            Assert.AreEqual(c8, fds.GetClass("AutoGen", "table4"));
            Assert.AreEqual(c9, fds.GetClass("AutoGen", "table5"));
            Assert.AreEqual(c10, fds.GetClass("AutoGen", "table6"));
            Assert.AreEqual(c11, fds.GetClass("AutoGen", "table7"));
            Assert.AreEqual(c12, fds.GetClass("AutoGen", "view1"));

            Assert.IsTrue(string.IsNullOrEmpty(c1.DefaultGeometryPropertyName));
            Assert.IsTrue(string.IsNullOrEmpty(c2.DefaultGeometryPropertyName));
            Assert.IsTrue(string.IsNullOrEmpty(c3.DefaultGeometryPropertyName));
            Assert.IsTrue(string.IsNullOrEmpty(c4.DefaultGeometryPropertyName));
            Assert.IsTrue(string.IsNullOrEmpty(c5.DefaultGeometryPropertyName));
            //Though this feature class has geometries, the XML schema says none
            //are designated
            Assert.IsTrue(string.IsNullOrEmpty(c6.DefaultGeometryPropertyName));
            Assert.IsTrue(string.IsNullOrEmpty(c7.DefaultGeometryPropertyName));
            Assert.IsFalse(string.IsNullOrEmpty(c8.DefaultGeometryPropertyName));
            Assert.IsTrue(string.IsNullOrEmpty(c9.DefaultGeometryPropertyName));
            Assert.IsTrue(string.IsNullOrEmpty(c10.DefaultGeometryPropertyName));
            Assert.IsTrue(string.IsNullOrEmpty(c11.DefaultGeometryPropertyName));
            Assert.IsTrue(string.IsNullOrEmpty(c12.DefaultGeometryPropertyName));
        }