protected function createNewLayer($map, $driver, $layerName, $geoType, $bSearchSpatialRef) { // if layer contains point objects if ($geoType == wkbPoint) { // before creating layer "entities" we create layer "blocks" $layerBlocks = OGR_DS_CreateLayer($driver, 'blocks', NULL, wkbMultiLineString, NULL); // in this layer we export representaion of all point objects (it's a square) $wktLine = 'MULTILINESTRING((-0.5 -0.5, 0.5 -0.5, 0.5 0.5, -0.5 0.5, -0.5 -0.5))'; $geoOGR = ogr_g_createfromwkt($wktLine, NULL, NULL); $dfnLayerBlocks = OGR_L_GetLayerDefn($layerBlocks); $dataLine = OGR_F_Create($dfnLayerBlocks); OGR_F_SetGeometry($dataLine, $geoOGR); for ($i = 0; $i < OGR_FD_GetFieldCount($dfnLayerBlocks); $i++) { $fieldDfn = OGR_FD_GetFieldDefn($dfnLayerBlocks, $i); if (OGR_Fld_GetNameRef($fieldDfn) == 'BlockName') { OGR_F_SetFieldString($dataLine, $i, 'SQUARE'); } } OGR_L_CreateFeature($layerBlocks, $dataLine); OGR_F_Destroy($dataLine); OGR_G_DestroyGeometry($geoOGR); } return parent::createNewLayer($map, $driver, 'entities', $geoType, false); }
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); } }