private static MunicipalityData Parse(StreamReader sr) { double west = 0; double east = 0; double north = 0; double south = 0; int countX = 0; int countY = 0; List <int> ids = new List <int>(); Dictionary <int, string> idToName = new Dictionary <int, string>(); var found = new Dictionary <int, bool>(); string line = null; string[] cells = null; bool skipLineRead = false; while (true) { if (!skipLineRead) { line = sr.ReadLine(); } else { skipLineRead = false; } if (line == null) { break; } cells = line.Split(','); var parameter = GridDataIO.CheckParameter(cells[0], cells[1], Parameters, out bool hasData); if (parameter == null) { #if UNITY_EDITOR Debug.LogWarning("Municipal budget file has unrecognized header parameter " + cells[0] + ". Should it go to the metadata?"); #endif continue; } #if UNITY_EDITOR if (found.TryGetValue((int)parameter.id, out bool f) && f) { Debug.LogWarning("Municipal budget file has duplicate metadata entry: " + parameter.label); } #endif found[(int)parameter.id] = true; switch (parameter.id) { case GridDataIO.ParamId.Metadata: if (hasData) { PatchDataIO.ReadCsvMetadata(sr, CsvTokens, ref line); skipLineRead = line != null; } break; case GridDataIO.ParamId.Categories: if (hasData) { idToName = ReadIdToName(sr, CsvTokens, ref line); skipLineRead = line != null; } break; case GridDataIO.ParamId.West: west = double.Parse(cells[1]); if (west < GeoCalculator.MinLongitude) { Debug.LogWarning("Municipal budget file has west below " + GeoCalculator.MinLongitude + ": " + west); } break; case GridDataIO.ParamId.North: north = double.Parse(cells[1]); if (north > GeoCalculator.MaxLatitude) { Debug.LogWarning("Municipal budget file has north above " + GeoCalculator.MaxLatitude + ": " + north); } break; case GridDataIO.ParamId.East: east = double.Parse(cells[1]); if (east > GeoCalculator.MaxLongitude) { Debug.LogWarning("Municipal budget file has east above " + GeoCalculator.MaxLongitude + ": " + east); } break; case GridDataIO.ParamId.South: south = double.Parse(cells[1]); if (south < GeoCalculator.MinLatitude) { Debug.LogWarning("Municipal budget file has south below " + GeoCalculator.MinLatitude + ": " + south); } break; case GridDataIO.ParamId.CountX: countX = int.Parse(cells[1]); break; case GridDataIO.ParamId.CountY: countY = int.Parse(cells[1]); break; case GridDataIO.ParamId.Values: ids = ReadValues(sr); break; default: #if UNITY_EDITOR Debug.Log("Municipal budget file will ignore row: " + line); #endif skipLineRead = false; break; } } #if UNITY_EDITOR foreach (var p in Parameters) { if (p.isRequired && !(found.TryGetValue((int)p.id, out bool f) && f)) { Debug.LogError("Didn't find '" + p.label + "' property in municipal budget file"); } } #endif int totalCount = countX * countY; if (ids.Count > totalCount) { Debug.Log("Municipal budget file has too many values. Expected: " + totalCount + ". Read: " + ids.Count); ids.RemoveRange(totalCount, ids.Count - totalCount); } else if (ids.Count < totalCount) { Debug.Log("Municipal budget file has insufficient values. Expected: " + totalCount + ". Read: " + ids.Count); ids.AddRange(System.Linq.Enumerable.Repeat(-1, totalCount - ids.Count)); } return(new MunicipalityData(ids.ToArray(), idToName, north, east, south, west, countX, countY)); }
private static void ParseCsv(ParseTaskData data) { GridDataIO.Parameter parameter = null; bool[] found = new bool[GridDataIO.Parameters.Length]; string line = null; string[] cells = null; bool skipLineRead = false; GridData grid = new GridData(); MultiGridData multigrid = null; while (true) { if (!skipLineRead) { line = data.sr.ReadLine(); } else { skipLineRead = false; } if (line == null) { break; } cells = line.Split(','); parameter = GridDataIO.CheckParameter(cells[0], cells[1], GridDataIO.Parameters, out bool hasData); if (parameter == null) { #if UNITY_EDITOR Debug.LogWarning("File " + data.filename + " has unrecognized header parameter " + cells[0] + ". Should it go to the metadata?"); #endif if (grid.metadata != null) { grid.metadata.Add(cells[0], cells[1]); } continue; } #if UNITY_EDITOR if (found[(int)parameter.id]) { Debug.LogWarning("File " + data.filename + " has duplicate metadata entry: " + parameter.label); } #endif found[(int)parameter.id] = true; switch (parameter.id) { case GridDataIO.ParamId.Metadata: if (hasData) { grid.metadata = PatchDataIO.ReadCsvMetadata(data.sr, GridDataIO.CsvTokens, ref line); skipLineRead = line != null; } break; //case GridDataIO.ParamId.NameToValue: // if (hasData) // { // nameToValues = GridDataIO.ReadCsvNameToValues(sr, CsvTokens, ref line); // skipLineRead = line != null; // } // break; case GridDataIO.ParamId.Categories: if (hasData) { grid.categories = GridDataIO.ReadCsvCategories(data.sr, data.filename, GridDataIO.CsvTokens, ref line); skipLineRead = line != null; } break; case GridDataIO.ParamId.Coloring: case GridDataIO.ParamId.Colouring: try { grid.coloring = (GridData.Coloring)Enum.Parse(typeof(GridData.Coloring), cells[1], true); } catch (Exception) { grid.coloring = GridData.Coloring.Single; } break; case GridDataIO.ParamId.West: grid.west = double.Parse(cells[1], CultureInfo.InvariantCulture); break; case GridDataIO.ParamId.North: grid.north = double.Parse(cells[1], CultureInfo.InvariantCulture); if (grid.north > GeoCalculator.MaxLatitude) { Debug.LogWarning("File " + data.filename + " has north above " + GeoCalculator.MaxLatitude + ": " + grid.north); } break; case GridDataIO.ParamId.East: grid.east = double.Parse(cells[1], CultureInfo.InvariantCulture); break; case GridDataIO.ParamId.South: grid.south = double.Parse(cells[1], CultureInfo.InvariantCulture); if (grid.south < GeoCalculator.MinLatitude) { Debug.LogWarning("File " + data.filename + " has south below " + GeoCalculator.MinLatitude + ": " + grid.south); } break; case GridDataIO.ParamId.CountX: grid.countX = int.Parse(cells[1]); break; case GridDataIO.ParamId.CountY: grid.countY = int.Parse(cells[1]); break; case GridDataIO.ParamId.Units: grid.units = cells[1]; break; case GridDataIO.ParamId.Values: multigrid = new MultiGridData(grid); ReadValues(data.sr, multigrid, data.filename); break; default: #if UNITY_EDITOR Debug.Log("File " + data.filename + " will ignore row: " + line); #endif skipLineRead = false; break; } } #if UNITY_EDITOR foreach (var p in GridDataIO.Parameters) { if (p.isRequired && !found[(int)p.id]) { Debug.LogError("Didn't find " + p.label + " in " + data.filename); } } #endif data.patch = multigrid; }