Exemplo n.º 1
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);
    }
Exemplo n.º 2
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;
    }
Exemplo n.º 3
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));
    }
Exemplo n.º 4
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;
    }