1

I am interacting with the uk's ONS open data geography portal api and can successfully retrieve a list of Westminster parliamentary constituencies in json, such as the following excerpt:

...
, "features": [
      {
         "attributes": {
            "objectid": 1,
            "pcon17cd": "E14000530",
            "pcon17nm": "Aldershot",
            "bng_e": 484884,
            "bng_n": 155126,
            "long": -0.7841,
            "lat": 51.28900146,
            "st_area(shape)": 53001999.87254762,
            "st_length(shape)": 40826.56914092781
         },
         "geometry": {
            "rings": [
               [
                  [
                     -0.7754662069876494,
                     51.331958902344475
                  ],
                  … load of long lat coordinates

I can use php json_decode to convert the data into a php array-like list called $decoded_data which contains stdObjects. I can use:

foreach( $decoded_data->features as $constituency ) { … }

to work with each constituency in turn to extract its various attributes, eg

 $constituencies[] = [
                                   'id'    => $constituency->attributes->objectid
                               ,   'code'  => $constituency->attributes->pcon17cd
                               ,   'name'  => $constituency->attributes->pcon17nm
    …
];

etc. This all works fine but when I get to:

, 'area' => $constituency->attributes->st_area(shape) 

I receive an error:

Fatal error: Call to a member function st_area() on null

All I am doing is trying to extract the value associated with this index (53001999.87254762 in the example provided above) but PHP is treating the attribute name as function and parsing in a variable 'shape' , neither of which actually exists.

I take it that 'st_area(shape)' is a function/method in ArcGIS or similar but that is not relevant. Here it is just the name of an attribute field.

What is the correct syntax to use to reference it and extract its value?

0 Answers0