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

标签 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;

      ?>

最佳答案

解决了

   $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
        )));

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

关于php - 如何使用数据库中的数据创建 LineString geoJson?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53608790/

相关文章:

php - CakePHP 上传插件 : Not uploading as attachment

jquery - $.each(someData.split (',' ) => 返回连接字符串,而不是项目数组

ruby-on-rails - 最佳实践 - RoR 4 Heroku - Cron 每小时从外部 API 填充数据库

Node.js 和 PostgresQL 与 Express

php - 保护对 PHP API 的访问

php - 如何限制使用 session 下载的文件数量

php - 一个更好的 php array_merge

json - Swift JSONDecoder typeMismatch 错误

python - 将 JSON 反序列化为对象

comparison - 测试 Postgresql 数组字段是空还是空的正确方法是什么