如何在php中正確編碼和解碼包含數組和物件的geoJson
P粉739079318
P粉739079318 2024-01-28 23:48:55
0
1
388

如何正確解碼 php.ini 中同時包含陣列和字串的 geoJson? 我遇到的問題是從資料庫讀取它並轉換回 json,坐標數組丟失。

我目前正在從(有效)geoJson.json 文件中讀取它,並使用以下命令將其存儲在mysql 數據庫中: $jsondata = json_decode($srcfile, true);效果很好- 它在(mysql )資料庫中看起來是正確的,並且仍然具有完整的座標數組。

原始資料如下所示:(除了有 1000 個座標)

{
 "type": "Feature",
 "geometry": {
      "coordinates": [
           [
                [
                     [
                          -64.59727115377405,
                          60.30061384178721
                     ],
                     [
                          -64.52477086139639,
                          60.29980770242815
                     ]
                ]
           ],
           [
                [
                     [
                          -64.59727115377405,
                          60.30061384178721
                     ],
                     [
                          -64.52477086139639,
                          60.29980770242815
                     ]
                ]
           ]

      ],
      "type": "MultiPolygon"
 },
 "properties": {
      "prov_type": "province",
      "prov_code": "24",
      "prov_name_fr": "Qu\u00e9bec",
      "geo_point_2d": [
           53.3945173679,
           -71.7823138976
      ],
      "prov_name_en": "Quebec",
      "year": "2019",
      "prov_area_code": "CAN"
 }

}

當我從資料庫中提取它並對其運行 json_encode($data) 時,輸出如下所示 - 缺少所有座標。

{
     "type":"Feature",
     "geometry":{
          "coordinates":,
          "type":"MultiPolygon"
     },
     "properties":{
          "prov_type":"province",
          "prov_code":"35",
          "prov_name_fr":"Ontario",
          "geo_point_2d":[50.4486575765,-86.0470011166],
          "prov_name_en":"Ontario",
          "year":"2019",
          "prov_area_code":"CAN"
     }
}
  1. 是否有更好的方式來儲存它,以便更容易使用? (我使用的是 modx xpdo,它似乎不支援模型中的 JSON 資料庫類型 - 它只是將其轉換為長文字)

  1. 將其編碼回有效 json 並保持座標完整的正確方法是什麼?

更新 - 新增了導入方法

public function importGeodata()
    {

        // $srcfile = file_get_contents('/var/www/vhosts/mcgill.local/src/core/components/mcgill/data/provinces.json');

        $srcfile = file_get_contents('/var/www/vhosts/mcgill.local/src/core/components/mcgill/data/us-states.json');

        $jsondata = json_decode($srcfile, true);

        // echo '<pre>';print_r($jsondata); echo '</pre>'; 


        foreach($jsondata['features'] as $feature)
        {
            // $search = $feature['properties']['prov_name_en']; // FOR CANADA
            $search = $feature['properties']['NAME']; // FOR USA
            

            if(!$updateObj = $this->modx->getObject('CatalogStates', array('name' => $search)))
            {
                echo '<br>Could not find object: ' . $search;
            }else{
                echo '<br>Found  object: '.$search;

  
                $updateObj->set('feature', json_encode(array($feature)));

                if(!$updateObj->save())
                {
                    echo '<br>Error saving object: ' . $search;
                }

            }

        }

        return;

    }

檢索資料:

#
public function getGeoJson()
      {

           $criteria = $this->modx->newQuery('CatalogStates');

           $criteria->where([
                'id:IN' => array(1,2),
           ]);

           if($states = $this->modx->getCollection('CatalogStates',$criteria)) // returns an array of objects
           {
                foreach($states as $state)
                {
                     $feature = $state->get('feature');
                     $coordinates = $feature['geometry']['coordinates'];
                     print_r($coordinates); // this returns the coordinates array
                     echo json_encode($coordinates); // this returns nothing!?
                }
           }
      }

P粉739079318
P粉739079318

全部回覆(1)
P粉766520991

好吧,奇怪的是,MODX 搞砸了我...MODX 使用雙方括號來表示應該處理或包含的程式碼 - 即

[[程式碼片段名稱]] [[*include_chunk_name]]

等等

當它看到座標結構時:

"coordinates": [
       [
            [
                 [
                      -64.59727115377405,
                      60.30061384178721
                 ],
                 [
                      -64.52477086139639,
                      60.29980770242815
                 ]
            ]
       ],
       [
            [
                 [
                      -64.59727115377405,
                      60.30061384178721
                 ],
                 [
                      -64.52477086139639,
                      60.29980770242815
                 ]
            ]
       ]

  ]

它基本上將其解釋為:

"coordinates": [[ // try to run a code snip here
            // run this code snip (and fail)
            [[-64.59727115377405,60.30061384178721], 
            // give up trying to output anything till we find a matching closing brace

所以實際的解決方案是:

json_encode($feature, JSON_PRETTY_PRINT );
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!