public static function getBody($dataObj) { // Check if the original data is not GeoJSON if (!empty($dataObj->geo_formatted) && $dataObj->geo_formatted) { if ($dataObj->source_definition['type'] == 'JSON') { return json_encode($dataObj->data); } elseif ($dataObj->source_definition['type'] == 'KML') { $geom = \geoPHP::load($dataObj->data, 'kml'); return $geom->out('json'); } } // Build the body $body = $dataObj->data; if (is_object($body)) { $body = get_object_vars($dataObj->data); } $features = array(); foreach ($body as $dataRow) { if (is_object($dataRow)) { $dataRow = get_object_vars($dataRow); } $geo = $dataObj->geo; //Guess lat/lon if no geo information was given for this if (empty($geo)) { if ($lat_long = GeoHelper::findLatLong($dataRow)) { $geo = array("latitude" => $lat_long[0], "longitude" => $lat_long[1]); } } $geometric_ids = self::findGeometry($geo, $dataRow); //Prevent geo information being duplicated in properties foreach ($geometric_ids[0] as $geometric_id) { unset($dataRow[$geometric_id]); } $feature = array('type' => 'Feature', 'geometry' => $geometric_ids[1], 'properties' => $dataRow); $id_prop = @$dataObj->source_definition['map_property']; if (!empty($id_prop) && !empty($dataRow[$id_prop])) { $feature['id'] = $dataRow[$id_prop]; unset($dataRow[$id_prop]); } array_push($features, $feature); } $result = array('type' => 'FeatureCollection', 'features' => $features); // Only add bounding box if we have features and are viewing the entire dataset. if (!empty($features) && empty($dataObj->paging)) { $result['bbox'] = self::boundingBox($features); } return json_encode($result); }
private static function printArray($val) { foreach ($val as $key => $value) { $array = $value; if (is_object($array)) { $array = get_object_vars($value); } $lat_long = GeoHelper::findLatLong($array); $coords = array(); if (!empty($array)) { $coordskey = GeoHelper::keyExists("coords", $array); if (!$coordskey) { $coordskey = GeoHelper::keyExists("coordinates", $array); } if ($lat_long) { $name = self::xmlgetelement($array); $extendeddata = self::getExtendedDataElement($array); } elseif ($coordskey) { if (is_array($array[$coordskey])) { if (!empty($array[$coordskey]['@text'])) { $array[$coordskey] = $array[$coordskey]['@text']; } } $coords = explode(";", $array[$coordskey]); unset($array[$coordskey]); $name = self::xmlgetelement($array); $extendeddata = self::getExtendedDataElement($array); } else { self::printArray($array); } if ($lat_long || count($coords) != 0) { $name = htmlspecialchars($key); if (!empty(self::$map_property) && !empty($array[self::$map_property])) { $name = $array[self::$map_property]; } $description = ''; if (!empty($key) && is_numeric($key)) { $description = "<![CDATA[<a href='" . \URL::to(self::$definition['collection_uri'] . '/' . self::$definition['resource_name']) . '/' . htmlspecialchars($key) . ".map'>" . \URL::to(self::$definition['collection_uri'] . '/' . self::$definition['resource_name']) . '/' . htmlspecialchars($key) . "</a>]]>"; } echo "<Placemark><name>" . $name . "</name><Description>" . $description . "</Description>"; echo $extendeddata; if ($lat_long) { // For data read from XML latitude and longitude will be an array of @value = 3.342... $lat_val = $array[$lat_long[0]]; $lon_val = $array[$lat_long[1]]; if (!empty($lat_long[2]) && !empty($array[$lat_long[2]])) { $z_val = $array[$lat_long[2]]; if (is_array($lat_val)) { $lat_val = reset($lat); } if (is_array($lon_val)) { $lon_val = reset($lon_val); } if (is_array($z_val)) { $z_val = reset($z_val); } if ($lat_val != 0 || $lon_val != 0) { echo "<Point><coordinates>" . $lon_val . "," . $lat_val . "," . $z_val . "</coordinates></Point>"; } } else { if (is_array($lat_val)) { $lat_val = reset($lat); } if (is_array($lon_val)) { $lon_val = reset($lon_val); } if ($lat_val != 0 || $lon_val != 0) { echo "<Point><coordinates>" . $lon_val . "," . $lat_val . "</coordinates></Point>"; } } } if (count($coords) > 0) { if (count($coords) == 1) { echo "<Polygon><outerBoundaryIs><LinearRing><coordinates>" . $coords[0] . "</coordinates></LinearRing></outerBoundaryIs></Polygon>"; } else { echo "<MultiGeometry>"; foreach ($coords as $coord) { echo "<LineString><coordinates>" . $coord . "</coordinates></LineString>"; } echo "</MultiGeometry>"; } } echo "</Placemark>"; } } } }