function makeAssociations($id_min, $id_max, $is_map = false) { $id = $id_min; while ($id <= $id_max) { echo "Computing associations for document {$id} ... \n"; // if associations with areas for current doc already existed, delete them //$deleted = Association::deleteAllFor($id, array('dr', 'dc', 'dd', 'dm')); //if ($deleted) echo "Deleted $deleted already existing associations. \n"; // compute new associations $areas = gisQuery::getAreasContaining($id); $maps = gisQuery::getMapsContaining($id); // perform association with these areas. foreach ($areas as $area) { switch ($area['type']) { case 1: // range $type = 'dr'; break; case 2: // country $type = 'dc'; break; case 3: // dept $type = 'dd'; break; } $a = new GeoAssociation(); echo "[type={$type}] found area : " . $area['id'] . "\n"; $a->doSaveWithValues($id, $area['id'], $type); // main, linked, type unset($a); } // perform association with these maps if current document is not a map. if (!$is_map) { foreach ($maps as $map) { $a = new GeoAssociation(); echo "[type=dm] found map : " . $map['id'] . "\n"; $a->doSaveWithValues($id, $map['id'], 'dm'); // main, linked, type unset($a); } } $id++; } }
// needed for doctrine connection to work sfContext::getInstance(); // Next Step : retrieve highest summits associated (in fact there should always be only one) with routes, and transfer summit geoassociations to route. // WARNING: set correct id limits // WARNING 2: routes must be consecutive. $id = 44918; $id_max = 57877; while ($id <= $id_max) { echo "Computing associations for route {$id} ... \n"; // if associations with areas for current doc already existed, delete them //$deleted = GeoAssociation::deleteAllFor($id, array('dr', 'dc', 'dd', 'dm')); //if ($deleted) echo "Deleted $deleted already existing geoassociations. \n"; // get all associated regions (3+maps) with this summit: //$summit_id is the id of associated summit with route $id $associated_docs = Association::findAllAssociatedDocs($id, array('id', 'module'), 'sr'); // summit-route foreach ($associated_docs as $doc) { if ($doc['module'] == 'summits') { $summit_id = $doc['id']; } } $associations = GeoAssociation::findAllAssociations($summit_id, array('dr', 'dc', 'dd', 'dm')); // replicate them with route_id instead of summit_id: foreach ($associations as $ea) { $a = new GeoAssociation(); $a->doSaveWithValues($id, $ea->get('linked_id'), $ea->get('type')); echo "Created association with " . $ea->get('linked_id') . " \n"; unset($a); } $id++; }
protected function endEdit() { //Test if form is submitted or not if ($this->success) { // try to perform association with linked_doc (if pertinent) $summit_id = $this->getRequestParameter('summit_id'); $id = $this->document->get('id'); $user_id = $this->getUser()->getId(); if ($this->new_document && $summit_id || $summit_id && !Association::find($summit_id, $id, 'sr')) { $sr = new Association(); $sr->doSaveWithValues($summit_id, $id, 'sr', $user_id); // main, linked, type // clear cache of associated summit ... $this->clearCache('summits', $summit_id, false, 'view'); } // here if we have created a new document and if $this->document->get('geom_wkt') is null, then use associated doc geom associations: // this allows us to filter on ranges even if no GPX is uploaded if ($this->new_document && $summit_id && !$this->document->get('geom_wkt')) { // get all associated regions (3+maps) with this summit: $associations = GeoAssociation::findAllAssociations($summit_id, array('dr', 'dc', 'dd', 'dv', 'dm')); // replicate them with route_id instead of summit_id: foreach ($associations as $ea) { $a = new GeoAssociation(); $a->doSaveWithValues($id, $ea->get('linked_id'), $ea->get('type')); } } // if we add a route to a summit-hut, link the route to the hut $hut_asso = Association::findAllAssociations($summit_id, 'sh'); if ($this->new_document && $summit_id && count($hut_asso) > 0) { // associate hut to summit $asso = new Association(); $hut_id = $hut_asso[0]->get('linked_id'); $asso->doSaveWithValues($hut_id, $id, 'hr', 2); // C2C user // clear cache of associated hut ... $this->clearCache('huts', $hut_id, false, 'view'); } parent::endEdit(); // redirect to document view } else { if ($this->link_with = $this->getRequestParameter('link')) { // linked_doc was already retrieved in filterAdditionalParameters if ($this->linked_doc) { $this->linked_doc->setBestCulture($this->getUser()->getCulturesForDocuments()); } } else { $this->linked_doc = $this->getHighestSummit(); } } }
protected function endEdit() { //Test if form is submitted or not if ($this->success) { // if this is the first version of the outing (aka creation) // set a flash message to encourage to also enhance the corresponding route if (is_null($this->document->getVersion())) { $this->setNotice('thanks for new outing'); } // try to perform association with linked_doc (if pertinent) $associated_id = $this->getRequestParameter('document_id'); $user_id = $this->getUser()->getId(); $id = $this->document->get('id'); if ($this->new_document && $associated_id || $associated_id && !Association::find($associated_id, $id)) { // we must get this document's module (site or route ?) $associated_doc = Document::find('Document', $associated_id, array('module')); if ($associated_doc) { $associated_module = $associated_doc->get('module'); $a = new Association(); if ($associated_module == 'routes') { $a->doSaveWithValues($associated_id, $id, 'ro', $user_id); // main, linked, type // clear cache of associated route ... $this->clearCache('routes', $associated_id, false, 'view'); } elseif ($associated_module == 'sites') { $a->doSaveWithValues($associated_id, $id, 'to', $user_id); // main, linked, type // clear cache of associated site ... $this->clearCache('sites', $associated_id, false, 'view'); } // here if we have created a new document and if $this->document->get('geom_wkt') is null, then use associated doc geom associations: // this allows us to filter on ranges even if no GPX is uploaded if ($this->new_document && $associated_id && !$this->document->get('geom_wkt')) { // get all associated regions (only regions, countries, depts, no maps !) with this summit: $associations = GeoAssociation::findAllAssociations($associated_id, array('dr', 'dc', 'dd', 'dv')); // replicate them with outing_id instead of (route_id or site_id): foreach ($associations as $ea) { $areas_id = $ea->get('linked_id'); $a = new GeoAssociation(); $a->doSaveWithValues($id, $areas_id, $ea->get('type')); // clear cache of associated areas $this->clearCache('areas', $areas_id, false, 'view'); } } } } // create also association with current user. if ($this->new_document) { $uo = new Association(); $uo->doSaveWithValues($user_id, $id, 'uo', $user_id); // main, linked, type // clear cache of current user $this->clearCache('users', $user_id, false, 'view'); } // create association with MW contest article, if requested if ($this->new_document) { $mw_contest_associate = $this->getRequestParameter('mw_contest_associate'); if ($mw_contest_associate) { $mw_article_id = sfConfig::get('app_mw_contest_id'); $oc = new Association(); $oc->doSaveWithValues($id, $mw_article_id, 'oc', $user_id); } } parent::endEdit(); // redirect to document view } }
function import_new_geometry() { global $conn, $comment, $is_map, $is_new_document, $culture, $name, $map_editor, $map_scale, $map_code, $keepassociations, $region_type, $newgeomtext, $no_oldgeom, $document_id, $a_type, $fullwipe, $oldgeom, $newgeom, $prgmsg; info("Importing the new geometry...\n"); try { $conn->beginTransaction(); $history_metadata = new HistoryMetadata(); $history_metadata->setComment(isset($comment) ? $comment : ($is_new_document ? 'Imported new ' . ($is_map ? 'map' : 'area') : 'Updated geometry')); $history_metadata->set('is_minor', false); $history_metadata->set('user_id', 2); // C2C user $history_metadata->save(); if ($is_new_document) { $doc = $is_map ? new Map() : new Area(); $doc->setCulture($culture); $doc->set('name', $name); if ($is_map) { $doc->set('editor', $map_editor); $doc->set('scale', $map_scale); $doc->set('code', $map_code); } else { $doc->set('area_type', $region_type); } } else { $doc = Document::find($is_map ? 'Map' : 'Area', $document_id); $name = $doc->get('name'); if (!$is_map) { $region_type = $doc->get('area_type'); } } $doc->set('geom_wkt', $newgeomtext); $doc->save(); info("Geometry uploaded.\n"); if ($keepassociations) { exit; } if ($is_new_document) { $document_id = $doc->get('id'); } // $conn->commit(); // $conn->beginTransaction(); if ($is_map) { $a_type = 'dm'; } else { switch ($region_type) { case 1: // range $a_type = 'dr'; break; case 2: // country $a_type = 'dc'; break; case 3: // dept $a_type = 'dd'; break; } } // Some explanation for the following queries: // - && operator is used to limit docs to the one whose bouding boxes overlap (it uses spatial index) // - ST_Within and ST_Intersects are used to work on the actual geometries // - ST_Within apears to be faster, so we use it for 'points' (like summits, huts etc), but we have // to use ST_Intersects for 'geometries' (like outings, maps etc) // - we only use a buffer of 200m for areas (because of boundaries imprecision), but not for maps // - areas are not linked together // - maps are not linked to outings, users, and other maps // if it is a new document, we create geoassociations for the whole geometry // but if its an updated one, we only create geoassociations for the part of the // new geometry that does not intersect with the old one if ($is_new_document || $no_oldgeom || $fullwipe) { // retrieve geom from the database $geomquery = '(SELECT geom FROM ' . ($is_map ? 'maps' : 'areas') . ' WHERE id=?)'; $geomqueryb = '(SELECT buffer(geom, 200) FROM ' . ($is_map ? 'maps' : 'areas') . ' WHERE id=?)'; $queryparam = array($document_id, $document_id); } else { $queryparam = array(); $creategeom = $conn->standaloneQuery("SELECT ST_Difference('{$newgeom}', '{$oldgeom}')")->fetchAll(); $creategeomb = $conn->standaloneQuery("SELECT ST_Difference(buffer('{$newgeom}', 200), buffer('{$oldgeom}', 200))")->fetchAll(); $creategeom = $creategeom[0]['st_difference']; $creategeomb = $creategeomb[0]['st_difference']; $geomquery = "'{$creategeom}'"; $geomqueryb = "'{$creategeomb}'"; } // for maps, we don't use buffer at all if ($is_map) { $geomqueryb = $geomquery; } $queries = array(); // point geometry $queries[] = array("SELECT id, module FROM documents WHERE geom && {$geomqueryb} " . "AND ST_Within(geom, {$geomqueryb}) " . "AND module IN('summits', 'huts', 'sites', 'parkings', 'products', 'portals', 'images'" . ($is_map ? '' : ", 'users'") . ')', $queryparam); // multipoint geometry $queries[] = array("SELECT id, module FROM documents WHERE geom && {$geomqueryb} " . "AND ST_Intersects(geom, {$geomqueryb}) AND module" . ($is_map ? "='routes'" : " IN('routes', 'outings')"), $queryparam); // for maps areas associations, we always compute 'full wipe', without buffer $geomquery = '(SELECT geom FROM ' . ($is_map ? 'maps' : 'areas') . ' WHERE id=?)'; $queries[] = array("SELECT id, module FROM documents WHERE geom && {$geomquery} " . "AND ST_Intersects(geom, {$geomquery}) AND module='" . ($is_map ? 'areas' : 'maps') . "'", array($document_id, $document_id)); $results_a = array(); foreach ($queries as $query) { $results_a[] = $conn->standaloneQuery($query[0], $query[1])->fetchAll(); } $results = array(); foreach ($results_a as $results_set) { foreach ($results_set as $d) { $results[] = $d; } } $prgmsg = "Create new associations..."; info($prgmsg); $tot = count($results); foreach ($results as $i => $d) { progression($i, $tot); // Apparently in some cases, we ar trying to create associations that // already exist, so we check first if (!GeoAssociation::find($document_id, $d['id'], null, false)) { $a = new GeoAssociation(); $created[$d['module']] = isset($created[$d['module']]) ? $created[$d['module']] + 1 : 1; // for map - area geoassociations, links must not be dm but dr, dc, dd... if ($is_map && $d['module'] === 'areas') { $area = Document::find('Area', $d['id']); switch ($area->get('area_type')) { case 1: // range $t_a_type = 'dr'; break; case 2: // country $t_a_type = 'dc'; break; case 3: // dept $t_a_type = 'dd'; break; } $a->doSaveWithValues($document_id, $d['id'], $t_a_type); } else { $a->doSaveWithValues($d['id'], $document_id, $a_type); } } // inherited docs: we add geoassociations for the 'inherited docs' from sites, routes and summits // but not if they already have a geometry (gps track) switch ($d['module']) { case 'sites': case 'routes': if ($is_map) { break; } // we do not link maps to outings $associated_outings = Association::findAllAssociatedDocs($d['id'], array('id', 'geom_wkt'), $d['module'] === 'routes' ? 'ro' : 'to'); if (count($associated_outings)) { foreach ($associated_outings as $outing) { if (!$outing['geom_wkt'] && GeoAssociation::find($outing['id'], $document_id, $a_type) === false) { // we create geoassociation (if it already existed, it has been deleted before in the script) $a = new GeoAssociation(); $a->doSaveWithValues($outing['id'], $document_id, $a_type); $created['outings'] = isset($created['outings']) ? $created['outings'] + 1 : 1; } } } break; case 'summits': // if summit is of type raid, we should not try to update its routes and outings summit_type=5 $summit = Document::find('Summit', $d['id']); if ($summit->get('summit_type') == 5) { break; } $associated_routes = Association::findAllAssociatedDocs($d['id'], array('id', 'geom_wkt'), 'sr'); if (count($associated_routes)) { foreach ($associated_routes as $route) { $i = $route['id']; if (!$route['geom_wkt'] && GeoAssociation::find($i, $document_id, $a_type) === false) { $a = new GeoAssociation(); $a->doSaveWithValues($i, $document_id, $a_type); $created['routes'] = isset($created['routes']) ? $created['routes'] + 1 : 1; if (!$is_map) { $associated_outings = Association::findAllAssociatedDocs($i, array('id', 'geom_wkt'), 'ro'); if (count($associated_outings)) { foreach ($associated_outings as $outing) { $j = $outing['id']; if (!$outing['geom_wkt'] && GeoAssociation::find($j, $document_id, $a_type) === false) { $a = new GeoAssociation(); $a->doSaveWithValues($j, $document_id, $a_type); $created['outings'] = isset($created['outings']) ? $created['outings'] + 1 : 1; } } } } } } } break; } } info("\n"); $conn->commit(); if (isset($created)) { associations_result($created); } if ($is_new_document) { info('Added new ' . ($is_map ? 'map' : 'area') . " {$name} ({$document_id})\n"); } else { info('Updated ' . ($is_map ? 'map' : 'area') . " ({$document_id})\n"); } } catch (Exception $e) { $conn->rollback(); throw $e; } }
public static function replicateGeoAssociations($geoassociations, $id, $delete_old = true, $replicate_maps_associations = true) { $nb_created = 0; if ($delete_old) { self::deleteAllFor($id, array('dr', 'dc', 'dm', 'dd')); } foreach ($geoassociations as $ea) { if (!($ea->get('type') == 'dm' && !$replicate_maps_associations)) { $a = new GeoAssociation(); if ($a->doSaveWithValues($id, $ea->get('linked_id'), $ea->get('type'))) { $nb_created++; } } } return $nb_created; }
public function executeAddroute() { $id = $this->getRequestParameter('document_id'); // check if a summit is already associated to hut. if not, create it $create_summit = Association::countMains($id, 'sh') == 0; if ($create_summit) { $document = Document::find('Hut', $id, array('elevation', 'geom_wkt')); $conn = sfDoctrine::Connection(); try { $conn->beginTransaction(); // create first version of document, with culture and geometry of hut document $hut_elevation = $document['elevation']; $hut_lat = $document['lat']; $hut_lon = $document['lon']; $hut_culture = $document->getCulture(); $hut_name = $document['name']; $history_metadata = new HistoryMetadata(); $history_metadata->setComment($this->__('Created summit synchronized with hut for access')); $history_metadata->set('is_minor', false); $history_metadata->set('user_id', 2); // C2C user $history_metadata->save(); $summit = new Summit(); $summit->setCulture($hut_culture); $summit->set('name', $hut_name); $summit->set('elevation', $hut_elevation); $summit->set('summit_type', 100); // set summit type to ' hut' $summit->set('lat', $hut_lat); $summit->set('lon', $hut_lon); $summit->save(); $conn->commit(); // add others culture versions foreach ($document->get('HutI18n') as $i18n) { $culture = $i18n->getCulture(); if ($culture != $hut_culture) { $conn->beginTransaction(); $hut_name = $i18n->getName(); $history_metadata = new HistoryMetadata(); $history_metadata->setComment($this->__('Created summit synchronized with hut for access')); $history_metadata->set('is_minor', false); $history_metadata->set('user_id', 2); // C2C user $history_metadata->save(); $summit->setCulture($culture); $summit->set('name', $hut_name); $summit->save(); $conn->commit(); } } } catch (Exception $e) { $conn->rollback(); return $this->setErrorAndRedirect($this->__('Failed to create synchronized summit'), "routes/edit?link={$summit_id}"); } $summit_id = $summit->get('id'); // get all associated regions (3+maps) with this hut: $associations = GeoAssociation::findAllAssociations($id, array('dr', 'dc', 'dd', 'dv', 'dm')); // replicate them with summit_id instead of id: foreach ($associations as $ea) { $a = new GeoAssociation(); $a->doSaveWithValues($summit_id, $ea->get('linked_id'), $ea->get('type')); } // associate hut to summit $asso = new Association(); $asso->doSaveWithValues($summit_id, $id, 'sh', 2); // C2C user } else { $associations = Association::findAllAssociations($id, 'sh'); $summit_id = $associations[0]->get('main_id'); } $this->clearCache('huts', $id); return $this->redirect("routes/edit?link={$summit_id}"); }
public static function createGeoAssociations($id, $delete_old = true, $associate_with_maps = true) { $nb_created = 0; // if associations with areas for current doc already existed, delete them if ($delete_old) { $deleted = GeoAssociation::deleteAllFor($id, array('dr', 'dc', 'dd', 'dv', 'dm')); c2cTools::log("executeEdit: deleted {$deleted} geom associations for document {$id}"); } // compute new associations $areas = self::getAreasContaining($id); // perform association with these areas. foreach ($areas as $area) { switch ($area['type']) { case 1: // range $type = 'dr'; break; case 2: // country $type = 'dc'; break; case 3: // dept $type = 'dd'; break; case 4: // valley $type = 'dv'; break; } $a = new GeoAssociation(); $a->doSaveWithValues($id, $area['id'], $type); // main, linked, type $nb_created++; } if ($associate_with_maps) { // compute new associations $maps = self::getMapsContaining($id); // perform association with these maps. foreach ($maps as $map) { $a = new GeoAssociation(); $a->doSaveWithValues($id, $map['id'], 'dm'); // main, linked, type $nb_created++; } } return $nb_created; }