MCPcopy Create free account
hub / github.com/NatLabRockies/SAM / QueryUtilityCompaniesbyZipcode

Method QueryUtilityCompaniesbyZipcode

src/urdb.cpp:269–378  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

267}
268
269bool OpenEI::QueryUtilityCompaniesbyZipcode(const wxString &zipcode, wxArrayString &names, wxString *err)
270{
271
272 names.Clear();
273
274 int zip = 0;
275
276 if (!zipcode.ToInt(&zip)) {
277 if (err) *err = wxString::Format("Invalid zipcode = %s. Zipcode must be an integer.", zipcode);
278 return false;
279 }
280 else if (zip <= 500 || zip > 99950) {
281 if (err) *err = wxString::Format("Invalid zipcode = %d. Zipcode must be a five-digit number between 00500 and 99950.", zip);
282 return false;
283 }
284
285 // geocode zip to lat/lon
286 double lat = 0, lon = 0;
287 if (!GeoTools::GeocodeDeveloper(zipcode, &lat, &lon)) {
288 if (err) *err = wxString::Format("Could not get lat/lon for zipcode = %d." + zipcode);
289 return false;
290 }
291
292 // NREL Developer API to list utility companies by lat/lon https://developer.nlr.gov/docs/electricity/utility-rates-v3/
293 wxString url = SamApp::WebApi("urdb_companies_by_lat_lon");
294 url.Replace("<LAT>", wxString::Format("%f",lat));
295 url.Replace("<LON>", wxString::Format("%f",lon));
296
297 wxString json_data = MyGet(url);
298 if (json_data.IsEmpty())
299 {
300 if (err) *err = wxString::Format("Web API call to urdb_companies_by_lat_lon returned empty JSON for lat = %f lon = %f.", lat, lon);
301 return false;
302 }
303
304// wxJSONReader reader;
305// wxJSONValue root;
306
307 rapidjson::GenericDocument < rapidjson::UTF16<> > reader;
308 reader.Parse(json_data.c_str());
309
310 if (reader.HasParseError())
311 {
312 if (err) *err = wxString::Format("Could not parse JSON for zipcode = %s.\nRapidJSON ParseErrorCode = %d.", zipcode, reader.GetParseError());;
313 return false;
314 }
315
316 wxString company_id = wxEmptyString;
317
318 if (reader.HasMember(L"outputs")) {
319 //wxJSONValue item_list = root.Item("outputs");
320 // does not resolve to OpenEI names only EIA names
321 //wxString buf = item_list.Item("utility_name").AsString();
322 // EIAID number for utility company
323 // some zip codes return company_id with multiple EIAIDs separated by single pipes
324 if (reader[L"outputs"].HasMember(L"company_id"))
325 company_id = reader[L"outputs"][L"company_id"].GetString();
326 }

Calls 6

MyGetFunction · 0.85
ClearMethod · 0.45
ParseMethod · 0.45
GetStringMethod · 0.45
GetObjectMethod · 0.45
AddMethod · 0.45

Tested by

no test coverage detected