Search a Table for Features Nearest to a Geometry

Description

Searches a named table within the repository for features that are nearest to a specified geometry, within a certain distance. You have the options of returning all of the attribute data, specific attribute data, and ordering the returned data by an attribute. Here are some basic rules for the searchNearest method:

  • Returns a feature collection plus the attributes.
  • Returns the distance value in the response using the distanceAttributeName parameter specified in the request.
  • Returns the distance in the unit specified in the withinDistance parameter.
  • Returns results, by default, closest to farthest
  • The q attribute must be the query type searchNearest.
  • Maximum number of features returned is defined using the maxFeatures parameter in the request or using the maxCandidates parameter in the Feature Service configuration. By default this value is 1000 features.
  • When specifying attribute and orderBy Matrix parameters you can list more than one using a comma separated list.
  • When using the orderBy matrix parameter you must specify the sort direction as either D for descending or A for ascending.

This operation returns a feature collection JSON that can be passed to a render map or render legend request in the Mapping service.

ACL Authorization Flow

To search a named table within the repository for features that are nearest to a specified geometry, the user (or the role they belong to) needs EXECUTE permission on the Named table.

HTTP GET URL Format

The following format is used for HTTP GET requests:

HTTP GET /tables/tablename/features.rep;[attributes=attname];[orderBy=attname dir]
?q=searchNearest&geometry=geom,srs&[withinDistance=distance unit]
&[distanceAttributeName=distanceAttributeName]&[geometryAttributeName=geomname]
&destinationSrs&[page=pagenumber]&[pageLength=pagelength]
&[maxFeatures=maxfeatures]

Parameters

For information on the parameter types listed below, see Request URL Data Types.

Parameter Type Required Description
tablename String yes The name of the table to return metadata. The table is specified by a Regex for the named table to render based on the location of the named table in the Repository. The name of the named table is defined between the /tables portion of the URL and the /metadata.rep portion of the URL. For example, to return the metadata for a named table located at /Samples/NamedTables/WorldTable in the Repository, the following URL would be used:
.../FeatureService/tables/Samples/NamedTables
/WorldTable/metadata.json;...
rep String yes The representation to be returned. Supported representation is json.
attributes=attname String no The attribute names of the table to be returned in the response. By default all attributes are returned. More than one attribute is defined by a comma separated list. This is a simple way of limiting the amount of information in the response to include only the information requested. Each feature in the resulting feature collection will only contain the specified attributes. For example, if the table contains four attributes, but only two are required in the response, those two attribute names can be specified here (separated by a comma) and the values for the other two will not be returned.
orderBy=attname dir String no The attribute name and direction to order the returned results. By default the results are ordered from closest to farthest based on the distance from the specified point. When specifying an attribute to order the results, you must include both the attribute name and the ordering direction (A for ascending(low to high) or D for descending(high to low)).
q=searchNearest String yes The query method to perform. This must be searchNearest.
geometry=geom,srs String yes The geometry used as the starting location for the search. The geometry must include the geometry type, coordinates and the spatial reference system for the geometry. For example:
 geometry={"type":"Polygon",
"coordinates":[[[-96.8,30.5],[-97.7,30.5],
[-97.7,29.9],[-96.8,29.9],[-96.8,30.5]]],
"crs":{"type":"name",
"properties":{"name":"epsg:4231"}}}
Note: If no CRS property is specified, then EPSG:4326 is considered the default CRS.
withinDistance=

distanceunit

String no The distance to search around the geometry. When specified, the distance must include the distance value and distance unit. The distance values returned in the response will be in the units specified in the request (default is meters).
distanceAttributeName=

geomname

String no The name of the distance attribute to be returned in the response. This attribute is used to return the distance value from the geometry to the found nearest features. If the distanceAttributeName parameter is not specified, the distance value to each feature will not be returned in the response.
geometryAttributeName=

geomname

String no The geometry definition attribute from the table that should be used for processing the spatial query. This attribute is only required for tables that contain more than one geometry attribute definition.
destinationSrs String no The coordinate system to return the resulting geometries. The default is the coordinate system of the data used (for example: epsg:4326).
page=pagenumber String no The page number to return.
pageLength=pagelength String no The number of features returned on each page. If the page parameter is specified in the request, you must define pageLength.
maxFeatures=

maxfeatures

String no The total number of features returned in the response. If the maxFeatures parameter is not specified in the request, the maxCandidates parameter in the Feature Service configuration is used. By default this value is 1000 features.
tablePointAttributeName=tblPointName String no Returns the nearest point on the searched table geometry. Specifies the name of an additional attribute to be added to the response containing the nearest point on the geometry from the table, of minimal distance to the specified request geometry. No attribute will be added if this is not specified.
geomPointAttributeName=geomPointAttributeName String no Returns the nearest point on the request geometry. Specifies the name of an additional attribute to be added to the response containing a point on the specified request geometry, of minimal distance to the geometry from the searched table. No attribute will be added if this is not specified.

Returns

Returns metadata for a named table in the repository.

Examples

Returns the nearest world capitals that are within 5000 miles from the specified point geometry.

http://hostname:port/rest/Spatial/FeatureService/tables/Samples/NamedTables/WorldcapTable/features.json?
	q=SearchNearest&geometry={"type":"Point","coordinates":[-97.672598,30.252092],"crs":
{"type":"name","properties":{"name":"epsg:4231"}}}&withinDistance=5000%20mi

Returns the closest three state capitals in the USA that are within 5000 miles from the specified geometry.

http://hostname:port/rest/Spatial/FeatureService/tables/Samples/NamedTables/USA_CAPS/features.json?
	q=SearchNearest&geometry={"type":"Polygon","coordinates":[[[-96.8,30.5],[-97.7,30.5],
[-97.7,29.9],[-96.8,29.9],[-96.8,30.5]]],"crs":{"type":"name","properties":{"name":"epsg:4231"}}}
&withinDistance=5000%20mi&maxFeatures=3

Returns the closest three state capitals in the USA that are within 1000 miles from the specified point geometry. The distance to each feature will be returned in the withinDistanceFromGeometry attribute in the response and the calculation will be based on the geometries in the GEOLOC column in the USA_CAPS named table.

"http://lhostname:port/rest/Spatial/FeatureService/tables/Samples/
NamedTables/USA_CAPS/features.json?q=SearchNearest&geometry={%22type%22:%22Point%22,%22coordinates%22:
[-97.672598,30.252092],%22crs%22:{%22type%22:%22name%22,%22properties%22:{%22name%22:%22epsg:4231%22}}}
&withinDistance=1000%20mi&maxFeatures=3&
distanceAttributeName=withinDistanceFromGeometry&geometryAttributeName=Obj"
format="json" scope="external">