Beispiel #1
0
    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);
                }
            }
        }
    }
Beispiel #2
0
    public static void ParseBinValues(BinaryReader br, PointData pointData)
    {
        // Allocate point data memory
        pointData.InitPointData();

        byte[] bytes = null;

        // Read values
        PatchDataIO.ReadArray(br, ref bytes, pointData.lons, 8);
        PatchDataIO.ReadArray(br, ref bytes, pointData.lats, 8);
        PatchDataIO.ReadArray(br, ref bytes, pointData.values, 4);

        // Read distribution values
        var count = br.ReadByte();

        if (count > 0)
        {
            int[] distributionValues = new int[count];
            PatchDataIO.ReadArray(br, ref bytes, distributionValues, 4);

            // Read max distribution value
            int maxDistributionValue = br.ReadInt32();
            pointData.SetDistribution(distributionValues, maxDistributionValue);
        }
    }
    public static void ParseBinValues(BinaryReader br, GridData grid)
    {
        // Allocate grid memory
        grid.InitGridValues(false);

        byte[] bytes = null;

        // Read values
        PatchDataIO.ReadArray(br, ref bytes, grid.values, 4);

        // Read if it has values mask
        var withMask = br.ReadBoolean();

        if (withMask)
        {
            grid.CreateMaskBuffer();

            // Read values mask
            PatchDataIO.ReadArray(br, ref bytes, grid.valuesMask);
        }

        // Read distribution values
        var count = br.ReadByte();

        if (count > 0)
        {
            int[] distributionValues = new int[count];
            PatchDataIO.ReadArray(br, ref bytes, distributionValues, 4);

            // Read max distribution value
            int maxDistributionValue = br.ReadInt32();
            grid.SetDistribution(distributionValues, maxDistributionValue);
        }
    }
Beispiel #4
0
    private static void WriteBinHeader(BinaryWriter bw, GridData grid)
    {
        PatchDataIO.WriteBinVersion(bw);
        PatchDataIO.WriteBinBoundsHeader(bw, grid);

        // Write Categories count
        bw.Write(grid.categories == null ? 0 : grid.categories.Length);
    }
Beispiel #5
0
    public static MultiGridData ParseBinHeader(BinaryReader br, string filename, MultiGridData multigrid)
    {
        // Read header
        PatchDataIO.SkipBinVersion(br);
        PatchDataIO.ReadBinBoundsHeader(br, filename, multigrid);

        return(multigrid);
    }
Beispiel #6
0
    private static void WriteBinHeader(BinaryWriter bw, PointData pointData)
    {
        PatchDataIO.WriteBinVersion(bw);
        PatchDataIO.WriteBinBoundsHeader(bw, pointData);

        // Write Categories count
        bw.Write(pointData.categories == null ? 0 : pointData.categories.Length);
    }
Beispiel #7
0
    private static GraphData ParseBinHeader(BinaryReader br, GraphData graph)
    {
        // Read header
        PatchDataIO.ReadBinBoundsHeader(br, graph);

        // Read cell sizes
        graph.cellSizeX = br.ReadDouble();
        graph.cellSizeY = br.ReadDouble();

        return(graph);
    }
Beispiel #8
0
    private static GraphData ParseBinHeader(BinaryReader br, string filename, GraphData graph)
    {
        // Read header
        PatchDataIO.SkipBinVersion(br);
        PatchDataIO.ReadBinBoundsHeader(br, filename, graph);

        // Read cell sizes
        graph.cellSizeX = br.ReadDouble();
        graph.cellSizeY = br.ReadDouble();

        return(graph);
    }
Beispiel #9
0
    public static void WriteBinProperties(BinaryWriter bw, PointData pointData)
    {
        bw.Write(pointData.minValue);
        bw.Write(pointData.maxValue);
        bw.Write(pointData.count);
        bw.Write(pointData.units);
        bw.Write((byte)pointData.coloring);

        // Write Metadata (if available)
        PatchDataIO.WriteBinMetadata(bw, pointData.metadata);

        // Write Categories (if available)
        WriteBinCategories(bw, pointData.categories, pointData.coloring == PointData.Coloring.Custom);
    }
