public static void SaveBin(this MultiGridData multigrid, string filename) { using (var bw = new BinaryWriter(File.Open(filename, FileMode.Create))) { PatchDataIO.WriteBinVersion(bw); PatchDataIO.WriteBinBoundsHeader(bw, multigrid); // Write Metadata (if available) PatchDataIO.WriteBinMetadata(bw, multigrid.metadata); bw.Write((byte)multigrid.coloring); int categoriesCount = multigrid.categories == null ? 0 : multigrid.categories.Length; bw.Write(categoriesCount); if (multigrid.categories != null) { // Write categories (without values) foreach (var c in multigrid.categories) { bw.Write(c.name); } // Write Grids foreach (var c in multigrid.categories) { GridDataIO.WriteBinProperties(bw, c.grid); GridDataIO.WriteBinValues(bw, c.grid); } } } }
private GridData QuickReadGridPatch(string filename) { var data = new GridData(); using (var br = new BinaryReader(File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) { GridDataIO.ParseBinHeader(br, filename, data); GridDataIO.ParseBinProperties(br, filename, data); } return(data); }
public static void ParseBinGrids(BinaryReader br, string filename, MultiGridData multigrid) { if (multigrid.categories != null) { int count = multigrid.categories.Length; for (int i = 0; i < count; i++) { var grid = new GridData(multigrid); GridDataIO.ParseBinProperties(br, filename, grid); GridDataIO.ParseBinValues(br, grid); grid.patch = multigrid.patch; multigrid.categories[i].grid = grid; } } }
public override bool SaveChanges() { if (activeLayer == null) { return(false); } bool updateBackend = false; Site activeSite = null; if (properties.name.HasChanged) { activeLayer.ChangeName(properties.name); updateBackend = true; } if (properties.group.HasChanged) { activeLayer.ChangeGroup(properties.group); updateBackend = true; } if (properties.color.HasChanged) { activeLayer.ChangeColor(properties.color); updateBackend = true; } if (updateBackend) { dataManagerPanel.UpdateActiveLayer(); if (!dataManagerPanel.UpdateBackend()) { return(false); } } #if !UNITY_WEBGL if (properties.site.HasChanged || properties.year.HasChanged || properties.units.HasChanged || properties.categories.HasChanged || properties.coloring.HasChanged || properties.metadata.HasChanged) { var originalSiteName = properties.site.OriginalValue; var site = dataManager.GetSite(originalSiteName); if (properties.site.HasChanged) { // Move layer to another site var newSite = dataManager.GetOrAddSite(properties.site); site.MoveLayerToSite(activeLayer, newSite); activeSite = site = newSite; } if (properties.year.HasChanged) { activeLayer.ChangeYear(properties.year.OriginalValue, properties.year, site); } if (properties.units.HasChanged || properties.categories.HasChanged || properties.coloring.HasChanged || properties.metadata.HasChanged) { var directories = dataManager.GetDataDirectories(); var binFiles = activeLayer.GetPatchFiles(directories, site.Name, Patch.BIN_EXTENSION); int count = binFiles.Count; for (int i = 0; i < count; ++i) { var file = binFiles[i]; var newFile = file + ".new"; try { if (File.Exists(newFile)) { File.Delete(newFile); } IntCategory[] categories = null; if (properties.categories.Value != null) { categories = properties.categories.Value.OfType <IntCategory>().ToArray(); } GridDataIO.UpdateBin(file, newFile, properties.units, properties.coloring, properties.metadata, categories); File.Delete(file); File.Move(newFile, file); } catch (Exception e) { Debug.LogException(e); } } } } #endif properties.Apply(); if (activeSite != null) { dataManagerPanel.RefreshSiteList(activeSite); } return(true); }
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; }