Example #1
0
        public void TransferMapDataWithinExtents_Test(string subDirectory, string mapDocumentFilename, string dataframeName, string outputShapefileDirectory)
        {
            string temp   = System.IO.Path.Combine(_dataDirectory, subDirectory);
            string mapDoc = System.IO.Path.Combine(temp, mapDocumentFilename);

            IMapDocument mapDocument = new MapDocumentClass();

            mapDocument.Open(mapDoc, null);

            IMap map = GeodatabaseUtil.GetMap(mapDocument, dataframeName);
            List <IFeatureLayer> layers     = GeodatabaseUtil.GetFeatureLayers(map);
            IDataset             dataset    = (IDataset)layers[0].FeatureClass;
            IGeoDataset          geoDataset = (IGeoDataset)layers[0].FeatureClass;

            string destination = System.IO.Path.Combine(temp, outputShapefileDirectory);

            if (Directory.Exists(destination))
            {
                Directory.Delete(destination, true);
            }
            Directory.CreateDirectory(destination);

            IWorkspace        outWorkspace        = GeodatabaseUtil.GetShapefileWorkspace(destination);
            IFeatureWorkspace outFeatureWorkspace = (IFeatureWorkspace)outWorkspace;
            IWorkspaceName    outWorkspaceName    = GeodatabaseUtil.GetWorkspaceName(outWorkspace);
            IFeatureClassName outFeatureclassName = GeodatabaseUtil.GetFeatureClassName(outWorkspaceName, dataset.Name);

            //ESRI.ArcGIS.ADF.Web.Geometry.Envelope envelope = new ESRI.ArcGIS.ADF.Web.Geometry.Envelope(227884.141, 4167884.377, 602406.528, 4470244.455);
            ESRI.ArcGIS.ADF.Web.Geometry.Envelope envelope = new ESRI.ArcGIS.ADF.Web.Geometry.Envelope(445092.693, 4360557.744, 541068.565, 4418287.592);

            Dictionary <int, KeyValuePair <string, IEnumInvalidObject> > invalidObjects = GeodatabaseUtil.TransferMapDataWithinExtents(map, outFeatureWorkspace, null, envelope, geoDataset.SpatialReference);
        }
Example #2
0
        public void GetAccessWorkspace_Test_Success(string subDirectory, string pgdbFileName)
        {
            string temp     = System.IO.Path.Combine(_dataDirectory, subDirectory);
            string pgdbFile = System.IO.Path.Combine(temp, pgdbFileName);

            Assert.IsNotNull(GeodatabaseUtil.GetAccessWorkspace(pgdbFile), "Unable to open workspace for the '" + pgdbFile + "' personal geodatabase");
        }
Example #3
0
        public void DoesFeatureClassExist_Test(string subDirectory, string pgdbFilename, string featureclassName)
        {
            string temp = System.IO.Path.Combine(_dataDirectory, subDirectory);
            string pgdb = System.IO.Path.Combine(temp, pgdbFilename);

            IWorkspace workspace = GeodatabaseUtil.GetAccessWorkspace(pgdb);

            Assert.IsTrue(FeatureClassUtil.DoesFeatureClassExist(workspace, featureclassName, true), "Unable to open FeatureClass inside a workspace");
        }
Example #4
0
        public void CreatePersonalGeodatabase_Test_Success(string pgdbFileName)
        {
            string pgdbFile = System.IO.Path.Combine(_dataDirectory, pgdbFileName);

            if (File.Exists(pgdbFile))
            {
                File.Delete(pgdbFile);
            }
            GeodatabaseUtil.CreatePersonalGeodatabase(pgdbFile);
        }
Example #5
0
        public void GetMap_Test(string subDirectory, string mapDocumentFilename, string dataframeName)
        {
            string temp = System.IO.Path.Combine(_dataDirectory, subDirectory);
            string map  = System.IO.Path.Combine(temp, mapDocumentFilename);

            IMapDocument mapDocument = new MapDocumentClass();

            mapDocument.Open(map, null);

            Assert.IsNotNull(GeodatabaseUtil.GetMap(mapDocument, dataframeName));
        }
Example #6
0
        public void TestGetAccessWorkspace(string pgdbFileName)
        {
            string pgdbFile = System.IO.Path.Combine(_dataDirectory, pgdbFileName);

            if (File.Exists(pgdbFile))
            {
                File.Delete(pgdbFile);
            }
            GeodatabaseUtil.CreatePersonalGeodatabase(pgdbFile);

            Assert.IsNotNull(GeodatabaseUtil.GetAccessWorkspace(pgdbFile), "Unable to open workspace for an access personal geodatabase");
        }
Example #7
0
        public void OpenFeatureClass_Test(string subDirectory, string pgdbFilename, string featureclassName)
        {
            string temp = System.IO.Path.Combine(_dataDirectory, subDirectory);
            string pgdb = System.IO.Path.Combine(temp, pgdbFilename);

            IWorkspace        workspace        = GeodatabaseUtil.GetAccessWorkspace(pgdb);
            IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace;

            IFeatureClass featureClass = FeatureClassUtil.OpenFeatureClass(featureWorkspace, featureclassName);

            Assert.IsNotNull(featureClass, "Unable to open Feature class '" + featureclassName + "' in the PGDB '" + pgdbFilename + "'.");
        }
        public void CreateTest(string subDirectory, string shapefile)
        {
            string            shapefileDirectory = System.IO.Path.Combine(_dataDirectory, subDirectory);
            IWorkspace        workspace          = GeodatabaseUtil.GetShapefileWorkspace(shapefileDirectory);
            IFeatureWorkspace featureWorkspace   = workspace as IFeatureWorkspace;
            IFeatureClass     featureClass       = featureWorkspace.OpenFeatureClass(shapefile);

            AoGISFields fields = new AoGISFields(featureClass.Fields);

            Assert.IsNotNull(fields, "Creating 'AoGISFields' failed.");
            Assert.IsNotNull(fields.GeometryType, "The geometry column was not found");
            Assert.AreEqual(fields.GeometryType, "MultiPolygon", "The geometry type does not match.");
            Assert.AreEqual(fields.AttributeFieldCount, 11, "The attribute field count does not match");
        }
Example #9
0
        public void GetFeatureLayers_FromDocument_Test(string subDirectory, string mapDocumentFilename, string dataframeName, int expectedLayerCount)
        {
            string temp   = System.IO.Path.Combine(_dataDirectory, subDirectory);
            string mapDoc = System.IO.Path.Combine(temp, mapDocumentFilename);

            IMapDocument mapDocument = new MapDocumentClass();

            mapDocument.Open(mapDoc, null);

            List <IFeatureLayer> layers = GeodatabaseUtil.GetFeatureLayers(mapDocument, dataframeName);

            Assert.IsNotNull(layers, "Unable to obtain layers from map");
            Assert.AreEqual(expectedLayerCount, layers.Count, "Number of layers from map do not match expected value.");
        }
Example #10
0
        public void GetWorkspaceName_Test(string subDirectory, string pgdbFileName)
        {
            string temp     = System.IO.Path.Combine(_dataDirectory, subDirectory);
            string pgdbFile = System.IO.Path.Combine(temp, pgdbFileName);

            IWorkspace workspace = GeodatabaseUtil.GetAccessWorkspace(pgdbFile);

            if (workspace != null)
            {
                Assert.IsNotNull(GeodatabaseUtil.GetWorkspaceName(workspace), "Unable to obtain the workspace name from the workspace.");
            }
            else
            {
                Assert.Fail("Unable to run test. A valid workspace input argument cannot be obtained.");
            }
        }
Example #11
0
        public void TestCreateTableWithValidArguments_Test(string pgdbFileName)
        {
            string pgdbFile = System.IO.Path.Combine(_dataDirectory, pgdbFileName);

            if (File.Exists(pgdbFile))
            {
                File.Delete(pgdbFile);
            }
            GeodatabaseUtil.CreatePersonalGeodatabase(pgdbFile);

            IWorkspaceFactory pgdbWSFactory    = new AccessWorkspaceFactoryClass();
            IWorkspace        workspace        = pgdbWSFactory.OpenFromFile(pgdbFile, 0);
            IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace;

            GeodatabaseUtil.CreateTable(featureWorkspace, "Test", null);
        }
