Example #1
0
    private void StartConverting(string assetPath)
    {
        if (GenerateImage == false && GenerateBinary == false && GenerateGeoJSON == false && GenerateHistogram == false)
        {
            Debug.Log("Nothing to do");
            return;
        }

        IsProcessing = true;

        try
        {
            using (StreamReader sr = new StreamReader(assetPath))
            {
                bool   isReadingHeader = false;
                string headerString    = "";

                string[] allLines         = File.ReadAllLines(assetPath);
                int      currentLineIndex = 0;

                for (int i = 0; i < HeaderMaxLines; i++)
                {
                    string line = allLines[currentLineIndex];
                    currentLineIndex++;

                    if (line.Length == 0)
                    {
                        continue;
                    }

                    if (line[0] == '!')
                    {
                        continue;
                    }

                    if (line[0] == '@')
                    {
                        if (isReadingHeader)
                        {
                            break;
                        }

                        isReadingHeader = true;
                    }

                    if (isReadingHeader)
                    {
                        headerString += line;
                        headerString += ',';
                    }
                }

                // 0       1        2           3   4           5   6   7   8           9        10     11     12        13        14  15         16
                // Header  DataType ColumnCount ?   NoDataValue ?   ?   ?   ColumnCount RowCount RowMin RowMax ColumnMin ColumnMax ?   ColumnSize RowSize

                string[] splittedHeader = headerString.Split(',');

                DataHeader header = new DataHeader();
                header.DataType    = splittedHeader[1];                                 //1
                header.NoDataValue = float.Parse(splittedHeader[4].Replace('.', ','));  //4
                header.ColumnCount = int.Parse(splittedHeader[8]);                      //8
                header.RowCount    = int.Parse(splittedHeader[9]);                      //9
                header.RowMin      = float.Parse(splittedHeader[10].Replace('.', ',')); //10
                header.RowMax      = float.Parse(splittedHeader[11].Replace('.', ',')); //11
                header.ColumnMin   = float.Parse(splittedHeader[12].Replace('.', ',')); //12
                header.ColumnMax   = float.Parse(splittedHeader[13].Replace('.', ',')); //13
                header.ColumnSize  = float.Parse(splittedHeader[15].Replace('.', ',')); //15
                header.RowSize     = float.Parse(splittedHeader[16].Replace('.', ',')); //16

                char[] separator = new char[] { ' ' };

                float[] floatArray     = new float[header.ColumnCount * header.RowCount];
                int     currentElement = 0;

                DateTime startTime = DateTime.UtcNow;
                DateTime time      = startTime;
                float    step      = 100000;

                float minValue = float.MaxValue;
                float maxValue = float.MinValue;

                while (currentElement < floatArray.Length && IsProcessing)
                {
                    string line = allLines[currentLineIndex];
                    currentLineIndex++;

                    string[] lineArray = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);

                    foreach (string str in lineArray)
                    {
                        float value = float.Parse(str.Replace('.', ','));

                        if (value != header.NoDataValue)
                        {
                            if (value < minValue)
                            {
                                minValue = value;
                            }

                            if (value > maxValue)
                            {
                                maxValue = value;
                            }
                        }

                        floatArray[currentElement] = value;
                        currentElement++;
                    }

                    if (currentElement % step == 0)
                    {
                        TimeSpan timeDiff = DateTime.UtcNow - time;
                        time = DateTime.UtcNow;

                        Debug.Log("Converting progress: " + currentElement + "/" + floatArray.Length);
                        Debug.Log("Converting speed: " + (step / timeDiff.TotalMinutes).ToString("########") + " entries per minute");
                        Debug.Log("End in " + (((floatArray.Length - currentElement) / step) * timeDiff.TotalSeconds).ToString("###.##") + " seconds");
                    }
                }

                Debug.Log("Converted: " + currentElement + "/" + floatArray.Length + " entries in " + (DateTime.UtcNow - startTime).TotalSeconds.ToString("####.#") + " seconds");
                Debug.Log("Min value: " + minValue);
                Debug.Log("Max value: " + maxValue);

                Debug.Log(" - - - - - - - - - - - - - - - - - - - - - - ");

                if (GenerateBinary)
                {
                    Debug.Log("Binary generating started");

                    DataHeader.Convert(header, assetPath.Split('.')[0] + ".header");

                    byte[] array = new byte[4 * header.ColumnCount * header.RowCount];
                    int    index = 0;

                    foreach (float value in floatArray)
                    {
                        byte[] bytes = BitConverter.GetBytes(value);

                        for (int k = 0; k < bytes.Length; k++)
                        {
                            array[index] = bytes[k];
                            index++;
                        }
                    }

                    using (FileStream fs = File.Create(assetPath.Split('.')[0] + ".bin", 2048, FileOptions.None))
                        using (BinaryWriter bw = new BinaryWriter(fs))
                        {
                            bw.Write(array);
                        }

                    Debug.Log("Binary generated");
                    NewAssetAvailable = true;
                }

                if (this.GenerateHistogram)
                {
                    Debug.Log("Histogram generating started");

                    int bucketSize = 100;

                    var histogramResult = HistogrammHelper.Bucketize(floatArray, bucketSize, header.NoDataValue);



                    switch (this.selectedHistogramLayer)
                    {
                    case Layers.Elevation:
                        layerDataAsset.elevationData       = histogramResult;
                        layerDataAsset.elevationbucketSize = bucketSize;
                        break;

                    case Layers.Porosity:
                        layerDataAsset.porosityData       = histogramResult;
                        layerDataAsset.porositybucketSize = bucketSize;
                        break;

                    case Layers.Thickness:
                        layerDataAsset.thicknessnData      = histogramResult;
                        layerDataAsset.thicknessBucketSize = bucketSize;
                        break;

                    case Layers.TOC:
                        layerDataAsset.tocData       = histogramResult;
                        layerDataAsset.tocBucketSize = bucketSize;
                        break;

                    case Layers.VShale:
                        layerDataAsset.vshaleData       = histogramResult;
                        layerDataAsset.vshaleBucketSize = bucketSize;
                        break;
                    }
                    Debug.Log($"{histogramResult} {bucketSize}");
                }

                if (GenerateImage)
                {
                    Debug.Log("Image generating started");

                    ImageWidth  = header.ColumnCount;
                    ImageHeight = header.RowCount;

                    Pixels = new Color[ImageWidth * ImageHeight];

                    for (int i = 0; i < floatArray.Length; i++)
                    {
                        if (UseCustomRange)
                        {
                            Pixels[i] = ConvertFloatToRGB(floatArray[i], CustomBottom, CustomTop, header.NoDataValue);
                        }
                        else
                        {
                            Pixels[i] = ConvertFloatToRGB(floatArray[i], minValue, maxValue, header.NoDataValue);
                        }
                    }

                    IsNewImageDataAvailable = true;
                }

                if (GenerateGeoJSON)
                {
                    Debug.Log("GeoJSON generating started");

                    int current       = 0;
                    int currentColumn = 0;
                    int currentRow    = 0;

                    GeoJSON gJSON = new GeoJSON();

                    foreach (float value in floatArray)
                    {
                        if (GeoJSONEntryCount != 0 && current >= GeoJSONEntryCount)
                        {
                            break;
                        }

                        currentColumn = (int)Mathf.Floor(((current / (float)header.ColumnCount) % 1) * header.ColumnCount);
                        currentRow    = (int)Mathf.Floor(current / (float)header.ColumnCount);
                        current++;

                        if (value != header.NoDataValue)
                        {
                            GeoPoint geogr = tr.TransformUTMToGeographic(new GeoPoint((header.ColumnMin + currentColumn * header.ColumnSize) * Foot, (header.RowMin + currentRow * header.RowSize) * Foot), enumProjection.UTM13N, enumEllipsoid.WGS84);
                            gJSON.AddPoint(geogr.Y, geogr.X, value);
                        }
                    }

                    string geoJSONString = gJSON.GetJSON();
                    File.WriteAllText(assetPath.Split('.')[0] + ".geojson", geoJSONString);

                    Debug.Log("GeoJSON generated");
                    NewAssetAvailable = true;
                }
            }
        }
        catch (IOException e)
        {
            Debug.Log("The file could not be read: " + e.Message);
        }
        catch (FormatException e)
        {
            Debug.Log("Parsing error: " + e.Message);
        }
        catch (Exception e)
        {
            Debug.Log("Unknown exception: " + e.Message);
        }
        finally
        {
            IsProcessing = false;
        }
    }