ID=$id;
$uploader = new FileUploader('WMS_LAYERS_COLUMN', $record);
$errMsg="";
if (!$uploader->setConfig($folderConf, $errMsg)) throw new HttpException("Błąd danych konfiguracyjnych" . '
' . "\n" . $errMsg, 404);
$uploader->findFolder();
return $uploader->getDestLocalPath(true);
}
private function getMainDirectory() {
$folderConf = FoldersConfig::getAll('WMS_LAYERS_COLUMN');
if (!isset($folderConf['mount_point'])) throw new HttpException("Błąd danych konfiguracyjnych" . '
' . "\n" . $errMsg, 404);
return $folderConf['mount_point'];
}
public function defaultAction() {
SE_Layout::gora();
SE_Layout::menu();
$ID_WMS_LAYERS=V::get('ID_WMS_LAYERS',0,$_GET,'int');
if ($ID_WMS_LAYERS<=0) die ('ID ERROR');
try {
$destFolderPath = $this->getDirectory($ID_WMS_LAYERS);
$files=array();
foreach (glob($destFolderPath."/*.tif",GLOB_BRACE) as $node) {
if (is_file($node)) $files[]=basename($node);
}
if (count($files)==0) throw new Exception('Nie znaleziono żadnego pliku tif!');
if (count($files)>1) throw new Exception('Znaleziono zbyt dużo plików tif!');
$validateGeoTifJson=shell_exec("/opt/local/bin/gdalinfo -json ".$destFolderPath."/*".$files[0]." 2>/dev/null");
$validateGeoTif=json_decode($validateGeoTifJson);
if (!$validateGeoTif) throw new Exception('Znaleziony plik '.$files[0].' nie jest poprawnym plikiem tif!');
if (!$validateGeoTif->coordinateSystem->wkt) throw new Exception('Znaleziony plik '.$files[0].' nie jest poprawnym plikiem GeoTif!');
$sqlObj = new stdClass();
$sqlObj->ID = $ID_WMS_LAYERS;
$sqlObj->FILE_STATUS='TO_GENERATE';
$sqlObj->FILE_STATUS_INFO='Plik oczekuje na przetworzenie';
$sqlObj->FILE_NAME=$files[0];
$affected = DB::getDB()->UPDATE_OBJ('WMS_LAYERS', $sqlObj);
if ($affected) SE_Layout::alert('success','Oznaczono plik '.$files[0].' do przetworzenia.');
else SE_Layout::alert('warning','Plik '.$files[0].' został już wcześniej oznaczony do przetworzenia.');
} catch (Exception $e) {
SE_Layout::alert('danger',$e->getMessage());
}
?>
fetchall("select count(*) as cnt from CRM_CONFIG where CONF_KEY='module_WMS' and CONF_VAL='on'");
if (!$moduleActive[0]['cnt']) throw new Exception("Module WMS disabled/not installed.");
### Znalezienie nowo przetworzonych rastrow i ew. oznaczenie map do wygenerowania
$wmsDirLocation=$this->getMainDirectory();
if (!file_exists($wmsDirLocation.'/.maps/single')) mkdir($wmsDirLocation.'/.maps/single',0755,true);
if (!file_exists($wmsDirLocation.'/.maps/single')) throw new Exception('Error during creating maps directory.');
$mapSchema='MAP
NAME "__NAME__"
STATUS ON
SIZE 400 300
EXTENT __EXTENT__
UNITS METERS
IMAGECOLOR 255 255 0
IMAGETYPE PNG
WEB
METADATA
WMS_TITLE "__NAME__"
WMS_ENABLE_REQUEST "*"
WMS_ONLINERESOURCE "https://__SERVER_NAME__/wms/single/__ID__"
WMS_SRS "EPSG:__EPSG__"
END
END
PROJECTION
"init=epsg:__EPSG__"
END
LAYER
NAME "__NAME__"
STATUS OFF
TYPE RASTER
DATA "__FILE__"
EXTENT __EXTENT__
PROJECTION
"init=epsg:__EPSG__"
END
METADATA
WMS_TITLE "__NAME__"
WMS_SRS "EPSG:__EPSG__"
WMS_EXTENT "__EXTENT__"
END
END
END
';
$search=array('__NAME__','__EXTENT__','__SERVER_NAME__','__ID__','__EPSG__','__FILE__');
$layers=DB::getPDO()->fetchall("select ID,LAYER_NAME,ID_WMS_MAPS,A_STATUS,FILE_NAME from WMS_LAYERS where FILE_STATUS='IN_PROGRESS'");
$completed=Array();
foreach ($layers as $layer) {
if (!file_exists('/tmp/generate_wms-'.$layer['ID'].'.lock')) {
unlink('/tmp/generate_wms-'.$layer['ID'].'.sh');
$destFolderPath = $this->getDirectory($layer['ID']);
if (file_exists($destFolderPath.'/.wms/.completed')) {
if (!file_exists($destFolderPath.'/.wms/.data')) {
DB::getDB()->UPDATE_OBJ('WMS_LAYERS',(object) array('ID'=>$layer['ID'],'FILE_STATUS'=>'TO_GENERATE','FILE_STATUS_INFO'=>'Wystąpił błąd prztwarzania, ale nastąpi kolejna próba jego przetworzenia'));
} else {
$data_arr=explode('|',file_get_contents($destFolderPath.'/.wms/.data'));
unlink($destFolderPath.'/.wms/.data');
$geom=$data_arr[0];
$extent=$data_arr[1];
DB::getDB()->UPDATE_OBJ('WMS_LAYERS',(object) array('ID'=>$layer['ID'],'FILE_STATUS'=>'GENERATED','FILE_STATUS_INFO'=>'Plik przetworzony poprawnie','the_geom'=>"GeomFromText('{$geom}')"));
unlink($destFolderPath.'/.wms/.completed');
if ($layer['A_STATUS']=='NORMAL') $completed[$layer['ID_WMS_MAPS']]=1;
}
} else {
DB::getDB()->UPDATE_OBJ('WMS_LAYERS',(object) array('ID'=>$layer['ID'],'FILE_STATUS'=>'TO_GENERATE','FILE_STATUS_INFO'=>'Wystąpił błąd prztwarzania, ale nastąpi kolejna próba jego przetworzenia'));
}
}
}
if ($completed) {
foreach ($completed as $ID_WMS_MAPS => $x) {
DB::getDB()->UPDATE_OBJ('WMS_MAPS',(object) array('ID'=>$ID_WMS_MAPS,'MAP_STATUS'=>'TO_GENERATE','MAP_STATUS_INFO'=>'Mapa oczekuje na przetworzenie'));
}
}
$bashSchema='
#rm -rf __PATH__/.wms
#mkdir __PATH__/.wms
lock="/tmp/generate_wms-__ID__.lock"
touch ${lock}
gdaltindex -t_srs epsg:4326 /tmp/generate_wms-__ID__-shp_wgs84.shp __PATH__/__FILE__ >/dev/null 2>&1
ogr2ogr -f CSV /tmp/generate_wms-__ID__-shp_wgs84.csv /tmp/generate_wms-__ID__-shp_wgs84.shp -lco GEOMETRY=AS_WKT
wgs84=`cat /tmp/generate_wms-__ID__-shp_wgs84.csv |tr -d "\n" | cut -d "\"" -f 2`
gdaltindex /tmp/generate_wms-__ID__-shp_orig.shp __PATH__/__FILE__ >/dev/null 2>&1
extent=`ogrinfo -al -so /tmp/generate_wms-__ID__-shp_orig.shp | grep Extent | sed "s/[(),\-]//g;s/ / /g;s/Extent: //g"`
rm -f /tmp/generate_wms-__ID__-shp_*
echo "${wgs84}|${extent}|" > __PATH__/.wms/.data
#/opt/local/bin/gdal_translate -of GTiff -co "TILED=YES" -co "COMPRESS=DEFLATE" -co "ZLEVEL=9" -co "BIGTIFF=YES" __PATH__/__FILE__ __PATH__/.wms/__FILE__ && /opt/local/bin/gdaladdo -r average __PATH__/.wms/__FILE__ 2 4 8 16 32 64 128
rm -f ${lock}
touch __PATH__/.wms/.completed
echo "Successfully completed."
';
$search=array('__ID__','__PATH__','__FILE__');
$layers=DB::getPDO()->fetchall("select ID,FILE_NAME from WMS_LAYERS where FILE_STATUS='TO_GENERATE'");
foreach ($layers as $layer) {
$destFolderPath = $this->getDirectory($layer['ID']);
if(!is_file($destFolderPath.'/'.$layer['FILE_NAME'])) {
DB::getDB()->UPDATE_OBJ('WMS_LAYERS',(object) array('ID'=>$layer['ID'],'FILE_STATUS'=>'ERROR','FILE_STATUS_INFO'=>'Wystąpił błąd pliku tif'));
throw new Exception("Błąd pliku: ".$layer['FILE_NAME']);
}
$xml=@simplexml_load_string(shell_exec('gdalsrsinfo -o xml '.$destFolderPath.'/'.$layer['FILE_NAME']));
$epsg=$xml->{'gml:srsID'}->{'gml:name'};
if (!$epsg) {
DB::getDB()->UPDATE_OBJ('WMS_LAYERS',(object) array('ID'=>$layer['ID'],'FILE_STATUS'=>'ERROR','FILE_STATUS_INFO'=>'Wystąpił błąd pliku tif - brak informacji o układzie współrzędnych'));
throw new Exception("Błąd pliku: ".$layer['FILE_NAME']);
}
DB::getDB()->UPDATE_OBJ('WMS_LAYERS',(object) array('ID'=>$layer['ID'],'FILE_STATUS'=>'IN_PROGRESS','FILE_STATUS_INFO'=>'Plik w trakcie przetwarzania','EPSG'=>$epsg));
$replace = array($layer['ID'],$destFolderPath,$layer['FILE_NAME']);
$bash = str_replace($search,$replace,$bashSchema);
file_put_contents('/tmp/generate_wms-'.$layer['ID'].'.sh',$bash);
shell_exec('nohup /bin/sh /tmp/generate_wms-'.$layer['ID'].'.sh > /tmp/generate_wms-'.$layer['ID'].'.log 2>&1 & echo $!');
}
} catch (Exception $e) {
echo $e->getMessage()."\n";
}
}
public function doGenerateAction() {
$this->doGenerate();
// echo "OK\n";
}
public function reinstallAction() {
$this->reinstall();
die('OK');
}
public function reinstall() {
$sqlList = array();
//$sqlList['RemoveTable'] = "DROP TABLE IF EXISTS `CRM_CONFIG`";
$sqlList['InstallTable'] = <<has_errors()) {
throw new Exception("DB Errors: " . implode("\n
", $db->get_errors()));
}
foreach ($sqlList as $sqlName => $sql) {
$res = $db->query($sql);
if ($db->has_errors()) {
throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n
", $db->get_errors()));
}
}
}
}