Categories
PHP

Конвертиране от DINI21 към TXT или XML

Преобразуване на файлове от дигитален нивелир DINI21 към XML или TXT файл

Базов код на PHP:

<?php
$submit_button = "Преобразуване";
$upload_dir = "./upload";
$output_file = "";

if (
    isset($_POST["submit"], $_POST["format"]) &&
    $_POST["submit"] === $submit_button
) {
    if (is_uploaded_file($_FILES["file"]["tmp_name"])) {
        $old_filename = htmlentities(
            $_FILES["file"]["name"],
            ENT_QUOTES,
            "UTF-8"
        );
        $new_filename = sprintf(
            "%s_%s.txt",
            md5($_FILES["file"]["name"]),
            time()
        );
        if (
            move_uploaded_file(
                $_FILES["file"]["tmp_name"],
                $upload_dir . DIRECTORY_SEPARATOR . $new_filename
            )
        ) {
            $input_file = file_get_contents(
                $upload_dir . DIRECTORY_SEPARATOR . $new_filename
            );
            unlink($upload_dir . DIRECTORY_SEPARATOR . $new_filename);
            $file_formats = [
                "TXT" => "txt",
                "XML" => "xml",
            ];
            $file_format = isset($file_formats[$_POST["format"]])
                ? $file_formats[$_POST["format"]]
                : $file_formats["TXT"];
            if (
                preg_match_all(
                    "/(\d+\s+Start-Line\s+BF\s+\d+)(.*?)(\d+\s+End-Line\s+\d+)/ism",
                    $input_file,
                    $matches
                )
            ) {
                if ($file_format == "xml") {
                    $output_file = "";
                    $output_file .= "";
                    $output_file .= "1";
                    $output_file .= "";
                    $output_file .= "";
                }
                foreach ($matches[0] as $line) {
                    $line = explode("\n", $line);
                    $back_benchmark = null;
                    $forward_benchmark = null;
                    $elevation = null;
                    $length = null;
                    for ($i = 1; $i < count($line) - 3; $i++) {
                        if ($i % 3 == 0) {
                            $rb = explode(
                                " ",
                                preg_replace(
                                    "/\s\s+/",
                                    " ",
                                    trim($line[$i - 1])
                                )
                            );
                            $rf = explode(
                                " ",
                                preg_replace("/\s\s+/", " ", trim($line[$i]))
                            );
                            if ($rb[1] != "X" && $rf[1] == "X") {
                                $back_benchmark = $rb[1];
                                $elevation += $rb[5] - $rf[5];
                                $length += $rb[7] + $rf[7];
                                continue;
                            } elseif ($rb[1] == "X" && $rf[1] == "X") {
                                $elevation += $rb[5] - $rf[5];
                                $length += $rb[7] + $rf[7];
                                continue;
                            } elseif ($rb[1] == "X" && $rf[1] != "X") {
                                $forward_benchmark = $rf[1];
                                $elevation += $rb[5] - $rf[5];
                                $length += $rb[7] + $rf[7];
                            } else {
                                $back_benchmark = $rb[1];
                                $forward_benchmark = $rf[1];
                                $elevation = $rb[5] - $rf[5];
                                $length = $rb[7] + $rf[7];
                            }
                            if ($file_format == "xml") {
                                $output_file .= "";
                                $output_file .= "" . $back_benchmark . "";
                                $output_file .= "" . $forward_benchmark . "";
                                $output_file .= "" . $elevation . "";
                                $output_file .= "" . $length . "";
                                $output_file .= "0";
                                $output_file .= "";
                                $output_file .= "\n";
                            } else {
                                $output_file .= sprintf(
                                    "%s %s %1.4f %1.2f 0\n",
                                    $back_benchmark,
                                    $forward_benchmark,
                                    $elevation,
                                    $length
                                );
                            }
                            $elevation = 0;
                            $length = 0;
                        }
                    }
                }
                if ($file_format == "xml") {
                    $output_file .= "";
                    $output_file .= "";
                }
            }
        } else {
            echo "Възникнала е грешка при качването на Файла " .
                $old_filename .
                "";
        }
    } else {
        echo "Няма избран файл за качване!";
    }
}