Greetings everyone,
I'm trying to execute a Closest Facility Task using the ArcGIS location service in the following link
I've run the following code to query the features used as facilities
private void queryAvailableWaterPoints() {
if (hasGeodatabaseLoad) {
for (Layer layer : mMapView.getMap().getOperationalLayers()) {
if (layer.getName().equals("Pontos_de_Água")) {
// queryAvailableWaterPoints is already working
FeatureLayer featureLayer = (FeatureLayer) layer;
FeatureTable featureTable = featureLayer.getFeatureTable();
featureTable.loadAsync();
featureTable.addDoneLoadingListener(new Runnable() {
@Override
public void run() {
QueryParameters query = new QueryParameters();
query.setWhereClause("Capacidade > 500 AND Tipo LIKE 'Autotanque'");
query.setMaxFeatures(0);
final ListenableFuture<FeatureQueryResult> queryFeaturesAsync = featureTable.queryFeaturesAsync(query);
queryFeaturesAsync.addDoneListener(() -> {
try {
FeatureQueryResult result = queryFeaturesAsync.get();
Iterator<Feature> resultIterator = result.iterator();
int numFeatures = 0;
ArrayList<Facility> mFacilities = new ArrayList<>();
ArrayList<Incident> mIncidents = new ArrayList<>();
// Loop through the diffrent water points and add them as facilities
while (resultIterator.hasNext()) {
Feature feature = resultIterator.next();
numFeatures++;
}
// Store the water points as Facilities for the 'Find closest facility to an incident' task
mFacilities.add(new Facility((Point) feature.getGeometry()));
}
// Store the user's current location as Incident for the 'Find closest facility to an incident' task
mIncidents.add(new Incident(new Point(location.getLongitude(), location.getLatitude(), mMapView.getSpatialReference())));
// Enable the closeRoute button and disable the remaining ActionBar buttons
enableDisableRouteMode(true);
//createProgressDialog();
executeClosestFacilityTask(mFacilities, mIncidents);
Log.i("feature", String.valueOf(numFeatures));
} catch (Exception e) {
Log.i("FeatureQueryResult", "There was an error");
}
});
}
});
}
}
} else {
Toast.makeText(mContext, "Carregue a base de dados", Toast.LENGTH_SHORT).show();
}
}
After this, I stored them as Facilities and the user location as an Incident.
In the executeClosestFacilityTask method, I ran the following code
private void executeClosestFacilityTask(ArrayList<Facility> mFacilities, ArrayList<Incident> mIncidents) {
// task to find the closest route between an incident and a facility
mClosestFacilityTask = new ClosestFacilityTask(this, getString(R.string.closest_facility));
mClosestFacilityTask.loadAsync();
mClosestFacilityTask.addDoneLoadingListener(() -> {
if (mClosestFacilityTask.getLoadStatus() == LoadStatus.LOADED) {
ListenableFuture<ClosestFacilityParameters> closestFacilityParametersFuture = mClosestFacilityTask.createDefaultParametersAsync();
closestFacilityParametersFuture.addDoneListener(() -> {
try {
mClosestFacilityParameters = closestFacilityParametersFuture.get();
// set new parameters to find route
Log.i("mFacilities", String.valueOf(mFacilities.size()));
Log.i("mIncidents", String.valueOf(mIncidents.size()));
mClosestFacilityParameters.setFacilities(mFacilities);
mClosestFacilityParameters.setIncidents(mIncidents);
mClosestFacilityParameters.setReturnRoutes(true);
mClosestFacilityParameters.setOutputSpatialReference(mMapView.getSpatialReference());
// mClosestFacilityParameters.setPointBarriers(pointBarriers);
// mClosestFacilityParameters.setPolylineBarriers(polylineBarriers);
// mClosestFacilityParameters.setPolygonBarriers(polygonBarriers);
// solve closest facilities
try {
// use the task to solve for the closest facility
ListenableFuture<ClosestFacilityResult> closestFacilityTaskResult = mClosestFacilityTask
.solveClosestFacilityAsync(mClosestFacilityParameters);
closestFacilityTaskResult.addDoneListener(() -> {
try {
ClosestFacilityResult closestFacilityResult = closestFacilityTaskResult.get();
// find the closest facility for each incident
for (int i = 0; i < mIncidents.size(); i++) {
if (closestFacilityResult.getRankedFacilityIndexes(i).size() > 0) {
// get the index of the closest facility to incident
Integer closestFacilityIndex = closestFacilityResult.getRankedFacilityIndexes(i).get(0);
// get the route to the closest facility
ClosestFacilityRoute closestFacilityRoute = closestFacilityResult.getRoute(closestFacilityIndex, i);
// display the route on the graphics overlay
mRouteOverlay.getGraphics().add(new Graphic(closestFacilityRoute.getRouteGeometry()));
// Hide the ProgressDialog
hideProgressDialog();
}
}
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
} catch (Exception ex) {
String error = "Error solving the closest facility task: " + ex.getMessage();
Toast.makeText(getApplicationContext(), error, Toast.LENGTH_LONG).show();
Log.e("tag", error);
}
} catch (ExecutionException | InterruptedException e) {
String error = "Error generating parameters: " + e.getMessage();
Log.e("error", error);
}
});
} else {
String error = "Closest facility task failed to load: " + mClosestFacilityTask.getLoadError().getCause();
Log.e("error", error);
Toast.makeText(this, "Tarefa 'Calcular ponto de água mais próximo' falhou'", Toast.LENGTH_LONG).show();
}
});
}
which yield the following error
com.example.testsample2 W java.util.concurrent.ExecutionException: com.esri.arcgisruntime.io.JsonEmbeddedException: Unable to complete operation.
2022-11-23 16:36:55.686 7879-7879 System.err com.example.testsample2 W at com.esri.arcgisruntime.internal.concurrent.b.get(SourceFile:18)
2022-11-23 16:36:55.686 7879-7879 System.err com.example.testsample2 W at com.example.testsample2.MainActivity.lambda$executeClosestFacilityTask$0$com-example-testsample2-MainActivity(MainActivity.java:902)
2022-11-23 16:36:55.686 7879-7879 System.err com.example.testsample2 W at com.example.testsample2.MainActivity$$ExternalSyntheticLambda5.run(Unknown Source:6)
2022-11-23 16:36:55.686 7879-7879 System.err com.example.testsample2 W at android.os.Handler.handleCallback(Handler.java:938)
2022-11-23 16:36:55.687 7879-7879 System.err com.example.testsample2 W at android.os.Handler.dispatchMessage(Handler.java:99)
2022-11-23 16:36:55.687 7879-7879 System.err com.example.testsample2 W at android.os.Looper.loopOnce(Looper.java:201)
2022-11-23 16:36:55.687 7879-7879 System.err com.example.testsample2 W at android.os.Looper.loop(Looper.java:288)
2022-11-23 16:36:55.687 7879-7879 System.err com.example.testsample2 W at android.app.ActivityThread.main(ActivityThread.java:7839)
2022-11-23 16:36:55.688 7879-7879 System.err com.example.testsample2 W at java.lang.reflect.Method.invoke(Native Method)
2022-11-23 16:36:55.688 7879-7879 System.err com.example.testsample2 W at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
2022-11-23 16:36:55.689 7879-7879 System.err com.example.testsample2 W at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
2022-11-23 16:36:55.689 7879-7879 System.err com.example.testsample2 W Caused by: com.esri.arcgisruntime.io.JsonEmbeddedException: Unable to complete operation.
2022-11-23 16:36:55.689 7879-7879 System.err com.example.testsample2 W at com.esri.arcgisruntime.io.JsonEmbeddedException$b.a(SourceFile:43)
2022-11-23 16:36:55.690 7879-7879 System.err com.example.testsample2 W at com.esri.arcgisruntime.io.JsonEmbeddedException$b.deserialize(SourceFile:1)
2022-11-23 16:36:55.690 7879-7879 System.err com.example.testsample2 W at com.google.gson.internal.bind.TreeTypeAdapter.read(TreeTypeAdapter.java:69)
2022-11-23 16:36:55.690 7879-7879 System.err com.example.testsample2 W at com.google.gson.Gson.fromJson(Gson.java:991)
2022-11-23 16:36:55.691 7879-7879 System.err com.example.testsample2 W at com.google.gson.Gson.fromJson(Gson.java:956)
2022-11-23 16:36:55.692 7879-7879 System.err com.example.testsample2 W at com.google.gson.Gson.fromJson(Gson.java:905)
2022-11-23 16:36:55.692 7879-7879 System.err com.example.testsample2 W at com.google.gson.Gson.fromJson(Gson.java:876)
2022-11-23 16:36:55.693 7879-7879 System.err com.example.testsample2 W at com.esri.arcgisruntime.io.JsonEmbeddedException.fromJson(SourceFile:1)
2022-11-23 16:36:55.693 7879-7879 System.err com.example.testsample2 W at com.esri.arcgisruntime.internal.io.handler.request.l.b(SourceFile:3)
2022-11-23 16:36:55.693 7879-7879 System.err com.example.testsample2 W at com.esri.arcgisruntime.internal.io.handler.request.m.a(SourceFile:61)
2022-11-23 16:36:55.695 7879-7879 System.err com.example.testsample2 W at com.esri.arcgisruntime.internal.io.handler.request.m.a(SourceFile:2)
2022-11-23 16:36:55.695 7879-7879 System.err com.example.testsample2 W at com.esri.arcgisruntime.internal.io.handler.request.c.a(SourceFile:169)
2022-11-23 16:36:55.696 7879-7879 System.err com.example.testsample2 W at com.esri.arcgisruntime.internal.io.handler.request.c.f(SourceFile:1)
2022-11-23 16:36:55.696 7879-7879 System.err com.example.testsample2 W at com.esri.arcgisruntime.internal.io.handler.request.r.x(SourceFile:1)
2022-11-23 16:36:55.697 7879-7879 System.err com.example.testsample2 W at com.esri.arcgisruntime.internal.io.handler.request.r.d(SourceFile:1)
2022-11-23 16:36:55.697 7879-7879 System.err com.example.testsample2 W at com.esri.arcgisruntime.internal.io.handler.request.c$a.call(SourceFile:1)
2022-11-23 16:36:55.697 7879-7879 System.err com.example.testsample2 W at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2022-11-23 16:36:55.698 7879-7879 System.err com.example.testsample2 W at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2022-11-23 16:36:55.699 7879-7879 System.err com.example.testsample2 W at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2022-11-23 16:36:55.699 7879-7879 System.err com.example.testsample2 W at java.lang.Thread.run(Thread.java:920)
I've tried to understand what is causing this bug but can't figure out.
If someone can help me into understanding what is behind this, I'll greatly thank and appreciate.
Thank you.
Cheers