<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># FixMyStreet::Geocode::Zurich
# Geocoding with Zurich web service.
#
# Thanks to http://msdn.microsoft.com/en-us/library/ms995764.aspx
# and http://noisemore.wordpress.com/2009/03/19/perl-soaplite-wsse-web-services-security-soapheader/
# for SOAP::Lite pointers
#
# Copyright (c) 2012 UK Citizens Online Democracy. All rights reserved.
# Email: matthew@mysociety.org; WWW: http://www.mysociety.org/

package FixMyStreet::Geocode::Zurich;

use strict;
use Digest::MD5 qw(md5_hex);
use Geo::Coordinates::CH1903Plus;
use Path::Tiny;
use Storable;
use Utils;

my ($soap, $method, $security);

sub setup_soap {
    return if $soap;

    # Variables for the SOAP web service
    my $geocoder = FixMyStreet-&gt;config('GEOCODER');
    return unless ref $geocoder eq 'HASH';

    my $url = $geocoder-&gt;{url};
    my $username = $geocoder-&gt;{username};
    my $password = $geocoder-&gt;{password};
    my $attr = 'http://ch/geoz/fixmyzuerich/service';
    my $action = "$attr/IFixMyZuerich/";

    require SOAP::Lite;
    # SOAP::Lite-&gt;import( +trace =&gt; [transport =&gt; \&amp;log_message ] );

    # Set up the SOAP handler
    $security = SOAP::Header-&gt;name("Security")-&gt;attr({
        'mustUnderstand' =&gt; 'true',
        'xmlns' =&gt; 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'
    })-&gt;value(
        \SOAP::Header-&gt;name(
            "UsernameToken" =&gt; \SOAP::Header-&gt;value(
                SOAP::Header-&gt;name('Username', $username),
                SOAP::Header-&gt;name('Password', $password)
            )
        )
    );
    $soap = SOAP::Lite-&gt;on_action( sub { $action . $_[1]; } )-&gt;proxy($url);
    $method = SOAP::Data-&gt;name('getLocation95')-&gt;attr({ xmlns =&gt; $attr });
}

sub admin_district {
    my ($e, $n) = @_;

    setup_soap();
    return unless $soap;

    my $attr = 'http://ch/geoz/fixmyzuerich/service';
    my $bo = 'http://ch/geoz/fixmyzuerich/bo';
    my $method = SOAP::Data-&gt;name('getInfoByLocation')-&gt;attr({ xmlns =&gt; $attr });
    my $location = SOAP::Data-&gt;name(
        'location' =&gt; \SOAP::Data-&gt;value(
            SOAP::Data-&gt;name('bo:easting', $e),
            SOAP::Data-&gt;name('bo:northing', $n),
        )
    )-&gt;attr({ 'xmlns:bo' =&gt; $bo });
    my $search = SOAP::Data-&gt;value($location);
    my $result;
    eval {
        $result = $soap-&gt;call($method, $security, $search);
    };
    if ($@) {
        warn $@ if FixMyStreet-&gt;config('STAGING_SITE');
        return 'The geocoder appears to be down.';
    }
    $result = $result-&gt;result;
    return $result;
}

# string STRING CONTEXT
# Looks up on Zurich web service a user-inputted location.
# Returns array of (LAT, LON, ERROR), where ERROR is either undef, a string, or
# an array of matches if there are more than one.
# If there is no ambiguity, returns only a {lat,long} hash, unless allow_single_match_string is true
# (because the auto-complete use of this (in /around) should send the matched name even though it's not ambiguous).
#
# The information in the query may be used to disambiguate the location in cobranded 
# versions of the site.

sub string {
    my ( $cls, $s, $c ) = @_;

    setup_soap();

    my $cache_dir = path(FixMyStreet-&gt;config('GEO_CACHE'), 'zurich')-&gt;absolute(FixMyStreet-&gt;path_to());
    my $cache_file = $cache_dir-&gt;child(md5_hex($s));
    my $result;
    $c-&gt;stash-&gt;{geocoder_url} = $s;
    if (-s $cache_file &amp;&amp; -M $cache_file &lt;= 7 &amp;&amp; !FixMyStreet-&gt;config('STAGING_SITE')) {
        $result = retrieve($cache_file);
    } else {
        my $search = SOAP::Data-&gt;name('search' =&gt; $s)-&gt;type('');
        my $count = SOAP::Data-&gt;name('count' =&gt; 10)-&gt;type('');
        eval {
            $result = $soap-&gt;call($method, $security, $search, $count);
        };
        if ($@) {
            warn $@ if FixMyStreet-&gt;config('STAGING_SITE');
            return { error =&gt; 'The geocoder appears to be down.' };
        }
        $result = $result-&gt;result;
        $cache_dir-&gt;mkdir;
        store $result, $cache_file if $result &amp;&amp; !FixMyStreet-&gt;config('STAGING_SITE');
    }

    if (!$result || !$result-&gt;{Location}) {
        return { error =&gt; _('Sorry, we could not parse that location. Please try again.') };
    }

    my $results = $result-&gt;{Location};
    $results = [ $results ] unless ref $results eq 'ARRAY';

    my ( $error, @valid_locations, $latitude, $longitude );
    foreach (@$results) {
        ($latitude, $longitude) =
            map { Utils::truncate_coordinate($_) }
            Geo::Coordinates::CH1903Plus::to_latlon($_-&gt;{easting}, $_-&gt;{northing});
        push (@$error, {
            address =&gt; $_-&gt;{text},
            latitude =&gt; $latitude,
            longitude =&gt; $longitude
        });
        push (@valid_locations, $_);
        last if lc($_-&gt;{text}) eq lc($s);
    }
    if (scalar @valid_locations == 1 &amp;&amp; ! $c-&gt;stash-&gt;{allow_single_geocode_match_strings} ) {
        return { latitude =&gt; $latitude, longitude =&gt; $longitude };
    }
    return { error =&gt; $error };
}

sub log_message {
    my ($in) = @_;
    eval {
        printf "log_message [$in]: %s\n\n", $in-&gt;content; # ...for example
    };
    if ($@) {
        print "log_message [$in]: ???? \n\n";
    }
}

1;

</pre></body></html>