public async Task <IAGSGroupTables> CreateAGS(Guid Id, Guid tablemapId, string[] agstables, string options, IDataService _dataService)
        {
            ge_data_table dt = await _dataService.GetFileAsClass <ge_data_table>(Id);

            ge_table_map map = await _dataService.GetFileAsClass <ge_table_map>(tablemapId);

            if (dt != null && map != null)
            {
                IAGSGroupTables ags_file = CreateAGS(dt, map, agstables, options);
                return(ags_file);
            }

            return(null);
        }
        public async Task <KMLDoc> CreateKML(Guid Id, Guid tablemapId, string options, IDataService _dataService)
        {
            ge_data_table dt = await _dataService.GetFileAsClass <ge_data_table>(Id);

            ge_table_map map = await _dataService.GetFileAsClass <ge_table_map>(tablemapId);

            if (dt != null && map != null)
            {
                KMLDoc kml_file = CreateKML(dt, map, options);
                return(kml_file);
            }

            return(null);
        }
        public KMLDoc CreateKML(ge_data_table dt_file, ge_table_map map, string options)
        {
            KMLDoc       kml          = new KMLDoc();
            ILocaService _locaService = new LocaService();

            foreach (table_map tm in map.table_maps.Where(m => m.destination == "kml"))
            {
                List <ge_data> list = ConvertDataTable <ge_data>(dt_file.dt, tm);
                foreach (ge_data d in list)
                {
                    string msg;
                    if (d.folder == null)
                    {
                        d.folder = dt_file.dt.TableName;
                    }
                    if (d.datumProjection == datumProjection.NONE)
                    {
                        d.datumProjection = datumProjection.OSGB36NG;
                    }
                    if (d.locName == "P2BH20")
                    {
                        Console.WriteLine("P2BH20");
                    }
                    _locaService.UpdateProjectionLoc(d, out msg, "");
                    d.phistory = msg;
                }
                string[] folders = list.Select(m => m.folder).Distinct().ToArray();
                foreach (string folder in folders)
                {
                    Folder f = new Folder();
                    f.name       = folder;
                    f.Placemarks = GetPlacemarks(list);
                    kml.Document.Folders.Add(f);
                }
            }

            return(kml);
        }
        public IAGSGroupTables CreateAGS(ge_data_table dt_file, ge_table_map map, string[] agstables, string options)
        {
            IAGSGroupTables ags_tables = new AGS404GroupTables();

            if (agstables.Contains("PROJ"))
            {
                List <PROJ> proj = getPROJ();
                ags_tables.AddTable(proj);
            }

            if (agstables.Contains("ERES"))
            {
                foreach (table_map tm in map.table_maps.Where(m => m.destination == "ERES"))
                {
                    if (tm != null)
                    {
                        List <ERES> list = ConvertDataTable <ERES>(dt_file.dt, tm);
                        ags_tables.AddTable(list);
                        if (agstables.Contains("ABBR"))
                        {
                            List <ABBR> abbr = getABBR(list);
                            ags_tables.AddTable(abbr);
                        }
                        if (agstables.Contains("UNIT"))
                        {
                            List <UNIT> unit = getUNIT(list);
                            ags_tables.AddTable(unit);
                        }
                    }
                }
            }

            if (agstables.Contains("TRAN"))
            {
                List <TRAN> tran = getTRAN();
                ags_tables.AddTable(tran);
            }

            if (agstables.Contains("POINT"))
            {
                foreach (table_map tm in map.table_maps.Where(m => m.destination == "LOCA"))
                {
                    List <POINT> list     = ConvertDataTable <POINT>(dt_file.dt, tm);
                    string[]     distinct = list.Select(m => m.PointID).Distinct().ToArray();
                    List <POINT> unique   = getFirsts(list, distinct);
                    ags_tables.AddTable(unique);
                    if (agstables.Contains("ABBR"))
                    {
                        List <ABBR> abbr = getABBR(list);
                        ags_tables.AddTable(abbr);
                    }
                }
            }

            if (agstables.Contains("SAMP"))
            {
                foreach (table_map tm in map.table_maps.Where(m => m.destination == "SAMP"))
                {
                    List <SAMP> list     = ConvertDataTable <SAMP>(dt_file.dt, tm);
                    string[]    distinct = list.Select(m => m.SAMP_ID).Distinct().ToArray();
                    List <SAMP> unique   = getFirsts(list, distinct);
                    ags_tables.AddTable(unique);
                }
            }

            if (agstables.Contains("MOND"))
            {
                foreach (table_map tm in map.table_maps.Where(m => m.destination == "MOND"))
                {
                    if (tm != null)
                    {
                        List <MOND> list = ConvertDataTable <MOND>(dt_file.dt, tm);
                        ags_tables.AddTable(list);
                        if (agstables.Contains("ABBR"))
                        {
                            List <ABBR> abbr = getABBR(list);
                            ags_tables.AddTable(abbr);
                        }
                        if (agstables.Contains("UNIT"))
                        {
                            List <UNIT> unit = getUNIT(list);
                            ags_tables.AddTable(unit);
                        }
                    }
                }
            }

            if (agstables.Contains("MONG"))
            {
                foreach (table_map tm in map.table_maps.Where(m => m.destination == "MONG"))
                {
                    if (tm != null)
                    {
                        List <MONG> list = ConvertDataTable <MONG>(dt_file.dt, tm);
                        ags_tables.AddTable(list);
                        if (agstables.Contains("ABBR"))
                        {
                            List <ABBR> abbr = getABBR(list);
                            ags_tables.AddTable(abbr);
                        }
                    }
                }
            }

            if (agstables.Contains("ABBR"))
            {
                foreach (table_map tm in map.table_maps.Where(m => m.destination == "ABBR"))
                {
                    if (tm != null)
                    {
                        List <ABBR> list = ConvertDataTable <ABBR>(dt_file.dt, tm);
                        ags_tables.AddTable(list);
                    }
                }
            }

            if (agstables.Contains("UNIT"))
            {
                foreach (table_map tm in map.table_maps.Where(m => m.destination == "UNIT"))
                {
                    if (tm != null)
                    {
                        List <UNIT> list = ConvertDataTable <UNIT>(dt_file.dt, tm);
                        ags_tables.AddTable(list);
                    }
                }
            }

            if (agstables.Contains("TYPE"))
            {
                foreach (table_map tm in map.table_maps.Where(m => m.destination == "TYPE"))
                {
                    if (tm != null)
                    {
                        List <TYPE> list = ConvertDataTable <TYPE>(dt_file.dt, tm);
                        ags_tables.AddTable(list);
                    }
                }
            }

            if (agstables.Contains("DICT"))
            {
                foreach (table_map tm in map.table_maps.Where(m => m.destination == "DICT"))
                {
                    if (tm != null)
                    {
                        List <DICT> list = ConvertDataTable <DICT>(dt_file.dt, tm);
                        ags_tables.AddTable(list);
                    }
                }
            }

            return(ags_tables);
        }
        public async Task <IActionResult> ProcessFileToAGS(Guid Id,
                                                           Guid?templateId,
                                                           string table,
                                                           string sheet,
                                                           Guid?tablemapId,
                                                           string[] agstables,
                                                           Guid?agslibraryId,
                                                           string options = "",
                                                           string format  = "view",
                                                           Boolean save   = false)
        {
            Boolean save_file = true;
            Boolean save_ags  = save;

            if (options == null)
            {
                options = "";
            }

            if (options.Contains("background"))
            {
                if (save == true && !options.Contains("save_ags"))
                {
                    options += ",save_ags";
                }

                return(await ProcessFileBackground(Id,
                                                   templateId,
                                                   table,
                                                   sheet,
                                                   tablemapId,
                                                   agstables,
                                                   options));
            }

            if (options.Contains("read_file_only"))
            {
                save_file = false;
            }

            var calc_resp = await ReadFile(Id, templateId.Value, table, sheet, "", save_file);

            var okResult = calc_resp as OkObjectResult;

            if (okResult == null)
            {
                var vwResult = calc_resp as ViewResult;

                if (vwResult == null)
                {
                    return(Json(calc_resp));
                }

                return(calc_resp);
                // return View ("OperationRequest",calc_resp);
            }

            ge_data_table _dt_file = okResult.Value as ge_data_table;

            if (options.Contains("view_file"))
            {
                if (format == "view")
                {
                    return(View("ReadData", _dt_file));
                }

                if (format == "json")
                {
                    return(Json(_dt_file));
                }

                return(Ok(_dt_file));
            }

            if (save_file == false)
            {
                ITableFileAGSService _tableFileAGSService = new TableFileAGSService();
                ge_table_map         _tm = await _dataTableFileService.GetFileAsClass <ge_table_map>(tablemapId.Value);

                if (_tm == null)
                {
                    return(Json($"table map for field not found for id={tablemapId.Value}"));
                }

                IAGSGroupTables _ags_tables = _tableFileAGSService.CreateAGS(_dt_file, _tm, agstables, options);

                if (agslibraryId != null)
                {
                    await _dataAGSService.SetLibraryAGSData(agslibraryId.Value, null);
                }

                await _dataAGSService.AddLibraryAGSData(_ags_tables, null);

                if (save_ags == true)
                {
                    var _data = await _dataTableFileService.GetDataByIdWithAll(Id);

                    var _tablemap = await _dataTableFileService.GetDataByIdWithAll(tablemapId.Value);

                    var _user = await GetUserAsync();

                    await _dataAGSService.CreateData(_data.projectId,
                                                     _user.Id,
                                                     _ags_tables,
                                                     _data.FileNameNoExtention() + ".ags",
                                                     $"AGS Conversion from data table {_data.filename} using table map {_tablemap.filename}",
                                                     "ags");
                }

                if (format == "view")
                {
                    return(View(_ags_tables));
                }

                if (format == "json")
                {
                    return(Json(_ags_tables));
                }

                return(Ok(_ags_tables));
                // return await View  (mond, format);
            }

            return(await CreateAGS(Id, tablemapId.Value, agstables, options, format, save_ags));
        }