{"id":389,"date":"2019-06-16T20:16:38","date_gmt":"2019-06-16T20:16:38","guid":{"rendered":"https:\/\/blog.get-map.org\/?p=389"},"modified":"2019-06-16T20:54:20","modified_gmt":"2019-06-16T20:54:20","slug":"umap-geojson-export-api-support","status":"publish","type":"post","link":"https:\/\/blog.get-map.org\/index.php\/2019\/06\/16\/umap-geojson-export-api-support\/","title":{"rendered":"Umap GeoJson Export API Support"},"content":{"rendered":"<p><a name=\"preamble\"><\/a> <\/p>\n<p>Upload of <a href=\"https:\/\/wiki.openstreetmap.org\/wiki\/UMap\">Umap<\/a> files exported manually via the Umap web interface has been supported by <a href=\"https:\/\/print.get-map.org\/\">my MapOSMatic instance<\/a> for quite a while, and the <a href=\"https:\/\/print.get-map.org\/about\/api\/\">MapOSMatic API<\/a> has also supported submitting a Umap file via an URL instead of uploading an actual file.<\/p>\n<p>There&#8217;s also a (so far inofficial) export URL for exporting an Umap for exporting a map directly, without manual browser interaction.<\/p>\n<p>See this <a href=\"https:\/\/github.com\/umap-project\/umap\/issues\/78\">Umap Github issue<\/a> for a more detailed description.<\/p>\n<p>The basic idea is that for a map with Id 123 the stable web access URL would be:<\/p>\n<table border=\"0\" bgcolor=\"#e8e8e8\" width=\"100%\" style=\"margin:0.2em 0;\">\n<tr>\n<td style=\"padding:0.5em;\">\n<pre style=\"margin:0; padding:0;\">https:\/\/umap.openstreetmap.fr\/m\/123<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<p>and the inofficial map export URL<\/p>\n<table border=\"0\" bgcolor=\"#e8e8e8\" width=\"100%\" style=\"margin:0.2em 0;\">\n<tr>\n<td style=\"padding:0.5em;\">\n<pre style=\"margin:0; padding:0;\">https:\/\/umap.openstreetmap.de\/en\/map\/123\/geojson<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<p>Unfortunately the export file retrieved this URL is not yet complete though, as unlike the manualy exported file unsing &#8220;Export all data&#8221; in the web interface, it only contain the maps meta data, and references to the actual data layers, but not the actual layer data itself.<\/p>\n<p>So when processing a file exported this way, we first need to check the &#8220;urls&#8221; dictionary from the map properties for the <code>datalayer_view<\/code> URL path, and combine this with the protocol and host part from the maps own export URL.<\/p>\n<p>For the original Umap instance the <code>datalayer_view<\/code> looks like this:<\/p>\n<table border=\"0\" bgcolor=\"#e8e8e8\" width=\"100%\" style=\"margin:0.2em 0;\">\n<tr>\n<td style=\"padding:0.5em;\">\n<pre style=\"margin:0; padding:0;\">\/en\/datalayer\/{pk}\/<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<p>and so the full URL template becomes<\/p>\n<table border=\"0\" bgcolor=\"#e8e8e8\" width=\"100%\" style=\"margin:0.2em 0;\">\n<tr>\n<td style=\"padding:0.5em;\">\n<pre style=\"margin:0; padding:0;\">https:\/\/umap.openstreetmap.de\/en\/datalayer\/{pk}\/<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<p>The <code>{pk}<\/code> placeholder needs to be replaced with the Ids from the <code>datalayers<\/code> list entries, which e.g. look like this:<\/p>\n<table border=\"0\" bgcolor=\"#e8e8e8\" width=\"100%\" style=\"margin:0.2em 0;\">\n<tr>\n<td style=\"padding:0.5em;\">\n<pre style=\"margin:0; padding:0;\">{\n\"name\": \"Layer 1\",\n\"id\": 123456,\n\"displayOnLoad\": true\n}<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<p>So we need to download actual layers data from URLs like<\/p>\n<table border=\"0\" bgcolor=\"#e8e8e8\" width=\"100%\" style=\"margin:0.2em 0;\">\n<tr>\n<td style=\"padding:0.5em;\">\n<pre style=\"margin:0; padding:0;\">https:\/\/umap.openstreetmap.de\/en\/datalayer\/123456\/<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<p>which returns JSON data we need to append to the <code>layers<\/code> list to form a complete export file.<\/p>\n<p>Once we fetched and merged all data layers we can then process the resulting JSON document the same way as the JSON exported from the web frontend.<\/p>\n<p>This should allow a more direct integration between Umap and MapOSMatic servers, to automate the process of creating printed representations of maps generated with Umap.<\/p>\n<p>An API request to render an umap with Id 123 would then simply look like this:<\/p>\n<table border=\"0\" bgcolor=\"#e8e8e8\" width=\"100%\" style=\"margin:0.2em 0;\">\n<tr>\n<td style=\"padding:0.5em;\">\n<pre style=\"margin:0; padding:0;\">{\n    \"umap_url\": \"https:\/\/umap.openstreetmap.fr\/en\/map\/123\/geojson\/\"\n}<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<p>In actual PHP code for example launching a map rendering job for this umap would look like this:<\/p>\n<table border=\"0\" bgcolor=\"#e8e8e8\" width=\"100%\" style=\"margin:0.2em 0;\">\n<tr>\n<td style=\"padding:0.5em;\">\n<pre style=\"margin:0; padding:0;\">&lt;?php\nrequire_once 'HTTP\/Request2.php';\n\nheader(\"Content-type: text\/plain\");\n\ndefine('BASE_URL', 'https:\/\/print.get-map.org\/apis\/v1\/');\n\n$data = ['umap_url' =&gt; 'https:\/\/umap.openstreetmap.fr\/en\/map\/123\/geojson\/'];\n\n$request = new HTTP_Request2(BASE_URL . \"jobs\");\n\n$request-&gt;setMethod(HTTP_Request2::METHOD_POST)\n        -&gt;addPostParameter('job', json_encode($data));\n\n$response = $request-&gt;send();\n$status   = $response-&gt;getStatus();\n\nif ($status &lt; 200 || $status &gt; 299) {\n    print_r($response-&gt;getBody());\n    exit;\n}\n\n$reply = json_decode($response-&gt;getBody());\necho \"Result URL: \".$reply-&gt;interactive.\"\\n\";<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Upload of Umap files exported manually via the Umap web interface has been supported by my MapOSMatic instance for quite a while, and the MapOSMatic API has also supported submitting a Umap file via an URL instead of uploading an actual file. There&#8217;s also a (so far inofficial) export URL for exporting an Umap for &hellip; <a href=\"https:\/\/blog.get-map.org\/index.php\/2019\/06\/16\/umap-geojson-export-api-support\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Umap GeoJson Export API Support&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.get-map.org\/index.php\/wp-json\/wp\/v2\/posts\/389"}],"collection":[{"href":"https:\/\/blog.get-map.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.get-map.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.get-map.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.get-map.org\/index.php\/wp-json\/wp\/v2\/comments?post=389"}],"version-history":[{"count":2,"href":"https:\/\/blog.get-map.org\/index.php\/wp-json\/wp\/v2\/posts\/389\/revisions"}],"predecessor-version":[{"id":391,"href":"https:\/\/blog.get-map.org\/index.php\/wp-json\/wp\/v2\/posts\/389\/revisions\/391"}],"wp:attachment":[{"href":"https:\/\/blog.get-map.org\/index.php\/wp-json\/wp\/v2\/media?parent=389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.get-map.org\/index.php\/wp-json\/wp\/v2\/categories?post=389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.get-map.org\/index.php\/wp-json\/wp\/v2\/tags?post=389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}