How to correctly decode geoJson that contains both arrays and strings in php.ini? The problem I'm having is reading it from the database and converting it back to json, the coordinate array is lost.
I'm currently reading it from a (valid) geoJson.json file and storing it in a mysql database using: $jsondata = json_decode($srcfile, true); which works great - it's in (mysql ) looks correct in the database and still has the complete array of coordinates.
The original data looks like this: (except there are 1000 coordinates)
{ "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" }
}
When I pull it from the database and run json_encode($data) on it, the output looks like this - all coordinates are missing.
{ "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" } }
or
Update - Added import method
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; }
Retrieve data:
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!? } } }
Well, weirdly, MODX screwed me up... MODX uses double square brackets to indicate code that should be processed or included - i.e.
[[code snippet name]] [[*include_chunk_name]]
etc
When it sees the coordinate structure:
It basically interprets it as:
So the actual solution is: