Geocode using NREL Developer API (MapQuest) for NREL builds of SAM
| 172 | |
| 173 | // Geocode using NREL Developer API (MapQuest) for NREL builds of SAM |
| 174 | bool GeoTools::GeocodeDeveloper(const wxString& address, double* lat, double* lon, double* tz, bool showprogress) { |
| 175 | wxBusyCursor _curs; |
| 176 | |
| 177 | bool success = false; |
| 178 | |
| 179 | wxString plusaddr = address; |
| 180 | plusaddr.Replace(", ", "+"); |
| 181 | plusaddr.Replace(",", "+"); |
| 182 | plusaddr.Replace(" ", " "); |
| 183 | plusaddr.Replace(" ", " "); |
| 184 | plusaddr.Replace(" ", "+"); |
| 185 | |
| 186 | wxString url = SamApp::WebApi("nrel_geocode_api") + "&location=" + plusaddr; |
| 187 | url.Replace("<GEOCODEAPIKEY>", wxString(geocode_api_key)); |
| 188 | |
| 189 | wxEasyCurl curl; |
| 190 | wxBusyCursor curs; |
| 191 | |
| 192 | // SAM issue 1968 |
| 193 | curl.AddHttpHeader("Content-Type: application/json"); |
| 194 | curl.AddHttpHeader("Accept: application/json"); |
| 195 | |
| 196 | if (showprogress) { |
| 197 | if (!curl.Get(url, "Geocoding address '" + address + "'...")) |
| 198 | return false; |
| 199 | } |
| 200 | else { |
| 201 | if (!curl.Get(url)) |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | // change from UTF8 to UTF16 encoding to address unicode characters per SAM issue 1848 |
| 206 | rapidjson::GenericDocument < rapidjson::UTF16<> > reader; |
| 207 | wxString str = curl.GetDataAsString(); |
| 208 | |
| 209 | rapidjson::ParseResult ok = reader.Parse(str.c_str()); |
| 210 | |
| 211 | if (!reader.HasParseError()) { |
| 212 | if (reader.HasMember(L"results")) { |
| 213 | if (reader[L"results"].IsArray()) { |
| 214 | if (reader[L"results"][0].HasMember(L"locations")) { |
| 215 | if (reader[L"results"][0][L"locations"].IsArray()) { |
| 216 | if (reader[L"results"][0][L"locations"][0].HasMember(L"latLng")) { |
| 217 | if (reader[L"results"][0][L"locations"][0][L"latLng"].HasMember(L"lat")) { |
| 218 | if (reader[L"results"][0][L"locations"][0][L"latLng"][L"lat"].IsNumber()) { |
| 219 | *lat = reader[L"results"][0][L"locations"][0][L"latLng"][L"lat"].GetDouble(); |
| 220 | if (reader[L"results"][0][L"locations"][0][L"latLng"].HasMember(L"lng")) { |
| 221 | if (reader[L"results"][0][L"locations"][0][L"latLng"][L"lng"].IsNumber()) { |
| 222 | *lon = reader[L"results"][0][L"locations"][0][L"latLng"][L"lng"].GetDouble(); |
| 223 | success = true; // only if lat and lon are numbers |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |