国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

如何在php中正確編碼和解碼包含數(shù)組和對象的geoJson
P粉739079318
P粉739079318 2024-01-28 23:48:55
0
1
693

如何正確解碼 php.ini 中同時包含數(shù)組和字符串的 geoJson? 我遇到的問題是從數(shù)據(jù)庫讀取它并轉換回 json,坐標數(shù)組丟失。

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

原始數(shù)據(jù)如下所示:(除了有 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"
 }

}

當我從數(shù)據(jù)庫中提取它并對其運行 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 數(shù)據(jù)庫類型 - 它只是將其轉換為長文本)

或者

  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;

    }

檢索數(shù)據(jù):

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 );
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板