admin
2025-01-22 7e31442f0e9b07764e9c6a9680d3d4aeba5fe1de
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
Quintiq file version 2.0
#parent: #root
Method ProcessGeocodeResponse (HTTPInterface httpinterface)
{
  #keys: '1[105690.0.469397562]'
  Description: 'Process the HTTP response (in XML form) to store it in result NamedValueTree'
  TextBody:
  [*
    // boon kiat Nov-7-2013 (created)
    ticks := OS::PrecisionCounter();
    
    library := this.LibOSM_OSMGISLibrary();
    utils := library.LibOSM_Utils();
    result := library.Result();
    resultroot := result.Root();
    
    xmldomimplementation := XMLDOMImplementation::Create();
    geocoderesponse := xmldomimplementation.CreateDocumentFromBinaryInput( httpinterface.BinaryResult(), httpinterface.Encoding() );
    utils.DebugDump( "geocode_response.xml", geocoderesponse );
    utils.DebugEndTrace( "Dumping the geocode response", ticks );
    
    ticks := OS::PrecisionCounter();
    
    arguments := library.Arguments();
    maxresultshandle := arguments.GetHandle( "maxresults" );
    targetsrswkthandle := arguments.GetHandle( "targetsrswkt" );
    argumentsroot := arguments.Root();
    maxresults := argumentsroot.Child( maxresultshandle ).GetValueAsNumber();
    
    geocodedaddresshandle := result.GetHandle( "geocodedaddress" );
    countryhandle := result.GetHandle( "country" );
    statehandle := result.GetHandle( "state" );
    cityhandle := result.GetHandle( "city" );
    streethandle := result.GetHandle( "street" );
    housenumberhandle := result.GetHandle( "housenumber" );
    postcodehandle := result.GetHandle( "postcode" );
    matchhandle := result.GetHandle( "match" );
    xhandle := result.GetHandle( "x" );
    yhandle := result.GetHandle( "y" );
    srswkthandle := result.GetHandle( "srswkt" );
    
    if( this.Endpoint() = 'OSM-NOMINATIM' )
    {
      resultaddresses := geocoderesponse.GetElementsByTagName( "place" );
    
      // Assuming fault when there is no result - QP #1459
      if( resultaddresses.Size() = 0 )
      {
        utils.HandleFault_Nominatim( "Geocode", httpinterface.ASyncCallId() );
      }
      else
      {
        size := Number::Min( resultaddresses.Size(), maxresults );
    
        coordformatsetting := utils.GetOrCorrectCoordFormatSetting();
        coordformatwkt := LibOSM_Utils::GetCoordFormatWKT( coordformatsetting );
        desiredcoordformatwkt := argumentsroot.ChildOrDefault( targetsrswkthandle ).GetValueAsString( "" );
        stringtorealconverter := StringToReal::ISOConverter();
        for ( idx := 0; idx < size; idx++ )
        {
          resultaddress := resultaddresses.Element( idx );
          geocodedaddress := resultroot.AddChild( geocodedaddresshandle );
          //Special static methods are created to retrieve the required node value, sets to default value if not present in response
          geocodedaddress.AddChild( countryhandle, LibOSM_GeocodingServer::GetNodeOrDefaultValue( resultaddress, "country_code", "" ) );
          geocodedaddress.AddChild( statehandle, LibOSM_GeocodingServer::GetNodeOrDefaultValue( resultaddress, "state", "" ) );
          geocodedaddress.AddChild( cityhandle, LibOSM_GeocodingServer::GetNodeOrDefaultValue( resultaddress, "city", "" ) );
          geocodedaddress.AddChild( streethandle, LibOSM_GeocodingServer::GetNodeOrDefaultValue( resultaddress, "road", "" ) );
          geocodedaddress.AddChild( housenumberhandle, LibOSM_GeocodingServer::GetNodeOrDefaultValue( resultaddress, "house_number", "" ) );
          geocodedaddress.AddChild( postcodehandle, LibOSM_GeocodingServer::GetNodeOrDefaultValue( resultaddress, "postcode", "" ) );
          geocodedaddress.AddChild( matchhandle, LibOSM_GeocodingServer::ImportanceScoreAsMatchValue_Nominatim( stringtorealconverter.Convert( resultaddress.GetAttribute( "importance" ).NodeValue() ) ) );
          //lon - x, lat - y
          x := stringtorealconverter.Convert( resultaddress.GetAttribute( "lon" ).NodeValue() );
          y := stringtorealconverter.Convert( resultaddress.GetAttribute( "lat" ).NodeValue() );
          if ( desiredcoordformatwkt.Length() > 0 and coordformatwkt <> desiredcoordformatwkt )
          {
            convertedx := x;
            convertedy := y;
            LibOSM_Utils::Convert( coordformatwkt, x, y, desiredcoordformatwkt, convertedx, convertedy );
            geocodedaddress.AddChild( xhandle, convertedx );
            geocodedaddress.AddChild( yhandle, convertedy );
            geocodedaddress.AddChild( srswkthandle, desiredcoordformatwkt );
          }
          else
          {
            geocodedaddress.AddChild( xhandle, x );
            geocodedaddress.AddChild( yhandle, y );
            geocodedaddress.AddChild( srswkthandle, coordformatwkt );
          }
        }
      }
    }
    else
    {
      resultinfo := geocoderesponse.GetElementByTagName( "info", 0 );
      statuscode := resultinfo.GetElementByTagName( "statusCode", 0 ).TextContent();
    
      // Assuming fault when statuscode is not 0 - QP #1459
      if( statuscode <> '0' )
      {
        utils.HandleFault_MapQuest( "Geocode", httpinterface.ASyncCallId(), resultinfo );
      }
      else
      {
        resultaddresses := geocoderesponse.GetElementsByTagName( "location" );
        size := Number::Min( resultaddresses.Size(), maxresults );
    
        coordformatsetting := utils.GetOrCorrectCoordFormatSetting();
        coordformatwkt := LibOSM_Utils::GetCoordFormatWKT( coordformatsetting );
        desiredcoordformatwkt := argumentsroot.ChildOrDefault( targetsrswkthandle ).GetValueAsString( "" );
        stringtorealconverter := StringToReal::ISOConverter();
        for ( idx := 0; idx < size; idx++ )
        {
          resultaddress := resultaddresses.Element( idx );
          geocodedaddress := resultroot.AddChild( geocodedaddresshandle );
          geocodedaddress.AddChild( countryhandle, resultaddress.GetElementByTagName( "adminArea1", 0 ).TextContent() ); //country
          geocodedaddress.AddChild( statehandle, resultaddress.GetElementByTagName( "adminArea3", 0 ).TextContent() );   //state
          geocodedaddress.AddChild( cityhandle, resultaddress.GetElementByTagName( "adminArea5", 0 ).TextContent() );    //city
          geocodedaddress.AddChild( streethandle, resultaddress.GetElementByTagName( "street", 0 ).TextContent() );
          //TODO - should the house number be an optional result node?
          geocodedaddress.AddChild( housenumberhandle, "" );
          geocodedaddress.AddChild( postcodehandle, resultaddress.GetElementByTagName( "postalCode", 0 ).TextContent() );
          geocodedaddress.AddChild( matchhandle, LibOSM_GeocodingServer::GeocodeQualityAsMatchValue_MapQuest( resultaddress.GetElementByTagName( "geocodeQuality", 0 ).TextContent() ) );
          //lng - x, lat - y
          //TODO - or use displayLatLng instead?
          x := stringtorealconverter.Convert( resultaddress.GetElementByTagName( "lng", 1 ).TextContent() );
          y := stringtorealconverter.Convert( resultaddress.GetElementByTagName( "lat", 1 ).TextContent() );
          if ( desiredcoordformatwkt.Length() > 0 and coordformatwkt <> desiredcoordformatwkt )
          {
            convertedx := x;
            convertedy := y;
            LibOSM_Utils::Convert( coordformatwkt, x, y, desiredcoordformatwkt, convertedx, convertedy );
            geocodedaddress.AddChild( xhandle, convertedx );
            geocodedaddress.AddChild( yhandle, convertedy );
            geocodedaddress.AddChild( srswkthandle, desiredcoordformatwkt );
          }
          else
          {
            geocodedaddress.AddChild( xhandle, x );
            geocodedaddress.AddChild( yhandle, y );
            geocodedaddress.AddChild( srswkthandle, coordformatwkt );
          }
        }
      }
    }
    
    utils.DebugEndTrace( "GeocodingServer.ProcessGeocodeResponse", ticks );
    
    utils.DebugDump( "geocode_result.txt", resultroot );
    
    library.Done();
  *]
}