Skip to content

Instantly share code, notes, and snippets.

@jaakla
Last active July 14, 2016 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaakla/af224376d34e6cc0515274bb95cf70ec to your computer and use it in GitHub Desktop.
Save jaakla/af224376d34e6cc0515274bb95cf70ec to your computer and use it in GitHub Desktop.
Combine Micello data API GeoJSON to flat GeoJSON what Carto.com likes
<?php
/* Parser for micello indoor geojson
* - https://www.micello.com/documentation/data
*/
//for debug
#error_reporting(E_ALL & ~E_NOTICE);
#ini_set('display_errors', 1);
//main class
class building {
$key = 'YOUR_KEY';
//object url pattern
public $object_url_pattern = 'https://mfs.micello.com/ms/v1/mfile/map/__objId__/mv/-/ev/-/geojson/com-map?key='.$key;
//entities url pattern
public $entities_url_pattern = 'http://mfs.micello.com/ms/v1/mfile/map/__objId__/mv/-/ev/-/geojson/com-entity?key='.$key;
//level url pattern
public $level_url_pattern = 'https://mfs.micello.com/ms/v1/mfile/map/__objId__/mv/-/ev/-/geojson/geojson-level-geom/__levelId__?key='.$key;
//object id (from REQUEST[bid])
public $obj_id;
//obj_url (ex: https://mfs.micello.com/ms/v1/mfile/map/78/mv/-/ev/-/geojson/com-map?key=)
public $obj_url;
//levels info (id, zlevel, level_no, etc.)
public $level_ids;
//obj_data (array)
public $obj_data;
//entities_data (array)
public $entities_data;
//final_data (levels array)
public $levels_data;
function __construct($bid) {
$this->obj_id = $bid;
$this->obj_url = str_replace('__objId__', $bid, $this->object_url_pattern);
}
//create object data array from json
public function get_object_data() {
if (isset($this->obj_url)) {
$this->obj_data = @json_decode(file_get_contents($this->obj_url), TRUE);
}
}
//create level info data array
public function get_levels() {
if (is_array($this->obj_data)) {
foreach ($this->obj_data['drawings'] as $per) {
foreach ($per['levels'] as $level) {
$level['drawing_id'] = $per['id'];
$this->level_ids[] = $level;
}
}
}
}
//create entities data array from json
public function get_entities_data() {
if (isset($this->obj_url)) {
if ($e_json = json_decode(file_get_contents(str_replace('__objId__', $this->obj_id, $this->entities_url_pattern)), TRUE)) {
foreach ($e_json['entities'] as $per) {
$this->entities_data[$per['id']] = $per['properties'];
}
}
}
}
//get level (current level_id) features
private function get_level_data($level_id) {
$url = str_replace('__levelId__', $level_id, str_replace('__objId__', $this->obj_id, $this->level_url_pattern));
$level_data = json_decode(file_get_contents($url), TRUE);
if (is_array($level_data))
return $level_data;
}
//create level features data array (for all levels) - final array
public function get_levels_data() {
if (is_array($this->level_ids)) {
foreach ($this->level_ids as $per) {
$ld = $this->get_level_data($per['id']);
if (is_array($ld['features'])) {
unset($cartojson_polygons);
foreach ($ld['features'] as $element) {
unset($polygons, $label, $deg);
$polygons['type'] = $element['type'];
$polygons['geometry'] = $element['geometry'];
$polygons['properties'] = $element['properties'];
if (is_array($polygons['properties']['entities']) && is_array($this->entities_data[$polygons['properties']['entities'][0]])) {
foreach ($this->entities_data[$polygons['properties']['entities'][0]] as $key => $val) {
if ($key == 'tags')
$polygons['properties'][$key] = $val[0];
//no need geometry key
elseif ($key != 'geometry')
$polygons['properties'][$key] = $val;
}
}
$polygons['properties']['object_id'] = $this->obj_id;
$polygons['properties']['drawing_id'] = $per['drawing_id'];
$polygons['properties']['level_id'] = $per['id'];
$polygons['properties']['zlevel'] = $per['properties']['zlevel'];
$polygons['properties']['level_main'] = $per['properties']['main'];
$polygons['properties']['level_name'] = $per['properties']['name'];
if ($label_area = $element['label_area']) {
$location = $element['location'];
$deg = rad2deg($label_area[4]);
#$labels['properties']['angle'] = (($deg<0)?(abs($deg)):(180-$deg));
#$labels['properties']['angle'] = (($deg<90)?(0-$deg):(180-$deg));
$polygons['properties']['angle'] = (($deg<90 && $deg>=0)?(0-$deg):(180-$deg));
}
$cartojson_polygons['features'][] = $polygons;
}
}
$data1[] = $cartojson_polygons;
}
}
for ($i=0; $i<count($data1); $i++) {
foreach ($data1[$i]['features'] as $per) {
$this->levels_data['features'][] = $per;
}
}
$this->levels_data['type'] = 'FeatureCollection';
}
}
//
if (!$_REQUEST['bid']) {
header('HTTP/1.0 404 Object id parameter missing', true, 404);
die('Object id parameter missing!');
}
else {
$b1 = new building((int)$_REQUEST['bid']);
$b1->get_object_data();
}
//
if (!is_array($b1->obj_data)) {
header('HTTP/1.0 404 Could not found data', true, 404);
die('Could not found data!');
}
else {
$b1->get_entities_data();
$b1->get_levels();
$b1->get_levels_data();
if (is_array($b1->levels_data)) {
header("Content-type: application/json; charset=utf-8");
echo json_encode($b1->levels_data);
exit();
}
else {
header('HTTP/1.0 404 Empty property', true, 404);
die('empty property!');
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment