如何使用数据库中的数据创建LineString geoJson?

时间:2018-12-04 08:41:33

标签: php json postgresql geojson

好的,所以我知道如何将数据从php转换为geojson格式转换为点类型,但是我不明白如何将其转换为lineString类型,这是代码,因此问题是如何将坐标数据转换为一个数组:

    include('testcon.php');


   $query=pg_query($connect,"SELECT number, state, data, latitude, 
       long "
        . "FROM schema.table "
        . "WHERE number = '63' AND "
        . "data BETWEEN '2018-12-01 00:00:00' and '2018-12-01 23:59:59' 
     limit 200 ");
    # Try query or error


  # Build GeoJSON feature collection array
        $geojson = array(
      'type'      => 'FeatureCollection',
         'features'  => array()
        );
         # Loop through rows to build feature arrays
          while($row = pg_fetch_array($query)) {
$feature = array(
    'type' => 'Feature', 
    'geometry' => array(
        'type' => 'Point',
        # Pass Longitude and Latitude Columns here
        'coordinates' => array($row['long'], $row['lat'])
    ),
    # Pass other attribute columns here
    'properties' => array(
        'indicativ' => $row['number'],
        'stare' => $row['state'],

        )
    );
    # Add feature arrays to feature collection array
    array_push($geojson['features'], $feature);
   }
 header('Content-type: application/json');
   echo json_encode($geojson, JSON_NUMERIC_CHECK);
   $conn = NULL;

      ?>

1 个答案:

答案 0 :(得分:-1)

解决了

   $pointlist=array();
        while($row = pg_fetch_assoc($query)) {
       $pointlist[]=array($row['long'], $row['lat']);

     };
             # Build GeoJSON feature collection array
           $geojson = array(
             'type'      => 'FeatureCollection',
             'features'  => array(
            #);
             #$feature = array(

    'type' => 'Feature', 
    'geometry' => array(
        'type'=> 'LineString',
        'coordinates'=> $pointlist
        )));

我创建一个空数组,然后遍历所有经纬度和经度列的数据,然后在坐标中得到了可变参数