Example #1
0
 public function get_index()
 {
     $safeName = \app\inc\Model::toAscii($_REQUEST['name'], array(), "_");
     if (is_numeric($safeName[0])) {
         $safeName = "_" . $safeName;
     }
     $srid = $_REQUEST['srid'] ?: "4326";
     $file = $_REQUEST['file'];
     $key = Connection::$param["postgisschema"] . "." . $safeName . ".rast";
     // Create new table
     $table = new Table($safeName);
     $res = $table->createAsRasterTable($srid);
     // Set bitmapsource
     $join = new Table("settings.geometry_columns_join");
     $json = '{"data":{"bitmapsource":"' . $file . '","_key_":"' . $key . '"}}';
     $data = (array) json_decode(urldecode($json));
     $join->updateRecord($data, "_key_");
     if ($res["success"]) {
         $response['success'] = true;
         $response['message'] = "Layer <b>{$safeName}</b> is created";
     } else {
         $response['success'] = false;
         $response['message'] = "Some thing went wrong. Check the log.";
         Session::createLog(array($res['message']), $_REQUEST['file']);
     }
     return Response::json($response);
 }
Example #2
0
 public function get_index()
 {
     $q = Input::getPath()->part(5);
     $split = explode(".", $q);
     if (sizeof($split) == 1) {
         return $this->layers->getAll($q, null, Session::isAuth(), Input::get("iex"), Input::get("parse"), Input::get("es"));
     } else {
         return $this->layers->getAll(null, $q, Session::isAuth(), Input::get("iex"), Input::get("parse"), Input::get("es"));
     }
 }
Example #3
0
 public function get_index()
 {
     $dir = App::$param['path'] . "app/tmp/" . Connection::$param["postgisdb"] . "/__bitmaps";
     $safeName = \app\inc\Model::toAscii($_REQUEST['name'], array(), "_");
     if (is_numeric($safeName[0])) {
         $safeName = "_" . $safeName;
     }
     $srid = $_REQUEST['srid'] ?: "4326";
     $cmd = "raster2pgsql " . "-s " . $srid . " -I -C -M -d " . $dir . "/" . $_REQUEST['file'] . " -F" . " -t 100x100 " . Connection::$param["postgisschema"] . "." . $safeName . " | PGPASSWORD="******"postgispw"] . " psql " . Connection::$param["postgisdb"] . " -U " . Connection::$param["postgisuser"] . " -h " . Connection::$param["postgishost"] . " -p " . Connection::$param["postgisport"];
     exec($cmd . ' 2>&1', $out);
     $err = false;
     // This is a HACK. raster2pgsql doesn't return the error to stdout or stderr.
     if (!isset($out[0])) {
         $out[0] = "ERROR: Unable to read raster file";
     }
     foreach ($out as $line) {
         if (strpos($line, 'ERROR') !== false) {
             $err = true;
             break;
         }
     }
     if (!$err) {
         $response['success'] = true;
         $response['cmd'] = $cmd;
         $response['message'] = "Raster layer <b>{$safeName}</b> is created";
         $key = Connection::$param["postgisschema"] . "." . $safeName . ".rast";
         $class = new \app\models\Classification($key);
         $arr = $class->getAll();
         if (empty($arr['data'])) {
             $class->insert();
             $class->update("0", \app\models\Classification::createClass("POLYGON"));
         }
         if ($_REQUEST['displayfile']) {
             $join = new Table("settings.geometry_columns_join");
             $json = '{"data":{"bitmapsource":"' . $_REQUEST['file'] . '","_key_":"' . $key . '"}}';
             $data = (array) json_decode(urldecode($json));
             $join->updateRecord($data, "_key_");
         }
     } else {
         $response['success'] = false;
         $response['message'] = "Some thing went wrong. Check the log.";
         Session::createLog($out, $_REQUEST['file']);
     }
     $response['cmd'] = $cmd;
     return Response::json($response);
 }
Example #4
0
<?php

include_once "../../../app/conf/App.php";
use app\conf\App;
use app\inc\Session;
new App();
Session::start();
\app\models\Database::setDb("mapcentia");
$sTable = 'users';
$prefix = $_SESSION['zone'] ? App::$param['domainPrefix'] . $_SESSION['zone'] . "." : "";
if (App::$param['domain']) {
    $host = "//" . $prefix . App::$param['domain'] . ":" . $_SERVER['SERVER_PORT'];
} else {
    $host = App::$param['host'];
}
if (App::$param['cdnSubDomain']) {
    $bits = explode("://", $host);
    $cdnHost = $bits[0] . "://" . App::$param['cdnSubDomain'] . "." . $bits[1];
} else {
    $cdnHost = $host;
}
Example #5
0
 public function sql($q, $index, $type, $id, $db)
 {
     // We create a unique index name
     $errors = false;
     $errors_in = array();
     $index = $db . "_" . $index;
     $name = "_" . rand(1, 999999999) . microtime();
     $name = $this->toAscii($name, null, "_");
     $view = "sqlapi.{$name}";
     $sqlView = "CREATE VIEW {$view} as {$q}";
     $this->execQuery($sqlView);
     $arrayWithFields = $this->getMetaData($view);
     // First check. Is the SQL valid
     if ($this->PDOerror) {
         $response['success'] = false;
         $response['message'] = $this->PDOerror;
         $response['code'] = 400;
         $sql = "DROP VIEW {$view}";
         $result = $this->execQuery($sql);
         $this->free($result);
         return $response;
     }
     $postgisVersion = $this->postgisVersion();
     $bits = explode(".", $postgisVersion["version"]);
     if ((int) $bits[1] > 0) {
         $ST_Force2D = "ST_Force2D";
     } else {
         $ST_Force2D = "ST_Force_2D";
     }
     foreach ($arrayWithFields as $key => $arr) {
         if ($arr['type'] == "geometry") {
             $fieldsArr[] = "ST_asGeoJson(ST_Transform({$ST_Force2D}(\"" . $key . "\")," . $this->srs . ")) as \"" . $key . "\"";
         } else {
             $fieldsArr[] = "\"{$key}\"";
         }
     }
     $sql = implode(",", $fieldsArr);
     $sql = "SELECT {$sql} FROM {$view}";
     $result = $this->execQuery($sql);
     // Second check. Catched eg. transform errors
     if ($this->PDOerror) {
         $response['success'] = false;
         $response['message'] = $this->PDOerror;
         $response['code'] = 400;
         $sql = "DROP VIEW {$view}";
         $result = $this->execQuery($sql);
         $this->free($result);
         return $response;
     }
     $i = 0;
     $json = "";
     $ch = curl_init(\app\conf\App::$param['esHost'] . ":9200/_bulk");
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_HEADER, 0);
     while ($row = $this->fetchRow($result, "assoc")) {
         $arr = array();
         foreach ($row as $key => $value) {
             if ($arrayWithFields[$key]['type'] == "geometry") {
                 $geometries[] = json_decode($row[$key]);
             } else {
                 $arr = $this->array_push_assoc($arr, $key, $value);
             }
         }
         if (sizeof($geometries) > 1) {
             $features = array("geometry" => array("type" => "GeometryCollection", "geometries" => $geometries), "type" => "Feature", "properties" => $arr);
         }
         if (sizeof($geometries) == 1) {
             $features = array("geometry" => $geometries[0], "type" => "Feature", "properties" => $arr);
         }
         if (sizeof($geometries) == 0) {
             $features = array("type" => "Feature", "properties" => $arr);
         }
         unset($geometries);
         $json .= json_encode(array("index" => array("_index" => $index, "_type" => $type, "_id" => $arr[$id])));
         $json .= "\n";
         $json .= json_encode($features);
         $json .= "\n";
         if (is_int($i / 1000)) {
             curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
             $buffer = curl_exec($ch);
             $obj = json_decode($buffer, true);
             if (isset($obj["errors"]) && $obj["errors"] == true) {
                 $errors = true;
                 $errors_in = array_merge($errors_in, $this->checkForErrors($obj));
             }
             $json = "";
         }
         $i++;
     }
     // Index the last bulk
     curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
     $buffer = curl_exec($ch);
     $obj = json_decode($buffer, true);
     if (isset($obj["errors"]) && $obj["errors"] == true) {
         $errors = true;
         $errors_in = array_merge($errors_in, $this->checkForErrors($obj));
     }
     curl_close($ch);
     if ($errors) {
         \app\inc\Session::createLogEs($errors_in);
     }
     $response['success'] = true;
     $response['errors'] = $errors;
     $response['errors_in'] = $errors_in;
     $response['message'] = "Indexed {$i} documents";
     $this->free($result);
     $sql = "DROP VIEW {$view}";
     $result = $this->execQuery($sql);
     $this->free($result);
     return $response;
 }
