Ejemplo n.º 1
0
        /// <summary>
        /// Main utility method to parse a Radarsat1 Metadata text file and populate the Radarsat1 Image Observation from the information
        /// contained within the file.
        ///
        /// The metadata is in RADARSAT CEOS format and is (c) Canadian Space Agency 1997 Agence spatiale canadienne, and processed and distributed by MDA Geospatial Services Inc.
        /// The data and metadata used within this software has been made freely available by the Canadian Federal Government under its Open Government initiative,
        /// subject to the following license terms: https://open.canada.ca/en/open-government-licence-canada
        /// </summary>
        /// <param name="metadataFileKey">The filepath/filename of the Radarsat1 metadata file</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <returns>A populated Radarsat1Observation object containing the metadata values, or null if the object couldn't be populated</returns>
        /// <throws>A FileLoadException if the file cannot be found or loaded</throws>
        public Radarsat1Observation <TFileStorageLocationType> GetRadarsat1ObservationFromMetadataFile(string radarsat1MetadataFilename)
        {
            Radarsat1Observation <TFileStorageLocationType> observation = null;
            // Radarsat 1 metadata files are in .txt format
            // ReSharper disable once RedundantAssignment
            var metadataFileText = string.Empty;

            if (radarsat1MetadataFilename.EndsWith(".txt"))
            {
                try
                {   // Open the Radarsat1 Metadata text file
                    using (StreamReader sr = new StreamReader(radarsat1MetadataFilename, Encoding.GetEncoding("iso-8859-1")))
                    {
                        metadataFileText = sr.ReadToEnd();
                    }
                }
                catch (Exception ex)
                {
                    throw new FileLoadException(ex.Message);
                }
                String       radarsatUniqueId   = Path.GetFileNameWithoutExtension(radarsat1MetadataFilename);
                String       sceneId            = metadataFileText.FindStringWithinAnchorText("SCENE_ID", "MDA ORDER NUMBER", true, true);
                String       mdaOrderNumber     = metadataFileText.FindStringWithinAnchorText("MDA ORDER NUMBER", "GEOGRAPHICAL AREA", true, true);
                String       geographicalArea   = metadataFileText.FindStringWithinAnchorText("GEOGRAPHICAL AREA", "SCENE START TIME", true, true);
                String       sceneStartTimeText = metadataFileText.FindStringWithinAnchorText("SCENE START TIME", "SCENE STOP TIME", true, true);
                const string dateFormatPattern  = "MMM dd yyyy HH:mm:ss.FFF";
                Guard.Against <ArgumentNullException>(
                    String.IsNullOrEmpty(sceneStartTimeText),
                    DeploySoftware_LaunchPad_Space_Resources.Exception_Radarsat1MetadataParser_GetRadarsat1ObservationFromMetadataFile_SceneStartTime_ArgumentNullExpection
                    );
                DateTime.TryParseExact(sceneStartTimeText,
                                       dateFormatPattern,
                                       CultureInfo.InvariantCulture,
                                       DateTimeStyles.None,
                                       out DateTime sceneStartTime);
                String sceneStopTimeText = metadataFileText.FindStringWithinAnchorText("SCENE STOP TIME", "ORBIT", true, true);
                Guard.Against <ArgumentNullException>(
                    String.IsNullOrEmpty(sceneStopTimeText),
                    DeploySoftware_LaunchPad_Space_Resources.Exception_Radarsat1MetadataParser_GetRadarsat1ObservationFromMetadataFile_SceneStopTime_ArgumentNullExpection
                    );
                DateTime.TryParseExact(sceneStopTimeText,
                                       dateFormatPattern,
                                       CultureInfo.InvariantCulture,
                                       DateTimeStyles.None,
                                       out DateTime sceneStopTime);
                String orbit          = metadataFileText.FindStringWithinAnchorText("ORBIT", "ORBIT DATA TYPE", true, true);
                String orbitDataType  = metadataFileText.FindStringWithinAnchorText("ORBIT DATA TYPE", "APPLICATION LUT", true, true);
                String applicationLut = metadataFileText.FindStringWithinAnchorText("APPLICATION LUT", "BEAM MODE", true, true);
                String beamMode       = metadataFileText.FindStringWithinAnchorText("BEAM MODE", "PRODUCT TYPE", true, true);
                String productType    = metadataFileText.FindStringWithinAnchorText("PRODUCT TYPE", "FORMAT", true, true);
                String format         = metadataFileText.FindStringWithinAnchorText("FORMAT", "# OF IMAGE LINES", true, true);
                Int32.TryParse(metadataFileText.FindStringWithinAnchorText("# OF IMAGE LINES", "# OF IMAGE PIXELS", true, true), out var numberImageLines);
                Int32.TryParse(metadataFileText.FindStringWithinAnchorText("# OF IMAGE PIXELS", "PIXEL SPACING", true, true), out var numberImagePixels);
                String pixelSpacing = metadataFileText.FindStringWithinAnchorText("PIXEL SPACING", "SCENE CENTRE", true, true);

                // when loading coordinates, disable celestial calculations (not needed)
                EagerLoad load = new EagerLoad
                {
                    Celestial = false,
                    Cartesian = true,
                    UTM_MGRS  = true
                };

                // get the scene centre coordinates. Centre is spelled properly in Canadian, eh.
                String sceneCentre = metadataFileText.FindStringWithinAnchorText("SCENE CENTRE", "CORNER COORDINATES", true, true);
                Guard.Against <ArgumentNullException>(
                    String.IsNullOrEmpty(sceneCentre),
                    DeploySoftware_LaunchPad_Space_Resources.Exception_Radarsat1MetadataParser_GetRadarsat1ObservationFromMetadataFile_SceneCentre_ArgumentNullExpection
                    );
                // ReSharper disable once PossibleNullReferenceException
                string[]   latLongSplit = sceneCentre.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                Coordinate c            = new Coordinate(load);
                c.Latitude  = GetLatitude(latLongSplit[0], c);
                c.Longitude = GetLongitude(latLongSplit[1], c);
                GeographicLocation centre = new GeographicLocation
                                            (
                    c.Latitude.ToDouble(),
                    c.Longitude.ToDouble(),
                    c.EagerLoadSettings
                                            );

                // get the image corner coordinates
                String cornerCoordinatesString = metadataFileText.FindStringWithinAnchorText("CORNER COORDINATES:", "For information on RADARSAT CEOS format see README.TXT", true, true);
                Guard.Against <ArgumentNullException>(
                    String.IsNullOrEmpty(cornerCoordinatesString),
                    DeploySoftware_LaunchPad_Space_Resources.Exception_Radarsat1MetadataParser_GetRadarsat1ObservationFromMetadataFile_CornerCoordinates_ArgumentNullExpection
                    );
                ImageObservationCornerCoordinates cornerCoords = GetCornerCoordinates(cornerCoordinatesString);

                ILicense     license   = new OpenGovernmentCanadaLicense();
                IUsageRights copyright = new Radarsat1DataUsageRights();

                // Create a new Radarsat1 Earth Observation image
                observation = new Radarsat1Observation <TFileStorageLocationType>(
                    null, // tenant Id - not used in this application
                    sceneId,
                    mdaOrderNumber,
                    geographicalArea,
                    sceneStartTime,
                    sceneStopTime,
                    orbit,
                    orbitDataType,
                    applicationLut,
                    beamMode,
                    productType,
                    format,
                    numberImageLines,
                    numberImagePixels,
                    pixelSpacing,
                    centre,
                    cornerCoords
                    )
                {
                    Name = radarsatUniqueId,

                    //Metadata.Description = radarsatUniqueId,
                    Copyright = copyright
                };
            }

            // ReSharper disable once PossibleNullReferenceException
            observation.Files = LoadExpectedObservationFiles(observation, radarsat1MetadataFilename);
            return(observation);
        }
Ejemplo n.º 2
0
        protected Radarsat1Observation <TFileStorageLocationType> .Radarsat1ObservationFiles LoadExpectedObservationFiles(Radarsat1Observation <TFileStorageLocationType> observation, string radarsat1MetadataFilename)
        {
            // get the list of related observation files
            String baseFilePath = radarsat1MetadataFilename.Substring(0, radarsat1MetadataFilename.Length - 4);
            IList <KeyValuePair <Radarsat1Observation <TFileStorageLocationType> .FileTypes, String> > expectedFiles = new List <KeyValuePair <Radarsat1Observation <TFileStorageLocationType> .FileTypes, String> >
            {
                new KeyValuePair <Radarsat1Observation <TFileStorageLocationType> .FileTypes, string>(
                    Radarsat1Observation <TFileStorageLocationType> .FileTypes.Nvol,
                    baseFilePath + "." + Radarsat1Observation <TFileStorageLocationType> .FileTypes.Nvol
                    ),
                new KeyValuePair <Radarsat1Observation <TFileStorageLocationType> .FileTypes, string>(
                    Radarsat1Observation <TFileStorageLocationType> .FileTypes.Sard, baseFilePath + "." + Radarsat1Observation <TFileStorageLocationType> .FileTypes.Sard
                    ),
                new KeyValuePair <Radarsat1Observation <TFileStorageLocationType> .FileTypes, string>(
                    Radarsat1Observation <TFileStorageLocationType> .FileTypes.Sarl, baseFilePath + "." + Radarsat1Observation <TFileStorageLocationType> .FileTypes.Sarl
                    ),
                new KeyValuePair <Radarsat1Observation <TFileStorageLocationType> .FileTypes, string>(
                    Radarsat1Observation <TFileStorageLocationType> .FileTypes.Sart, baseFilePath + "." + Radarsat1Observation <TFileStorageLocationType> .FileTypes.Sart
                    ),
                new KeyValuePair <Radarsat1Observation <TFileStorageLocationType> .FileTypes, string>(
                    Radarsat1Observation <TFileStorageLocationType> .FileTypes.Tfw, baseFilePath + "." + Radarsat1Observation <TFileStorageLocationType> .FileTypes.Tfw
                    ),
                new KeyValuePair <Radarsat1Observation <TFileStorageLocationType> .FileTypes, string>(
                    Radarsat1Observation <TFileStorageLocationType> .FileTypes.Tif, baseFilePath + "." + Radarsat1Observation <TFileStorageLocationType> .FileTypes.Tif
                    ),
                new KeyValuePair <Radarsat1Observation <TFileStorageLocationType> .FileTypes, string>(
                    Radarsat1Observation <TFileStorageLocationType> .FileTypes.Vol, baseFilePath + "." + Radarsat1Observation <TFileStorageLocationType> .FileTypes.Vol
                    )
            };

            // initialize the list of observation files
            Radarsat1Observation <TFileStorageLocationType> .Radarsat1ObservationFiles observationFiles = new Radarsat1Observation <TFileStorageLocationType> .Radarsat1ObservationFiles();

            var location = new TFileStorageLocationType();

            location.RootPath = new System.Uri(Directory.GetCurrentDirectory());

            // add each expected file type (if it exists)
            if (File.Exists(expectedFiles[0].Value))
            {
                observationFiles.Nvol = new NvolFile <Guid, TFileStorageLocationType>()
                {
                    Id       = SequentialGuid.NewGuid(),
                    Name     = Path.GetFileName(expectedFiles[0].Value),
                    Location = location
                };
            }
            if (File.Exists(expectedFiles[1].Value))
            {
                observationFiles.Sard = new SardFile <Guid, TFileStorageLocationType>()
                {
                    Id       = SequentialGuid.NewGuid(),
                    Name     = Path.GetFileName(expectedFiles[1].Value),
                    Location = location
                };
            }
            if (File.Exists(expectedFiles[2].Value))
            {
                observationFiles.Sarl = new SarlFile <Guid, TFileStorageLocationType>()
                {
                    Id       = SequentialGuid.NewGuid(),
                    Name     = Path.GetFileName(expectedFiles[2].Value),
                    Location = location
                };
            }
            if (File.Exists(expectedFiles[3].Value))
            {
                observationFiles.Sart = new SartFile <Guid, TFileStorageLocationType>()
                {
                    Id       = SequentialGuid.NewGuid(),
                    Name     = Path.GetFileName(expectedFiles[3].Value),
                    Location = location
                };
            }
            if (File.Exists(expectedFiles[4].Value))
            {
                observationFiles.Tfw = new TifWorldFile <Guid, TFileStorageLocationType>()
                {
                    Id       = SequentialGuid.NewGuid(),
                    Name     = Path.GetFileName(expectedFiles[4].Value),
                    Location = location
                };
            }
            if (File.Exists(expectedFiles[5].Value))
            {
                observationFiles.Tif = new TifFile <Guid, TFileStorageLocationType>()
                {
                    Id       = SequentialGuid.NewGuid(),
                    Name     = Path.GetFileName(expectedFiles[5].Value),
                    Location = location
                };
            }
            if (File.Exists(expectedFiles[6].Value))
            {
                observationFiles.Vol = new VolFile <Guid, TFileStorageLocationType>()
                {
                    Id       = SequentialGuid.NewGuid(),
                    Name     = Path.GetFileName(expectedFiles[6].Value),
                    Location = location
                };
            }
            return(observationFiles);
        }