How to correctly encode and decode geoJson containing arrays and objects in php
P粉739079318
P粉739079318 2024-01-28 23:48:55
0
1
399

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"
     }
}
  1. Is there a better way to store it so it's easier to use? (I'm using modx xpdo, which doesn't seem to support the JSON database type in the model - it just converts it to long text)

or

  1. What is the correct way to encode it back to valid json and keep the coordinates intact?

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!?
                }
           }
      }

P粉739079318
P粉739079318

reply all(1)
P粉766520991

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:

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

  ]

It basically interprets it as:

"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

So the actual solution is:

json_encode($feature, JSON_PRETTY_PRINT );
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!