Example #12
0
        public void TestCreateFeatureClassWithValidArguments_Test(string pgdbFileName)
        {
            string pgdbFile = System.IO.Path.Combine(_dataDirectory, pgdbFileName);

            if (File.Exists(pgdbFile))
            {
                File.Delete(pgdbFile);
            }
            GeodatabaseUtil.CreatePersonalGeodatabase(pgdbFile);

            IWorkspaceFactory pgdbWSFactory = new AccessWorkspaceFactoryClass();
            IWorkspace        workspace     = pgdbWSFactory.OpenFromFile(pgdbFile, 0);

            ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
            ISpatialReference        spatialReference        = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_NAD1983);

            spatialReference.SetDomain(-5120900, 900714804574.099, -9998100, 900709927374.099);

            GeodatabaseUtil.CreateFeatureClass(workspace, "Test", spatialReference, esriFeatureType.esriFTSimple, esriGeometryType.esriGeometryPoint, null, null, null, null);
        }
Example #13
0
        public void GetFeatureClassName_Test(string subDirectory, string pgdbFileName, string featureClassName)
        {
            string temp     = System.IO.Path.Combine(_dataDirectory, subDirectory);
            string pgdbFile = System.IO.Path.Combine(temp, pgdbFileName);

            IFeatureClassName fcName = GeodatabaseUtil.GetFeatureClassName(GeodatabaseUtil.GetPGDBWorkspaceName(pgdbFile), featureClassName);

            Assert.IsNotNull(fcName, "Unable to get the IFeatureClassName for the FeatureClass '" + featureClassName + "' in the pgdb file '" + pgdbFile + "'.");

            IName name = (IName)fcName;

            if (name != null)
            {
                Assert.IsNotNull(name.Open(), "FeatureClass name obtained was invalid.");
            }
            else
            {
                Assert.Fail("Unable to run test. A valid IName reference could not be obtained.");
            }
        }
Example #14
0
        public void ToXMLTest(string subDirectory, string shapefile)
        {
            string            shapefileDirectory = System.IO.Path.Combine(_dataDirectory, subDirectory);
            IWorkspace        workspace          = GeodatabaseUtil.GetShapefileWorkspace(shapefileDirectory);
            IFeatureWorkspace featureWorkspace   = workspace as IFeatureWorkspace;
            IFeatureClass     featureClass       = featureWorkspace.OpenFeatureClass(shapefile);

            target = new AoPolygonFCLayer(featureClass, "USStates", "FID");
            IQueryFilter queryFilter = new QueryFilterClass();

            queryFilter.WhereClause = "FID < 10";
            target.Search(null);

            StringBuilder text   = new StringBuilder();
            StringWriter  writer = new StringWriter(text);

            System.Xml.XmlWriter xwriter = System.Xml.XmlWriter.Create(writer);
            target.ToXML(xwriter);
            string result = text.ToString();

            Assert.IsTrue(!String.IsNullOrEmpty(result), "Serialization of Polygon FC layer failed");
        }
Example #15
0
        public void TransferData_Test(string subDirectory, string inputPGDBFile, string inputFeatureClassName)
        {
            string temp = System.IO.Path.Combine(_dataDirectory, subDirectory);
            string pgdb = System.IO.Path.Combine(temp, inputPGDBFile);

            IWorkspace        inWorkspace        = GeodatabaseUtil.GetAccessWorkspace(pgdb);
            IFeatureWorkspace inFeatureWorkspace = (IFeatureWorkspace)inWorkspace;
            IFeatureClass     inFeatureClass     = FeatureClassUtil.OpenFeatureClass(inFeatureWorkspace, inputFeatureClassName);
            IGeoDataset       geoDataset         = (IGeoDataset)inFeatureClass;

            string[] files = Directory.GetFiles(temp, inputFeatureClassName + ".*", SearchOption.TopDirectoryOnly);

            foreach (string item in files)
            {
                File.Delete(item);
            }

            IWorkspace        outWorkspace        = GeodatabaseUtil.GetShapefileWorkspace(temp);
            IFeatureWorkspace outFeatureWorkspace = (IFeatureWorkspace)outWorkspace;
            IWorkspaceName    outWorkspaceName    = GeodatabaseUtil.GetWorkspaceName(outWorkspace);
            IFeatureClassName outFeatureclassName = GeodatabaseUtil.GetFeatureClassName(outWorkspaceName, inputFeatureClassName);

            IEnumInvalidObject invalidObjects = GeodatabaseUtil.TransferData(inFeatureWorkspace, inFeatureClass, outFeatureWorkspace, outFeatureclassName, null, null, geoDataset.SpatialReference);
        }
Example #16
0
        public void GetShapefileWorkspace_Test_Failure(string shapefileDirectory)
        {
            IWorkspace workspace = GeodatabaseUtil.GetShapefileWorkspace(shapefileDirectory);

            Assert.AreEqual(workspace, null, "Did not fail when opening a shapefile workspace for an invalid directory");
        }
Example #17
0
        static void Main(string[] args)
        {
            string[]        prefixes = new string[] { "/f$", "/t$", "/o$" };
            CmdArgExtractor cae      = new CmdArgExtractor(prefixes, '/', '$');

            WindsorContainer container = new WindsorContainer(new XmlInterpreter());

            XmlConfigurator.Configure();

            IAoInitialize license = new AoInitializeClass();

            license.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcEditor);

            ConnectionStringSettings connStr       = ConfigurationManager.ConnectionStrings["SQL2008"];
            SqlConnection            sqlConnection = new SqlConnection(connStr.ConnectionString);

            sqlConnection.Open();
            SQL2008Database sqlDatabase = new SQL2008Database(connStr.ConnectionString);

            if ((args.Length != 0) && (args[0].Contains("?")))
            {
                Console.WriteLine("Please use the /f$ option to specify the shapefile path");
                Console.WriteLine("(OPTIONAL) the /t$ option to specify a table name. If not specified, the name of the shapefile will be used as the table name.");
                Console.WriteLine("(OPTIONAL) the /o$ option to specify whether to overwrite (Y/N) if a table with the same name exists. If not specified, the existing table will not be overwritten and the import will be aborted.");
                return;
            }

            //if( !cae.ValidArgsPrefixes( args ) )
            //{
            //    Console.WriteLine( "Invalid parameters specified." );
            //    Console.WriteLine( "" );
            //    Console.WriteLine( "Please use the /f: option to specify the shapefile path" );
            //    Console.WriteLine( "(OPTIONAL) the /t: option to specify a table name. If not specified, the name of the shapefile will be used as the table name." );
            //    Console.WriteLine( "(OPTIONAL) the /o: option to specify whether to overwrite if a table with the same name exists. If not specified, the existing table will not be overwritten and the import will be aborted." );
            //    return;
            //}

            string[ , ] inputParameters = cae.GetArgsTwoDimArray(args);

            Dictionary <string, string> inputParams = new Dictionary <string, string>();

            for (int i = 0; i < args.Length; i++)
            {
                inputParams.Add(inputParameters[0, i].ToLower(), inputParameters[1, i].ToLower());
            }

            if (!inputParams.ContainsKey("f"))
            {
                Console.WriteLine("A shapefile path must be specified to continue.");
                return;
            }

            string inputShapeFile = inputParams["f"];

            if (!File.Exists(inputShapeFile))
            {
                Console.WriteLine("The shapefile path is not valid.");
                return;
            }

            string shapefileDirectory = "C:\\SHPData\\WGS84";
            string shapefile          = "natarea";

            shapefileDirectory = Path.GetDirectoryName(Path.GetFullPath(inputShapeFile));
            shapefile          = Path.GetFileNameWithoutExtension(inputShapeFile);
            string tableName = shapefile;

            if (inputParams.ContainsKey("t"))
            {
                tableName = inputParams["t"];
            }

            if (sqlDatabase.ContainsTable(tableName))
            {
                Console.WriteLine("A table with the same name '" + tableName + "' already exists in the database.");
                if (inputParams.ContainsKey("o") && ((inputParams["o"] == "y") || (inputParams["o"] == "yes")))
                {
                    Console.WriteLine("Deleting the existing table and creating a new table.");
                    sqlDatabase.DeleteTable(tableName);
                }
                else
                {
                    Console.WriteLine("Aborting import.");
                    return;
                }
            }

            IWorkspace        workspace        = GeodatabaseUtil.GetShapefileWorkspace(shapefileDirectory);
            IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace;
            IFeatureClass     featureClass     = featureWorkspace.OpenFeatureClass(shapefile);

            DefaultAoGeometryTypeProvider aoGeometryTypeProvider = new DefaultAoGeometryTypeProvider();
            string geometryType = aoGeometryTypeProvider.GetGeometryType(featureClass.ShapeType);

            if (String.IsNullOrEmpty(geometryType))
            {
                Console.WriteLine("The geometry type of the source FeatureClass is not supported.");
                Console.ReadLine();
                return;
            }

            System.Collections.IDictionary parameters = new System.Collections.Hashtable();
            parameters.Add("featureClass", featureClass);
            parameters.Add("layerName", tableName);
            parameters.Add("keyFieldName", featureClass.OIDFieldName);
            AoFCLayer shapeFileLayer = container.Resolve <AoFCLayer>("Ao" + geometryType, parameters);

            if (shapeFileLayer == null)
            {
                Console.WriteLine("Unable to create the input framework layer for the FeatureClass. Please check the component configuration.");
                Console.ReadLine();
                return;
            }

            ISupportsGISFields shapeFileFields = shapeFileLayer as ISupportsGISFields;
            IGISFields         inputFields     = shapeFileFields.GetGISFields();

            SqlCommand sqlCommand;

            int?srid = null;

            if (shapeFileLayer is ISupportsSRID)
            {
                srid = (shapeFileLayer as ISupportsSRID).Srid;
            }

            IGISLayer sql2008Layer = null;

            //Create the dictionary with the parameters to create the corresponding SQL 2008 layer
            System.Collections.IDictionary sqlParameters = new System.Collections.Hashtable();
            sqlParameters.Add("tableName", tableName);
            sqlParameters.Add("shapeFieldName", "SHAPE");
            sqlParameters.Add("layerName", tableName);
            sqlParameters.Add("keyFieldName", featureClass.OIDFieldName);

            if (srid.HasValue && WKIDRanges.IsGeographic(srid.Value))
            {
                //Create the geography table with the fields from the FeatureClass
                sqlDatabase.CreateGeographyTable(inputFields, tableName, "SHAPE");
                sqlCommand = new SqlCommand("Select * from " + tableName, sqlConnection);
                sqlParameters.Add("dbCommand", sqlCommand);
                sql2008Layer = container.Resolve <IGISLayer>("Sql2008Geog" + geometryType, sqlParameters);
            }
            else
            {
                //Create the geometry table with the fields from the FeatureClass
                sqlDatabase.CreateGeometryTable(inputFields, tableName, "SHAPE");
                sqlCommand = new SqlCommand("Select * from " + tableName, sqlConnection);
                sqlParameters.Add("dbCommand", sqlCommand);
                sql2008Layer = container.Resolve <IGISLayer>("Sql2008Geom" + geometryType, sqlParameters);
            }

            //Get the editable layer reference to the destination sql table layer
            IGISEditableLayer editableLayer = sql2008Layer as IGISEditableLayer;

            //Quit the application if a valid editable layer reference could not be obtained
            if (editableLayer == null)
            {
                Console.WriteLine("The destination is either invalid or does not support editing.");
                return;
            }

            Console.WriteLine("Importing features...");
            int count = 0;

            shapeFileLayer.Search(null);
            while (shapeFileLayer.MoveNext())
            {
                try
                {
                    editableLayer.Add(shapeFileLayer.Current);
                }
                catch (Exception ex)
                {
                    _log.Error("An unexpected error occured while adding feature. " + ex.Message, ex);
                }
                Console.SetCursorPosition(0, Console.CursorTop);
                Console.Write(++count);
            }

            license.Shutdown();

            Console.WriteLine();
            Console.WriteLine("Import completed. Press any key to exit.");
            Console.ReadKey(false);
        }
Example #18
0
        public void GetShapefileWorkspace_Test_Success()
        {
            IWorkspace workspace = GeodatabaseUtil.GetShapefileWorkspace(_dataDirectory);

            Assert.AreNotEqual(workspace, null, "Did not open a shapefile workspace for a valid directory");
        }