Skip to content

Commit

Permalink
Allow define raster table schema name
Browse files Browse the repository at this point in the history
Since allowing uppercase in raster table name, it is necessary to allow define raster table schema name to do not modify the profile.
  • Loading branch information
rldhont committed Nov 19, 2024
1 parent b443ad1 commit 105320b
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 62 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ with some extra keywords : backend, tests, test, translation, funders, important

### Changed

* Allow define raster table schema name

## 0.5.5 - 2024-10-24

### Fixed
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Go to the configuration part.
* Modules need to be enabled in Lizmap by editing the configuration file `lizmap/var/config`.


For Lizmap 3.4 or Lizmap 3.5, add in the file `lizmap/var/config/localconfig.ini.php`, in the `[module]` section,
For Lizmap 3.4 or Lizmap 3.5, add in the file `lizmap/var/config/localconfig.ini.php`, in the `[module]` section,
the reference to these 2 modules. Do not remove references to other modules in this section.

```ini
Expand Down Expand Up @@ -83,7 +83,7 @@ lizmap/install/set_rights.sh

## Configuration

It's necessary to go in the administration panel of Lizmap Web Client to set up the module.
It's necessary to go in the administration panel of Lizmap Web Client to set up the module.

![Administration panel](altiProfilAdmin.png)

Expand All @@ -95,6 +95,7 @@ altisource=Source of data

;if database
altiProfileProvider=database
altiProfileSchema=dem_schema
altiProfileTable=dem_table
srid=3957
; profilUnit = PERCENT or DEGREES - unit for the profil
Expand Down Expand Up @@ -126,7 +127,7 @@ altiProfileProvider=ign
## Override the configuration for a single project

For every project published in Lizmap, for instance `my_project.qgs`, you can add a new file with extension `.alti` at the end of the file.
In our example, it would be `my_project.qgs.alti`.
In our example, it would be `my_project.qgs.alti`.

This file allows you to override some settings.

Expand All @@ -135,6 +136,7 @@ For instance
```ini
[altiProfil]
altisource="DEM Paris high-resolution"
altiProfileSchema=srtm
altiProfileTable=srtm_paris_high_resolution
srid=3857
```
Expand Down
1 change: 1 addition & 0 deletions altiProfil/install/config/altiProfil.ini.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ altisource=Source des données

;si cas database
;altiProfileProvider=database
altiProfileSchema=dem_schema
altiProfileTable=dem_table
srid=3957
; profilUnit = PERCENT or DEGREES - choix de l'unité de calcul du profil
Expand Down
76 changes: 73 additions & 3 deletions altiProfil/lib/AltiConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function __construct()
}
$defaultValues = array(
'altisource' => '',
'altiProfileSchema' => '',
'altiProfileTable' => '',
'altiProfileProvider'=>'ign',
'ignServiceUrl' => 'https://data.geopf.fr/altimetrie/1.0/calcul',
Expand All @@ -26,9 +27,42 @@ function __construct()

);
$values = parse_ini_file($altiProfilConfigFile, true, INI_SCANNER_TYPED);
$this->parameters = array_merge($defaultValues, $values['altiProfil']);
if ($values && array_key_exists('altiProfil', $values)) {
$this->parameters = array_merge($defaultValues, $values['altiProfil']);
} else {
$this->parameters = $defaultValues;
}
}

public function setProjectConfig($repository, $project)
{
$p = \lizmap::getProject($repository.'~'.$project);
if (!$p) {
return false;
}

$alti_config_file = $p->getQgisPath() . '.alti';
if (!file_exists($alti_config_file)) {
return false;
}

$values = parse_ini_file($alti_config_file, true, INI_SCANNER_TYPED);
if (!$values || !array_key_exists('altiProfil', $config)) {
return false;
}

$this->parameters = array_merge($this->parameters, $values['altiProfil']);
return true;
}

public function getValue($key)
{
if (array_key_exists($key, $this->parameters)) {
return $this->parameters[$key];
}

return null;
}

public function getProvider()
{
Expand All @@ -55,14 +89,50 @@ public function getSrid()
return $this->parameters['srid'];
}

public function getAltiResolution()
{
return $this->parameters['altiresolution'];
}

public function getAltiProfileSchema()
{
return $this->parameters['altiProfileSchema'];
}

public function getAltiProfileTable()
{
return $this->parameters['altiProfileTable'];
}

public function getAltiResolution()
public function quotedSchemaTableName()
{
return $this->parameters['altiresolution'];
if ($this->parameters['altiProfileSchema'] != '') {
return sprintf(
'"%1$s"."%2$s"',
$this->parameters['altiProfileSchema'],
$this->parameters['altiProfileTable']
);
} else {
return sprintf(
'"%1$s"',
$this->parameters['altiProfileTable']
);
}
}

/**
* Check connection
*/
public function checkConnection() {
try{
$sql = sprintf('SELECT rast FROM %1$s limit 0', $this->quotedSchemaTableName());
$cnx = \jDb::getConnection( 'altiProfil' );
$qResult = $cnx->query( $sql);
return true;
} catch (\Exception $e) {
jLog::log("AltiProfil Admin :: ".$e->getMessage());
}
return false;
}

/**
Expand Down
46 changes: 13 additions & 33 deletions altiProfil/lib/AltiServicesFromDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
Class AltiServicesFromDB {

protected $Srid = "";
protected $AltiProfileTable = "";
protected $Altisource = "";
protected $profilUnit = "";
protected $repository = Null;
Expand All @@ -19,35 +18,16 @@ function __construct(\AltiProfil\AltiConfig $altiConfig, $repository, $project)
{
$this->config = $altiConfig;
$this->Srid = $altiConfig->getSrid();
$this->AltiProfileTable = $altiConfig->getAltiProfileTable();
$this->Altisource = $altiConfig->getAltisource();
$this->profilUnit = $altiConfig->getProfilUnit();
$this->AltiResolution = $altiConfig->getAltiResolution();

// Get project config: override table and source per project
$this->repository = $repository;
$this->project = $project;
$p = \lizmap::getProject($repository.'~'.$project);
if( $p ){
$alti_config_file = $p->getQgisPath() . '.alti';
if (file_exists($alti_config_file)) {
$config = parse_ini_file($alti_config_file, True);
if ($config and array_key_exists('altiProfil', $config)) {
if (array_key_exists('srid', $config['altiProfil'])) {
$this->Srid = $config['altiProfil']['srid'];
}
if (array_key_exists('altiProfileTable', $config['altiProfil'])) {
$this->AltiProfileTable = $config['altiProfil']['altiProfileTable'];
}
if (array_key_exists('altisource', $config['altiProfil'])) {
$this->Altisource = $config['altiProfil']['altisource'];
}
}
}
}
$this->config->setProjectConfig($repository, $project);
}


/**
* Get alti from one point based on database
**/
Expand All @@ -63,16 +43,16 @@ private function queryAlti($lon, $lat) {

$sql = sprintf('
SELECT ST_Value(
"%1$s".rast,
r.rast,
ST_Transform(ST_SetSRID(ST_MakePoint(%2$f,%3$f),4326),%4$s)
) as z
FROM "%1$s"
FROM %1$s r
WHERE ST_Intersects(
"%1$s".rast,
r.rast,
ST_Transform(ST_SetSRID(ST_MakePoint(%2$f,%3$f),4326),%4$s)
)',
$this->AltiProfileTable,
$this->config->quotedSchemaTableName(),
$lon,
$lat,
$this->Srid
Expand Down Expand Up @@ -134,10 +114,10 @@ protected function queryProfil($p1Lon, $p1Lat, $p2Lon, $p2Lat){
-- Get DEM elevation for each
SELECT
p.geom AS geom,
ST_Value("%1$s".rast, 1, p.geom) AS val,
ST_Value(r.rast, 1, p.geom) AS val,
resolution
FROM "%1$s", points2d p
WHERE ST_Intersects("%1$s".rast, p.geom)
FROM %1$s r, points2d p
WHERE ST_Intersects(r.rast, p.geom)
),
-- Instantiate 3D points
points3d AS (
Expand All @@ -157,7 +137,7 @@ protected function queryProfil($p1Lon, $p1Lat, $p2Lon, $p2Lat){
)
-- Build 3D line from 3D points
SELECT loc AS x, ST_Z(geom) as y, ST_X(geom) as lon, ST_Y(geom) as lat, resolution FROM xz',
$this->AltiProfileTable,
$this->config->quotedSchemaTableName(),
$p1Lon, $p1Lat,
$this->Srid,
$p2Lon, $p2Lat,
Expand Down Expand Up @@ -189,9 +169,9 @@ protected function queryProfil($p1Lon, $p1Lat, $p2Lon, $p2Lat){
AS geom
), RasterCells AS (
-- Intersect the line with the DEM
SELECT ST_Clip("%1$s".rast, line.geom, -9999, TRUE) as rast
FROM "%1$s", line
WHERE ST_Intersects("%1$s".rast, line.geom)
SELECT ST_Clip(r.rast, line.geom, -9999, TRUE) as rast
FROM %1$s r, line
WHERE ST_Intersects(r.rast, line.geom)
), rasterSlopStat AS (
-- Compute the slope and the statistics
Select (ST_SummaryStatsAgg(ST_Slope(rast, 1, \'32BF\', \'%7$s\', 1.0), 1, TRUE, 1)).*
Expand All @@ -204,7 +184,7 @@ protected function queryProfil($p1Lon, $p1Lat, $p2Lon, $p2Lat){
FROM rasterSlopStat
',
$this->AltiProfileTable,
$this->config->quotedSchemaTableName(),
$p1Lon, $p1Lat,
$this->Srid,
$p2Lon, $p2Lat,
Expand Down
40 changes: 19 additions & 21 deletions altiProfilAdmin/controllers/config.classic.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@ class configCtrl extends jController {

function __construct( $request ) {
parent::__construct( $request );
if (method_exists('jApp', 'varConfigPath')) {
// LWC >= 3.6
$monfichier = \jApp::varConfigPath('altiProfil.ini.php');
$this->ini = new \Jelix\IniFile\IniModifier($monfichier);
} else {
$monfichier = \jApp::configPath('altiProfil.ini.php');
$this->ini = new jIniFileModifier($monfichier);
}
$this->config = new \AltiProfil\AltiConfig();
}

/**
Expand All @@ -47,20 +40,15 @@ function index() {
// Set form data values
foreach ( $form->getControls() as $ctrl ) {
if ( $ctrl->type != 'submit' ){
$val = $this->ini->getValue( $ctrl->ref, 'altiProfil' );
$val = $this->config->getValue( $ctrl->ref );
$form->setData( $ctrl->ref, $val );
}
}
$tableName = $form->getData('altiProfileTable');
if ($form->getData('altiProfileProvider') == 'database' && !empty($tableName)) {
try{
$sql = 'SELECT rast FROM "'.$tableName.'" limit 0';
$cnx = \jDb::getConnection( 'altiProfil' );
$qResult = $cnx->query( $sql);
if ($form->getData('altiProfileProvider') == 'database' && !empty($form->getData('altiProfileTable'))) {
if ($this->config->checkConnection()) {
$form->setData('connection_check', jLocale::get('altiProfilAdmin~admin.form.connection_check.ok'));
} catch (\Exception $e) {
$form->setData('connection_check' , jLocale::get('altiProfilAdmin~admin.form.connection_check.error'));
jLog::log("AltiProfil Admin :: ".$e->getMessage());
} else {
$form->setData('connection_check' , jLocale::get('altiProfilAdmin~admin.form.connection_check.error'));
}
} else {
$form->getControl('connection_check')->deactivate();
Expand Down Expand Up @@ -88,7 +76,7 @@ public function modify(){
// Set form data values
foreach ( $form->getControls() as $ctrl ) {
if ( $ctrl->type != 'submit' ){
$val = $this->ini->getValue( $ctrl->ref, 'altiProfil' );
$val = $this->config->getValue( $ctrl->ref );
$form->setData( $ctrl->ref, $val );
}
}
Expand Down Expand Up @@ -161,14 +149,24 @@ function save(){
return $rep;
}

$ini = null;
if (method_exists('jApp', 'varConfigPath')) {
// LWC >= 3.6
$monfichier = \jApp::varConfigPath('altiProfil.ini.php');
$ini = new \Jelix\IniFile\IniModifier($monfichier);
} else {
$monfichier = \jApp::configPath('altiProfil.ini.php');
$ini = new jIniFileModifier($monfichier);
}

// Save the data
foreach ( $form->getControls() as $ctrl ) {
if ( $ctrl->type != 'submit' ){
$val = $form->getData( $ctrl->ref );
$this->ini->setValue( $ctrl->ref, $val, 'altiProfil' );
$ini->setValue( $ctrl->ref, $val, 'altiProfil' );
}
}
$this->ini->save();
$ini->save();

// Redirect to the validation page
$rep= $this->getResponse("redirect");
Expand Down
4 changes: 4 additions & 0 deletions altiProfilAdmin/forms/config.form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<group ref="database">
<label locale="altiProfilAdmin~admin.form.group.database.label"/>

<input ref="altiProfileSchema" type="string" defaultvalue="dem_schema" pattern='/^[a-zA-Z0-9_]+$/'>
<label locale="altiProfilAdmin~admin.form.altiProfileSchema.label" />
</input>

<input ref="altiProfileTable" type="string" defaultvalue="dem_table" pattern='/^[a-zA-Z0-9_]+$/'>
<label locale="altiProfilAdmin~admin.form.altiProfileTable.label" />
</input>
Expand Down
1 change: 1 addition & 0 deletions altiProfilAdmin/locales/en_US/admin.UTF-8.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ form.dock.label=Dock panel
form.dock.dock.label=Dock
form.dock.minidock.label=Mini-dock
form.dock.rightdock.label=Right-dock
form.altiProfileSchema.label=Database schema
form.altiProfileTable.label=Database table
form.srid.label=SRID
form.ignServiceUrl.label=IGN service URL
Expand Down
1 change: 1 addition & 0 deletions altiProfilAdmin/locales/fr_FR/admin.UTF-8.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ form.dock.label=Panneau
form.dock.dock.label=Dock
form.dock.minidock.label=Mini-dock
form.dock.rightdock.label=Right-dock
form.altiProfileSchema.label=Schéma de la base
form.altiProfileTable.label=Table de la base
form.srid.label=SRID
form.ignServiceUrl.label=URL du service IGN
Expand Down
1 change: 1 addition & 0 deletions altiProfilAdmin/locales/ro_RO/admin.UTF-8.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ form.dock.label=Panoul de andocare
form.dock.dock.label=Doc
form.dock.minidock.label=Mini-doc
form.dock.rightdock.label=Dock-dreapta
form.altiProfileSchema.label=Schema bazei de date
form.altiProfileTable.label=Tabelul bazei de date
form.srid.label=SRID
form.ignServiceUrl.label=URL serviciu IGN
Expand Down
Loading

0 comments on commit 105320b

Please sign in to comment.