Skip to content

Instantly share code, notes, and snippets.

@garysheppardjr
Last active May 25, 2018 17:00
Show Gist options
  • Save garysheppardjr/88a2e1631e62d605936f0c38f9625144 to your computer and use it in GitHub Desktop.
Save garysheppardjr/88a2e1631e62d605936f0c38f9625144 to your computer and use it in GitHub Desktop.
A Java program that performs "batch" geocoding using an offline locator file with ArcGIS Runtime
package com.esri.samples.search.offline_geocode;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.tasks.geocode.GeocodeParameters;
import com.esri.arcgisruntime.tasks.geocode.GeocodeResult;
import com.esri.arcgisruntime.tasks.geocode.LocatorTask;
import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* A program that performs "batch" geocoding using an offline locator file. It's
* not true batch geocoding because we call the LocatorTask once for each address,
* but it seems to be the closest thing ArcGIS Runtime has to batch geocoding.
*/
public class OfflineBatchGeocodeSample {
private static final Logger LOGGER = Logger.getLogger(OfflineBatchGeocodeSample.class.getName());
public static void main(String[] args) {
// Comment out the following line to use a Developer license during development.
ArcGISRuntimeEnvironment.setLicense("TODO insert ArcGIS Runtime license string (Lite or higher)");
// Keep track of whether it's time to exit the program.
AtomicBoolean done = new AtomicBoolean(false);
// Create a locator task based on a .loc locator file.
final String locatorPath = new File("samples-data/sanfrancisco/SanFranciscoLocator.loc").getAbsolutePath();
LocatorTask locatorTask = new LocatorTask(locatorPath);
GeocodeParameters params = new GeocodeParameters();
params.setMaxResults(1);
locatorTask.addDoneLoadingListener(() -> {
/**
* You would probably have a table of addresses. Here we create some
* in code and loop through them.
*/
final int addressCount = 1000;
// Keep track of the count done so we know when to exit
AtomicInteger doneCount = new AtomicInteger(0);
for (int i = 1; i <= addressCount; i++) {
final String searchString = i + " Howard St";
// Geocode this address.
ListenableFuture<List<GeocodeResult>> future = locatorTask.geocodeAsync(searchString, params);
future.addDoneListener(() -> {
try {
// When the geocode completes, get the result.
List<GeocodeResult> results = future.get();
if (null != results && 0 < results.size()) {
GeocodeResult result = results.get(0);
LOGGER.log(Level.INFO, "{0}: {1} ({2})", new Object[]{
searchString, result.getDisplayLocation(), result.getLabel()
});
} else {
LOGGER.log(Level.INFO, "{0} not found", searchString);
}
} catch (InterruptedException | ExecutionException ex) {
LOGGER.log(Level.SEVERE, null, ex);
} finally {
doneCount.incrementAndGet();
}
});
}
// Wait until all geocode calls are done.
while (doneCount.get() < addressCount) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
LOGGER.log(Level.SEVERE, null, ex);
}
}
// Cause the app to exit.
done.set(true);
});
locatorTask.loadAsync();
// Wait until the app should exit.
while (!done.get()) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
LOGGER.log(Level.SEVERE, null, ex);
}
}
}
}
May 25, 2018 12:42:52 PM com.esri.samples.search.offline_geocode.OfflineBatchGeocodeSample lambda$null$0
INFO: 1 Market St: Point: [-122.394797, 37.794392, 0.000000, NaN] SR: 4326 (1 Market St, San Francisco, CA, 94105)
May 25, 2018 12:42:52 PM com.esri.samples.search.offline_geocode.OfflineBatchGeocodeSample lambda$null$0
INFO: 49 Market St: Point: [-122.395171, 37.794048, 0.000000, NaN] SR: 4326 (49 Market St, San Francisco, CA, 94105)
May 25, 2018 12:42:52 PM com.esri.samples.search.offline_geocode.OfflineBatchGeocodeSample lambda$null$0
INFO: 41 Market St: Point: [-122.395113, 37.794095, 0.000000, NaN] SR: 4326 (41 Market St, San Francisco, CA, 94105)
May 25, 2018 12:42:52 PM com.esri.samples.search.offline_geocode.OfflineBatchGeocodeSample lambda$null$0
INFO: 43 Market St: Point: [-122.395128, 37.794083, 0.000000, NaN] SR: 4326 (43 Market St, San Francisco, CA, 94105)
May 25, 2018 12:42:52 PM com.esri.samples.search.offline_geocode.OfflineBatchGeocodeSample lambda$null$0
INFO: 39 Market St: Point: [-122.395099, 37.794107, 0.000000, NaN] SR: 4326 (39 Market St, San Francisco, CA, 94105)
...
May 25, 2018 12:42:52 PM com.esri.samples.search.offline_geocode.OfflineBatchGeocodeSample lambda$null$0
INFO: 997 Market St: Point: [-122.456326, 37.693675, 0.000000, NaN] SR: 4326 (997 Market St, Daly City, CA, 94014)
May 25, 2018 12:42:52 PM com.esri.samples.search.offline_geocode.OfflineBatchGeocodeSample lambda$null$0
INFO: 984 Market St: Point: [-122.456230, 37.693571, 0.000000, NaN] SR: 4326 (984 Market St, Daly City, CA, 94014)
May 25, 2018 12:42:52 PM com.esri.samples.search.offline_geocode.OfflineBatchGeocodeSample lambda$null$0
INFO: 998 Market St: Point: [-122.456014, 37.693868, 0.000000, NaN] SR: 4326 (998 Market St, CA, 94005)
May 25, 2018 12:42:52 PM com.esri.samples.search.offline_geocode.OfflineBatchGeocodeSample lambda$null$0
INFO: 995 Market St: Point: [-122.456348, 37.693631, 0.000000, NaN] SR: 4326 (995 Market St, Daly City, CA, 94014)
May 25, 2018 12:42:52 PM com.esri.samples.search.offline_geocode.OfflineBatchGeocodeSample lambda$null$0
INFO: 999 Market St: Point: [-122.456302, 37.693714, 0.000000, NaN] SR: 4326 (999 Market St, Daly City, CA, 94014)
May 25, 2018 12:42:52 PM com.esri.samples.search.offline_geocode.OfflineBatchGeocodeSample lambda$null$0
INFO: 1000 Market St: Point: [-122.455987, 37.693884, 0.000000, NaN] SR: 4326 (1000 Market St, CA, 94005)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment