Example #1
0
 function __construct($driverName, $exportExt, $json, $map, $bExportAtt = false, $optDataSourceCreation = NULL, $bSearchSpatialRef = false)
 {
     $bOk = false;
     $msVersion = null;
     // MapServer version
     $groups = null;
     // Layers list that will be exported
     parent::__construct($json);
     OGRRegisterAll();
     // looking for destination driver
     for ($iDriver = 0; $iDriver < OGRGetDriverCount() && !$bOk; $iDriver++) {
         if (!strcasecmp(OGR_DR_GetName(OGRGetDriver($iDriver)), $driverName)) {
             $this->driverOGRDest = OGRGetDriver($iDriver);
             $bOk = true;
             break;
         }
     }
     if ($bOk) {
         // is it possible to create new data source with current driver
         if (!OGR_Dr_TestCapability($this->driverOGRDest, ODrCCreateDataSource)) {
             $this->driverOGRDest = null;
             $bOk = false;
             return;
         }
     }
     if ($bOk) {
         $msVersion = $_SESSION['MS_VERSION'];
         $groups = (array) $this->jsonList[0];
         // Create the destination directory
         @mkdir($this->tempFilePath, 0700);
         // for each layers that will be exported
         foreach ($groups as $grp) {
             $baseDest = null;
             $layerList = array();
             $attNameList = $grp->header;
             $ObjList = $grp->values;
             if (!$ObjList[0][0]->shplink) {
                 continue;
             }
             // if we need to export attributs
             if ($bExportAtt) {
                 include_once '../common/pluginsMapUtils.inc.php';
                 for ($iAtt = 0; $iAtt < count($attNameList); $iAtt++) {
                     if ($attNameList[$iAtt] == '@') {
                         array_splice($attNameList, $iAtt, 1);
                         $iAtt--;
                         continue;
                     }
                     // remove accents and spaces:
                     $newAttName = PluginsMapUtils::decodeMapFileVal($map, $attNameList[$iAtt], 'UTF-8');
                     $newAttName = str_replace($this->bad, $this->good, $attNameList[$iAtt]);
                     if (is_numeric($newAttName[0])) {
                         $newAttName = 'z' . $newAttName;
                     }
                     $attNameList[$iAtt] = $newAttName;
                 }
             }
             // for each objects that we want to export
             foreach ($ObjList as $Obj) {
                 $layerSrc = null;
                 // Source layer
                 $layerDest = null;
                 // Destination layer
                 $baseDest = null;
                 // Destination DataBase
                 $geoType = 0;
                 $shpLink = $Obj[0]->shplink;
                 $layerName = $shpLink[0];
                 $IdObj = $shpLink[1];
                 // if it's the first time we found current layer
                 if (!isset($layerList[$layerName])) {
                     // getting mapserver layer
                     $layerSrc = $map->getLayerByName($layerName);
                     // use layers with complex queries that are too long to select results
                     $newdata = $layerSrc->getMetaData('PM_RESULT_DATASUBSTITION');
                     if ($newdata != '') {
                         $layerSrc->set('data', $newdata);
                     }
                     // create destination data base
                     $output = $this->tempFilePath . "\\{$layerName}.{$exportExt}";
                     $output = str_replace('/', '\\', $output);
                     $baseDest = OGR_Dr_CreateDataSource($this->driverOGRDest, $output, $optDataSourceCreation);
                     if (!$baseDest) {
                         continue;
                     }
                     // is it possible to create new layers in current data source ?
                     if (!OGR_DS_TestCapability($baseDest, ODsCCreateLayer)) {
                         if ($baseDest) {
                             OGR_DS_Destroy($baseDest);
                         }
                         continue;
                     }
                     // create new layer in the destination data base
                     $geoType = $this->typeList[$layerSrc->type];
                     $layerDest = $this->createNewLayer($map, $baseDest, $layerName, $geoType, $bSearchSpatialRef);
                     // add new attribut in destination layer
                     if ($bExportAtt) {
                         $idAtt = 0;
                         foreach ($attNameList as $attName) {
                             $att = OGR_Fld_Create($attName, OFTString);
                             OGR_L_CreateField($layerDest, $att, $idAtt);
                             $idAtt++;
                         }
                     }
                     // saving all in layerList
                     $layerList[$layerName] = array();
                     $layerList[$layerName][] = $layerSrc;
                     $layerList[$layerName][] = $baseDest;
                     $layerList[$layerName][] = $layerDest;
                     $layerList[$layerName][] = $geoType;
                     // add file to files list will be exorted
                     $this->addFileToFileList($output);
                 } else {
                     $layerSrc = $layerList[$layerName][0];
                     $baseDest = $layerList[$layerName][1];
                     $layerDest = $layerList[$layerName][2];
                     $geoType = $layerList[$layerName][3];
                 }
                 // gettint shape object
                 $srcShp = PMCommon::resultGetShape($msVersion, $layerSrc, null, $IdObj, -1);
                 // export geometry of the object in WKT
                 $geoWKT = $srcShp->toWkt();
                 // create new geometry OGR object from WKT geometry
                 $geoOGR = $this->createNewOGRGeometry($geoWKT);
                 // create new data line on destination layer
                 $layerDestDefn = OGR_L_GetLayerDefn($layerDest);
                 $Data = OGR_F_Create($layerDestDefn);
                 // add geometry in data line
                 OGR_F_SetGeometry($Data, $geoOGR);
                 // if we need to export attributs
                 if ($bExportAtt) {
                     // add attributs values in data line
                     for ($iAtt = 1; $iAtt < count($Obj); $iAtt++) {
                         $newAttVal = PluginsMapUtils::decodeLayerVal($map, $layerName, $Obj[$iAtt], 'UTF-8');
                         OGR_F_SetFieldString($Data, $iAtt - 1 + $this->nbFieldBase, $newAttVal);
                     }
                 }
                 $this->insertSpecialFields($geoType, $layerDestDefn, $Data);
                 // add data line in destination layer
                 OGR_L_CreateFeature($layerDest, $Data);
                 OGR_F_Destroy($Data);
                 OGR_G_DestroyGeometry($geoOGR);
             }
             foreach ($layerList as $l) {
                 if (isset($l[1]) && $l[1]) {
                     OGR_DS_Destroy($l[1]);
                 }
             }
         }
         // files compression
         $this->zipFiles();
         // remove directory
         rmdir($this->tempFilePath);
     }
 }
Example #2
0
 /**
  * Init function
  */
 function __construct($json, $map)
 {
     parent::__construct($json);
     $msVersion = $_SESSION['MS_VERSION'];
     $grouplist = $_SESSION['grouplist'];
     //pm_logDebug(3, $grouplist, 'grouplist in exportSHP');
     $groups = (array) $this->jsonList[0];
     $fileList = array();
     $valueList = array();
     $dbfFieldList = array();
     $cpyPrjFile = '';
     @mkdir($this->tempFilePath, 0700);
     foreach ($groups as $grp) {
         $layerNameList = array();
         $layerList = array();
         $values = $grp->values;
         if (!$values[0][0]->shplink) {
             continue;
         }
         // headers with invalid characters
         include_once '../common/pluginsMapUtils.inc.php';
         $headerListTmp = $grp->header;
         $headerList = array();
         foreach ($headerListTmp as $n => $h) {
             if ($h != '@') {
                 $newHeader = PluginsMapUtils::decodeMapFileVal($map, $h, 'ISO-8859-1');
                 // remove accents and spaces:
                 // adpated from http://www.lecoindunet.com/zone_php/scripts_utiles/remplacer-les-caracteres-accentues-dune-chaine-en-php-72
                 $bad = array(' ', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'Ð', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', '?', '?', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', '?', '?', 'L', 'l', 'N', 'n', 'N', 'n', 'N', 'n', '?', 'O', 'o', 'O', 'o', 'O', 'o', 'Œ', 'œ', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'Š', 'š', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Ÿ', 'Z', 'z', 'Z', 'z', 'Ž', 'ž', '?', 'ƒ', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', '?', '?', '?', '?', '?', '?');
                 $good = array('_', 'A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
                 $newHeader = str_replace($bad, $good, $newHeader);
                 // upper case
                 $newHeader = strtoupper($newHeader);
                 // if header already exists :'FIELD_' + index
                 if (in_array($newHeader, $headerList)) {
                     $newHeader = "FIELD_{$n}";
                 }
                 $headerList[] = $newHeader;
                 // if '@' not at the beginning
             } else {
                 if ($n != 0) {
                     $headerList = array();
                     break;
                 }
             }
         }
         foreach ($values as $vList) {
             $dbfRow = array();
             foreach ($vList as $n => $v) {
                 if ($shplink = $v->shplink) {
                     //pm_logDebug(3, $shplink, "shplink");
                     $layerName = $shplink[0];
                     $shpIdxTmp = $shplink[1];
                     $shpIdx = preg_replace('/^.*@/', '', $shpIdxTmp);
                     $tileShpIdx = -1;
                     if (!in_array($layerName, $layerNameList)) {
                         $typeList = array(MS_SHP_POINT, MS_SHP_ARC, MS_SHP_POLYGON);
                         $srcLayer = $map->getLayerByName($layerName);
                         // use layers with complex queries that are too long to select results
                         $newdata = $srcLayer->getMetaData('PM_RESULT_DATASUBSTITION');
                         if ($newdata != '') {
                             $srcLayer->set('data', $newdata);
                         }
                         $srcLayerType = $srcLayer->type;
                         //error_log("type: $srcLayerType");
                         $layerNameList[] = $layerName;
                         $layerList[] = $srcLayer;
                         $valueList[$layerName] = array();
                         $srcLayer->open();
                         //pm_logDebug(3, $layerItems);
                         $outShpFname = "{$this->tempFilePath}/{$layerName}";
                         $shpFile = ms_newShapeFileObj($outShpFname, $typeList[$srcLayerType]);
                         $dbfFileName = "{$outShpFname}.dbf";
                         $cpyPrjFile = '';
                         unset($proj);
                         $proj = $srcLayer->getProjection();
                         if (empty($proj)) {
                             $proj = $map->getProjection();
                         }
                         if (!empty($proj)) {
                             $prjFile = dirname(__FILE__) . "\\shp\\";
                             $pos = strpos($proj, 'epsg:');
                             if ($pos !== false) {
                                 $proj = substr($proj, $pos + 5);
                                 $prjFile .= "{$proj}.prj";
                             }
                             if (file_exists($prjFile) && !is_dir($prjFile)) {
                                 $cpyPrjFile = "{$outShpFname}.prj";
                                 copy($prjFile, $cpyPrjFile);
                             }
                         }
                         $fileList[$layerName] = array();
                         $fileList[$layerName][] = "{$outShpFname}.shp";
                         $fileList[$layerName][] = "{$outShpFname}.shx";
                         $fileList[$layerName][] = $dbfFileName;
                         if (file_exists($cpyPrjFile)) {
                             $fileList[$layerName][] = $cpyPrjFile;
                         }
                     }
                     $srcShp = PMCommon::resultGetShape($msVersion, $srcLayer, null, $shpIdx, -1);
                     // changed for compatibility with PG layers and MS >= 5.6
                     $shpFile->addShape($srcShp);
                 } else {
                     if ($headerList) {
                         $hyperlink = $v->hyperlink;
                         if ($hyperlink) {
                             $val = $hyperlink[2];
                         } else {
                             $val = $v;
                         }
                         $fldName = $headerList[$n - 1];
                         $dbfRow[] = utf8_decode($val);
                         if (!isset($dbfFieldList[$layerName])) {
                             $dbfFieldList[$layerName] = array();
                         }
                         if (!isset($dbfFieldList[$layerName][$fldName])) {
                             $dbfFieldList[$layerName][$fldName] = array();
                         }
                         $dbfFieldList[$layerName][$fldName] = $this->getFieldType(trim($val), $dbfFieldList[$layerName][$fldName]);
                     }
                 }
             }
             $valueList[$layerName][] = $dbfRow;
         }
         // some clean up
         //            PMCommon::freeMsObj($shpFile);
         // shapefileObj: changes are committed only when the object is destroyed
         $shpFile->free();
         unset($shpFile);
         foreach ($layerList as $l) {
             $l->close();
         }
     }
     // write dBase files
     foreach ($fileList as $layerName => $files) {
         $dbfFileName = $fileList[$layerName][2];
         // Modified by Thomas RAFFIN (SIRAP)
         // bug export for groups (with many layers)
         $dbfFieldListTmp = $dbfFieldList[$layerName];
         $valueListTmp = $valueList[$layerName];
         $this->writeDbf($dbfFileName, $dbfFieldListTmp, $valueListTmp);
     }
     // Write all files to zip and remove tmp dir
     $this->tempFileLocation .= '.zip';
     $zipFilePath = "{$this->tempFilePath}.zip";
     $filesToZip = array();
     foreach ($fileList as $files) {
         foreach ($files as $file) {
             $filesToZip[] = $file;
         }
     }
     PMCommon::packFilesZip($zipFilePath, $filesToZip, true, true);
     rmdir($this->tempFilePath);
 }
Example #3
0
 /**
  * Calculte map size
  */
 private function calculateSize()
 {
     $width = $this->refMapWidth;
     //$this->internalMap->width;
     $height = $this->refMapHeight;
     //$this->internalMap->height;
     if ($this->autoSize && ($this->decreaseHeight || $this->decreaseWidth)) {
         $minHeight = $this->refMapHeight / 3;
         $minWidth = $this->refMapWidth / 3;
         $stepHeight = $this->decreaseHeight ? $this->stepSize : 0;
         $stepWidth = $this->decreaseWidth ? $this->stepSize : 0;
         $size = PluginsMapUtils::calculateSize($this->internalMap, $this->refMapExtent, $this->refMapHeight, $this->refMapWidth, $minHeight, $minWidth, $stepHeight, $stepWidth);
         $width = $size['width'];
         $height = $size['height'];
     }
     pm_logDebug(4, $this->refMapExtent, 'refMapExtent 3:');
     pm_logDebug(4, "width={$width};height={$height}");
     $this->internalMap->setSize($width, $height);
     $this->internalMap->setExtent($this->refMapExtent->minx, $this->refMapExtent->miny, $this->refMapExtent->maxx, $this->refMapExtent->maxy);
     $this->refMapExtent = $this->internalMap->extent;
     $this->refMapWidth = $width;
     $this->refMapHeight = $height;
     $this->refMapMinX = $this->refMapExtent->minx;
     $this->refMapMinY = $this->refMapExtent->miny;
     $this->refMapMaxX = $this->refMapExtent->maxx;
     $this->refMapMaxY = $this->refMapExtent->maxy;
 }
Example #4
0
         $result[] = $attribute;
     }
     echo "{\"attributes\":" . json_encode($result) . "}";
     // Request = ask for field type
 } else {
     if ($operation == 'getattributetype') {
         $attributeType = '';
         $attributeName = $_REQUEST['attributename'];
         if ($attributeName && $layerName) {
             $msLayer = $map->getLayerByName($layerName);
             if ($msLayer && ($msLayer->connectiontype == MS_POSTGIS || $msLayer->connectiontype == MS_ORACLESPATIAL)) {
                 // get dns string containing : type of database, user name, password, host and database :
                 $dsn = PluginsMapUtils::getDSNConnection($msLayer);
                 if ($dsn) {
                     // data substitution :
                     $data = PluginsMapUtils::getQueryParamsFromDataString($msLayer, false, true);
                     // DB :
                     $edb = new Easy_MDB2();
                     $edb->setDSN($dsn);
                     $edb->start();
                     // Query :
                     $db_table = $data['db_table'];
                     $mdb2FieldType = $edb->getFieldType($db_table, $attributeName);
                     switch ($mdb2FieldType) {
                         case 'integer':
                         case 'decimal':
                         case 'float':
                         case 'boolean':
                             $attributeType = 'N';
                             break;
                         case 'text':
Example #5
0
 /**
  * 
  * Decode object fields values
  * 
  * @param ms MapObj $map mapfile
  * @param string $layerName Layer's name
  * @param string $valIn value to decode
  * @param string $charset string output charset (false -> $_SESSION['defCharset'])
  * 
  * Attention : ne pas utiliser cette fonction si les données viennent de MDB2 (ou PDO ?)
  */
 public static function decodeLayerVal($map, $layerName, $valIn, $charset = false)
 {
     $valOut = $valIn;
     if (!$charset) {
         $charset = $_SESSION['defCharset'];
     }
     if ($layer = $map->getLayerByName($layerName)) {
         // use layer encoding
         if ($layerEncoding = trim($layer->getMetaData('LAYER_ENCODING'))) {
             if ($layerEncoding != $charset) {
                 $valOut = iconv($layerEncoding, $charset, $valIn);
             }
             // use same encoding as mapfile
         } else {
             $valOut = PluginsMapUtils::decodeMapFileVal($map, $valIn);
         }
     }
     return $valOut;
 }