1

I am trying to output, from a single request, a geojson structure like so:

    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {},
            "geometry": {
                "type": "MultiLineString",
                "coordinates": [
                    [
                        [-1.102108, 52.671813], 
                        [-1.142189,52.620366]
                    ],
                    [
                        [-1.102108, 52.671813], 
                        [-1.447277, 52.939653]
                    ],
                    [
                        [-1.102108, 52.671813], 
                        [-1.447277, 52.914816]
                    ],
                    [
                        [-1.102108, 52.671813], 
                        [-1.884783, 52.509112]
                    ]
                ]
            }
        },
        {
            "type": "Feature",
            "properties": {},
            "geometry": {
                "type": "MultiPoint",
                "coordinates": [
                    [-1.102108, 52.671813],
                    [-1.142189, 52.620366],
                    [-1.447277, 52.939653],
                    [-1.447277, 52.914816],
                    [-1.884783, 52.509112]
                ]
            }
        }            
    ]

I have the multilinestring part sorted, and i know how to get the data for the multipoint part, bit i cant find a way, from the single elements api endpoint, how to add the multipoint feature onto the end of the returned array / json.

Here is my elements api endpoint :

    'geojson' => function() {

        function getCoordinates(UserModel $user){
            $return = array();
            foreach ($user->connections as $connection) {
                $return[] = array(array($user->longitude,$user->latitude),array($connection->longitude,$connection->latitude));
            }
            return $return;
        }

        return [
            'elementType' => ElementType::User,
            'paginate' => false,
            'transformer' => function(UserModel $user) {
                return [                        
                        "type" => "Feature",
                        "geometry" => array("type" => "MultiLineString", "coordinates" => getCoordinates($user)),
                        "properties" => array("name" => "Test")
                ];
            },
        ];
    },

I know i can probably do this using two requests, but i want to do it in one.

0

If anyone stumbles onto this, i gave up looking for a solution and created my own plugin, to get you started, someone else with the same problem posted some sample code here that returns json from a db query : With Element API, can I combine two entry types into a JSON object, keyed by type?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.