Number accuracy changes when creating Geojson file, how to resolve?

Asked

Viewed 11 times

0

Hello! I have a file that queries in BD and from the result creates a Geojson file. The problem is that in the Geojson file the accuracy of the number increases greatly, unlike the result obtained by the BD, leaving the file too heavy. How to solve this precision problem?

The result of the BD is something like (value of the 3rd field and coordinates):

1, "Noruega", 0.944, "{""type"":""MultiPolygon"",""coordinates"":[[[[5.08746,61.350219],[5.098782,61.345131],[5.095636,61.376375],...

My Geojson file looks like this (value of 3rd field and coordinates):

{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"rank":1,"localizacao":"Noruega","valor":0.94399999999999995026200849679298698902130126953125},"geometry":{"type":"MultiPolygon","coordinates":[[[[5.08746000000000009322320693172514438629150390625,61.35021900000000272257238975726068019866943359375] ...

My conversion file to Geojson in PHP is this:

function geraGeoJSON($sql, $nomeArquivo) {
    $conn = Connect::getInstance();
    $error = Connect::getError();

    if ($error) {
        echo $error->getMessage();
        exit;
    }


    if (isset($_GET['bbox'])) {
        $bbox = explode(',', $_GET['bbox']);
        $sql = $sql . ' WHERE public.ST_Transform(geom, 4326) && public.ST_SetSRID(public.ST_MakeBox2D(public.ST_Point(' . $bbox[0] . ', ' . $bbox[1] . '), public.ST_Point(' . $bbox[2] . ', ' . $bbox[3] . ')),4326);';
    }

    $rs = $conn->query($sql);
    if (!$rs) {
        echo 'Ocorreu um erro na consulta SQL.\n';
        exit;
    }

    $geojson = array(
        'type' => 'FeatureCollection',
        'features' => array()
    );

    while ($row = $rs->fetch(PDO::FETCH_ASSOC)) {
        $properties = $row;
        $properties['valor'] = round($properties['valor'], 3);

        unset($properties['geojson']);
        unset($properties['geom']);
        $feature = array(
            'type' => 'Feature',
            'properties' => $properties,
            'geometry' => json_decode($row['geojson'], true)
        );
        array_push($geojson['features'], $feature);
    }
    
    $arquivo = fopen('../../assets/js/temp-geojson/' . $nomeArquivo . '.js', 'w');
    if ($arquivo == false) {
        echo json_encode(false);
        die('Não foi possível criar o arquivo.');
    }
    fwrite($arquivo, '' . json_encode($geojson, JSON_NUMERIC_CHECK));
    fclose($arquivo);

    $conn = NULL;
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.