public function createMapFromTable($table) { $split = explode(".", $table); $type = $split[1]; if (mb_substr($type, 0, 1, 'utf-8') == "_") { $type = "a" . $type; } $table = new \app\models\Table($table); $schema = $table->getMapForEs(); $map = array("mappings" => array($type => array("properties" => array("properties" => array("type" => "object", "properties" => array()), "geometry" => array())))); foreach ($schema as $key => $value) { if ($value["type"] == "geometry") { $map["mappings"][$type]["properties"]["geometry"] = array("type" => "geo_shape"); } elseif ($value["type"] == "string" || $value["type"] == "text") { $map["mappings"][$type]["properties"]["properties"]["properties"][$key] = array("type" => "string", "search_analyzer" => "str_search_analyzer", "index_analyzer" => "str_index_analyzer"); } elseif ($value["type"] == "timestamptz") { $map["mappings"][$type]["properties"]["properties"]["properties"][$key] = array("type" => "date", "format" => "Y-MM-dd HH:mm:ss.SSSSSSZ"); } elseif ($value["type"] == "date") { $map["mappings"][$type]["properties"]["properties"]["properties"][$key] = array("type" => "date"); } elseif ($value["type"] == "int") { $map["mappings"][$type]["properties"]["properties"]["properties"][$key] = array("type" => "integer"); } elseif ($value["type"] == "number") { $map["mappings"][$type]["properties"]["properties"]["properties"][$key] = array("type" => "float"); } elseif ($value["type"] == "boolean") { $map["mappings"][$type]["properties"]["properties"]["properties"][$key] = array("type" => "boolean"); } } $response = array("map" => $map); return $response["map"]["mappings"]; }
public function post_records() { $table = new \app\models\Table(null); $name = $table->create($_REQUEST['name'], $_REQUEST['type'], $_REQUEST['srid']); // Set layer editable $join = new \app\models\Table("settings.geometry_columns_join"); $data = (array) json_decode(urldecode('{"data":{"editable":true,"_key_":"' . \app\conf\Connection::$param["postgisschema"] . '.' . $name['tableName'] . '.the_geom"}}')); $response = $this->auth(); return !$response['success'] ? $response : $join->updateRecord($data, "_key_"); }
public function createMapFromTable($table) { $split = explode(".", $table); $type = $split[1]; if (mb_substr($type, 0, 1, 'utf-8') == "_") { $type = "a" . $type; } $tableObj = new \app\models\Table($table); $schema = $tableObj->getMapForEs(); $map = array("mappings" => array($type => array("properties" => array("properties" => array("type" => "object", "properties" => array()))))); $layer = new \app\models\Layer(); $esTypes = $layer->getElasticsearchMapping($table); $arr = array(); foreach ($esTypes["data"] as $key => $value) { $arr[$value["column"]] = array("elasticsearchtype" => $value["elasticsearchtype"], "format" => $value["format"], "index" => $value["index"], "analyzer" => $value["analyzer"], "index_analyzer" => $value["index_analyzer"], "search_analyzer" => $value["search_analyzer"], "type" => $value["type"], "boost" => $value["boost"], "null_value" => $value["null_value"]); } foreach ($schema as $key => $value) { $pgType = $value["type"]; $mapArr = array(); $mapArr["type"] = $arr[$key]["elasticsearchtype"]; if (isset($arr[$key]["format"]) && $arr[$key]["format"]) { $mapArr["format"] = $arr[$key]["format"]; } if (isset($arr[$key]["index"]) && $arr[$key]["index"]) { $mapArr["index"] = $arr[$key]["index"]; } if (isset($arr[$key]["analyzer"]) && $arr[$key]["analyzer"]) { $mapArr["analyzer"] = $arr[$key]["analyzer"]; } if (isset($arr[$key]["search_analyzer"]) && $arr[$key]["search_analyzer"]) { $mapArr["search_analyzer"] = $arr[$key]["search_analyzer"]; } if (isset($arr[$key]["index_analyzer"]) && $arr[$key]["index_analyzer"]) { $mapArr["index_analyzer"] = $arr[$key]["index_analyzer"]; } if (isset($arr[$key]["boost"]) && $arr[$key]["boost"]) { $mapArr["boost"] = $arr[$key]["boost"]; } if (isset($arr[$key]["null_value"]) && $arr[$key]["null_value"]) { $mapArr["null_value"] = $arr[$key]["null_value"]; } if ($pgType == "geometry") { if ($mapArr["type"] == "geo_point") { $map["mappings"][$type]["properties"]["geometry"]["properties"]["coordinates"] = $mapArr; } else { $map["mappings"][$type]["properties"]["geometry"] = $mapArr; } } else { $map["mappings"][$type]["properties"]["properties"]["properties"][$key] = $mapArr; } } $response = array("map" => $map); return $response["map"]["mappings"]; }
function getColumnsForExtGridAndStore($createKeyFrom = NULL) { $fieldconfArr = (array) json_decode($this->getGeometryColumns($this->table, "fieldconf")); foreach ($fieldconfArr as $key => $value) { if ($value->properties == "*") { $table = new \app\models\Table($this->table); $distinctValues = $table->getGroupByAsArray($key); $fieldconfArr[$key]->properties = json_encode($distinctValues["data"], JSON_NUMERIC_CHECK); } } if ($this->geomType == "POLYGON" || $this->geomType == "MULTIPOLYGON") { $type = "Polygon"; } elseif ($this->geomType == "POINT" || $this->geomType == "MULTIPOINT") { $type = "Point"; } elseif ($this->geomType == "LINESTRING" || $this->geomType == "MULTILINESTRING" || $this->geomType == "LINE") { $type = "Path"; } if (substr($this->geomType, 0, 5) == "MULTI") { $multi = true; } else { $multi = false; } foreach ($this->metaData as $key => $value) { if ($value['type'] != "geometry" && $key != $this->primeryKey['attname']) { $fieldsForStore[] = array("name" => $key, "type" => $value['type']); $columnsForGrid[] = array("header" => $key, "dataIndex" => $key, "type" => $value['type'], "typeObj" => $value['typeObj'], "properties" => $fieldconfArr[$key]->properties ?: null); } } if ($createKeyFrom) { $fieldsForStore[] = array("name" => "_key_", "type" => "string"); $fieldsForStore[] = array("name" => "pkey", "type" => "string"); } $response["forStore"] = $fieldsForStore; $response["forGrid"] = $columnsForGrid; $response["type"] = $type; $response["multi"] = $multi; return $response; }
public function getAll($schema = false, $layer = false, $auth, $includeExtent = false, $parse = false, $es = false) { // TODO use the function settings.getColumns() instead $where = $auth ? "(authentication<>'foo' OR authentication is NULL)" : "(authentication='Write' OR authentication='None')"; $case = "CASE WHEN ((layergroup = '' OR layergroup IS NULL) AND baselayer != true) THEN 9999999 else sort_id END"; if ($schema) { $ids = explode(",", $schema); $qMarks = str_repeat('?,', count($ids) - 1) . '?'; $sql = "SELECT *, ({$case}) as sort FROM settings.geometry_columns_view WHERE {$where} AND f_table_schema in ({$qMarks}) ORDER BY sort"; } elseif ($layer) { $sql = "SELECT *, ({$case}) as sort FROM settings.geometry_columns_view WHERE {$where} AND f_table_schema = :sSchema AND f_table_name = :sName ORDER BY sort"; } else { $sql = "SELECT *, ({$case}) as sort FROM settings.geometry_columns_view WHERE {$where} ORDER BY sort"; } $sql .= \app\conf\App::$param["reverseLayerOrder"] ? " DESC" : " ASC"; $res = $this->prepare($sql); try { if ($schema) { $res->execute($ids); } elseif ($layer) { $split = explode(".", $layer); $res->execute(array("sSchema" => $split[0], "sName" => $split[1])); } else { $res->execute(); } } catch (\PDOException $e) { $response['success'] = false; $response['message'] = $e->getMessage(); $response['code'] = 401; return $response; } while ($row = $this->fetchRow($res, "assoc")) { $arr = array(); $primeryKey = $this->getPrimeryKey("{$row['f_table_schema']}.{$row['f_table_name']}"); $resVersioning = $this->doesColumnExist("{$row['f_table_schema']}.{$row['f_table_name']}", "gc2_version_gid"); $versioning = $resVersioning["exists"]; if ($row['type'] != "RASTER" && $includeExtent == true) { $srsTmp = "900913"; $sql = "SELECT ST_Xmin(ST_Extent(public.ST_Transform(\"" . $row['f_geometry_column'] . "\",{$srsTmp}))) AS xmin,ST_Xmax(ST_Extent(public.ST_Transform(\"" . $row['f_geometry_column'] . "\",{$srsTmp}))) AS xmax, ST_Ymin(ST_Extent(public.ST_Transform(\"" . $row['f_geometry_column'] . "\",{$srsTmp}))) AS ymin,ST_Ymax(ST_Extent(public.ST_Transform(\"" . $row['f_geometry_column'] . "\",{$srsTmp}))) AS ymax FROM {$row['f_table_schema']}.{$row['f_table_name']}"; $resExtent = $this->prepare($sql); try { $resExtent->execute(); } catch (\PDOException $e) { //print_r($e); } $extent = $this->fetchRow($resExtent, "assoc"); } foreach ($row as $key => $value) { if ($key == "type" && $value == "GEOMETRY") { $def = json_decode($row['def']); if (isset($def->geotype) && $def->geotype != "Default") { $value = "MULTI" . $def->geotype; } } if ($key == "layergroup" && !$value) { $value = "<font color='red'>[Ungrouped]</font>"; } if ($key == "fieldconf" && $value) { $obj = json_decode($value, true); if (is_array($obj)) { foreach ($obj as $k => $val) { if ($obj[$k]["properties"] == "*") { $table = new \app\models\Table($row['f_table_schema'] . "." . $row['f_table_name']); $distinctValues = $table->getGroupByAsArray($k); $obj[$k]["properties"] = json_encode($distinctValues["data"], JSON_NUMERIC_CHECK); } } $value = json_encode($obj); } else { $value = null; } } if ($parse) { if (($key == "fieldconf" || $key == "def" || $key == "class" || $key == "classwizard") && $value) { $value = json_decode($value); } } $arr = $this->array_push_assoc($arr, $key, $value); } $arr = $this->array_push_assoc($arr, "pkey", $primeryKey['attname']); $arr = $this->array_push_assoc($arr, "versioning", $versioning); if ($includeExtent == true) { $arr = $this->array_push_assoc($arr, "extent", $extent); } // Is indexed? if ($es) { $type = $row['f_table_name']; if (mb_substr($type, 0, 1, 'utf-8') == "_") { $type = "a" . $type; } $url = (App::$param['esHost'] ?: "http://127.0.0.1") . ":9200/{$this->postgisdb}_{$row['f_table_schema']}/{$type}/"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, true); // we want headers curl_setopt($ch, CURLOPT_NOBODY, true); // we don't need body curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $output = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpcode == "200") { $arr = $this->array_push_assoc($arr, "indexed_in_es", true); } else { $arr = $this->array_push_assoc($arr, "indexed_in_es", false); } } // Session is sub-user we always check privileges if (isset($_SESSION) && $_SESSION['subuser']) { $privileges = (array) json_decode($row["privileges"]); if ($_SESSION['subuser'] == false || $_SESSION['subuser'] != false && $privileges[$_SESSION['usergroup'] ?: $_SESSION['subuser']] != "none" && $privileges[$_SESSION['usergroup'] ?: $_SESSION['subuser']] != false) { $response['data'][] = $arr; } elseif ($schema != false && $_SESSION['subuser'] == $schema) { $response['data'][] = $arr; } } else { $response['data'][] = $arr; } } $response['data'] = isset($response['data']) ? $response['data'] : array(); if (!isset($this->PDOerror)) { $response['auth'] = $auth ?: false; $response['success'] = true; $response['message'] = "geometry_columns_view fetched"; } else { $response['success'] = false; $response['message'] = $this->PDOerror[0]; $response['code'] = 401; } return $response; }
public function get_index() { $dir = App::$param['path'] . "app/tmp/" . Connection::$param["postgisdb"] . "/__vectors"; $safeName = \app\inc\Model::toAscii($_REQUEST['name'], array(), "_"); $skipFailures = $_REQUEST["ignoreerrors"] == "true" ? true : false; $append = $_REQUEST["append"] == "true" ? true : false; $overwrite = $_REQUEST["overwrite"] == "true" ? true : false; if (is_numeric($safeName[0])) { $safeName = "_" . $safeName; } //Check if file is .zip $zipCheck1 = explode(".", $_REQUEST['file']); $zipCheck2 = array_reverse($zipCheck1); if (strtolower($zipCheck2[0]) == "zip" || strtolower($zipCheck2[0]) == "rar") { $ext = array("shp", "tab", "geojson", "gml", "kml", "mif", "gdb"); $folderArr = array(); $safeNameArr = array(); for ($i = 0; $i < sizeof($zipCheck1) - 1; $i++) { $folderArr[] = $zipCheck1[$i]; } $folder = implode(".", $folderArr); if (strtolower($zipCheck2[0]) == "zip") { // ZIP start $zip = new \ZipArchive(); $res = $zip->open($dir . "/" . $_REQUEST['file']); if ($res === false) { $response['success'] = false; $response['message'] = "Could not unzip file"; return Response::json($response); } $zip->extractTo($dir . "/" . $folder); $zip->close(); // ZIP end } if (strtolower($zipCheck2[0]) == "rar") { // RAR start $rar_file = rar_open($dir . "/" . $_REQUEST['file']); if (!$rar_file) { $response['success'] = false; $response['message'] = "Could not unrar file"; return Response::json($response); } $list = rar_list($rar_file); foreach ($list as $file) { $entry = rar_entry_get($rar_file, $file); $file->extract($dir . "/" . $folder); // extract to the current dir } rar_close($rar_file); // RAR end } if ($handle = opendir($dir . "/" . $folder)) { while (false !== ($entry = readdir($handle))) { if ($entry !== "." && $entry !== "..") { $zipCheck1 = explode(".", $entry); $zipCheck2 = array_reverse($zipCheck1); if (in_array(strtolower($zipCheck2[0]), $ext)) { $_REQUEST['file'] = $folder . "/" . $entry; for ($i = 0; $i < sizeof($zipCheck1) - 1; $i++) { $safeNameArr[] = $zipCheck1[$i]; } $safeName = \app\inc\Model::toAscii(implode(".", $safeNameArr), array(), "_"); break; } $_REQUEST['file'] = $folder; } } } } $srid = $_REQUEST['srid'] ?: "4326"; $encoding = $_REQUEST['encoding'] ?: "LATIN1"; switch ($_REQUEST['type']) { case "Point": $type = "point"; break; case "Polygon": $type = "multipolygon"; break; case "Line": $type = "multilinestring"; break; case "Geometry": $type = "geometry"; break; default: $type = "PROMOTE_TO_MULTI"; break; } $model = new \app\inc\Model(); $tableExist = $model->isTableOrView(Connection::$param["postgisschema"] . "." . $safeName); $tableExist = $tableExist["success"]; if ($tableExist == true && $overwrite == false && $append == false) { $response['success'] = false; $response['message'] = "'{$safeName}' exists already, use 'Overwrite'"; $response['code'] = 406; return $response; } if ($_REQUEST["append"] == "true") { $sql = "DELETE FROM " . Connection::$param["postgisschema"] . "." . $safeName; $res = $model->prepare($sql); try { $res->execute(); } catch (\PDOException $e) { $response['success'] = false; $response['message'] = "Could not delete from {$safeName}"; $response['code'] = 406; return $response; } } $cmd = "PGCLIENTENCODING={$encoding} ogr2ogr " . ($skipFailures ? "-skipfailures " : " ") . ($append ? "-append " : " ") . ($overwrite == true && $append == false ? "-overwrite " : " ") . "-dim 2 " . ($append ? "" : "-lco 'GEOMETRY_NAME=the_geom' ") . ($append ? "" : "-lco 'FID=gid' ") . ($append ? "" : "-lco 'PRECISION=NO' ") . ($append ? "" : "-lco 'PG_USE_COPY=YES' ") . "-a_srs 'EPSG:{$srid}' " . "-f 'PostgreSQL' PG:'host=" . Connection::$param["postgishost"] . " user="******"postgisuser"] . " password="******"postgispw"] . " dbname=" . Connection::$param["postgisdb"] . " active_schema=" . Connection::$param["postgisschema"] . "' " . "'" . $dir . "/" . $_REQUEST['file'] . "' " . "-nln {$safeName} " . "-nlt {$type}"; exec($cmd . ' 2>&1', $out, $err); $geoType = $model->getGeometryColumns(Connection::$param["postgisschema"] . "." . $safeName, "type"); $key = Connection::$param["postgisschema"] . "." . $safeName . ".the_geom"; $class = new \app\models\Classification($key); $arr = $class->getAll(); // Set layer editable $join = new \app\models\Table("settings.geometry_columns_join"); $json = '{"data":{"editable":true,"_key_":"' . $key . '"}}'; $data = (array) json_decode(urldecode($json)); $join->updateRecord($data, "_key_"); if (empty($arr['data'])) { $class->insert(); $class->update("0", \app\models\Classification::createClass($geoType)); } $def = new \app\models\Tile($key); $arr = $def->get(); if (empty($arr['data'][0])) { $json = '{ "theme_column":"", "label_column":"", "query_buffer":"", "opacity":"", "label_max_scale":"", "label_min_scale":"", "meta_tiles":false, "meta_size":"3", "meta_buffer":"10", "ttl":""}'; $def->update($json); } if ($out[0] == "") { $response['success'] = true; $response['message'] = "Layer <b>{$safeName}</b> is created"; $response['type'] = $geoType; // Bust cache, in case of layer already exist \app\controllers\Tilecache::bust(Connection::$param["postgisschema"] . "." . $safeName); } else { $response['success'] = false; $response['message'] = $safeName . ": Some thing went wrong. Check the log."; $response['out'] = $out[0]; Session::createLog($out, $_REQUEST['file']); // Make sure the table is dropped if not skipping failures and it didn't exists before if ($skipFailures == false && $tableExist == false) { $sql = "DROP TABLE " . Connection::$param["postgisschema"] . "." . $safeName; $res = $model->prepare($sql); try { $res->execute(); } catch (\PDOException $e) { } } } $response['cmd'] = $cmd; return $response; }
if (!file_exists($dir)) { @mkdir($dir); } if (is_numeric($safeName[0])) { $safeName = "_" . $safeName; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); $fp = fopen($dir . "/" . $tempFile, 'w+'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_exec($ch); curl_close($ch); fclose($fp); if ($deleteAppend == "1") { \app\models\Database::setDb($db); $table = new \app\models\Table($schema . "." . $safeName); if (!$table->exits) { // If table doesn't exists, when do not try to delete/append $o = "-overwrite"; } else { $o = "-append"; $sql = "DELETE FROM {$schema}.{$safeName}"; $res = $table->prepare($sql); try { $res->execute($values); } catch (\PDOException $e) { // Set the success of the job to false print_r($e); \app\models\Database::setDb("gc2scheduler"); $model = new \app\inc\Model(); $sql = "UPDATE jobs SET lastcheck=:lastcheck WHERE id=:id";