Example #6
0
 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;
 }
Example #7
0
 public function get_log()
 {
     $response['data'] = \app\inc\Session::getLog();
     $response['success'] = true;
     return $response;
 }
Example #8
0
 public function getXml($schema)
 {
     $hostName = \app\conf\App::$param['host'];
     $postgisdb = $this->postgisdb;
     $postgisschema = $schema;
     $tables = new \app\models\Layer();
     $meta = $tables->getAll(false, \app\inc\Session::isAuth());
     $rows = $meta['data'];
     foreach ($rows as $arr) {
         if ($arr['baselayer'] == "t") {
             $baseLayer = $arr['f_table_schema'] . "." . $arr['f_table_name'];
         }
         if ($schema == $arr['f_table_schema']) {
             if ($arr['f_table_title']) {
                 $titles[$arr['f_table_name']] = $arr['f_table_title'];
             } else {
                 $titles[$arr['f_table_name']] = $arr['f_table_name'];
             }
             if ($arr['layergroup']) {
                 $groups[$arr['f_table_name']] = $arr['layergroup'];
             } else {
                 $groups[$arr['f_table_name']] = "Default group";
             }
         }
     }
     $xml .= "<BaseMap>\n";
     $xml .= "\n\t<BaseMapSource>\n\t   <Label>Base Map</Label>\n        <Extent srs='EPSG:3857'>\n          <LowerCorner>-180 -90</LowerCorner>\n          <UpperCorner>180 90</UpperCorner>\n        </Extent>\n        <DefaultBaseMap/>\n\t</BaseMapSource>";
     if ($baseLayer) {
         $xml .= "<BaseMapSource>\n\t    <Label>{$baseLayer}</Label>\n\t    <Extent srs='EPSG:3857'>\n\t\t<LowerCorner>-20037508.34 -20037508.34</LowerCorner>\n\t\t<UpperCorner>20037508.34 20037508.34</UpperCorner>\n\t    </Extent>";
         $xml .= "\t\t<WMSServer src='{$hostName}/wms/{$postgisdb}/{$postgisschema}/'>\n";
         $xml .= "\t\t\t<Layer id='{$baseLayer}'></Layer>\n";
         $xml .= "\t\t</WMSServer>";
         $xml .= "</BaseMapSource>";
     }
     foreach ($rows as $row) {
         if ($schema == $row['f_table_schema']) {
             //$table = new \app\models\Table("{$row['f_table_schema']}.{$row['f_table_name']}");
             $xml .= "<BaseMapSource supplemental='true'>\n";
             if ($row['f_table_title']) {
                 $xml .= "\t<Label>{$row['f_table_title']}</Label>\n";
             } else {
                 $xml .= "\t<Label>{$row['f_table_name']}</Label>\n";
             }
             $xml .= "<Extent srs='EPSG:3857'>\n\t\t<LowerCorner>-20037508.34 -20037508.34</LowerCorner>\n\t\t<UpperCorner>20037508.34 20037508.34</UpperCorner>\n\t    </Extent>";
             $xml .= "\t\t<WMSServer src='{$hostName}/wms/{$postgisdb}/{$postgisschema}/'>\n";
             if ($baseLayer) {
                 $xml .= "\t\t\t<Layer id='{$baseLayer}'></Layer>\n";
             }
             $xml .= "\t\t\t<Layer id='{$postgisschema}.{$row['f_table_name']}'></Layer>\n";
             $xml .= "\t\t</WMSServer>";
             $xml .= "</BaseMapSource>\n";
         }
     }
     /* End of writing WMS as baselayers */
     $xml .= "</BaseMap>\n";
     /* Start of writing WFS as overlays */
     $xml .= "<Overlays>\n";
     foreach ($rows as $row) {
         if ($schema == $row['f_table_schema'] && !$row['wmssource']) {
             $table = new \app\models\Table("{$row['f_table_schema']}.{$row['f_table_name']}");
             switch ($row['type']) {
                 case "POINT":
                     $geoType = "point";
                     break;
                 case "LINESTRING":
                     $geoType = "line";
                     break;
                 case "POLYGON":
                     $geoType = "polygon";
                     break;
                 case "MULTIPOINT":
                     $geoType = "multipoint";
                     break;
                 case "MULTILINESTRING":
                     $geoType = "multiline";
                     break;
                 case "MULTIPOLYGON":
                     $geoType = "multipolygon";
                     break;
             }
             $xml .= "<DataLayer editable='true' canAdd='true' canDelete='true'>\n";
             $xml .= "\t<DataSource>\n";
             $xml .= "\t\t<WFSLayer src='{$hostName}/wfs/{$postgisdb}/{$postgisschema}/4326' shapeType='{$geoType}' typeName='{$postgisdb}:{$row['f_table_name']}'/>\n";
             $xml .= "\t</DataSource>\n";
             $xml .= "\t<GeometryField property='{$row['f_geometry_column']}'/>\n";
             $xml .= "\t<Form>\n";
             $cartomobileArr = (array) json_decode($row['cartomobile']);
             foreach ($table->metaData as $key => $value) {
                 if ($value['type'] != "geometry" && $key != $table->primeryKey['attname'] && $cartomobileArr[$key]->available == 1) {
                     $xml .= "\t\t<FormField property='{$key}'>\n";
                     $xml .= "\t\t\t<Label>{$key}</Label>\n";
                     if (!$cartomobileArr[$key]->cartomobiletype) {
                         switch ($table->metaData[$key]['type']) {
                             case "int":
                                 $type = "Number";
                                 break;
                             case "number":
                                 $type = "Number";
                                 break;
                             case "string":
                                 $type = "SingleText";
                                 break;
                             case "text":
                                 $type = "TextBox";
                                 break;
                         }
                     } elseif ($cartomobileArr[$key]->cartomobiletype == "PictureURL") {
                         $type = "SingleText";
                     } else {
                         $type = $cartomobileArr[$key]->cartomobiletype;
                     }
                     $xml .= "\t\t\t<{$type} ";
                     switch ($type) {
                         case "ChoiceList":
                             $xml .= ">\n";
                             $arr = (array) json_decode($cartomobileArr[$key]->properties);
                             foreach ($arr as $key => $choice) {
                                 $xml .= "\t\t\t\t<Choice value='{$key}'>{$choice}</Choice>\n";
                             }
                             $xml .= "\t\t\t</ChoiceList>\n";
                             break;
                         case "Picture":
                             $arr = (array) json_decode($cartomobileArr[$key]->properties);
                             foreach ($arr as $key => $value) {
                                 $xml .= "{$key}='{$value}' ";
                             }
                             $xml .= "/>\n";
                             break;
                         default:
                             $xml .= "/>\n";
                             break;
                     }
                     $xml .= "\t\t</FormField>\n";
                 }
             }
             $xml .= "\t</Form>\n";
             //$xml.="\t<PinField property='".$table->primeryKey['attname']."'/>\n";
             if ($row['f_table_title']) {
                 $xml .= "\t<Label>{$row['f_table_title']}</Label>\n";
             } else {
                 $xml .= "\t<Label>{$row['f_table_name']}</Label>\n";
             }
             $xml .= "</DataLayer>\n";
         }
     }
     $xml .= "</Overlays>\n";
     /* End of writing WFS as overlays */
     return $xml;
 }