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" } }
oder
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!? } } }
好吧,奇怪的是,MODX 搞砸了我...MODX 使用雙方括號(hào)來(lái)表示應(yīng)該處理或包含的代碼 - 即
[[代碼片段名稱]] [[*include_chunk_name]]
等等
當(dāng)它看到坐標(biāo)結(jié)構(gòu)時(shí):
"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
所以實(shí)際的解決方案是:
json_encode($feature, JSON_PRETTY_PRINT );