Coding standards
Security in ResourceSpace
Developer reference
Database
User api session functions
Geolocation functions

geo_setcoords_from_country()

Description

Fill missing resource geolocation using the country metadata field.

Parameters

ColumnTypeDefaultDescription
$output_newline string Line separator for CLI or HTML progress output.
[]
'schema';

Return

void

Location

include/geolocation_functions.php lines 9 to 48

Definition

 
function geo_setcoords_from_country(string $output_newline = PHP_EOL): void
{
    
$country_field = ps_query("SELECT ref FROM resource_type_field WHERE name='country'", [], 'schema');
    if (
count($country_field) === 0 || $country_field[0]['ref'] === '') {
        echo 
" - Country field not found. Must have a field with shorthand name set to 'country'." . $output_newline;
        return;
    }
    
$resource_countries = ps_query(
        
'SELECT DISTINCT rn.resource, UPPER(n.name) name
         FROM resource_node rn
         JOIN node n ON n.ref=rn.node
         JOIN resource r ON r.ref=rn.resource
         WHERE n.resource_type_field=? AND (r.geo_lat IS NULL OR r.geo_long IS NULL)'
,
        [
'i', $country_field[0]['ref']]
    );
    
// Retain one country per resource, as before, without sorting every resource.
    
$resource_names = array_column($resource_countries, 'name', 'resource');
    unset(
$resource_countries);
    
$countries = [];
    foreach (
$resource_names as $ref => $country) {
        if (
$country !== '') {
            
$countries[$country][] = $ref;
        }
    }
    unset(
$resource_names);
    
$codes = build_codes();
    
$coords = build_coords();
    foreach (
$countries as $country => $refs) {
        
$latlong = fetch_country_coords($country, $codes, $coords);
        
update_country_coords($refs, $latlong);
        echo 
escape(' - Country=' . $country . '; Resources=' . count($refs)
            . (
count($latlong) === 2 ? '' : '; coordinates not found')) . $output_newline;
        if (
PHP_SAPI === 'cli') {
            if (
ob_get_level() > 0) {
                
ob_flush();
            }
            
flush();
        }
    }
}

This article was last updated 11th November 2026 19:05 Europe/London time based on the source file dated 11th November 2026 14:55 Europe/London time.