Exemple #1
0
        /// <summary>
        /// Writes a geojson file from the given database into the open stream
        /// Selects items to write from the given selection criteria
        /// </summary>
        /// <param name="db">The apDatabase to dump</param>
        /// <param name="geojOutStream">The open outstream</param>
        /// <param name="rangeLimitNm">Range Limit in nm</param>
        /// <param name="Lat">Center Lat (decimal)</param>
        /// <param name="Lon">Center Lon (decimal)</param>
        /// <param name="navTypes">Type of nav items to include</param>
        /// <returns>True ??!!</returns>
        public static bool WriteGeoJson(apDatabase db, Stream geojOutStream,
                                        double rangeLimitNm = -1.0, double Lat = 0, double Lon = 0, AptTypes[] aptTypes = null)
        {
            /*
             *  {
             *  "type": "FeatureCollection",
             *  "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
             *  "features": [
             *                    entry, entry
             *              ]
             *   }
             */
            string head = $"{{\n\"type\": \"FeatureCollection\",\n" +
                          $"\"crs\": {{ \"type\": \"name\", \"properties\": {{ \"name\": \"urn:ogc:def:crs:OGC:1.3:CRS84\" }} }}," +
                          $"\"features\": [";
            string foot = $"]\n}}";

            using (var sw = new StreamWriter(geojOutStream, Encoding.UTF8)) {
                sw.WriteLine(head);
                if (rangeLimitNm > 0)
                {
                    WriteFile(sw, db.GetSubtable(rangeLimitNm, Lat, Lon, aptTypes));
                }
                else
                {
                    WriteFile(sw, db.GetTable( ));
                }
                sw.WriteLine(foot);
            }
            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Write complete airport db into the supplied database as one transaction
        /// </summary>
        /// <param name="db">The airport db to dump</param>
        /// <param name="sqConnection">The db connection</param>
        /// <returns>The result string, either empty or error</returns>
        public static string WriteSqDB(apDatabase db, SQLiteConnection sqConnection)
        {
            string ret = "";

            using (SQLiteCommand sqlite_cmd = sqConnection.CreateCommand( )) {
                sqlite_cmd.CommandText = "BEGIN TRANSACTION;";
                sqlite_cmd.ExecuteNonQuery( );
                try {
                    ret = WriteFile(sqConnection, db.GetTable( ));
                    sqlite_cmd.CommandText = "COMMIT;";
                    sqlite_cmd.ExecuteNonQuery( );
                }
                catch {
                    sqlite_cmd.CommandText = "ROLLBACK;";
                    sqlite_cmd.ExecuteNonQuery( );
                }
                finally {
                    if (!string.IsNullOrEmpty(ret))
                    {
                        ret = $"ERROR - inserting rows failed: {ret}\n";
                    }
                }
            }
            return(ret);
        }
Exemple #3
0
 /// <summary>
 /// Write the airport db as CSV formatted file
 /// </summary>
 /// <param name="db">The database to dump</param>
 /// <param name="csvOutStream">The stream to write to</param>
 /// <returns>True for success</returns>
 public static bool WriteCsv(apDatabase db, Stream csvOutStream)
 {
     using (var sw = new StreamWriter(csvOutStream, Encoding.UTF8)) {
         sw.WriteLine(apRec.CsvHeader);
         WriteFile(sw, db.GetTable(  ));
     }
     return(true);
 }
Exemple #4
0
        /// <summary>
        /// Reads all data from the given file
        /// </summary>
        /// <param name="db">The apDatabase to fill from the file</param>
        /// <param name="csvFile">The file to read</param>
        /// <returns>The result string, either empty or error</returns>
        public static string ReadDb(apDatabase db, string csvFile)
        {
            if (!File.Exists(csvFile))
            {
                return($"File does not exist\n");
            }

            return(ReadDbFile(db, csvFile));
        }
Exemple #5
0
        /// <summary>
        /// Reads one db file
        /// </summary>
        /// <param name="db">The apDatabase to fill from the file</param>
        /// <param name="fName">The qualified filename</param>
        /// <returns>The result string, either empty or error</returns>
        private static string ReadDbFile(apDatabase db, string fName)
        {
            var    icaoPre = Path.GetFileNameWithoutExtension(fName);
            string ret     = "";

            using (var sr = new StreamReader(fName)) {
                string buffer = sr.ReadLine( ); // header line
                buffer = sr.ReadLine( );
                while (!sr.EndOfStream)
                {
                    var rec = FromNative(buffer);
                    if (rec.IsValid)
                    {
                        ret += db.Add(rec); // collect adding information
                    }
                    buffer = sr.ReadLine( );
                }
                //
            }
            return(ret);
        }