/** * Postprocessing for building a geom from the list of grid squares to make an SQL based check easy */ function data_cleaner_without_polygon_data_cleaner_postprocess($id, $db) { $db->query('create temporary table geoms_without_polygon (geom geometry)'); try { $r = $db->select('key, header_name')->from('verification_rule_data')->where('verification_rule_id', $id)->in('header_name', array('10km_GB', '10km_Ireland', '1km_GB', '1km_Ireland', '10km_CI', '1km_CI'))->get()->result(); $wktList = array(); foreach ($r as $gridSquare) { switch ($gridSquare->header_name) { case '10km_GB': case '1km_GB': $system = 'osgb'; break; case '10km_Ireland': case '1km_Ireland': $system = 'osie'; break; case '10km_CI': case '1km_CI': $system = 'utm30ed50'; break; default: continue; // we don't know this grid square type - should not have come back from the query } $srid = kohana::config('sref_notations.internal_srid'); try { $wktList[] = "(st_geomfromtext('" . spatial_ref::sref_to_internal_wkt($gridSquare->key, $system) . "', {$srid}))"; } catch (Exception $e) { kohana::debug('alert', 'Did not import grid square ' . $gridSquare->key . " for rule {$id}"); error::log_error('Importing without polygon rules', $e); } } if (!empty($wktList)) { $db->query("insert into geoms_without_polygon values " . implode(',', $wktList)); } $date = date("Ymd H:i:s"); $uid = $_SESSION['auth_user']->id; $db->query("delete from verification_rule_data where verification_rule_id={$id} and header_name='geom'"); $db->query('insert into verification_rule_data (verification_rule_id, header_name, data_group, key, value, value_geom, created_on, created_by_id, updated_on, updated_by_id) ' . "select {$id}, 'geom', 1, 'geom', '-', st_union(geom), '{$date}', {$uid}, '{$date}', {$uid} from geoms_without_polygon"); $db->query('drop table geoms_without_polygon'); } catch (Exception $e) { $db->query('drop table geoms_without_polygon'); throw $e; } }
/** * Allow a service request to triangulate between 2 systems. GET parameters are: * from_sref * from_system * to_system * to_precision (optional) */ public function convert_sref() { try { $wkt = spatial_ref::sref_to_internal_wkt($_GET['from_sref'], $_GET['from_system']); if (array_key_exists('precision', $_GET)) { $precision = $_GET['precision']; } else { $precision = null; } if (array_key_exists('metresAccuracy', $_GET)) { $metresAccuracy = $_GET['metresAccuracy']; } else { $metresAccuracy = null; } echo spatial_ref::internal_wkt_to_sref($wkt, $_GET['to_system'], $precision, null, $metresAccuracy); } catch (Exception $e) { $this->handle_error($e); } }
/** * Fills the temporary table called occdelta, which contains details of each proposed record to * verify. This is only done when calling verify directly, since occdelta is prepared by the * scheduled_tasks process when auto-checks are being run on the schedule. */ private function prepareOccdelta($db, $sample, $occurrences) { $website_id = $this->website_id; $srid = kohana::config('sref_notations.internal_srid'); $last_sref = ''; $last_sref_system = ''; foreach ($occurrences as $occurrence) { $record = array_merge($sample, $occurrence); $survey_id = $record['sample:survey_id']; $sref = $record['sample:entered_sref']; $sref_system = $record['sample:entered_sref_system']; $taxa_taxon_list_id = $record['occurrence:taxa_taxon_list_id']; if (isset($record['sample:geom'])) { $geom = $record['sample:geom']; } else { // avoid recalculating the geom if we don't have to as this is relatively expensive if ($sref !== $last_sref || $sref_system !== $last_sref_system) { $geom = spatial_ref::sref_to_internal_wkt($sref, $sref_system); } $last_sref = $sref; $last_sref_system = $sref_system; } $date = $record['sample:date']; $vd = vague_date::string_to_vague_date($date); $date_start = $vd[0]; $date_end = $vd[1]; $date_type = $vd[2]; $db->query("insert into occdelta (website_id, survey_id, date_start, date_end, date_type, public_entered_sref, entered_sref_system, public_geom, taxa_taxon_list_id)\n values ({$website_id}, {$survey_id}, '{$date_start}', '{$date_end}', '{$date_type}', '{$sref}', '{$sref_system}', st_geomfromtext('{$geom}', {$srid}), {$taxa_taxon_list_id});"); } // patch in some extra details about the taxon required for each cache entry $db->query("update occdelta o set taxon_meaning_id=ttl.taxon_meaning_id, taxon=ttl.taxon, taxa_taxon_list_external_key=ttl.external_key " . "from list_taxa_taxon_lists ttl where ttl.id=o.taxa_taxon_list_id"); }
/** * Handle the case where a new record is created with a centroid_sref but without the geom being pre-calculated. * E.g. when importing from a shape file, or when JS is disabled on the client. */ protected function preSubmit() { // Allow a location to be submitted with a spatial ref and system but no centroid_geom. If so we // can work out the Geom if (!empty($this->submission['fields']['centroid_sref']['value']) && !empty($this->submission['fields']['centroid_sref_system']['value']) && empty($this->submission['fields']['centroid_geom']['value'])) { try { $this->submission['fields']['centroid_geom']['value'] = spatial_ref::sref_to_internal_wkt($this->submission['fields']['centroid_sref']['value'], $this->submission['fields']['centroid_sref_system']['value']); } catch (Exception $e) { $this->errors['centroid_sref'] = $e->getMessage(); } } elseif (empty($this->submission['fields']['centroid_sref']['value']) && empty($this->submission['fields']['centroid_geom']['value']) && !empty($this->submission['fields']['boundary_geom']['value'])) { kohana::log('debug', 'working out centroid from boundary'); // if the geom is supplied for the boundary, but not the centroid sref, then calculate it. // First, convert the boundary geom to a centroid using any provided system, else use LatLong (EPSG:4326) $boundary = $this->submission['fields']['boundary_geom']['value']; if (!empty($this->submission['fields']['centroid_sref_system']['value'])) { $centroid = $this->calcCentroid($boundary, $this->submission['fields']['centroid_sref_system']['value']); } else { $centroid = $this->calcCentroid($boundary); } $this->submission['fields']['centroid_geom']['value'] = $centroid['wkt']; $this->submission['fields']['centroid_sref']['value'] = $centroid['sref']; $this->submission['fields']['centroid_sref_system']['value'] = $centroid['sref_system']; } // Empty boundary geom is allowed but must be null if (isset($this->submission['fields']['boundary_geom']['value']) && empty($this->submission['fields']['boundary_geom']['value'])) { $this->submission['fields']['boundary_geom']['value'] = null; } return parent::presubmit(); }
/** * Before submission, map vague dates to their underlying database fields. */ protected function preSubmit() { if (array_key_exists('date', $this->submission['fields'])) { $vague_date = vague_date::string_to_vague_date($this->submission['fields']['date']['value']); $this->submission['fields']['date_start']['value'] = $vague_date[0]; $this->submission['fields']['date_end']['value'] = $vague_date[1]; $this->submission['fields']['date_type']['value'] = $vague_date[2]; } // Allow a sample to be submitted with a spatial ref and system but no Geom. If so we // can work out the Geom if (array_key_exists('entered_sref', $this->submission['fields']) && array_key_exists('entered_sref_system', $this->submission['fields']) && !(array_key_exists('geom', $this->submission['fields']) && $this->submission['fields']['geom']['value']) && $this->submission['fields']['entered_sref']['value'] && $this->submission['fields']['entered_sref_system']['value']) { try { $this->submission['fields']['geom']['value'] = spatial_ref::sref_to_internal_wkt($this->submission['fields']['entered_sref']['value'], $this->submission['fields']['entered_sref_system']['value']); } catch (Exception $e) { $this->errors['entered_sref'] = $e->getMessage(); } } return parent::presubmit(); }