Working with GeocodeServer


In this topic


Overview

The capabilities of an ArcGIS Server geocode service using the SOAP application programming interface (API) are defined by the GeocodeServerBindingStub object methods. These methods initiate stateless requests to a geocode service and return results. The capabilities of GeocodeServerBindingStub mirror the stateless ArcObjects interface implemented by the geocode server object IGeocodeServer
Two important differences between the SOAP proxy and ArcObjects Component Object Model (COM) proxies are as follows:
  • The GeocodeServerBindingStub object is a native Java object. IGeocodeServer is an interface to a remote COM object on the geographic information system (GIS) server (ArcSOC.exe). 
  • Input parameters and output results will share a common naming scheme but are different object types. The GeocodeServerBindingStub object uses native Java objects (value objects), while the IGeocodeServer interface uses a reference to remote COM objects on the GIS server. For example, both share the method geocodeAddress. However, GeocodeServerBindingStub takes two input parameters, Java objects of type PropertySet. The ArcObjects IGeocodeServer interface takes two input parameters: an interface reference to COM objects running on the GIS server of type IPropertySet. Likewise, GeocodeServerBindingStub returns a native Java PropertySet value object, while IGeocodeServer returns an IPropertySet reference to a COM object on the GIS server.
The following sample code snippets illustrate the use of selected methods to accomplish a common action. The samples use the Web service SOAP proxy (GeocodeServerBindingStub) and value objects included with the Web Application Developer Framework (ADF) com.esri.arcgisws package (located in arcgis_agsws_stubs.jar). The same patterns apply to dynamically generated proxy classes and value objects except that the proxy class name is <service name>_GeocodeServer and the namespace for the proxy and value objects is user defined. 
The proxy and value objects for an ArcGIS Server service need only be generated once per service type. For example, once the Web service proxy and value objects for an ArcGIS Server geocode service have been created, they can be reused for other ArcGIS Server Web geocode services. For dynamic proxy classes, use the setURL method to define the Web service endpoint for the ArcGIS Server Web geocode service you want to use. For Web ADF proxy classes that are pre-generated, the proxy constructor requires a java.net.URL input parameter to define the Web service endpoint for the ArcGIS Server Web geocode service you want to use. 

Geocoding an address

The following sample code shows how to geocode an address.
[Java]
String endpoint = 
    "http://myserver:8399/arcgis/services/portland_shp_loc/GeocodeServer?";
GeocodeServerBindingStub geocodeServer = new GeocodeServerBindingStub(new
    java.net.URL(endpoint), null);
// Define address inputs.
PropertySet geocodePropSet = new PropertySet();
PropertySetProperty[] propArray = new PropertySetProperty[2];
PropertySetProperty geocodeProp = new PropertySetProperty();
geocodeProp.setKey("STREET");
geocodeProp.setValue("1121 Division St.");
propArray[0] = geocodeProp;
PropertySetProperty geocodeProp1 = new PropertySetProperty();
geocodeProp1.setKey("ZONE");
geocodeProp1.setValue("97202");
propArray[1] = geocodeProp1;
geocodePropSet.setPropertyArray(propArray);
// Geocode the address.
PropertySet results = geocodeServer.geocodeAddress(geocodePropSet, null);
PropertySetProperty[] resultsArray = results.getPropertyArray();
PointN geocodePoint = null;
for (int i = 0; i < resultsArray.length; i++){
    PropertySetProperty result = resultsArray[i];
    String val = result.getKey().toString();
    if (val.equals("Shape")){
        geocodePoint = (PointN)result.getValue();
        double x = geocodePoint.getX();
        double y = geocodePoint.getY();
        System.out.println(x + ", " + y);
    }
    else if (result.getKey().equals("Status")){
        if (result.getValue().toString().equals("U")){
            // Invalid address.
            return ;
        }
    }
}

Reverse geocoding a point

The point used in a reverse geocode process requires a spatial reference. In addition, the spatial reference of the point and locator must be the same. To get the spatial reference of a geocode service, you need to get the result fields, find the geometry field, and use its definition to get the spatial reference. All of these items are illustrated in the following sample code.
[Java]
String endpoint = 
    "http://myserver:8399/arcgis/services/my_gc_serverobject/GeocodeServer?";
GeocodeServerBindingStub geocodeServer = new GeocodeServerBindingStub(new
    java.net.URL(endpoint), null);
// Define address inputs.
PropertySet geocodePropSet = new PropertySet();
PropertySetProperty[] propArray = new PropertySetProperty[2];
PropertySetProperty geocodeProp = new PropertySetProperty();
geocodeProp.setKey("ReverseDistanceUnits");
geocodeProp.setValue("Meters");
propArray[0] = geocodeProp;
PropertySetProperty geocodeProp1 = new PropertySetProperty();
geocodeProp1.setKey("ReverseDistance");
geocodeProp1.setValue("100");
propArray[1] = geocodeProp1;
geocodePropSet.setPropertyArray(propArray);

// Create the point to reverse geocode.
PointN inputPoint = new PointN();
inputPoint.setX(7649794.0);
inputPoint.setY(677525.0);
// The point to reverse geocode must be in the same coordinate system as the locator.
SpatialReference resultsr = null;

Fields resultflds = geocodeServer.getResultFields(geocodePropSet);

for (int i = 0; i < resultflds.getFieldArray().length; i++){
    Field fld = resultflds.getFieldArray()[i];
    if (fld.getType().getValue().equals("esriFieldTypeGeometry")){
        resultsr = fld.getGeometryDef().getSpatialReference();
    }
}

inputPoint.setSpatialReference(resultsr);

// Reverse geocode.
PropertySet results = geocodeServer.reverseGeocode(inputPoint, false, geocodePropSet)
    ;
PropertySetProperty[] resultsArray = results.getPropertyArray();
for (int i = 0; i < resultsArray.length; i++){
    PropertySetProperty result = resultsArray[i];
    System.out.println(result.getKey().toString());
    System.out.println(result.getValue().toString());
    if (result.getValue()instanceof PointN){
        PointN geocodePoint = (PointN)result.getValue();
        double x = geocodePoint.getX();
        double y = geocodePoint.getY();
        System.out.println(x + ", " + y);
    }
}

Obtaining locator properties

The following sample code shows how to obtain locator properties.
[Java]
String endpoint = 
    "http://myserver:8399/arcgis/services/my_gc_serverobject/GeocodeServer?";
GeocodeServerBindingStub geocodeServer = new GeocodeServerBindingStub(new
    java.net.URL(endpoint), null);
PropertySet locatorProperties = geocodeServer.getLocatorProperties();
PropertySetProperty[] locatorArray = locatorProperties.getPropertyArray();
for (int i = 0; i < locatorArray.length; i++){
    PropertySetProperty locatorResult = locatorArray[i];
    System.out.println(locatorResult.getKey().toString());
    System.out.println(locatorResult.getValue().toString());
}

//Address inputs.
PropertySet inputProperties = geocodeServer.getDefaultInputFieldMapping();
PropertySetProperty[] inputArray = inputProperties.getPropertyArray();
for (int i = 0; i < inputArray.length; i++){
    PropertySetProperty inputResult = inputArray[i];
    System.out.println(inputResult.getKey().toString());
    System.out.println(inputResult.getValue().toString());
}