Beispiel #10
0
    private static GridData ParseBin(BinaryReader br, GridData grid)
    {
        // Read header
        ParseBinHeader(br, grid);

        grid.minValue = br.ReadSingle();
        grid.maxValue = br.ReadSingle();
        grid.countX   = br.ReadInt32();
        grid.countY   = br.ReadInt32();
        grid.units    = br.ReadString();
        grid.coloring = (GridData.Coloring)br.ReadByte();

        // Read Metadata (if available)
        grid.metadata = PatchDataIO.ReadBinMetadata(br);

        // Read Categories (if available)
        if (grid.categories != null)
        {
            ReadBinCategories(br, grid);
            if (grid.categories[0].name.EqualsIgnoreCase("None"))
            {
                grid.categoryMask &= ~1u;
            }
        }

        // Allocate grid memory
        grid.InitGridValues();

        byte[] bytes = null;

        // Read values
        bytes = ReadArray(br, bytes, grid.values, 4);

        // Read value masks
        bytes = ReadArray(br, bytes, grid.valuesMask);

        // Read distribution values
        var count = br.ReadByte();

        int[] distributionValues = new int[count];
        bytes = ReadArray(br, bytes, distributionValues, 4);

        // Read max distribution value
        int maxDistributionValue = br.ReadInt32();

        grid.SetDistribution(distributionValues, maxDistributionValue);

        return(grid);
    }
Beispiel #11
0
    public static void WriteBinProperties(BinaryWriter bw, GridData grid)
    {
        bw.Write(grid.minValue);
        bw.Write(grid.maxValue);
        bw.Write(grid.countX);
        bw.Write(grid.countY);
        bw.Write(grid.units);
        bw.Write((byte)grid.coloring);

        // Write Metadata (if available)
        PatchDataIO.WriteBinMetadata(bw, grid.metadata);

        // Write Categories (if available)
        WriteBinCategories(bw, grid.categories, grid.coloring == GridData.Coloring.Custom);
    }
Beispiel #12
0
    private static GridData ParseBinHeader(BinaryReader br, GridData grid)
    {
        // Read header
        PatchDataIO.ReadBinBoundsHeader(br, grid);

        // Read categories count
        int count = br.ReadInt32();

        if (count > 0)
        {
            grid.categories = new Category[count];
        }

        return(grid);
    }
Beispiel #13
0
    public static PointData ParseBinHeader(BinaryReader br, string filename, PointData pointData)
    {
        // Read header
        PatchDataIO.SkipBinVersion(br);
        PatchDataIO.ReadBinBoundsHeader(br, filename, pointData);

        // Read categories count
        int count = br.ReadInt32();

        if (count > 0)
        {
            pointData.categories = new IntCategory[count];
        }

        return(pointData);
    }
Beispiel #14
0
    public static GridData ParseBinHeader(BinaryReader br, string filename, GridData grid)
    {
        // Read header
        PatchDataIO.SkipBinVersion(br);
        PatchDataIO.ReadBinBoundsHeader(br, filename, grid);

        // Read categories count
        int count = br.ReadInt32();

        if (count > 0)
        {
            grid.categories = new IntCategory[count];
        }

        return(grid);
    }
Beispiel #15
0
    public static void SaveBin(this GridData grid, string filename)
    {
        using (var bw = new BinaryWriter(File.Open(filename, FileMode.Create)))
        {
            PatchDataIO.WriteBinBoundsHeader(bw, grid);

            // Write Categories count
            bw.Write(grid.categories == null ? 0 : grid.categories.Length);

            bw.Write(grid.minValue);
            bw.Write(grid.maxValue);
            bw.Write(grid.countX);
            bw.Write(grid.countY);
            bw.Write(grid.units);
            bw.Write((byte)grid.coloring);

            // Write Metadata (if available)
            PatchDataIO.WriteBinMetadata(bw, grid.metadata);

            // Write Categories (if available)
            WriteBinCategories(bw, grid.categories);

            // Write values
            byte[] byteArray = new byte[grid.values.Length * 4];
            Buffer.BlockCopy(grid.values, 0, byteArray, 0, byteArray.Length);
            bw.Write(byteArray);

            // Write mask
            byteArray = new byte[grid.valuesMask.Length];
            Buffer.BlockCopy(grid.valuesMask, 0, byteArray, 0, byteArray.Length);
            bw.Write(byteArray);

            // Write distribution values
            var distribution = grid.DistributionValues;
            bw.Write((byte)distribution.Length);
            byteArray = new byte[distribution.Length * 4];
            Buffer.BlockCopy(distribution, 0, byteArray, 0, byteArray.Length);
            bw.Write(byteArray);

            // Write max distribution value
            bw.Write(grid.MaxDistributionValue);
        }
    }
Beispiel #16
0
    public static void ParseBinProperties(BinaryReader br, MultiGridData multigrid)
    {
        // Read Metadata (if available)
        multigrid.metadata = PatchDataIO.ReadBinMetadata(br);

        // Read coloring
        multigrid.coloring = (GridData.Coloring)br.ReadByte();

        // Read categories (without values)
        int gridCount = br.ReadInt32();

        if (gridCount > 0)
        {
            multigrid.categories = new GridCategory[gridCount];
            for (int i = 0; i < gridCount; i++)
            {
                multigrid.categories[i] = new GridCategory(br.ReadString(), null, Color.white);
            }
        }
    }
Beispiel #17
0
    public static void SaveBin(this GraphData graph, string filename)
    {
        using (var bw = new BinaryWriter(File.Open(filename, FileMode.Create)))
        {
            PatchDataIO.WriteBinVersion(bw);
            PatchDataIO.WriteBinBoundsHeader(bw, graph);

            bw.Write(graph.cellSizeX);
            bw.Write(graph.cellSizeY);

            int count = graph.nodes.Count;
            bw.Write(count);
            for (int n = 0; n < count; n++)
            {
                GraphNode node = graph.nodes[n];
                bw.Write(node.longitude);
                bw.Write(node.latitude);
                bw.Write(node.classifications);
                bw.Write(node.index);
            }

            for (int n = 0; n < count; n++)
            {
                GraphNode node = graph.nodes[n];
                for (int l = 0; l < node.links.Count; l++)
                {
                    var link      = node.links[l];
                    int index     = node.index;
                    int linkIndex = link.index;
                    if (linkIndex > index)
                    {
                        bw.Write(index);
                        bw.Write(linkIndex);
                        bw.Write(node.linkDistances[l]);
                        bw.Write(node.linkClassifications[l]);
                    }
                }
            }
        }
    }
Beispiel #18
0
    public static void ParseBinProperties(BinaryReader br, string filename, PointData pointData)
    {
        pointData.minValue = br.ReadSingle();
        pointData.maxValue = br.ReadSingle();
        pointData.count    = br.ReadInt32();
        pointData.units    = br.ReadString();
        pointData.coloring = (PointData.Coloring)br.ReadByte();

        // Read Metadata (if available)
        pointData.metadata = PatchDataIO.ReadBinMetadata(br);

        // Read Categories (if available)
        if (pointData.categories != null)
        {
            ReadBinCategories(br, pointData, pointData.coloring == PointData.Coloring.Custom);

            int categoriesCount = pointData.categories.Length;
            if (categoriesCount > CategoryFilter.MaxCategories)
            {
                Debug.LogWarning(filename + " has more than " + CategoryFilter.MaxCategories + " categories.");
            }
        }
    }
Beispiel #19
0
    private static void ParseCsv(ParseTaskData data)
    {
        Parameter parameter = null;

        bool[] found = new bool[Parameters.Length];

        string line = null;

        string[] cells        = null;
        bool     skipLineRead = false;

        PointData pointData = new PointData();

        List <float> nameToValues = null;

        Color[] colors = null;

        while (true)
        {
            if (!skipLineRead)
            {
                line = data.sr.ReadLine();
            }
            else
            {
                skipLineRead = false;
            }

            if (line == null)
            {
                break;
            }

            cells     = line.Split(',');
            parameter = CheckParameter(cells[0], cells[1], 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 (pointData.metadata != null)
                {
                    pointData.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 ParamId.Metadata:
                if (hasData)
                {
                    pointData.metadata = PatchDataIO.ReadCsvMetadata(data.sr, CsvTokens, ref line);
                    skipLineRead       = line != null;
                }
                break;

            case ParamId.NameToValue:
                if (hasData)
                {
                    nameToValues = ReadCsvNameToValues(data.sr, CsvTokens, ref line);
                    skipLineRead = line != null;
                }
                break;

            case ParamId.Categories:
                if (hasData)
                {
                    pointData.categories = ReadCsvCategories(data.sr, data.filename, CsvTokens, ref line);
                    AssignCustomColors(pointData, ref colors);
                    skipLineRead = line != null;
                }
                break;

            case ParamId.Coloring:
            case ParamId.Colouring:
                try
                {
                    pointData.coloring = (PointData.Coloring)Enum.Parse(typeof(PointData.Coloring), cells[1], true);
                    AssignCustomColors(pointData, ref colors);
                }
                catch (Exception)
                {
                    pointData.coloring = PointData.Coloring.Single;
                }
                break;

            case ParamId.Colors:
            case ParamId.Colours:
                if (hasData)
                {
                    colors = ReadCsvColors(data.sr, CsvTokens, ref line);
                    AssignCustomColors(pointData, ref colors);
                    skipLineRead = line != null;
                }
                break;

            case ParamId.West:
                pointData.west = double.Parse(cells[1], CultureInfo.InvariantCulture);
                //if (pointData.west < GeoCalculator.MinLongitude)
                //    Debug.LogWarning("File " + data.filename + " has west below " + GeoCalculator.MinLongitude + ": " + pointData.west);
                break;

            case ParamId.North:
                pointData.north = double.Parse(cells[1], CultureInfo.InvariantCulture);
                if (pointData.north > GeoCalculator.MaxLatitude)
                {
                    Debug.LogWarning("File " + data.filename + " has north above " + GeoCalculator.MaxLatitude + ": " + pointData.north);
                }
                break;

            case ParamId.East:
                pointData.east = double.Parse(cells[1], CultureInfo.InvariantCulture);
                //if (pointData.east > GeoCalculator.MaxLongitude)
                //    Debug.LogWarning("File " + data.filename + " has east above " + GeoCalculator.MaxLongitude + ": " + pointData.east);
                break;

            case ParamId.South:
                pointData.south = double.Parse(cells[1], CultureInfo.InvariantCulture);
                if (pointData.south < GeoCalculator.MinLatitude)
                {
                    Debug.LogWarning("File " + data.filename + " has south below " + GeoCalculator.MinLatitude + ": " + pointData.south);
                }
                break;

            case ParamId.Count:
                pointData.count = int.Parse(cells[1]);
                break;

            case ParamId.Units:
                pointData.units = cells[1];
                break;

            case ParamId.Values:
                ReadValues(data.sr, data.filename, pointData, nameToValues);
                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 Parameters)
        {
            if (p.isRequired && !found[(int)p.id])
            {
                Debug.LogError("Didn't find '" + p.label + "' property in " + data.filename);
            }
        }
#endif

        if (pointData.IsCategorized)
        {
            pointData.RemapCategories(data.filename, false);
        }
        else
        {
            pointData.UpdateDistribution();
        }

        data.patch = pointData;
    }
Beispiel #20
0
    private static GridData ParseCsv(StreamReader sr, string filename)
    {
        Parameter parameter = null;

        bool[] found = new bool[Parameters.Length];

        string line = null;

        string[] cells        = null;
        bool     skipLineRead = false;

        GridData   grid         = new GridData();
        List <int> nameToValues = null;

        while (true)
        {
            if (!skipLineRead)
            {
                line = sr.ReadLine();
            }
            else
            {
                skipLineRead = false;
            }

            if (line == null)
            {
                break;
            }

            cells     = line.Split(',');
            parameter = CheckParameter(cells[0], cells[1]);

            if (parameter == null)
            {
                continue;
            }

            found[(int)parameter.id] = true;

            switch (parameter.id)
            {
            case ParamId.Metadata:
                if (parameter.hasData)
                {
                    grid.metadata = PatchDataIO.ReadCsvMetadata(sr, CsvTokens, ref line);
                    skipLineRead  = line != null;
                }
                break;

            case ParamId.NameToValue:
                if (parameter.hasData)
                {
                    nameToValues = ReadCsvNameToValues(sr, CsvTokens, ref line);
                    skipLineRead = line != null;
                }
                break;

            case ParamId.Categories:
                if (parameter.hasData)
                {
                    grid.categories = ReadCsvCategories(sr, CsvTokens, ref line);
                    if (grid.categories != null && grid.categories[0].name.EqualsIgnoreCase("None"))
                    {
                        grid.categoryMask &= ~1u;
                    }
                    skipLineRead = line != null;
                }
                break;

            case ParamId.Coloring:
                try
                {
                    grid.coloring = (GridData.Coloring)Enum.Parse(typeof(GridData.Coloring), cells[1], true);
                }
                catch (Exception)
                {
                    grid.coloring = GridData.Coloring.Single;
                }
                break;

            case ParamId.West:
                grid.west = double.Parse(cells[1]);
                break;

            case ParamId.North:
                grid.north = double.Parse(cells[1]);
                break;

            case ParamId.East:
                grid.east = double.Parse(cells[1]);
                break;

            case ParamId.South:
                grid.south = double.Parse(cells[1]);
                break;

            case ParamId.CountX:
                grid.countX = int.Parse(cells[1]);
                break;

            case ParamId.CountY:
                grid.countY = int.Parse(cells[1]);
                break;

            case ParamId.Units:
                grid.units = cells[1];
                break;

            case ParamId.Values:
                ReadValues(sr, grid, nameToValues, filename);
                break;

            default:
                skipLineRead = false;
                break;
            }
        }

#if UNITY_EDITOR
        foreach (var p in Parameters)
        {
            if (p.isRequired && !found[(int)p.id])
            {
                Debug.LogError("Didn't find " + p.label + " in " + filename);
                return(null);
            }
        }
#endif

        grid.UpdateDistribution(false);

        return(grid);
    }
Beispiel #21
0
    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));
    }
Beispiel #22
0
    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;
    }
Beispiel #23
0
 public static IEnumerator LoadCsv(string filename, PatchDataLoadedCallback <MultiGridData> callback)
 {
     yield return(FileRequest.GetText(filename, (sr) => PatchDataIO.ParseAsync(sr, filename, ParseCsv, callback)));
 }
Beispiel #24
0
    private IEnumerator InitLayers()
    {
        if (dataLayers == null)
        {
            messageController.Show(false);
            yield break;
        }

#if !UNITY_WEBGL
        if (!Directory.Exists(Paths.Sites))
        {
            Debug.LogError("Data path '" + Paths.Sites + "' doesn't exist");
            messageController.Show(false);
            yield break;
        }
#endif

        // Show Message
        messageController.Show(true);
        messageController.SetProgress(0);

#if UNITY_WEBGL
        messageController.SetMessage("Downloading database ...");

        yield return(PatchDataIO.StartReadingHeaders());

        messageController.SetProgress(0.5f);
        yield return(null);

        allPatchFiles.Clear();
        yield return(FileRequest.GetText(Paths.DataWebDBPatches, (tr) =>
        {
            string line;
            while ((line = tr.ReadLine()) != null)
            {
                allPatchFiles.Add(line);
            }
        }));
#else
        // Find valid data directories
        InitValidDataDirs();
#endif

        messageController.SetMessage("Initializing database ...");
        messageController.SetProgress(0);
        yield return(null);

        // Rebuild the UI
        int groupCount = 0;
        int layerCount = 0;
        dataLayers.Show(false);
        foreach (var group in groups)
        {
            var groupController = dataLayers.AddLayerGroup(group.name);
            foreach (var layer in group.layers)
            {
                dataLayers.AddLayer(layer, groupController);
                layer.BuildFileList();
                layerCount++;
            }
            groupCount++;
        }
        dataLayers.Show(true);

        yield return(null);

        // Create the patches/sites for each layer
        float index         = 0;
        int   counter       = 0;
        float invLayerCount = 1f / layerCount;
        var   bounds        = map.MapCoordBounds;
        foreach (var group in groups)
        {
            foreach (var layer in group.layers)
            {
                if (layer.PatchCount > 0)
                {
                    int   patchCount    = 0;
                    float invPatchCount = 1f / layer.PatchCount;
                    var   patches       = layer.CreatePatches();
                    while (patches.MoveNext())
                    {
                        patchCount++;
                        messageController.SetProgress((index + (patchCount * invPatchCount)) * invLayerCount);
                        while (patches.RunThruChildren())
                        {
                            counter = 0;
                            yield return(null);
                        }
                        if (++counter % 50 == 0)
                        {
                            yield return(null);
                        }
                    }
                }

#if SAFETY_CHECK
                if (layers.ContainsKey(layer.name))
                {
                    Debug.LogError(layer.name + " layer already exists. Looks like a duplicate line in layers.csv");
                }
                else
#endif
                layers.Add(layer.name, layer);

                index++;
            }
        }

        CreateSitesList();

        // Update DataLayers UI (activate those layers that have sites/patches within view bounds)
        dataLayers.UpdateLayers();

        // Hide Message
        messageController.Show(false);

        // Clean up the cached files & directories
#if UNITY_WEBGL
        PatchDataIO.FinishReadingHeaders();
        allPatchFiles.Clear();
        allPatchFiles = null;
#else
        DataDirs = null;
#endif

        if (OnDataLoaded != null)
        {
            OnDataLoaded();
        }
    }
Beispiel #25
0
    public IEnumerator GetPatchFiles(List <string> paths, UnityAction <List <string> > callback)
    {
        var patchFiles = new List <string>();

#if UNITY_WEBGL
        string start = Name + "_";

        int count = paths.Count;
        for (int i = 0; i < count; i++)
        {
            string filepath = paths[i];
            string filename = Path.GetFileName(filepath);

            if (filename.StartsWith(start))
            {
                patchFiles.Add(Paths.Sites + filepath);
                i++;

                for (; i < count; i++)
                {
                    filepath = paths[i];
                    filename = Path.GetFileName(filepath);
                    if (!filename.StartsWith(start))
                    {
                        break;
                    }

                    patchFiles.Add(Paths.Sites + filepath);
                }
                break;
            }
        }
#else
        const int        MaxPatchesPerFrame = 100;
        HashSet <string> hs        = new HashSet <string>();
        string           binFilter = Name + "_*." + Patch.BIN_EXTENSION;
        string           csvFilter = Name + "_*." + Patch.CSV_EXTENSION;
        foreach (var dir in paths)
        {
            hs.Clear();

            var binFiles = Directory.GetFiles(dir, binFilter);
            int count    = binFiles.Length;
            for (int i = 0; i < count; i++)
            {
                var binFile = binFiles[i];
                if (PatchDataIO.CheckBinVersion(binFile))
                {
                    patchFiles.Add(binFile);
                    hs.Add(binFile);
                }
                if (++fileCounter % MaxPatchesPerFrame == 0)
                {
                    yield return(null);
                }
            }

            var csvFiles = Directory.GetFiles(dir, csvFilter);
            count = csvFiles.Length;
            for (int i = 0; i < count; i++)
            {
                var    csvFile = csvFiles[i];
                string binFile = Path.ChangeExtension(csvFile, Patch.BIN_EXTENSION);
                if (hs.Contains(binFile))
                {
                    // Check if CSV is newer than the BIN
                    if (File.GetLastWriteTime(csvFile) >= File.GetLastWriteTime(binFile))
                    {
                        patchFiles[patchFiles.IndexOf(binFile)] = csvFile;
                    }
                }
                else
                {
                    patchFiles.Add(csvFile);
                }
                if (++fileCounter % MaxPatchesPerFrame == 0)
                {
                    yield return(null);
                }
            }
        }
#endif

#if SAFETY_CHECK
        // Check if all patch filenames match exactly (without ignoring case)
        foreach (var patchFile in patchFiles)
        {
            if (!patchFile.Contains(Name))
            {
                Debug.LogWarning("Patch " + patchFile + " has wrong case, should be: " + Name);
            }
        }
#endif

        callback(patchFiles);

#if UNITY_WEBGL
        yield break;
#endif
    }
Beispiel #26
0
    private IEnumerator InitLayers()
    {
        if (dataLayers == null)
        {
            CloseProgressDialog();
            yield break;
        }

#if !UNITY_WEBGL
        if (!Directory.Exists(Paths.Sites))
        {
            Debug.LogError("Data path '" + Paths.Sites + "' doesn't exist. Trying to create it.");
            CloseProgressDialog();
            Directory.CreateDirectory(Paths.Sites);
            yield break;
        }
#endif


#if UNITY_WEBGL
        // Show progress message
        progressDialog.SetMessage(Translator.Get("Downloading data") + "...");
        progressDialog.SetProgress(0);

        yield return(PatchDataIO.StartReadingHeaders());

        progressDialog.SetProgress(0.5f);
        yield return(null);

        var paths = new List <string>();
        yield return(FileRequest.GetText(Paths.DataWebDBPatches, (tr) =>
        {
            string line;
            while ((line = tr.ReadLine()) != null)
            {
                paths.Add(line);
            }
        }));
#else
        // Find valid data directories
        var paths = GetDataDirectories();
#endif

        // Show progress message
        progressDialog.SetMessage(Translator.Get("Loading") + " ...");
        progressDialog.SetProgress(0);

        yield return(null);

        // Count the layers
        int layerCount = 0;
        foreach (var group in groups)
        {
            foreach (var layer in group.layers)
            {
                layerCount++;
            }
        }

        // Rebuild the UI
        dataLayers.Show(false);

        // Create the patches/sites for each layer
        float index         = 0;
        float time          = 0;
        float invLayerCount = 1f / layerCount;
        var   bounds        = map.MapCoordBounds;
        foreach (var group in groups)
        {
            var groupController = dataLayers.AddLayerGroup(group.name);
            foreach (var layer in group.layers)
            {
                dataLayers.AddLayer(layer, groupController);

                List <string> patchFiles = null;
                var           filesIt    = layer.GetPatchFiles(paths, (pf) => patchFiles = pf);
                if (filesIt.MoveNext())
                {
                    do
                    {
                        yield return(null);
                    } while (filesIt.MoveNext());
                    time = Time.realtimeSinceStartup + MaxProcessingTimePerFrame;
                }
#if UNITY_EDITOR
                if (findUnusedLayers && patchFiles.Count == 0)
                {
                    Debug.LogWarning("Layer " + layer.Name + " doesn't have any files.");
                }
#endif

                if (patchFiles.Count > 0)
                {
                    int   patchCount    = 0;
                    float invPatchCount = 1f / patchFiles.Count;
                    var   patches       = layer.CreatePatches(patchFiles);
                    while (patches.MoveNext())
                    {
                        patchCount++;
                        progressDialog.SetProgress((index + (patchCount * invPatchCount)) * invLayerCount);
                        if (patches.RunThruChildren())
                        {
                            do
                            {
                                if (Time.realtimeSinceStartup > time)
                                {
                                    yield return(null);

                                    time = Time.realtimeSinceStartup + MaxProcessingTimePerFrame;
                                }
                            }while (patches.RunThru());
                        }
                        else if (Time.realtimeSinceStartup > time)
                        {
                            yield return(null);

                            time = Time.realtimeSinceStartup + MaxProcessingTimePerFrame;
                        }
                    }
                }
                else if (Time.realtimeSinceStartup > time)
                {
                    yield return(null);

                    time = Time.realtimeSinceStartup + MaxProcessingTimePerFrame;
                }
                index++;
            }
        }

        PatchDataIO.StopParsingThread();

#if UNITY_EDITOR && !UNITY_WEBGL
        if (findUnusedPatches)
        {
            var extensions = new string[] { "*.csv", "*.bin" };
            var dirs       = GetDataDirectories();
            foreach (var dir in dirs)
            {
                foreach (var ext in extensions)
                {
                    var files = Directory.GetFiles(dir, ext);
                    foreach (var file in files)
                    {
                        if (Path.GetFileName(file).StartsWith("_"))
                        {
                            continue;
                        }
                        var layerName = Patch.GetFileNameLayer(file);
                        if (!dataLayers.HasLayer(layerName))
                        {
                            Debug.LogWarning("Patch is being ignored: " + file);
                        }
                    }
                }
            }
        }
#endif

        dataLayers.Show(true);
        yield return(null);

        // Update DataLayers UI (activate those layers that have sites/patches within view bounds)
        dataLayers.UpdateLayers();

        // Hide Message
        CloseProgressDialog();

        // Clean up
#if UNITY_WEBGL
        PatchDataIO.FinishReadingHeaders();
#endif

        OnDataLoaded?.Invoke();
    }