TransWikia.com

Creating polygon GeoJSON from PostGIS and rendering in Leaflet

Geographic Information Systems Asked by kassandra on September 5, 2020

Drawing the polygons on QGIS and then import the layer directly on the PostgreSQL / PostGIS db. Then I create a JSON file via PHP and read it to the Leaflet. Everything works but zooming I see a lot of difference in rendering of the polygons which are much more simplified in the web-gis.

I set "smoothFactor": 0 but it’s the same.
enter image description here

    function strutture_pol () {
    global $conn;
    $sql = 'SELECT strutture_pol.*, public.ST_AsGeoJSON(public.ST_Transform((geom),4326),6) AS geojson, datazione.id_data_gen as data_gen, datazione.id_data_spe as data_spec, datazione.id_pref1 as prefisso1, datazione.id_sec_in as datain,  datazione.id_pref2 as prefisso2, datazione.id_sec_fin as datafin   FROM strutture_pol ';
    $sql .= 'INNER JOIN datazione ON strutture_pol.id_data = datazione.id ';
    $sql .= 'WHERE strutture_pol.id_data = datazione.id ';
    $stmt = $conn->prepare($sql);
    $stmt->execute();
 # Build GeoJSON feature collection array
    $geojson = array(
   'type'      => 'FeatureCollection',
   'features'  => array()
    );
# Loop through rows to build feature arrays
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
       $properties = $row;
        # Remove geojson and geometry fields from properties
        unset($properties['geojson']);
        unset($properties['geom']);
          $feature = array(
            'type' => 'Feature',
             'geometry' => json_decode($row['geojson'], true),
             'properties' => $properties
             );
          # Add feature arrays to feature collection array
           array_push($geojson['features'], $feature);
      }
      header('Content-type: application/json');
       header('Access-Control-Allow-Origin: *');
      echo json_encode($geojson, JSON_NUMERIC_CHECK);
    }

One Answer

You are using 6 as the maxdecimaldigits param, so the shapes' accuracy is decreased. Try to remove it (it defaults to 9) or set it by your preferences.

public.ST_AsGeoJSON(public.ST_Transform((geom),4326),6)

Correct answer by NettaB on September 5, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP