Request: Populate group from external file

Another request! I have a feeling I know what the response will be because it would potentially open the system up to vulnerabilities - but I thought I’d ask anyway. :slight_smile:

It would be cool if a certain group (or whole map) could be updated automatically when it’s loaded, with the source file being something saved externally. What I’m thinking of is some kind of export tool from a database which would generate the appropriate GEOJSON file, and for the map to automatically read this file in to display the up-to-date data without having to manually import this file into the map (and then delete the old group) every time something needs to be updated.

Certainly the import function in MapHub makes things pretty easy, but a live link to something so there isn’t a need to continually export/import the data would be amazing!

Hi Ross,

What you are asking for is possible (or will be possible) with the MapHub API. Have a look here:
https://docs.maphub.net/api/

You can write a very simply Python script for example, which creates the GeoJSON from your database and uploads it every 5 minutes or something similar!

Zsolt

Hi Zsolt,

That’s great news, thanks! I’m not overly familiar with Python, but I’ll have a look and see what I can do with it.

I’m in the process of building a PHP/MySQL system that will hold a lot of other information about certain things and it will be great to be able to do a GeoJSON export from this and have the map pick it up automatically.

Hi Ross,

You absolutely won’t need Python for it if you are using PHP. Here is a page about how to make POST request from PHP:

Zsolt

Fantastic, thanks for that. I’m probably a few weeks away from this stage, but I’ll hopefully get on alright.

One question which I don’t think is covered by the API documentation: is it possible for a group to be inserted from an external source and for a current group to be deleted (i.e. the external data replacing the current data)? Or is it only possible to add new data and old data would have to be removed manually?

No, that’s not possible now, but I’ll add support for replace_map, which will replace the whole map’s data with whatever you give it.

Hi Zsolt,

I was wondering if you could help me out with the PHP side of this as I’m having trouble finding out where I’m going wrong.

I’ve never used cURL, so I was attempting a ‘normal’ PHP function, although it’s possible that I’m falling at the first hurdle by not using cURL!

This is the code I’m using (with API key and map ID removed):

function post($url, $filename){
$filecontents = file_get_contents($filename);
$options = array(
‘http’ =>
array(
‘method’ => ‘POST’,
‘header’ => ‘Content-type: application/x-www-form-urlencoded\r\n’ .
‘Authorization: Token XXXXXXXXXXX\r\n’ .
‘MapHub-API-Arg: {“map_id”: “XXXXXX”, “file_type”: “geojson”}’,
‘content’ => $filecontents
)
);
$streamcontext = stream_context_create($options);
$result = file_get_contents($url, false, $streamcontext);
if($result === false){
$error = error_get_last();
throw new Exception('POST request failed: ’ . $error[‘message’]);
}
return $result;
}
post(‘https://maphub.net/api/1/map/append’, ‘File.geojson’);

And this is the error I’m getting:

Fatal error : Uncaught Exception: POST request failed: file_get_contents(https://maphub.net/api/1/map/append): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden

Is there something I’m missing or doing wrong here? Forgive me, I’ve not had much experience with APIs, so any help would be appreciated. It’s definitely picking the file up properly as I’ve tried echoing $filecontents.

Many thanks!

Just an update on this - I think we’re going to go for a manual upload of a generated GeoJSON file for now because it’s only 1 group that needs replaced anyway, not the whole map. So I might not actually need this.

Might be worth adding a standard PHP solution to the API documentation in the future though - I’m sure it would be useful to some people. :slight_smile: