So kodieren und dekodieren Sie GeoJson, das Arrays und Objekte enthält, in PHP korrekt
P粉739079318
P粉739079318 2024-01-28 23:48:55
0
1
382

Wie dekodiere ich GeoJson, das sowohl Array als auch String enthält, in php.ini korrekt? Das Problem, das ich habe, besteht darin, es aus der Datenbank zu lesen und zurück in JSON zu konvertieren. Das Koordinatenarray geht verloren.

Ich lese es gerade aus einer (gültigen) geoJson.json-Datei und speichere es in einer MySQL-Datenbank mit: $jsondata = json_decode($srcfile, true); was großartig funktioniert – es ist in der (MySQL-)Datenbank und sieht korrekt aus hat immer noch das vollständige Koordinatenarray.

Die Originaldaten sehen so aus: (außer dass es 1000 Koordinaten gibt)

{
 "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"
 }

}

Wenn ich es aus der Datenbank ziehe und json_encode($data) darauf ausführe, sieht die Ausgabe so aus – alle Koordinaten fehlen.

{
     "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. Gibt es eine bessere Möglichkeit, es aufzubewahren, damit es einfacher zu verwenden ist? (Ich verwende modx xpdo und es scheint den JSON-Datenbanktyp im Modell nicht zu unterstützen – es konvertiert ihn nur in Langtext)

oder

  1. Was ist der richtige Weg, um es wieder in gültiges JSON zu kodieren und die Koordinaten intakt zu halten?

Update – Importmethode hinzugefügt

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;

    }

Daten abrufen:

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

Antworte allen(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 );
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!