Esempio n. 1
0
        private DEMFileSource ParseGDALSource(XmlReader reader)
        {
            DEMFileSource source = new DEMFileSource();

            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.IsPathRelative = 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)
            {
                _logger?.LogError($"Error parsing GDAL source: {ex.Message}");
            }

            return(source);
        }
Esempio n. 2
0
        private IEnumerable <DEMFileSource> GetSources(DEMDataSet dataSet, string vrtFileName)
        {
            Uri localVrtUri  = new Uri(Path.GetFullPath(vrtFileName), UriKind.Absolute);
            Uri remoteVrtUri = new Uri(dataSet.DataSource.IndexFilePath, UriKind.Absolute);

            double[] geoTransform;
            var      registration = dataSet.FileFormat.Registration;
            Dictionary <string, string> properties;

            // Create an XmlReader
            using (FileStream fileStream = new FileStream(vrtFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (XmlReader reader = XmlReader.Create(fileStream))
                {
                    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))
                        {
                            DEMFileSource source = ParseGDALSource(reader);

                            // SetLocalFileName
                            source.SourceFileNameAbsolute = new Uri(remoteVrtUri, source.SourceFileName).ToString();
                            source.LocalFileName          = new Uri(localVrtUri, source.SourceFileName).LocalPath;

                            // 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];

                            if (registration == DEMFileRegistrationMode.Grid)
                            {
                                source.BBox = new BoundingBox(Math.Round(source.OriginLon + geoTransform[1] / 2, 10),
                                                              Math.Round(source.DestLon - +geoTransform[1] / 2, 10),
                                                              Math.Round(source.DestLat - geoTransform[5] / 2, 10),
                                                              Math.Round(source.OriginLat + geoTransform[5] / 2, 10));
                            }
                            else
                            {
                                source.OriginLon = Math.Round(source.OriginLon, 10);
                                source.OriginLat = Math.Round(source.OriginLat, 10);
                                source.DestLon   = Math.Round(source.DestLon, 10);
                                source.DestLat   = Math.Round(source.DestLat, 10);
                                source.BBox      = new BoundingBox(source.OriginLon, source.DestLon, source.DestLat, source.OriginLat);
                            }


                            isOnFirstSource = false;

                            yield return(source);
                        }
                    }
                }
        }