<p>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 : <a href="https://craftcms.stackexchange.com/questions/21857/with-element-api-can-i-combine-two-entry-types-into-a-json-object-keyed-by-typ">With Element API, can I combine two entry types into a JSON object, keyed by type?</a></p>

CC BY-SA 3.0
2018-01-11T13:26:48.580Z
2018-01-11T13:26:48.580Z
BillyMedia
<p>I am trying to output, from a single request, a geojson structure like so:</p>

<pre><code>    "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]
                ]
            }
        }            
    ]
</code></pre>

<p>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.</p>

<p>Here is my elements api endpoint :</p>

<pre><code>    'geojson' =&gt; function() {

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

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

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

<plugin-elementapi><craft-pro><json>
Using elements-api, how do i create a geojson structure with Features within a FeatureCollections
# Using elements-api, how do i create a geojson structure with Features within a FeatureCollections

(Asked by: username_0 on 2018-01-08)

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.

**Tags:** plugin-elementapi, craft-pro, json


---

## 1 answers

### Answer by username_0 on 2018-01-11. Score: 0 | Accepted answer

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?](https://craftcms.stackexchange.com/questions/21857/with-element-api-can-i-combine-two-entry-types-into-a-json-object-keyed-by-typ)


---

