Skip to content

Commit

Permalink
Manage exception while retrieving urban_heat_modelling:gadm_level_1_usa
Browse files Browse the repository at this point in the history
  • Loading branch information
inigo-cobian committed Feb 28, 2024
1 parent a75774b commit 8e3a1db
Showing 1 changed file with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@
import org.integratedmodelling.klab.components.geospace.extents.Space;
import org.integratedmodelling.klab.data.resources.Resource;
import org.integratedmodelling.klab.exceptions.KlabMissingCredentialsException;
import org.integratedmodelling.klab.exceptions.KlabResourceAccessException;
import org.integratedmodelling.klab.exceptions.KlabResourceNotFoundException;
import org.integratedmodelling.klab.exceptions.KlabValidationException;
import org.integratedmodelling.klab.rest.ExternalAuthenticationCredentials;
import org.integratedmodelling.klab.rest.ResourceReference;
import org.integratedmodelling.klab.scale.Scale;

import kong.unirest.GetRequest;
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;
import kong.unirest.json.JSONException;
import kong.unirest.json.JSONObject;

@UrnAdapter(type = "recreationidb", version = Version.CURRENT)
Expand Down Expand Up @@ -149,25 +155,32 @@ public void encodeData(Urn urn, Builder builder, IGeometry geometry, IContextual

}

private List<String> getStatesFromEnvelope(IEnvelope envelope) {
HttpResponse<JsonNode> response = Unirest.get("https://integratedmodelling.org/aux-geoserver/ows?service=WFS&version=2.0.0&request=GetFeature&typeNames=urban_heat_modelling:gadm_level_1_usa&bbox="
+ envelope.getMinX() +"," + envelope.getMinY() + "," + envelope.getMaxX() + "," + envelope.getMaxY()
+ ",EPSG:4326&outputFormat=application/json").asJson();

if (!response.isSuccess()) {
throw new KlabResourceNotFoundException("Cannot retrieve information from geoserver resource \"urban_heat_modelling:gadm_level_1_usa\".");
}

JSONObject statesData = response.getBody().getObject();
try {
return statesData.getJSONArray("features").toList().stream().map(
feature -> ((JSONObject) feature).getJSONObject("properties").getString("iso_1").replaceFirst("US-", "")).toList();
} catch (JSONException e) {
throw new KlabResourceAccessException(e);
}
}

private final List<String> USA_STATES = Arrays.asList("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA",
"HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV",
"NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA",
"WA", "WV", "WI", "WY");

private List<String> buildRecreationIDBInput(Map<String, String> parameters, IEnvelope envelope)
throws UnsupportedEncodingException {
double west = envelope.getMinX();
double east = envelope.getMaxX();
double south = envelope.getMinY();
double north = envelope.getMaxY();
JSONObject statesData = Unirest.get("https://integratedmodelling.org/aux-geoserver/ows?service=WFS&version=2.0.0&request=GetFeature&typeNames=urban_heat_modelling:gadm_level_1_usa&bbox="
+ west +"," + south + "," + east + "," + north
+ ",EPSG:4326&outputFormat=application/json")
.asJson().getBody().getObject();

List<String> states = statesData.getJSONArray("features").toList().stream().map(
feature -> ((JSONObject) feature).getJSONObject("properties").getString("iso_1").replaceFirst("US-", "")).toList();
ArrayList<String> query = new ArrayList<>();
List<String> states = getStatesFromEnvelope(envelope);
List<String> query = new ArrayList<>();
// TODO revise this zombie code before making a PR. It might be useful
// states = parameters.containsKey(STATE) ? Arrays.asList(parameters.get(STATE).split(",")) : USA_STATES;
for (Map.Entry<String, String> entry : parameters.entrySet()) {
Expand Down

0 comments on commit 8e3a1db

Please sign in to comment.