Пример #1
0
        public void Decompress()
        {
            if (OsmFile.Extension != ".zip")
            {
                return;
            }

            DirectoryInfo osmFileDir = new DirectoryInfo(OsmFile.DirectoryName);

            try
            {
                ZipArchive      archive  = ZipFile.OpenRead(OsmFile.FullName);
                ZipArchiveEntry entry    = archive.Entries[0];
                string          fileName = string.Format("{0}\\{1}", osmFileDir.FullName, entry.FullName);
                entry.ExtractToFile(fileName);
                FileInfo unzippedFile = new FileInfo(fileName);
                if (unzippedFile.Exists)
                {
                    archive.Dispose();
                    archive = null;
                    OsmFile.Delete();
                    OsmFile = unzippedFile;
                    Zipped  = false;
                }
                else
                {
                    throw new Exception("Unable to decompress OSM file.");
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogException(ex);
            }
        }
Пример #2
0
        public Stream OpenWrite()
        {
            try
            {
                if (OsmFile.FullName == string.Empty)
                {
                    throw new ArgumentException("File name is null");
                }
                else if (!OsmFile.Exists)
                {
                    OsmFile.Create();
                }

                if (Zipped == true)
                {
                    if (ZipArchive != null)
                    {
                        ZipArchive.Dispose();
                        ZipArchive = null;
                    }

                    ZipArchive = new ZipArchive(OsmFile.OpenRead(), ZipArchiveMode.Update);
                    ZipArchiveEntry osmFileEntry = ZipArchive.CreateEntry(string.Format("{0}.osm", OsmFile.Name.Substring(0, OsmFile.Name.Length - OsmFile.Extension.Length)));
                    return(osmFileEntry.Open());
                }
                else
                {
                    return(OsmFile.OpenWrite());
                }
            } catch (Exception ex)
            {
                LogHelper.LogException(ex);
                return(null);
            }
        }
Пример #3
0
        public Stream OpenRead()
        {
            try
            {
                if (!OsmFile.Exists)
                {
                    throw new FileNotFoundException(m_fileInfo.Name);
                }

                if (Zipped == true)
                {
                    if (ZipArchive != null)
                    {
                        ZipArchive.Dispose();
                        ZipArchive = null;
                    }

                    ZipArchive = ZipFile.Open(OsmFile.FullName, ZipArchiveMode.Read);
                    ZipArchiveEntry entry = ZipArchive.GetEntry(OsmFile.Name.Substring(0, OsmFile.Name.Length - OsmFile.Extension.Length));
                    return(entry.Open());
                }
                else
                {
                    return(OsmFile.OpenRead());
                }
            } catch (Exception ex)
            {
                LogHelper.LogException(ex);
                return(null);
            }
        }
Пример #4
0
        public void Compress()
        {
            if (OsmFile.Extension == ".zip" || OsmFile.Extension == ".pbf")
            {
                if (Zipped || OsmFile.Extension == ".pbf") // PBF files are already compressed, we don't deal with them
                {
                    return;
                }
                else
                {
                    Zipped = true;
                    return;
                }
            }

            FileInfo      tempFile   = null;
            DirectoryInfo osmFileDir = new DirectoryInfo(OsmFile.DirectoryName);
            DirectoryInfo tempDir    = new DirectoryInfo(string.Format("{0}\\temp\\{1}", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), DateTime.Now.Ticks));

            string name    = OsmFile.Name;
            string zipName = string.Format("{0}\\{1}.zip", osmFileDir.FullName, name);

            try
            {
                tempDir.Create();
                tempFile = new FileInfo(string.Format("{0}\\{1}", tempDir.FullName, OsmFile.Name));
                OsmFile.CopyTo(tempFile.FullName);
                ZipFile.CreateFromDirectory(tempDir.FullName, zipName);

                FileInfo zipFile = new FileInfo(zipName);
                if (zipFile.Exists)
                {
                    zipFile.LastWriteTimeUtc = OsmFile.LastWriteTimeUtc;
                    zipFile.CreationTimeUtc  = OsmFile.CreationTimeUtc;
                    File.Delete(OsmFile.FullName);
                    OsmFile = zipFile;
                }
                else
                {
                    throw new Exception("Unable to compress OSM file, zipFile does not exist.");
                }
                Zipped = true;
            }
            catch (Exception ex)
            {
                LogHelper.LogException(ex);
            }
            finally
            {
                if (tempDir.Exists)
                {
                    tempDir.Delete(true);
                }
            }
        }
Пример #5
0
        public void WriteAllText(string input)
        {
            try
            {
                if (OsmFile.FullName == string.Empty)
                {
                    throw new ArgumentException("File name is null");
                }
                else if (Zipped == true)
                {
                    if (ZipArchive != null)
                    {
                        ZipArchive.Dispose();
                        ZipArchive = null;
                    }

                    ZipArchive = new ZipArchive(OsmFile.OpenRead(), ZipArchiveMode.Update);
                    ZipArchiveEntry osmFileEntry = null;
                    string          entryName    = string.Format("{0}.osm", OsmFile.Name.Substring(0, OsmFile.Name.Length - OsmFile.Extension.Length));
                    try
                    {
                        osmFileEntry = ZipArchive.GetEntry(entryName);
                    }
                    catch (Exception ex)
                    {
                    }
                    if (osmFileEntry == null)
                    {
                        osmFileEntry = ZipArchive.CreateEntry(entryName);
                    }

                    using (Stream stream = osmFileEntry.Open())
                    {
                        using (StreamWriter sw = new StreamWriter(stream))
                        {
                            sw.Write(input);
                        }
                    }
                }
                else
                {
                    File.WriteAllText(OsmFile.FullName, input);
                }
            } catch (Exception ex)
            {
                LogHelper.LogException(ex);
            }
        }
Пример #6
0
        public byte[] ReadAllBytes()
        {
            try
            {
                if (!OsmFile.Exists)
                {
                    throw new FileNotFoundException(OsmFile.FullName);
                }
                else if (Zipped == true)
                {
                    if (ZipArchive != null)
                    {
                        ZipArchive.Dispose();
                        ZipArchive = null;
                    }

                    ZipArchive = new ZipArchive(OsmFile.OpenRead(), ZipArchiveMode.Update);
                    ZipArchiveEntry osmFileEntry = ZipArchive.CreateEntry(string.Format("{0}.osm", OsmFile.Name.Substring(0, OsmFile.Name.Length - OsmFile.Extension.Length)));

                    using (Stream stream = osmFileEntry.Open())
                    {
                        using (MemoryStream memStream = new MemoryStream())
                        {
                            byte[] buffer = new byte[16 * 1024];
                            int    read;
                            while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                memStream.Write(buffer, 0, read);
                            }
                            return(memStream.ToArray());
                        }
                    }
                }
                else
                {
                    return(File.ReadAllBytes(OsmFile.FullName));
                }
            } catch (Exception ex)
            {
                LogHelper.LogException(ex);
                return(null);
            }
        }
Пример #7
0
        public static bool SplitWay(this OsmFile osmFile, OSMWay wayToSplit, LatitudeLongitude splitStart, LatitudeLongitude splitEnd)
        {
            try
            {
                // Find where split path intersects way
                List <OSMNode> nodesForWay = new List <OSMNode>();
                foreach (string nodeId in wayToSplit.NodeReferences)
                {
                    OSMNode node;
                    if (osmFile.NodesById.TryGetValue(nodeId, out node))
                    {
                        nodesForWay.Add(node);
                    }
                }

                // Create separate ways
                // Remove intersection connections
                return(true);
            } catch (Exception ex)
            {
            }
            return(false);
        }
Пример #8
0
        public string ReadAllText()
        {
            try
            {
                if (!OsmFile.Exists)
                {
                    throw new FileNotFoundException(OsmFile.FullName);
                }
                else if (Zipped == true)
                {
                    if (ZipArchive != null)
                    {
                        ZipArchive.Dispose();
                        ZipArchive = null;
                    }

                    ZipArchive = new ZipArchive(OsmFile.OpenRead(), ZipArchiveMode.Update);
                    ZipArchiveEntry osmFileEntry = ZipArchive.CreateEntry(string.Format("{0}.osm", OsmFile.Name.Substring(0, OsmFile.Name.Length - OsmFile.Extension.Length)));

                    using (Stream stream = osmFileEntry.Open())
                    {
                        using (StreamReader sr = new StreamReader(stream))
                        {
                            return(sr.ReadToEnd());
                        }
                    }
                }
                else
                {
                    return(File.ReadAllText(OsmFile.FullName));
                }
            } catch (Exception ex)
            {
                LogHelper.LogException(ex);
                return("");
            }
        }