In the past two days, I have provided a query example for a brother I know online. I would like to summarize it more or less and share it with you here.
Why is it a combination of multiple conditions? For spatial queries, sometimes the query conditions are very complex. For example, a certain attribute of a certain element must be greater than or equal to a certain attribute. The examples provided on the official website do not provide a description of compound queries. However, after viewing the API, you will find a very important sentence:
A Where clause for the query. Any legal SQL WHERE clause operating on the fields in the layer is allowed, for example: Where = pop2000> 350000.
That is to say, in the spatial query, the where attribute of the query is actually the string of the SQL where query, so we can write the query condition in a way similar to the SQL where query statement. For more information, see the following example.
I. Foreground
The interface is designed as follows:
The front-end XAML code is as follows:
Mainpage. XAML
View code
Note:
The Online Web editing feature of the elements is added to the front-end page. If the elements come from the ArcSDE database, you can also synchronize them with the new database to the back-end.
Ii. Background code
The backend mainly implements query. When querying, querytask specifies the query layer and declares the query task. querytask receives the query parameters through the executeasync method, starts the query, and returns the query results through the querycompleted event.
Query specifies the query conditions and query-related parameters. A list of common query parameters is listed below:
Parameter List |
Parameter description |
Geometry |
Specify the space filter to be queried. You can use query. spatialrelationship to describe the spatial relationship of the query. Valid geometric types include mappoint, polyline, polygon, envelope, or multipoint. |
Outfields |
The filtered field set is the attribute contained in the element. |
Returngeometry |
Returns the geometric element of the query. If all fields are returned, the returngeometry is set to true. |
Source |
If dynamic layers are queried, set the layer source. |
Spatialrelationship |
Describe the spatial relationship between the queried elements and the input geometric elements. |
Text |
Corresponding fields displayed in the element service layer |
Where |
A query statement, expressed by a valid SQL statement. |
The following describes how to implement the query:
First, declare querytask and the target layer of the query.
// The Query Task querytask = new querytask (); // The target layer featurelayer;
// Declare a draw to draw the queried area for Spatial Query
Draw mydraw;
Assign values and instantiate values in Constructors
public MainPage() { InitializeComponent(); featurelayer = map1.Layers["RiverSourceLayer"] as FeatureLayer; editorWidget.Loaded += new RoutedEventHandler(editorWidget_Loaded); queryTask.Url = featurelayer.Url; queryTask.ExecuteCompleted += new EventHandler<QueryEventArgs>(queryTask_ExecuteCompleted); queryTask.Failed += new EventHandler<TaskFailedEventArgs>(queryTask_Failed);
Mydraw = new draw (map1 );
Mydraw. drawmode = drawmode. polygon;
Mydraw. isenabled = false;
Mydraw. fillsymbol = new simplefillsymbol ()
{
Borderbrush = new solidcolorbrush (colors. Black ),
Borderthickness = 3,
Fill = new solidcolorbrush (colors. Red ),
};
Mydraw. drawcomplete + = new eventhandler <draweventargs> (mydraw_drawcomplete );
FiledsCombox.ItemsSource = AttributeString.GraphicAttributesEnNameString; }
You can customize two query methods:
Startquerybysql (string sqlstring): queries by SQL where statement.
private void StartQueryBySQL(string sqlString) { if (ExpressionTextBox.Text == "") return; Query query = new Query(); query.ReturnGeometry = true; query.OutFields.AddRange(AttributeString.GraphicAttributesEnNameString); query.Where = sqlString; query.OutSpatialReference = map1.SpatialReference; queryTask.ExecuteAsync(query); }
Startquerybyspatial (ESRI. ArcGIS. Client. Geometry. Geometry geometry): query by drawn Space Image
private void StartQueryBySpatial(ESRI.ArcGIS.Client.Geometry.Geometry geometry) { Query query = new Query(); query.ReturnGeometry = true; query.OutFields.AddRange(AttributeString.GraphicAttributesEnNameString); query.Geometry = geometry; query.OutSpatialReference = map1.SpatialReference; queryTask.ExecuteAsync(query); }
Next, click different buttons to query and process the query results. For example, the query results are displayed on the map.
Note: Because the query is a vertex element, only the symbol display operation is performed on the point in the completed event completion function of the query. If the query result is a surface element, it must be processed on the opposite side, you can add them by yourself.
The following code completes result processing:
Private void querytask_executecompleted (Object sender, queryeventargs e) {graphicslayer = map1.layers ["queryresultlayer"] As graphicslayer; graphicslayer. cleargraphics (); If (E. featureset. features. count> 0) {foreach (Graphic resultfeature in E. featureset. features) {resultfeature. symbol = new simplemarkersymbol () {color = new solidcolorbrush (colors. red), size = 18, style = simplemarkers Ymbol. simplemarkerstyle. Circle,}; graphicslayer. Graphics. Add (resultfeature) ;}} else {MessageBox. Show ("no target element found! ");}}
When querying a space, submit a space query request in the draw competed event:
private void myDraw_DrawComplete(object sender, DrawEventArgs e) { StartQueryBySpatial(e.Geometry);
myDraw.IsEnabled = false;
}
The other part of the code is a bit of control over the query input and some processing of program bugs (of course, some bugs are still not fixed, but the overall query function is not affected, you can modify and improve it on your own ).
The complete code is provided below: Coding-behind
Mainpage. csattributestring. CS
The final effect (the result is represented by a red dot. You can click the element to view its attribute list ):
1. Where query:
A. query condition 1: source_id> 1 and source_id <20 and source_id! = 14
Query results:
B. query condition 2: source_id> 1 and source_emissions is not null and source_emissions <200
Query results:
C. Spatial Query
Input query polygon:
Query the elements in a polygon:
Download source code and test data: [Download Code]
Data usage instructions: the data in the compressed file is an XML file exported by ArcMap. When using this file, create a new geographic database In ArcMap, right-click it, and select this XML file, that is, the data of the cost instance. We recommend that you publish the data service as a factor service to support online Web editing.
(All Rights Reserved. For details, refer to the source)