Skip to content

Commit

Permalink
#3 Added a custom error type
Browse files Browse the repository at this point in the history
- inspect error results
- access feature attributes
  • Loading branch information
esride-jts committed Sep 20, 2024
1 parent d1a07e8 commit 60246a1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
15 changes: 15 additions & 0 deletions spatial-data-science/src/platformshell/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::fmt;

#[derive(Debug)]
pub struct LocationServicesError {
pub code: i32,
pub message: String,
}

impl fmt::Display for LocationServicesError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ArcGIS Location Platform Error {}: {}", self.code, self.message)
}
}

impl std::error::Error for LocationServicesError {}
27 changes: 23 additions & 4 deletions spatial-data-science/src/platformshell/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use geo::Point;
use platformshell::LocationServicesError;
use reqwest::Url;
use serde_esri::features::FeatureSet;
use serde_json::json;
use serde_json::Value;
use std::env;
use std::io::Read;

Expand Down Expand Up @@ -62,14 +64,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// Query the feature service
let urban_hri_url = env::var("URBAN_HEAT_RISK_INDEX_FEATURE_SERVICE_URL")?;
let out_fields = "HRI, TEMP";
let query_url = Url::parse_with_params(
&(urban_hri_url + "/query"),
&[
("where", "1=1"),
("geometryType", "esriGeometryPoint"),
("geometry", &location_str),
("inSR", &location_wkid_str),
("outFields", "*"),
("outFields", &out_fields),
("returnGeometry", "true"),
("f", "json"),
],
Expand All @@ -80,19 +83,35 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Read the request into a String
response.read_to_string(&mut body)?;

// Parse the response body as JSON
let json_body: Value = serde_json::from_str(&body)?;

// Check if the JSON contains an error
if let Some(error) = json_body.get("error") {
let code = error.get("code").and_then(Value::as_i64).unwrap_or(0) as i32;
let message = error.get("message").and_then(Value::as_str).unwrap_or("Unknown error").to_string();
//eprintln!("{}", message);
return Err(Box::new(LocationServicesError { code, message }));
}
//println!("{:?}", body);

// Parse into a 2D FeatureSet
let hri_featureset: FeatureSet<2> = serde_json::from_str(&body)?;

// Extract and print the JSON representation of the geometries
for hri_feature in hri_featureset.features {
// Extract and print the JSON representation of the features
for hri_feature in hri_featureset.features {
if let Some(hri_attributes) = hri_feature.attributes {
for (key, value) in hri_attributes.iter() {
println!("{}: {}", key, value);
}
}
if let Some(hri_geometry) = hri_feature.geometry {
if let Some(polygon) = hri_geometry.as_polygon() {
let json_geometry = json!(polygon);
println!("{}", serde_json::to_string_pretty(&json_geometry)?);
} else {
eprintln!("Geometry is not a polygon.");
//eprintln!("Geometry is not a polygon.");
return Err(Box::new(LocationServicesError { code:9999, message:"Geometry is not a polygon.".to_string() }));
}
}
}
Expand Down

0 comments on commit 60246a1

Please sign in to comment.