示例#1
0
        public Datum[] GetDatums()
        {
            using var returnListStruct = new CSMStringListContainer();

            lock (TGLLock.CsdManagementLock)
            {
                var resultCode = CsdManagement.csmGetListOfDatums(returnListStruct);

                if (resultCode == csmErrorCode.cecSuccess)
                {
                    var datums = returnListStruct.stringList.Split(new[] { CsdManagement.STRING_SEPARATOR }, StringSplitOptions.RemoveEmptyEntries)
                                 .Select(s => new Datum(
                                             datumSystemId: int.Parse(s.Split(CsdManagement.ITEM_SEPERATOR)[0]),
                                             datumType: int.Parse(s.Split(CsdManagement.ITEM_SEPERATOR)[1]),
                                             datumName: s.Split(CsdManagement.ITEM_SEPERATOR)[3]));

                    if (datums == null)
                    {
                        throw new Exception("Error attempting to retrieve list of datums, null result");
                    }

                    if (!datums.Any())
                    {
                        throw new Exception("No datums found");
                    }

                    return(datums.ToArray());
                }
            }

            return(null);
        }
示例#2
0
        /// <summary>
        /// Setup the underlying CoreXDotNet singleton management classes.
        /// </summary>
        private void SetupTGL()
        {
            var xmlFilePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "CoordSystemDatabase.xml");

            if (!File.Exists(xmlFilePath))
            {
                throw new Exception($"Failed to find TGL CSD database file '{xmlFilePath}'.");
            }

            using var reader = new StreamReader(xmlFilePath);
            var xmlData = reader.ReadToEnd();

            CsdManagement.csmLoadCoordinateSystemDatabase(xmlData)
            .Validate($"attempting to load coordinate system database '{xmlFilePath}'");

            _log.LogInformation($"CoreX {nameof(SetupTGL)}: GeodeticDatabasePath='{GeodeticDatabasePath}'");

            if (string.IsNullOrEmpty(GeodeticDatabasePath))
            {
                throw new Exception("Environment variable TGL_GEODATA_PATH must be set to the Geodetic data folder.");
            }
            if (!Directory.Exists(GeodeticDatabasePath))
            {
                _log.LogInformation($"Failed to find directory '{GeodeticDatabasePath}' defined by environment variable TGL_GEODATA_PATH.");
            }
            else
            {
                CoreXGeodataLogger.DumpGeodataFiles(_log, GeodeticDatabasePath);
            }

            CsdManagement.csmSetGeodataPath(GeodeticDatabasePath);
            GeodeticX.geoSetGeodataPath(GeodeticDatabasePath);
        }
示例#3
0
        private static bool ValidateCsib(string csib)
        {
            var sb    = new StringBuilder();
            var bytes = Encoding.ASCII.GetBytes(csib);

            for (var i = 0; i < bytes.Length; i++)
            {
                sb.Append(bytes[i]).Append(' ');
            }

            var blocks = sb.ToString().TrimEnd().Split(' ');
            var data   = new sbyte[blocks.Length];

            var index = 0;

            foreach (var b in blocks)
            {
                data[index++] = (sbyte)Convert.ToByte(b);
            }

            using var csmCsibData = new CSMCsibBlobContainer(data);
            using var csFromCSIB  = new CSMCoordinateSystemContainer();

            lock (TGLLock.CsdManagementLock)
            {
                CsdManagement.csmImportCoordSysFromCsib(csmCsibData, csFromCSIB)
                .Validate("attempting to import coordinate system from CSMCsibBlobContainer");
            }

            return(true);
        }
示例#4
0
        private string GetCSIBFromCSD(ICoordinateSystem coordinateSystem)
        {
            using var retStructFromICoordinateSystem = new CSMCsibBlobContainer();

            lock (TGLLock.CsdManagementLock)
            {
                CsdManagement.csmGetCSIBFromCoordinateSystem(coordinateSystem, false, Utils.FileListCallBack, Utils.EmbeddedDataCallback, retStructFromICoordinateSystem)
                .Validate("attempting to get CSMCsibBlobContainer from ICoordinateSystem");
            }

            return(GetCSIB(retStructFromICoordinateSystem));
        }
示例#5
0
        /// <inheritdoc/>
        private ICoordinateSystem GetDatumBySystemId(int datumSystemId)
        {
            using var datumContainer = new CSMCoordinateSystemContainer();

            lock (TGLLock.CsdManagementLock)
            {
                CsdManagement.csmGetDatumFromCSDSelectionById((uint)datumSystemId, false, null, null, datumContainer)
                .Validate($"attempting to retrieve datum {datumSystemId} by id.");
            }

            return(datumContainer.GetSelectedRecord());
        }
示例#6
0
        /// <inheritdoc/>
        public CoordinateSystem GetCSDFromCSIB(string csibString)
        {
            if (string.IsNullOrEmpty(csibString))
            {
                throw new ArgumentNullException(nameof(csibString), "CSIB string cannot be null");
            }

            using var csContainer = new CSMCoordinateSystemContainer();

            var csmCsibData = CreateCSMCsibBlobContainer(csibString);

            lock (TGLLock.CsdManagementLock)
            {
                CsdManagement.csmImportCoordSysFromCsib(csmCsibData, csContainer).
                Validate("attempting to import coordinate system from CSIB");
            }

            return(ConvertICoordinateSystem(csContainer.GetSelectedRecord()));
        }
示例#7
0
        /// <inheritdoc/>
        public CoordinateSystem GetCSDFromDCFileContent(string dcFileStr)
        {
            if (string.IsNullOrEmpty(dcFileStr))
            {
                throw new ArgumentNullException(nameof(dcFileStr), "DC file string cannot be null");
            }

            // We may receive coordinate system file content that's been uploaded (encoded) from a web api, must decode first.
            var fileContent = dcFileStr.DecodeFromBase64();

            using var csContainer = new CSMCoordinateSystemContainer();

            lock (TGLLock.CsdManagementLock)
            {
                CsdManagement.csmGetCoordinateSystemFromDCFile(fileContent, false, Utils.FileListCallBack, Utils.EmbeddedDataCallback, csContainer)
                .Validate("attempting to retrieve the DC file's CSD");
            }

            return(ConvertICoordinateSystem(csContainer.GetSelectedRecord()));
        }
示例#8
0
        private CSMCsibBlobContainer GetCSIBFromDCFileContent(string fileContent)
        {
            // We may receive coordinate system file content that's been uploaded (encoded) from a web api, must decode first.
            fileContent = fileContent.DecodeFromBase64();

            lock (TGLLock.CsdManagementLock)
            {
                var csmCsibBlobContainer = new CSMCsibBlobContainer();

                // Slow, takes 2.5 seconds, need to speed up somehow?
                var result = CsdManagement.csmGetCSIBFromDCFileData(
                    fileContent,
                    false,
                    Utils.FileListCallBack,
                    Utils.EmbeddedDataCallback,
                    csmCsibBlobContainer);

                if (result != (int)csmErrorCode.cecSuccess)
                {
                    switch ($"{result}")
                    {
                    case "cecGRID_FILE_OPEN_ERROR":
                    {
                        var geoidModelName = CalibrationFileHelper.GetGeoidModelName(Encoding.UTF8.GetBytes(fileContent));
                        throw new InvalidOperationException($"{nameof(GetCSIBFromDCFileContent)}: Geodata file not found for geoid model '{geoidModelName}'");
                    }

                    default:
                    {
                        throw new InvalidOperationException($"{nameof(GetCSIBFromDCFileContent)}: Get CSIB from file content failed, error {result}");
                    }
                    }
                }

                return(csmCsibBlobContainer);
            }
        }
示例#9
0
        /// <inheritdoc/>
        public string GetCSIBFromCSDSelection(string zoneGroupNameString, string zoneNameQueryString)
        {
            lock (TGLLock.CsdManagementLock)
            {
                using var retStructZoneGroups = new CSMStringListContainer();

                CsdManagement.csmGetListOfZoneGroups(retStructZoneGroups)
                .Validate("attempting to retrieve list of zone groups");

                var zoneGroups = retStructZoneGroups
                                 .stringList
                                 .Split(new[] { CsdManagement.STRING_SEPARATOR }, StringSplitOptions.RemoveEmptyEntries)
                                 .Select(s => s.Split(CsdManagement.ITEM_SEPERATOR)[1]);

                if (!zoneGroups.Any())
                {
                    throw new Exception("The count of zone groups should be greater than 0");
                }

                var zoneGroupName = zoneGroupNameString.Substring(zoneGroupNameString.IndexOf(",") + 1);
                using var retStructListOfZones = new CSMStringListContainer();

                CsdManagement.csmGetListOfZones(zoneGroupName, retStructListOfZones)
                .Validate($"attempting to retrieve list of zones for group '{zoneGroupName}'");

                var zones = retStructListOfZones
                            .stringList
                            .Split(new[] { CsdManagement.STRING_SEPARATOR }, StringSplitOptions.RemoveEmptyEntries);

                if (zones.Length == 0)
                {
                    throw new Exception($"The count of zones in {zoneGroupName} should be greater than 0");
                }

                var zoneElement = Array.Find(zones, element => element.EndsWith(zoneNameQueryString, StringComparison.OrdinalIgnoreCase));

                if (string.IsNullOrEmpty(zoneElement))
                {
                    throw new Exception($"Could not find '{zoneNameQueryString}' in the list of zones for group '{zoneGroupName}'");
                }

                var zoneName = zoneElement.Substring(zoneElement.IndexOf(",") + 1);
                var items    = zoneElement.Split(CsdManagement.ITEM_SEPERATOR);

                var zoneId = uint.Parse(items[0]);

                using var retCsStruct = new CSMCoordinateSystemContainer();

                CsdManagement.csmGetCoordinateSystemFromCSDSelectionDefaults(zoneGroupName, zoneName, false, Utils.FileListCallBack, Utils.EmbeddedDataCallback, retCsStruct)
                .Validate($"attempting to retrieve coordinate system from CSD selection; zone group: '{zoneGroupName}', zone: {zoneName}");

                var coordinateSystem = retCsStruct.GetSelectedRecord();

                // Many of our test calibration files fail validation; is this expected or do we have a parsing problem?
                // This validation logic was taken from TGL unit test classes, may not be correctly implemented.
                // coordinateSystem.Validate();

                var zoneID  = unchecked ((uint)coordinateSystem.ZoneSystemId());
                var datumID = unchecked ((uint)coordinateSystem.DatumSystemId());
                var geoidID = unchecked ((uint)coordinateSystem.GeoidSystemId());

                if (coordinateSystem.DatumSystemId() > 0)
                {
                    return(GetCSIBFromCSD(coordinateSystem));
                }
                else
                {
                    var datumResult = GetDatumBySystemId(1034);
                    using var csibFromIDs = new CSMCsibBlobContainer();

                    CsdManagement.csmGetCSIBFromCSDSelectionById(zoneID, datumId: 1034, geoidId: 0, false, Utils.FileListCallBack, Utils.EmbeddedDataCallback, csibFromIDs);

                    var csib = GetCSIB(csibFromIDs);

                    if (!string.IsNullOrEmpty(csib))
                    {
                        return(csib);
                    }
                }

                throw new Exception($"Error attempting to retrieve coordinate system from CSD selection; zone group: '{zoneGroupName}', zone: {zoneName}, no datum found");
            }
        }