예제 #1
0
        private static void Test_GetMetadataFromVRT(ElevationService elevationService, DEMDataSet dataSet)
        {
            GDALVRTFileService gdalService = new GDALVRTFileService(elevationService.GetDEMLocalPath(dataSet), dataSet);

            gdalService.Setup(false);

            GDALSource source = gdalService.Sources().FirstOrDefault(s => s.SourceFileName.EndsWith("N043E006_AVE_DSM.tif"));
        }
        private GDALSource ParseGDALSource(XmlReader reader)
        {
            GDALSource source = new GDALSource();

            try
            {
                source.Type = reader.Name;
                int depth = reader.Depth;
                while (reader.Read())
                {
                    if (reader.Depth == depth)
                    {
                        break;
                    }

                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        //<xs:element name="SourceFilename" type="SourceFilenameType"/>
                        //<xs:element name="OpenOptions" type="OpenOptionsType"/>
                        //<xs:element name="SourceBand" type="xs:string"/>  <!-- should be refined into xs:nonNegativeInteger or mask,xs:nonNegativeInteger -->
                        //<xs:element name="SourceProperties" type="SourcePropertiesType"/>
                        //<xs:element name="SrcRect" type="RectType"/>
                        //<xs:element name="DstRect" type="RectType"/>

                        switch (reader.Name)
                        {
                        case "SourceFilename":

                            if (reader.HasAttributes)
                            {
                                while (reader.MoveToNextAttribute())
                                {
                                    switch (reader.Name)
                                    {
                                    case "relativeToVRT":
                                        reader.ReadAttributeValue();
                                        source.RelativeToVRT = reader.Value == "1";
                                        break;
                                    }
                                }
                                reader.MoveToElement();
                            }
                            source.SourceFileName = reader.ReadElementContentAsString();

                            break;

                        case "DstRect":
                            //<DstRect xOff="249600" yOff="8400" xSize="1201" ySize="1201" />
                            if (reader.HasAttributes)
                            {
                                while (reader.MoveToNextAttribute())
                                {
                                    switch (reader.Name)
                                    {
                                    case "xOff":
                                        reader.ReadAttributeValue();
                                        source.DstxOff = int.Parse(reader.Value);
                                        break;

                                    case "yOff":
                                        reader.ReadAttributeValue();
                                        source.DstyOff = int.Parse(reader.Value);
                                        break;

                                    case "xSize":
                                        reader.ReadAttributeValue();
                                        source.DstxSize = int.Parse(reader.Value);
                                        break;

                                    case "ySize":
                                        reader.ReadAttributeValue();
                                        source.DstySize = int.Parse(reader.Value);
                                        break;
                                    }
                                }
                                reader.MoveToElement();
                            }

                            break;

                        case "NODATA":

                            source.NoData = reader.ReadElementContentAsDouble();
                            break;

                        default:

                            source.Properties[reader.Name] = reader.ReadElementContentAsString();
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError($"Error parsing GDAL source: {ex.Message}");
            }

            return(source);
        }
        /// <summary>
        /// Enumerates throught all the sources
        /// Supports only VRTRasterBand with ComplexSource or SimpleSource
        /// </summary>
        /// <returns></returns>
        public IEnumerable <GDALSource> Sources()
        {
            if (_useMemCache && _cacheByDemName.ContainsKey(_vrtFileName))
            {
                foreach (var item in _cacheByDemName[_vrtFileName])
                {
                    yield return(item);
                }
            }
            else
            {
                // Create an XmlReader
                using (XmlReader reader = XmlReader.Create(_vrtFileName))
                {
                    if (reader.ReadToFollowing("GeoTransform"))
                    {
                        _geoTransform = ParseGeoTransform(reader.ReadElementContentAsString());
                    }
                    else
                    {
                        throw new Exception("GeoTransform element not found!");
                    }

                    string sourceName = "";
                    if (reader.ReadToFollowing("VRTRasterBand"))
                    {
                        _properties = new Dictionary <string, string>();
                        while (reader.Read())
                        {
                            if (reader.NodeType == XmlNodeType.Element)
                            {
                                if (reader.Name == "ComplexSource" || reader.Name == "SimpleSource")
                                {
                                    sourceName = reader.Name;
                                    break;
                                }
                                _properties[reader.Name] = reader.ReadElementContentAsString();
                            }
                        }

                        bool isOnFirstSource = true;
                        while (isOnFirstSource || reader.ReadToFollowing(sourceName))
                        {
                            GDALSource source = ParseGDALSource(reader);

                            // SetLocalFileName
                            source.SourceFileNameAbsolute = new Uri(_remoteVrtUri, source.SourceFileName).ToString();
                            source.LocalFileName          = new Uri(_localVrtUri, source.SourceFileName).AbsolutePath;

                            // Transform origin
                            // Xp = padfTransform[0] + P * padfTransform[1] + L * padfTransform[2];
                            // Yp = padfTransform[3] + P * padfTransform[4] + L * padfTransform[5];
                            source.OriginLon = _geoTransform[0] + source.DstxOff * _geoTransform[1] + source.DstyOff * _geoTransform[2];
                            source.OriginLat = _geoTransform[3] + source.DstxOff * _geoTransform[4] + source.DstyOff * _geoTransform[5];
                            source.DestLon   = _geoTransform[0] + (source.DstxOff + source.DstxSize) * _geoTransform[1] + (source.DstyOff + source.DstySize) * _geoTransform[2];
                            source.DestLat   = _geoTransform[3] + (source.DstxOff + source.DstxSize) * _geoTransform[4] + (source.DstyOff + source.DstySize) * _geoTransform[5];
                            source.BBox      = new BoundingBox(source.OriginLon, source.DestLon, source.DestLat, source.OriginLat);
                            isOnFirstSource  = false;

                            yield return(source);
                        }
                    }
                }
            }
        }