00001 <?php
00002
00070 define('MAPAPP_VERSION', 0.1);
00071
00072 require_once('mapmanager/mapmanager.php');
00073 require_once('smartyapp.php');
00074
00170 class MapApp extends SmartyApp
00171 {
00173 var $map_manager;
00174
00179 function MapApp(&$map_manager)
00180 {
00181 $this->SmartyApp();
00182 $this->setMapManager($map_manager);
00183
00184
00185 $smarty =& $this->getSmarty();
00186 $web =& $map_manager->get('web');
00187
00188 $imagepath = realpath($web->imagepath);
00189 if (substr($imagepath, -1) == DIRECTORY_SEPARATOR)
00190 $imagepath = substr($imagepath, 0, -1);
00191
00192 $smarty->template_dir = $smarty->config_dir = $map_manager->get('shapepath');
00193 $smarty->compile_dir = $smarty->cache_dir = $imagepath;
00194 }
00195
00196 function assign()
00197 {
00198 $smarty =& $this->getSmarty();
00199 $control_box =& $this->getControlBox();
00200 $map_manager =& $this->getMapManager();
00201
00202
00203 $mapext =& $control_box->get('mapext');
00204 $mapext->setValue(rectToString($map_manager->get('extent')));
00205
00206
00207 $refmap =& $control_box->get('ref_map');
00208 $reference =& $map_manager->get('reference');
00209 if ($reference->status == MS_ON)
00210 $refmap->setDisplay(true);
00211 else
00212 $refmap->setDisplay(false);
00213
00214
00215 $layer_choice =& $control_box->get('layer_choice');
00216 if (sizeof($map_manager->getSelectableLayerNames()))
00217 $layer_choice->setDisplay(true);
00218 else
00219 $layer_choice->setDisplay(false);
00220
00221
00222 $smarty->assign('scale', $map_manager->get('scale'));
00223
00224
00225 $legend =& $map_manager->get('legend');
00226 if ($legend->status == MS_ON) {
00227 $imageObj = $map_manager->drawLegend();
00228 $smarty->assign('legend', $imageObj->saveWebImage());
00229 }
00230
00231
00232 $scalebar =& $map_manager->get('scalebar');
00233 if ($scalebar->status == MS_ON) {
00234 $imageObj = $map_manager->drawScaleBar();
00235 $smarty->assign('scalebar', $imageObj->saveWebImage());
00236 }
00237
00238 parent::assign();
00239 }
00240
00241
00242
00247 function setMapManager(&$map_manager)
00248 {
00249 $this->map_manager =& $map_manager;
00250 }
00251
00256 function &getMapManager()
00257 {
00258 return $this->map_manager;
00259 }
00260
00261 function getTemplate()
00262 {
00263 $map_manager =& $this->getMapManager();
00264 $web = $map_manager->get('web');
00265
00266 return $web->template;
00267 }
00268
00269 function setTemplate($template)
00270 {
00271 $map_manager =& $this->getMapManager();
00272 $web =& $map_manager->get('web');
00273 $web->set('template', $template);
00274 }
00275
00276
00277
00287 function initMapExt()
00288 {
00289 $map_manager =& $this->getMapManager();
00290 $control_box =& $this->getControlBox();
00291
00292 if (!$control_box->exists('mapext'))
00293 $mapext =& $control_box->addHidden('mapext', rectToString($map_manager->get('extent')));
00294 else
00295 $mapext =& $control_box->get('mapext');
00296
00297 if (!$mapext->actionExists('set_extent'))
00298 $mapext->addAction(FC_EVENT_UPDATE, 'set_extent', array(&$this, 'doSetExtent'));
00299 }
00300
00308 function initMainMap()
00309 {
00310 $control_box =& $this->getControlBox();
00311
00312 if (!$control_box->exists('main_map'))
00313 $main_map =& $control_box->addImage('main_map');
00314 else
00315 $main_map =& $control_box->get('main_map');
00316
00317 if (!$main_map->actionExists('define_click'))
00318 $main_map->addAction(FC_EVENT_CLICK, 'define_click', array(&$this, 'doDefineMapClick'));
00319 }
00320
00328 function initRefMap()
00329 {
00330 $control_box =& $this->getControlBox();
00331
00332 if (!$control_box->exists('ref_map'))
00333 $ref_map =& $control_box->addImage('ref_map');
00334 else
00335 $ref_map =& $control_box->get('ref_map');
00336
00337 if (!$ref_map->actionExists('define_click'))
00338 $ref_map->addAction(FC_EVENT_CLICK, 'define_click', array(&$this, 'doDefineRefMapClick'));
00339
00340 if (!$ref_map->actionExists('zoom'))
00341 $ref_map->addAction(FC_EVENT_CLICK, 'zoom', array(&$this, 'doSetRefMapZoom'));
00342 }
00343
00350 function initZoomLevel()
00351 {
00352 $control_box =& $this->getControlBox();
00353
00354 if (!$control_box->exists('zoom_level')) {
00355 $zoom_level =& $control_box->addSingleSelect('zoom_level', array('zoom_high' => array('High', 5),
00356 'zoom_medium' => array('Medium', 3),
00357 'zoom_low' => array('Low', 2)));
00358 $zoom_level->select('zoom_high');
00359 } else {
00360 $zoom_level =& $control_box->get('zoom_level');
00361 }
00362
00363 if (!$zoom_level->actionExists('zoom'))
00364 $zoom_level->addAction(FC_EVENT_UPDATE, 'zoom', array(&$this, 'doSetZoomLevel'));
00365
00366 $zoom_level->triggerAction('zoom');
00367 }
00368
00376 function initMapTools()
00377 {
00378 $control_box =& $this->getControlBox();
00379
00380 if (!$control_box->exists('map_tools'))
00381 $map_tools =& $control_box->addRadioChoice('map_tools', array('zoom_in' => array('Zoom in', 1),
00382 'zoom_out' => array('Zoom out', 2),
00383 'pan' => array('Pan', 3)));
00384 $this->initZoomIn();
00385 $this->initZoomOut();
00386 $this->initPan();
00387
00388 $control =& $control_box->get('map_tools');
00389 $control->triggerActions();
00390 }
00391
00398 function initZoomIn()
00399 {
00400 $control_box =& $this->getControlBox();
00401
00402 $map_tools =& $control_box->get('map_tools');
00403
00404 $zoom_in =& $map_tools->get('zoom_in');
00405
00406 if (!$zoom_in->actionExists('zoom_on'))
00407 $zoom_in->addAction(FC_EVENT_SELECT, 'zoom_on', array(&$this, 'doSetZoomInOn'));
00408 if (!$zoom_in->actionExists('zoom_off'))
00409 $zoom_in->addAction(FC_EVENT_DESELECT, 'zoom_off', array(&$this, 'doSetZoomInOff'));
00410 }
00411
00418 function initZoomOut()
00419 {
00420 $control_box =& $this->getControlBox();
00421
00422 $map_tools =& $control_box->get('map_tools');
00423
00424 $zoom_out =& $map_tools->get('zoom_out');
00425
00426 if (!$zoom_out->actionExists('zoom_on'))
00427 $zoom_out->addAction(FC_EVENT_SELECT, 'zoom_on', array(&$this, 'doSetZoomOutOn'));
00428 if (!$zoom_out->actionExists('zoom_off'))
00429 $zoom_out->addAction(FC_EVENT_DESELECT, 'zoom_off', array(&$this, 'doSetZoomOutOff'));
00430
00431 $zoom_out->setDisabled(true);
00432 }
00433
00440 function initPan()
00441 {
00442 $control_box =& $this->getControlBox();
00443
00444 $map_tools =& $control_box->get('map_tools');
00445
00446 $pan =& $map_tools->get('pan');
00447
00448 if (!$pan->actionExists('zoom_on'))
00449 $pan->addAction(FC_EVENT_SELECT, 'zoom_on', array(&$this, 'doSetZoomPanOn'));
00450 if (!$pan->actionExists('zoom_off'))
00451 $pan->addAction(FC_EVENT_DESELECT, 'zoom_off', array(&$this, 'doSetZoomPanOff'));
00452
00453 $pan->setDisabled(true);
00454 }
00455
00462 function initZoomAll()
00463 {
00464 $control_box =& $this->getControlBox();
00465
00466 if (!$control_box->exists('zoom_all'))
00467 $zoom_all =& $control_box->addSubmit('zoom_all', 'Zoom all');
00468 else
00469 $zoom_all =& $control_box->get('zoom_all');
00470
00471 if (!$zoom_all->actionExists('zoom'))
00472 $zoom_all->addAction(FC_EVENT_CLICK, 'zoom', array(&$this, 'doSetZoomAll'));
00473
00474 $zoom_all->setDisabled(true);
00475 }
00476
00484 function initRefresh()
00485 {
00486 $control_box =& $this->getControlBox();
00487
00488 if (!$control_box->exists('refresh'))
00489 $control_box->addSubmit('refresh', 'Refresh');
00490 }
00491
00499 function initLayerChoice()
00500 {
00501 $map_manager =& $this->getMapManager();
00502 $control_box =& $this->getControlBox();
00503
00504 if (!$control_box->exists('layer_choice'))
00505 $layer_choice =& $control_box->addCheckboxChoice('layer_choice');
00506 else
00507 $layer_choice =& $control_box->get('layer_choice');
00508
00509 if (!$layer_choice->actionExists('layer_choice'))
00510 $layer_choice->addAction(FC_EVENT_UPDATE, 'toggle_layers', array(&$this, 'doToggleAllLayersState'));
00511
00512 $selectable_layers = $map_manager->getSelectableLayerNames();
00513 $layer_count = sizeof($selectable_layers);
00514
00515
00516 for ($i = $layer_count - 1; $i >= 0; $i--)
00517 {
00518 $layer_id = $selectable_layers[$i];
00519 $layer = $map_manager->getLayerByName($layer_id);
00520 $layer_name = $layer->getMetaData('LABEL');
00521 if ($layer_name == '')
00522 $layer_name = $layer_id;
00523
00524 $control =& $layer_choice->addChoice($layer_id, $layer_name, ($layer->status != MS_OFF) ? true : false);
00525
00526 }
00527 }
00528
00535 function init()
00536 {
00537 parent::init();
00538
00539 $this->initMapExt();
00540 $this->initMainMap();
00541 $this->initZoomLevel();
00542 $this->initMapTools();
00543
00544
00545
00546
00547 $this->initZoomAll();
00548 $this->initRefMap();
00549 $this->initRefresh();
00550 $this->initLayerChoice();
00551 }
00552
00560 function updateZoomControls()
00561 {
00562 $map_manager =& $this->getMapManager();
00563 $control_box =& $this->getControlBox();
00564
00565 $map_tools =& $control_box->get('map_tools');
00566 $zoom_in =& $map_tools->get('zoom_in');
00567 $zoom_out =& $map_tools->get('zoom_out');
00568 $pan =& $map_tools->get('pan');
00569 $zoom_all =& $control_box->get('zoom_all');
00570
00571 if ($map_manager->zoomedToMinScale()) {
00572 $zoom_in->setDisabled(true);
00573 } else {
00574 $zoom_in->setDisabled(false);
00575 }
00576
00577 if ($map_manager->zoomedToMaxExtent()) {
00578 $zoom_out->setDisabled(true);
00579 $pan->setDisabled(true);
00580 $zoom_all->setDisabled(true);
00581 } else {
00582 $pan->setDisabled(false);
00583
00584 if ($map_manager->zoomedToMaxScale()) {
00585 $zoom_out->setDisabled(true);
00586 $zoom_all->setDisabled(true);
00587 } else {
00588 $zoom_out->setDisabled(false);
00589 $zoom_all->setDisabled(false);
00590 }
00591 }
00592
00593 $zoom_type = $map_manager->getZoomType();
00594
00595 if (!$zoom_in->disabled() and !$zoom_in->selected() and $zoom_type == MM_ZOOM_IN)
00596 $zoom_in->select();
00597 elseif ($zoom_in->selected() and $zoom_type != MM_ZOOM_IN)
00598 $zoom_in->deselect();
00599
00600
00601 if (!$zoom_out->disabled() and !$zoom_out->selected() and $zoom_type == MM_ZOOM_OUT)
00602 $zoom_out->select();
00603 elseif ($zoom_out->selected() and $zoom_type != MM_ZOOM_OUT)
00604 $zoom_out->deselect();
00605
00606 if (!$pan->disabled() and !$pan->selected() and $zoom_type == MM_ZOOM_PAN)
00607 $pan->select();
00608 elseif ($pan->selected() and $zoom_type != MM_ZOOM_PAN)
00609 $pan->deselect();
00610 }
00611
00612
00613
00614
00622 function doSetExtent(&$control)
00623 {
00624 $map_manager =& $this->getMapManager();
00625 $rect = stringToRect($control->getValue());
00626 $map_manager->setextent($rect->minx, $rect->miny, $rect->maxx, $rect->maxy);
00627 }
00628
00637 function doToggleAllLayersState(&$control)
00638 {
00639 $map_manager =& $this->getMapManager();
00640 $value = $control->getValue();
00641
00642 if (is_array($value)) {
00643 foreach($map_manager->getSelectableLayerNames() as $name)
00644 {
00645 if (in_array($name, $value))
00646 $map_manager->turnLayerOn($name, true);
00647 else
00648 $map_manager->turnLayerOff($name, true);
00649 }
00650 } else {
00651 foreach($map_manager->getSelectableLayerNames() as $name)
00652 {
00653 $layer = $map_manager->getLayerByName($name);
00654
00655 if ($name != $value)
00656 $map_manager->turnLayerOff($name, true);
00657 else
00658 $map_manager->turnLayerOn($name, true);
00659 }
00660 }
00661 }
00662
00671 function doToggleLayerState(&$control)
00672 {
00673 $layer_name = $control->getValue();
00674 $map_manager =& $this->getMapManager();
00675
00676 $layer = $map_manager->getLayerByName($layer_name);
00677 if ($control->selected())
00678 $layer->set('status', MS_ON);
00679 else
00680 $layer->set('status', MS_OFF);
00681 }
00682
00690 function doSetZoomAll(&$control)
00691 {
00692 $map_manager =& $this->getMapManager();
00693 $map_manager->setZoomType(MM_ZOOM_EXTENT);
00694
00695 $map_manager->setZoomExtent($map_manager->getMaxExtent());
00696 }
00697
00704 function doSetZoomLevel(&$control)
00705 {
00706 $map_manager =& $this->getMapManager();
00707 $map_manager->setZoomLevel($control->getValue());
00708 }
00709
00718 function doDefineMapClick(&$image_input)
00719 {
00720 list($x, $y) = $image_input->getValue();
00721 $image_point = ms_newpointobj();
00722 $image_point->setXY($x, $y);
00723
00724 $map_manager =& $this->getMapManager();
00725 $map_point = $map_manager->imagePointToMapPoint($image_point);
00726
00727 $map_manager->setMapClick($map_point, MM_CLICK_MAIN);
00728 }
00729
00738 function doDefineRefMapClick(&$image_input)
00739 {
00740 list($x, $y) = $image_input->getValue();
00741 $image_point = ms_newpointobj();
00742 $image_point->setXY($x, $y);
00743
00744 $map_manager =& $this->getMapManager();
00745 $map_point = $map_manager->imagePointToRefMapPoint($image_point);
00746
00747 $map_manager->setMapClick($map_point, MM_CLICK_REF);
00748 }
00749
00758 function doSetRefMapZoom(&$image_input)
00759 {
00760 $map_manager =& $this->getMapManager();
00761 $map_manager->setZoomType(MM_ZOOM_PAN);
00762 }
00763
00772 function doSetZoomPanOn(&$control)
00773 {
00774
00775
00776 $map_manager =& $this->getMapManager();
00777 $map_manager->setZoomType(MM_ZOOM_PAN);
00778 }
00779
00788 function doSetZoomPanOff(&$control)
00789 {
00790
00791
00792 $map_manager =& $this->getMapManager();
00793 if ($map_manager->getZoomType() == MM_ZOOM_PAN)
00794 $map_manager->setZoomType(MM_ZOOM_NONE);
00795 }
00796
00805 function doSetZoomInOn(&$control)
00806 {
00807
00808
00809 $map_manager =& $this->getMapManager();
00810 $map_manager->setZoomType(MM_ZOOM_IN);
00811 }
00812
00821 function doSetZoomInOff(&$control)
00822 {
00823
00824
00825 $map_manager =& $this->getMapManager();
00826 if ($map_manager->getZoomType() == MM_ZOOM_IN)
00827 $map_manager->setZoomType(MM_ZOOM_NONE);
00828 }
00829
00838 function doSetZoomOutOn(&$control)
00839 {
00840
00841
00842 $map_manager =& $this->getMapManager();
00843 $map_manager->setZoomType(MM_ZOOM_OUT);
00844 }
00845
00854 function doSetZoomOutOff(&$control)
00855 {
00856
00857
00858 $map_manager =& $this->getMapManager();
00859 if ($map_manager->getZoomType() == MM_ZOOM_OUT)
00860 $map_manager->setZoomType(MM_ZOOM_NONE);
00861 }
00862
00863
00864
00865
00874 function imageObjToImage(&$imageObj, &$image)
00875 {
00876 if (!is_a($image, 'ImageInput')) {
00877 $class = get_class($image);
00878 trigger_error("The control object must be an 'ImageInput'; id $image_id returned '$class'", E_USER_ERROR);
00879 }
00880
00881 $url = $imageObj->saveWebImage();
00882 $image->setSrc($url);
00883 }
00884
00890 function drawToImage(&$image)
00891 {
00892 $map_manager =& $this->getMapManager();
00893 $imageObj = $map_manager->draw();
00894 $this->imageObjToImage($imageObj, $image);
00895 }
00896
00902 function drawReferenceMapToImage(&$image)
00903 {
00904 $map_manager =& $this->getMapManager();
00905 $imageObj = $map_manager->drawReferenceMap();
00906 $this->imageObjToImage($imageObj, $image);
00907 }
00908
00909
00910
00916 function draw($return = true)
00917 {
00918 $control_box =& $this->getControlBox();
00919 $main_map =& $control_box->get('main_map');
00920
00921 $this->drawToImage($main_map);
00922
00923 if ($return)
00924 return $main_map->toString();
00925 }
00926
00932 function drawReferenceMap($return = true)
00933 {
00934 $control_box =& $this->getControlBox();
00935 $ref_map =& $control_box->get('ref_map');
00936
00937 $this->drawReferenceMapToImage($ref_map);
00938
00939 if ($return)
00940 return $ref_map->toString();
00941 }
00942
00943
00944
00945
00946
00947 function version()
00948 {
00949 return MAPAPP_VERSION;
00950 }
00951
00952
00953
00954
00955
00956 function toString()
00957 {
00958 $control_box =& $this->getControlBox();
00959
00960
00961 $main_map =& $control_box->get('main_map');
00962 if ($main_map->display())
00963 $this->draw(false);
00964
00965
00966 $map_manager =& $this->getMapManager();
00967 $reference =& $map_manager->get('reference');
00968
00969 if ($reference->status == MS_ON) {
00970 $ref_map =& $control_box->get('ref_map');
00971 if ($ref_map->display())
00972 $this->drawReferenceMap(false);
00973 }
00974
00975 return parent::toString();
00976 }
00977
00978
00979
00980
00981
00982 function __sleep()
00983 {
00984 $sleep_vars = parent::__sleep();
00985
00986 array_push($sleep_vars, 'map_manager');
00987 return $sleep_vars;
00988 }
00989 }
00990
00991
00992 ?>